From f3a8cbd993f116db716fd2a4b9c794b5f405bc39 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Wed, 7 Jul 2010 18:34:09 +0000 Subject: [PATCH] Switch back to multiple items per Zotero Commons bucket - Creating buckets not yet supported - Some now-unused single-item-per-bucket code still needs to be removed - Child attachments are not displayed in Zotero Commons pane - Manual refreshing via bucket right-click (or a Firefox restart) is currently necessary after adding items - Double-clicking bucket takes you to bucket page; double-clicking item takes you to named anchor --- chrome/content/zotero/overlay.js | 69 +- chrome/content/zotero/overlay.xul | 2 +- .../zotero/xpcom/collectionTreeView.js | 100 +- chrome/content/zotero/xpcom/commons.js | 1072 ++++++++++------- chrome/content/zotero/xpcom/itemTreeView.js | 25 +- chrome/content/zotero/xpcom/notifier.js | 2 +- 6 files changed, 741 insertions(+), 529 deletions(-) diff --git a/chrome/content/zotero/overlay.js b/chrome/content/zotero/overlay.js index 827ca6f51..5ab40eeff 100644 --- a/chrome/content/zotero/overlay.js +++ b/chrome/content/zotero/overlay.js @@ -1343,7 +1343,7 @@ var ZoteroPane = new function() } var itemGroup = this.itemsView._itemGroup; - if (!itemGroup.isTrash() && !itemGroup.isCommons() && !this.canEdit()) { + if (!itemGroup.isTrash() && !itemGroup.isBucket() && !this.canEdit()) { this.displayCannotEditLibraryMessage(); return; } @@ -1383,7 +1383,7 @@ var ZoteroPane = new function() else if (itemGroup.isShare()) { return; } - else if (itemGroup.isCommons()) { + else if (itemGroup.isBucket()) { var prompt = toDelete; } // Do nothing in trash view if any non-deleted items are selected @@ -1465,22 +1465,24 @@ var ZoteroPane = new function() } } - this.refreshCommons = function() { - if (this.collectionsView - && this.collectionsView.selection - && this.collectionsView.selection.count == 1 - && this.collectionsView.selection.currentIndex != -1) { - var itemGroup = this.collectionsView._getItemAtRow(this.collectionsView.selection.currentIndex); - if (itemGroup && itemGroup.isCommons()) { - var self = this; - Zotero.Commons.syncBucketList(function () { - self.itemsView.refresh(); - self.itemsView.sort(); - - // On a manual refresh, also check for new OCRed files - Zotero.Commons.syncFiles(); - }); - } + this.refreshCommonsBucket = function() { + if (!this.collectionsView + || !this.collectionsView.selection + || this.collectionsView.selection.count != 1 + || this.collectionsView.selection.currentIndex == -1) { + return false; + } + + var itemGroup = this.collectionsView._getItemAtRow(this.collectionsView.selection.currentIndex); + if (itemGroup && itemGroup.isBucket()) { + var self = this; + itemGroup.ref.refreshItems(function () { + self.itemsView.refresh(); + self.itemsView.sort(); + + // On a manual refresh, also check for new OCRed files + //Zotero.Commons.syncFiles(); + }); } } @@ -1783,7 +1785,7 @@ var ZoteroPane = new function() exportFile: 9, loadReport: 10, emptyTrash: 11, - refreshCommons: 12 + refreshCommonsBucket: 12 }; var itemGroup = this.collectionsView._getItemAtRow(this.collectionsView.selection.currentIndex); @@ -1854,8 +1856,8 @@ var ZoteroPane = new function() else if (itemGroup.isHeader()) { // Don't show menu for headers } - else if (itemGroup.isCommons()) { - show = [m.refreshCommons]; + else if (itemGroup.isBucket()) { + show = [m.refreshCommonsBucket]; } // Group else if (itemGroup.isGroup()) { @@ -2156,11 +2158,15 @@ var ZoteroPane = new function() // TODO: implement menu for remote items if (!itemGroup.editable) { for (var i in m) { - switch (i) { - case 'exportItems': - case 'createBib': - case 'loadReport': - continue; + // Still show export/bib/report for non-editable views + // (other than Commons buckets, which aren't real items) + if (!itemGroup.isBucket()) { + switch (i) { + case 'exportItems': + case 'createBib': + case 'loadReport': + continue; + } } disable.push(m[i]); var index = enable.indexOf(m[i]); @@ -2273,9 +2279,8 @@ var ZoteroPane = new function() return; } - if (itemGroup.isCommons()) { - // TODO: take to a search of the user's buckets? - //ZoteroPane.loadURI(itemGroup.ref.uri); + if (itemGroup.isBucket()) { + ZoteroPane.loadURI(itemGroup.ref.uri); event.stopPropagation(); } } @@ -2302,9 +2307,9 @@ var ZoteroPane = new function() ZoteroPane.collectionsView.selection.currentIndex ); - if (itemGroup.isCommons()) { - var bucket = Zotero.Commons.getBucketFromItem(item); - ZoteroPane.loadURI(bucket.uri); + if (itemGroup.isBucket()) { + var uri = itemGroup.ref.getItemURI(item); + ZoteroPane.loadURI(uri); event.stopPropagation(); return; } diff --git a/chrome/content/zotero/overlay.xul b/chrome/content/zotero/overlay.xul index de1299d99..5b609616f 100644 --- a/chrome/content/zotero/overlay.xul +++ b/chrome/content/zotero/overlay.xul @@ -107,7 +107,7 @@ - + diff --git a/chrome/content/zotero/xpcom/collectionTreeView.js b/chrome/content/zotero/xpcom/collectionTreeView.js index b19503cdd..44986f793 100644 --- a/chrome/content/zotero/xpcom/collectionTreeView.js +++ b/chrome/content/zotero/xpcom/collectionTreeView.js @@ -39,7 +39,7 @@ Zotero.CollectionTreeView = function() this._treebox = null; this.itemToSelect = null; this._highlightedRows = {}; - this._unregisterID = Zotero.Notifier.registerObserver(this, ['collection', 'search', 'share', 'group', 'commons']); + this._unregisterID = Zotero.Notifier.registerObserver(this, ['collection', 'search', 'share', 'group', 'bucket']); this.showDuplicates = false; } @@ -149,6 +149,16 @@ Zotero.CollectionTreeView.prototype.setTree = function(treebox) */ Zotero.CollectionTreeView.prototype.refresh = function() { + // Record open states before refreshing + if (this._dataItems) { + for (var i=0, len=this._dataItems.length; i/, '')); + } + catch (e) { + alert("Invalid response retrieving file upload parameters"); + return; + } + + var zipsXML = xml.file.(@source == 'original').(format == 'Zotero ZIP Item'); + + Zotero.debug(zipsXML); + + var zips = []; + + // Generate a list of extracted PDFs with OCRed equivalents + var ocrOriginals = {}; + var ocrPDFXML = xml.file.(@source == 'derivative').(format == 'Additional Text PDF').@name; + for each(var pdf in ocrPDFXML) { + var fn = pdf.toString().replace(/_text\.pdf$/, '.pdf'); + ocrOriginals[fn] = true; + } + + for each(var zipXML in zipsXML) { + var key = zipXML.@name.toString(); + var title = zipXML.title.toString(); + + var childrenXML = xml.file.(typeof zipsource != 'undefined').(zipsource == key).(format != 'Zotero RDF'); + var children = []; + for each(var childXML in childrenXML) { + var childKey = childXML.@name.toString(); + children.push({ + key: childKey, + title: childXML.title.toString(), + ocr: ocrOriginals[childKey] ? true : false + }); + } + +/* + // See if we already have this item + if (self._items[key]) { + continue; + } +*/ + zips.push({ + key: key, + title: title, + children: children + }); + } + + self._itemsLoading = false; + self._itemsLoaded = true; + + Zotero.debug(zips); + + Zotero.Notifier.trigger('refresh', 'bucket', self.id); + + // Get RDF for new items, pulling off a stack + var process = function (zips) { + if (!zips.length) { + Zotero.debug("Commons: No more ZIPs to process"); + + if (callback) { + callback(); + } + + return; + } + + let zip = zips.shift(); + + // See if we already have this item + if (self._items[zip.key]) { + process(zips); + return; + } + + var rdfURI = self.downloadURI + '/' + zip.key; + rdfURI = rdfURI.replace(/\.zip$/, "_zotero.rdf"); + + Zotero.Utilities.HTTP.doGet(rdfURI, function (xmlhttp) { + // If RDF not available, skip item + if (xmlhttp.status != 200) { + Zotero.debug("RDF not found at " + xmlhttp.channel.originalURI.spec); + process(zips); + return; + } + + Zotero.debug(xmlhttp.responseText); + + var translate = new Zotero.Translate("import"); + translate.setString(xmlhttp.responseText); + translate.getTranslators() + translate.setTranslator(Zotero.Commons.RDF_IMPORT_TRANSLATOR.translatorID); + translate.setHandler("itemDone", function (translation, item) { + var typeID = Zotero.ItemTypes.getID(item.itemType); + var newItem = new Zotero.Item(typeID); + newItem.id = Zotero.ID.getBigInt(); + + // Add item data to virtual item + for (var field in item) { + // Skip empty fields + if (!item[field]) { + continue; + } + var fieldID = Zotero.ItemFields.getID(field); + if (!fieldID) { + continue; + } + + try { + newItem.setField(fieldID, item[field]); + } + catch (e) { + Zotero.debug(e); + } + } + + // Add creators to virtual item + for (var i=0; i 1) { + throw ("Commons: More than one local item linked to remote item " + uri); } - this._metadataLoading = true; - var self = this; + var item = Zotero.URI.getURIItem(rels[0].subject); + if (!item) { + Zotero.debug("Linked local item not found for URI " + this.uri, 2); + return false; + } - Zotero.Utilities.HTTP.doGet(this.rdfURI, function (xmlhttp) { - // If RDF not available, use the bucket metadata - if (xmlhttp.status != 200) { - Zotero.debug("RDF for bucket " + self.name + " not available"); - - /*Zotero.Utilities.HTTP.doGet(self.metadataURI, function (xmlhttp) { - if (xmlhttp.status != 200) { - Zotero.debug(xmlhttp.status); - Zotero.debug(xmlhttp.responseText); - return; - } - - Zotero.debug(xmlhttp.responseText); - - // Strip XML declaration and convert to E4X - var xml = new XML(xmlhttp.responseText.replace(/<\?xml.*\?>/, '')); - var title = xml.title; - if (!self._item) { - self._item = new Zotero.Item("document"); - self._item.id = Zotero.ID.getBigInt(); - } - self._item.setField('title', title); - - self._metadataLoading = false; - - if (callback) { - callback(); - } - });*/ - return; + return item; +} + + +Zotero.Commons.Bucket.prototype._getCachedItems = function () { + var items = []; + for each(var item in this._items) { + items.push(item); + } + return items; +} + + +Zotero.Commons.Bucket.prototype._getIAKeyByItemID = function (id) { + for (var key in this._items) { + if (this._items[key].id == id) { + return key; } - - Zotero.debug(xmlhttp.responseText); - - var translate = new Zotero.Translate("import"); - translate.setString(xmlhttp.responseText); - translate.getTranslators() - translate.setTranslator(Zotero.Commons.RDF_IMPORT_TRANSLATOR.translatorID); - translate.setHandler("itemDone", function (translation, item) { - var typeID = Zotero.ItemTypes.getID(item.itemType); - var newItem = new Zotero.Item(typeID); - newItem.id = Zotero.ID.getBigInt(); - - for (var field in item) { - // Skip empty fields - if (!item[field]) { - continue; - } - var fieldID = Zotero.ItemFields.getID(field); - if (!fieldID) { - continue; - } - Zotero.debug('setting field ' + fieldID + ' to ' + item[field]); - try { - newItem.setField(fieldID, item[field]); - } - catch(e) { Zotero.debug(e); } - } - - self._item = newItem; - self._metadataLoading = false; - - if (callback) { - callback(); - } - return; - }); - translate.translate(false, false); - }); + } + return false; } diff --git a/chrome/content/zotero/xpcom/itemTreeView.js b/chrome/content/zotero/xpcom/itemTreeView.js index a8c16facb..d7204d02c 100644 --- a/chrome/content/zotero/xpcom/itemTreeView.js +++ b/chrome/content/zotero/xpcom/itemTreeView.js @@ -52,7 +52,7 @@ Zotero.ItemTreeView = function(itemGroup, sourcesOnly) this._dataItems = []; this.rowCount = 0; - this._unregisterID = Zotero.Notifier.registerObserver(this, ['item', 'collection-item', 'share-items', 'commons']); + this._unregisterID = Zotero.Notifier.registerObserver(this, ['item', 'collection-item', 'share-items', 'bucket']); } @@ -260,7 +260,6 @@ Zotero.ItemTreeView.prototype.refresh = function() */ Zotero.ItemTreeView.prototype.notify = function(action, type, ids, extraData) { - Zotero.debug('============='); if (!this._treebox || !this._treebox.treeBody) { Components.utils.reportError("Treebox didn't exist in itemTreeView.notify()"); return; @@ -270,7 +269,7 @@ Zotero.ItemTreeView.prototype.notify = function(action, type, ids, extraData) Zotero.debug("Item row map didn't exist in itemTreeView.notify()"); return; } - Zotero.debug(1); + var itemGroup = this._itemGroup; var madeChanges = false; @@ -285,10 +284,9 @@ Zotero.ItemTreeView.prototype.notify = function(action, type, ids, extraData) this.refresh(); } } - else if (type == 'commons') { - if (itemGroup.isCommons()) { + else if (type == 'bucket') { + if (itemGroup.isBucket()) { this.refresh(); - this.sort(); } } else if (savedSelection.length == 1 && savedSelection[0] == ids[0]) { @@ -457,7 +455,6 @@ Zotero.ItemTreeView.prototype.notify = function(action, type, ids, extraData) } else if(action == 'add') { - Zotero.debug(2); // If saved search or trash, just re-run search if (itemGroup.isSearch() || itemGroup.isTrash()) { this.refresh(); @@ -502,7 +499,6 @@ Zotero.ItemTreeView.prototype.notify = function(action, type, ids, extraData) if(madeChanges) { - Zotero.debug(3); // If adding and this is the active window, select the item if(action == 'add' && ids.length===1 && activeWindow) { @@ -515,7 +511,7 @@ Zotero.ItemTreeView.prototype.notify = function(action, type, ids, extraData) // Reset to Info tab this._ownerDocument.getElementById('zotero-view-tabbox').selectedIndex = 0; - Zotero.debug(4); + this.selectItem(ids[0]); } // If single item is selected and was modified @@ -544,7 +540,6 @@ Zotero.ItemTreeView.prototype.notify = function(action, type, ids, extraData) } else { - Zotero.debug(4); var previousRow = this._itemRowMap[ids[0]]; if (sort) { @@ -1117,8 +1112,6 @@ Zotero.ItemTreeView.prototype.sort = function(itemID) */ Zotero.ItemTreeView.prototype.selectItem = function(id, expand, noRecurse) { - Zotero.debug('============='); - Zotero.debug(Zotero.suppressUIUpdates); // Don't change selection if UI updates are disabled (e.g., during sync) if (Zotero.suppressUIUpdates) { return; @@ -1262,7 +1255,10 @@ Zotero.ItemTreeView.prototype.deleteSelection = function (force) var itemGroup = this._itemGroup; - if (itemGroup.isTrash()) { + if (itemGroup.isBucket()) { + itemGroup.ref.deleteItems(ids); + } + else if (itemGroup.isTrash()) { Zotero.Items.erase(ids); } else if (itemGroup.isGroup() || (force && itemGroup.isWithinGroup())) { @@ -1274,9 +1270,6 @@ Zotero.ItemTreeView.prototype.deleteSelection = function (force) else if (itemGroup.isCollection()) { itemGroup.ref.removeItems(ids); } - else if (itemGroup.isCommons()) { - Zotero.Commons.deleteItems(ids); - } this._treebox.endUpdateBatch(); } diff --git a/chrome/content/zotero/xpcom/notifier.js b/chrome/content/zotero/xpcom/notifier.js index d82e5954e..0a231d6b8 100644 --- a/chrome/content/zotero/xpcom/notifier.js +++ b/chrome/content/zotero/xpcom/notifier.js @@ -28,7 +28,7 @@ Zotero.Notifier = new function(){ var _disabled = false; var _types = [ 'collection', 'creator', 'search', 'share', 'share-items', 'item', - 'collection-item', 'item-tag', 'tag', 'group', 'commons' + 'collection-item', 'item-tag', 'tag', 'group', 'bucket' ]; var _inTransaction; var _locked = false;