/* ***** BEGIN LICENSE BLOCK ***** Copyright (c) 2006 Center for History and New Media George Mason University, Fairfax, Virginia, USA http://chnm.gmu.edu Licensed under the Educational Community License, Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.opensource.org/licenses/ecl1.php Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ***** END LICENSE BLOCK ***** */ //////////////////////////////////////////////////////////////////////////////// /// /// CollectionTreeView /// -- handles the link between an individual tree and the data layer /// -- displays only collections, in a hierarchy (no items) /// //////////////////////////////////////////////////////////////////////////////// /* * Constructor the the CollectionTreeView object */ Zotero.CollectionTreeView = function() { this._treebox = null; this.refresh(); this._unregisterID = Zotero.Notifier.registerColumnTree(this); } /* * Called by the tree itself */ Zotero.CollectionTreeView.prototype.setTree = function(treebox) { if(this._treebox) return; this._treebox = treebox; //select Library this.selection.select(0); } /* * Reload the rows from the data access methods * (doesn't call the tree.invalidate methods, etc.) */ Zotero.CollectionTreeView.prototype.refresh = function() { this._dataItems = new Array(); this.rowCount = 0; this._showItem(new Zotero.ItemGroup('library',null),0,1); var newRows = Zotero.getCollections(); for(var i = 0; i < newRows.length; i++) this._showItem(new Zotero.ItemGroup('collection',newRows[i]), 0, this._dataItems.length); //itemgroup ref, level, beforeRow var savedSearches = Zotero.Searches.getAll(); for(var i = 0; i < savedSearches.length; i++) this._showItem(new Zotero.ItemGroup('search',savedSearches[i]), 0, this._dataItems.length); //itemgroup ref, level, beforeRow this._refreshHashMap(); } /* * Redisplay everything */ Zotero.CollectionTreeView.prototype.reload = function() { var openCollections = new Array(); for(var i = 0; i < this.rowCount; i++) if(this.isContainer(i) && this.isContainerOpen(i)) openCollections.push(this._getItemAtRow(i).ref.getID()); var oldCount = this.rowCount; this._treebox.beginUpdateBatch(); this.refresh(); this._treebox.rowCountChanged(0,this.rowCount - oldCount); for(var i = 0; i < openCollections.length; i++) { var row = this._collectionRowMap[openCollections[i]]; if(row != null) this.toggleOpenState(row); } this._treebox.invalidate(); this._treebox.endUpdateBatch(); } /* * Called by Zotero.Notifier on any changes to collections in the data layer */ Zotero.CollectionTreeView.prototype.notify = function(action, type, ids) { var madeChanges = false; var ids = Zotero.flattenArguments(ids); if(action == 'remove') { //Since a remove involves shifting of rows, we have to do it in order //sort the ids by row var rows = new Array(); for (var i in ids) { switch (type) { case 'collection': if(this._collectionRowMap[ids[i]] != null) { rows.push(this._collectionRowMap[ids[i]]); } break; case 'search': if(this._searchRowMap[ids[i]] != null) { rows.push(this._searchRowMap[ids[i]]); } break; } } if(rows.length > 0) { rows.sort(function(a,b) { return a-b }); for(var i=0, len=rows.length; i= 0; i--) if(this.getLevel(i) < thisLevel) return i; return -1; } Zotero.CollectionTreeView.prototype.hasNextSibling = function(row, afterIndex) { var thisLevel = this.getLevel(row); for(var i = afterIndex + 1; i < this.rowCount; i++) { var nextLevel = this.getLevel(i); if(nextLevel == thisLevel) return true; else if(nextLevel < thisLevel) return false; } } /* * Opens/closes the specified row */ Zotero.CollectionTreeView.prototype.toggleOpenState = function(row) { var count = 0; //used to tell the tree how many rows were added/removed var thisLevel = this.getLevel(row); this._treebox.beginUpdateBatch(); if(this.isContainerOpen(row)) { while((row + 1 < this._dataItems.length) && (this.getLevel(row + 1) > thisLevel)) { this._hideItem(row+1); count--; //count is negative when closing a container because we are removing rows } } else { var newRows = Zotero.getCollections(this._getItemAtRow(row).ref.getID()); //Get children for(var i = 0; i < newRows.length; i++) { count++; this._showItem(new Zotero.ItemGroup('collection',newRows[i]), thisLevel+1, row+i+1); //insert new row } } this._dataItems[row][1] = !this._dataItems[row][1]; //toggle container open value this._treebox.rowCountChanged(row+1, count); //tell treebox to repaint these this._treebox.invalidateRow(row); this._treebox.endUpdateBatch(); this._refreshHashMap(); } //////////////////////////////////////////////////////////////////////////////// /// /// Additional functions for managing data in the tree /// //////////////////////////////////////////////////////////////////////////////// /* * Delete the selection */ Zotero.CollectionTreeView.prototype.deleteSelection = function() { if(this.selection.count == 0) return; //collapse open collections for(var i=0; i