Add context menu to translate icon, allowing the user to save to Zotero with alternative translators and to perform library lookup or use lookup engines without first saving the item

This commit is contained in:
Simon Kornblith 2011-08-28 20:12:08 +00:00
parent 46dc429d73
commit 953ec31166
6 changed files with 118 additions and 33 deletions

View file

@ -125,7 +125,7 @@ var Zotero_Browser = new function() {
* Scrapes a page (called when the capture icon is clicked
* @return void
*/
function scrapeThisPage(/*libraryID, collectionID*/) {
function scrapeThisPage(translator) {
if (Zotero.locked) {
Zotero_Browser.progress.changeHeadline(Zotero.getString("ingester.scrapeError"));
var desc = Zotero.localeJoin([
@ -159,7 +159,7 @@ var Zotero_Browser = new function() {
}
// translate into specified library and collection
_getTabObject(this.tabbrowser.selectedBrowser).translate(libraryID, collectionID);
_getTabObject(Zotero_Browser.tabbrowser.selectedBrowser).translate(libraryID, collectionID, translator);
}
/*
@ -480,23 +480,85 @@ var Zotero_Browser = new function() {
}
}
/**
* Called when status bar icon is right-clicked
*/
this.onStatusPopupShowing = function(e) {
var popup = e.target;
while(popup.hasChildNodes()) popup.removeChild(popup.lastChild);
var tab = _getTabObject(this.tabbrowser.selectedBrowser);
var translators = tab.page.translators;
for(var i=0, n=translators.length; i<n; i++) {
let translator = translators[i];
var menuitem = document.createElement("menuitem");
menuitem.setAttribute("label",
Zotero.getString("ingester.saveToZoteroUsing", translator.label));
menuitem.setAttribute("image", (translator.itemType === "multiple"
? "chrome://zotero/skin/treesource-collection.png"
: Zotero.ItemTypes.getImageSrc(translator.itemType)));
menuitem.setAttribute("class", "menuitem-iconic");
menuitem.addEventListener("command", function(e) {
scrapeThisPage(translator);
}, false);
popup.appendChild(menuitem);
}
popup.appendChild(document.createElement("menuseparator"));
var menuitem = document.createElement("menuitem");
menuitem.setAttribute("label", Zotero.getString("locate.libraryLookup.label"));
menuitem.setAttribute("tooltiptext", Zotero.getString("locate.libraryLookup.tooltip"));
menuitem.setAttribute("image", "chrome://zotero/skin/locate-library-lookup.png");
menuitem.setAttribute("class", "menuitem-iconic");
menuitem.addEventListener("command", _constructLookupFunction(tab, function(event, obj) {
var urls = [];
for each(var item in obj.newItems) {
var url = Zotero.OpenURL.resolve(item);
if(url) urls.push(url);
}
ZoteroPane.loadURI(urls, event);
}), false);
popup.appendChild(menuitem);
var locateEngines = Zotero.LocateManager.getVisibleEngines();
Zotero_LocateMenu.addLocateEngines(popup, locateEngines,
_constructLookupFunction(tab, function(e, obj) {
Zotero_LocateMenu.locateItem(e, obj.newItems);
}), true);
}
//////////////////////////////////////////////////////////////////////////////
//
// Private Zotero_Browser methods
//
//////////////////////////////////////////////////////////////////////////////
function _constructLookupFunction(tab, success) {
return function(e) {
tab.page.translate.setTranslator(tab.page.translators[0]);
tab.page.translate.clearHandlers("done");
tab.page.translate.clearHandlers("itemDone");
tab.page.translate.setHandler("done", function(obj, status) {
if(status) {
success(e, obj);
Zotero_Browser.progress.close();
} else {
Zotero_Browser.progress.changeHeadline(Zotero.getString("ingester.lookup.error"));
Zotero_Browser.progress.startCloseTimer(8000);
}
});
Zotero_Browser.progress.show();
Zotero_Browser.progress.changeHeadline(Zotero.getString("ingester.lookup.performing"));
tab.page.translate.translate(false);
}
}
/*
* Gets a data object given a browser window object
*
* NOTE: Browser objects are associated with document objects via keys generated
* from the time the browser object is opened. I'm not sure if this is the
* appropriate mechanism for handling this, but it's what PiggyBank used and it
* appears to work.
*
* Currently, the data object contains only one property: "translators," which
* is an array of translators that should work with the given page as returned
* from Zotero.Translate.getTranslator()
*/
function _getTabObject(browser) {
if(!browser) return false;
@ -506,8 +568,8 @@ var Zotero_Browser = new function() {
return browser.zoteroBrowserData;
}
/*
* adds an annotation
/**
* Adds an annotation
*/
function _add(type, e) {
var tab = _getTabObject(Zotero_Browser.tabbrowser.selectedBrowser);
@ -582,7 +644,7 @@ Zotero_Browser.Tab.prototype.detectTranslators = function(rootDoc, doc) {
var translate = new Zotero.Translate.Web();
translate.setDocument(doc);
translate.setHandler("translators", function(obj, item) { me._translatorsAvailable(obj, item) });
translate.getTranslators();
translate.getTranslators(true);
} else if(doc.documentURI.substr(0, 7) == "file://") {
this._attemptLocalFileImport(doc);
}
@ -646,7 +708,7 @@ Zotero_Browser.Tab.prototype._attemptLocalFileImport = function(doc) {
* @param {Integer} libraryID
* @param {Integer} collectionID
*/
Zotero_Browser.Tab.prototype.translate = function(libraryID, collectionID) {
Zotero_Browser.Tab.prototype.translate = function(libraryID, collectionID, translator) {
if(this.page.translators && this.page.translators.length) {
Zotero_Browser.progress.show();
Zotero_Browser.isScraping = true;
@ -660,14 +722,13 @@ Zotero_Browser.Tab.prototype.translate = function(libraryID, collectionID) {
var me = this;
if(!this.page.hasBeenTranslated) {
// use first translator available
this.page.translate.setTranslator(this.page.translators[0]);
this.page.translate.setHandler("select", me._selectItems);
this.page.translate.setHandler("done", function(obj, item) { Zotero_Browser.finishScraping(obj, item) });
this.page.hasBeenTranslated = true;
}
// use first translator available
this.page.translate.setTranslator(translator ? translator : this.page.translators[0]);
this.page.translate.clearHandlers("done");
this.page.translate.clearHandlers("itemDone");
this.page.translate.setHandler("done", function(obj, item) { Zotero_Browser.finishScraping(obj, item) });
this.page.translate.setHandler("itemDone", function(obj, dbItem, item) { Zotero_Browser.itemDone(obj, dbItem, item, collection) });
this.page.translate.translate(libraryID);
@ -728,10 +789,14 @@ Zotero_Browser.Tab.prototype._translatorsAvailable = function(translate, transla
this.page.translate = translate;
this.page.translators = translators;
this.page.document = translate.document;
this.page.translate.clearHandlers("select");
this.page.translate.setHandler("select", this._selectItems);
} else if(translate.type != "import" && translate.document.documentURI.length > 7
&& translate.document.documentURI.substr(0, 7) == "file://") {
this._attemptLocalFileImport(translate.document);
}
Zotero_Browser.updateStatus();
}

View file

@ -50,7 +50,7 @@ var Zotero_LocateMenu = new function() {
var availableEngines = _getAvailableLocateEngines(selectedItems);
// add engines that are available for selected items
if(availableEngines.length) {
_addLocateEngines(locateMenu, availableEngines, true);
Zotero_LocateMenu.addLocateEngines(locateMenu, availableEngines, null, true);
}
} else {
// add "no items selected"
@ -207,16 +207,21 @@ var Zotero_LocateMenu = new function() {
/**
* Add locate engine options to a menu
* @param {menupopup} menu The menu to add menu items to
* @param {Zotero.LocateManater.LocateEngine[]} engines The list of engines to add to the menu
* @param {Zotero.LocateManager.LocateEngine[]} engines The list of engines to add to the menu
* @param {Function|null} items Function to call to locate items
* @param {Boolean} showIcons Whether menu items should have associated icons
*/
function _addLocateEngines(menu, engines, showIcons) {
this.addLocateEngines = function(menu, engines, locateFn, showIcons) {
if(!locateFn) {
locateFn = this.locateItem;
}
for each(var engine in engines) {
var menuitem = _createMenuItem(engine.name, null, engine.description);
menuitem.setAttribute("class", "menuitem-iconic");
menuitem.setAttribute("image", engine.icon);
menu.appendChild(menuitem);
menuitem.addEventListener("command", _locateItem, false);
menuitem.addEventListener("command", locateFn, false);
}
}
@ -270,8 +275,10 @@ var Zotero_LocateMenu = new function() {
/**
* Locate selected items
*/
function _locateItem(event) {
var selectedItems = _getSelectedItems();
this.locateItem = function(event, selectedItems) {
if(!selectedItems) {
selectedItems = _getSelectedItems();
}
// find selected engine
var selectedEngine = Zotero.LocateManager.getEngineByName(event.target.label);

View file

@ -69,7 +69,12 @@
<!-- Scrape Code -->
<hbox id="urlbar-icons">
<image src="chrome://zotero/skin/treeitem-book.png" id="zotero-status-image" onclick="Zotero_Browser.scrapeThisPage()" position="1" hidden="true"/>
<popupset>
<menupopup id="zotero-status-image-context" onpopupshowing="Zotero_Browser.onStatusPopupShowing(event)"/>
</popupset>
<image src="chrome://zotero/skin/treeitem-book.png" id="zotero-status-image"
onclick="if(event.button === 0) Zotero_Browser.scrapeThisPage()" context="zotero-status-image-context"
position="1" hidden="true"/>
</hbox>
<menupopup id="menu_ToolsPopup">

View file

@ -269,7 +269,7 @@ Zotero.LocateManager = new function() {
var ns = engine._urlNamespaces[nsPrefix];
if(!ns) return false;
} else {
if(param === "searchTerms") return [item.getField("title")];
if(param === "searchTerms") return [item.title];
return false;
}
@ -277,7 +277,7 @@ Zotero.LocateManager = new function() {
// take a normal "title," even though we don't use it, because it is valid (but not
// preferred) OpenURL
if(param === "title") {
var title = item.getField("title");
var title = item.title;
return (title ? [encodeURIComponent(title)] : false);
}
@ -298,7 +298,7 @@ Zotero.LocateManager = new function() {
} else if(param === "year") {
return (itemOpenURL["rft.date"] ? [itemOpenURL["rft.date"][0].substr(0, 4)] : false);
} else {
var result = item.getField(param);
var result = item[param];
return (result ? [encodeURIComponent(result)] : false);
}
} else {
@ -412,6 +412,10 @@ Zotero.LocateManager = new function() {
if(responseType && responseType !== "text/html") {
throw "LocateManager supports only responseType text/html";
}
if(item.toArray) {
item = item.toArray();
}
var itemAsOpenURL = Zotero.OpenURL.createContextObject(item, "1.0", true);

View file

@ -43,7 +43,7 @@
<script src="reportInterface.js"/>
<script src="timelineInterface.js"/>
<script src="recognizePDF.js"/>
<script src="browser.js"/>
<script src="browser.js" type="application/javascript;version=1.8"/>
<script src="lookup.js"/>
<script src="locateMenu.js" type="application/javascript;version=1.8"/>

View file

@ -398,6 +398,7 @@ save.link = Saving Link…
save.link.error = An error occurred while saving this link.
ingester.saveToZotero = Save to Zotero
ingester.saveToZoteroUsing = Save to Zotero using "%S"
ingester.scraping = Saving Item…
ingester.scrapeComplete = Item Saved
ingester.scrapeError = Could Not Save Item
@ -412,6 +413,9 @@ ingester.importReferRISDialog.checkMsg = Always allow for this site
ingester.importFile.title = Import File
ingester.importFile.text = Do you want to import the file "%S"?\n\nItems will be added to a new collection.
ingester.lookup.performing = Performing Lookup…
ingester.lookup.error = An error occurred while performing lookup for this item.
db.dbCorrupted = The Zotero database '%S' appears to have become corrupted.
db.dbCorrupted.restart = Please restart %S to attempt an automatic restore from the last backup.
db.dbCorruptedNoBackup = The Zotero database '%S' appears to have become corrupted, and no automatic backup is available.\n\nA new database file has been created. The damaged file was saved in your Zotero directory.