/* Zotero Copyright (C) 2006 Center for History and New Media, George Mason University, Fairfax, VA http://chnm.gmu.edu/ */ //////////////////////////////////////////////////////////////////////////////// /// /// 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 */ Scholar.CollectionTreeView = function() { this._treebox = null; this.refresh(); this._unregisterID = Scholar.Notifier.registerColumnTree(this); } /* * Called by the tree itself */ Scholar.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.) */ Scholar.CollectionTreeView.prototype.refresh = function() { this._dataItems = new Array(); this.rowCount = 0; this._showItem(new Scholar.ItemGroup('library',null),0,1); var newRows = Scholar.getCollections(); for(var i = 0; i < newRows.length; i++) this._showItem(new Scholar.ItemGroup('collection',newRows[i]), 0, this._dataItems.length); //itemgroup ref, level, beforeRow var savedSearches = Scholar.Searches.getAll(); for(var i = 0; i < savedSearches.length; i++) this._showItem(new Scholar.ItemGroup('search',savedSearches[i]), 0, this._dataItems.length); //itemgroup ref, level, beforeRow this._refreshHashMap(); } /* * Redisplay everything */ Scholar.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 Scholar.Notifier on any changes to collections in the data layer */ Scholar.CollectionTreeView.prototype.notify = function(action, type, ids) { var madeChanges = false; var ids = Scholar.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; } Scholar.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 */ Scholar.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 = Scholar.getCollections(this._getItemAtRow(row).ref.getID()); //Get children for(var i = 0; i < newRows.length; i++) { count++; this._showItem(new Scholar.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 */ Scholar.CollectionTreeView.prototype.deleteSelection = function() { if(this.selection.count == 0) return; //collapse open collections for(var i=0; i