zotero/chrome/content/zotero/lookup.js

226 lines
6.5 KiB
JavaScript
Raw Normal View History

2009-12-28 09:47:49 +00:00
/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2009-2011 Center for History and New Media
George Mason University, Fairfax, Virginia, USA
http://zotero.org
2009-12-28 09:47:49 +00:00
This file is part of Zotero.
Zotero is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
2009-12-28 09:47:49 +00:00
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Zotero is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
2009-12-28 09:47:49 +00:00
You should have received a copy of the GNU Affero General Public License
2009-12-28 09:47:49 +00:00
along with Zotero. If not, see <http://www.gnu.org/licenses/>.
***** END LICENSE BLOCK *****
*/
/**
* Handles UI for lookup panel
* @namespace
*/
var Zotero_Lookup = new function () {
/**
* Performs a lookup by DOI, PMID, or ISBN
*/
this.accept = Zotero.Promise.coroutine(function* (textBox) {
var identifiers = Zotero.Utilities.Internal.extractIdentifiers(textBox.value);
if (!identifiers.length) {
Zotero.alert(
window,
Zotero.getString("lookup.failure.title"),
Zotero.getString("lookup.failureToID.description")
);
return false;
}
var libraryID = false;
2.0b3 megacommit - Support for group libraries - General support for multiple libraries of different types - Streamlined sync support - Using solely libraryID and key rather than itemID, and removed all itemID-changing code - Combined two requests for increased performance and decreased server load - Added warning on user account change - Provide explicit error message on SSL failure - Removed snapshot and link toolbar buttons and changed browser context menu options and drags to create parent items + snapshots - Closes #786, Add numPages field - Fixes #1063, Duplicate item with tags broken in Sync Preview - Added better purging of deleted tags - Added local user key before first sync - Add clientDateModified to all objects for more flexibility in syncing - Added new triples-based Relation object type, currently used to store links between items copied between local and group libraries - Updated zotero.org translator for groups - Additional trigger-based consistency checks - Fixed broken URL drag in Firefox 3.5 - Disabled zeroconf menu option (no longer functional) Developer-specific changes: - Overhauled data layer - Data object constructors no longer take arguments (return to 1.0-like API) - Existing objects can be retrieved by setting id or library/key properties - id/library/key must be set for new objects before other fields - New methods: - ZoteroPane.getSelectedLibraryID() - ZoteroPane.getSelectedGroup(asID) - ZoteroPane.addItemFromDocument(doc, itemType, saveSnapshot) - ZoteroPane.addItemFromURL(url, itemType) - ZoteroPane.canEdit() - Zotero.CollectionTreeView.selectLibrary(libraryID) - New Zotero.URI methods - Changed methods - Many data object methods now take a libraryID - ZoteroPane.addAttachmentFromPage(link, itemID) - Removed saveItem and saveAttachments parameters from Zotero.Translate constructor - translate() now takes a libraryID, null for local library, or false to not save items (previously on constructor) - saveAttachments is now a translate() parameter - Zotero.flattenArguments() better handles passed objects - Zotero.File.getFileHash() (not currently used)
2009-05-14 18:23:40 +00:00
var collection = false;
2009-04-11 04:03:23 +00:00
try {
libraryID = ZoteroPane_Local.getSelectedLibraryID();
collection = ZoteroPane_Local.getSelectedCollection();
2012-11-23 23:07:20 -06:00
} catch(e) {
/** TODO: handle this **/
}
var successful = 0; //counter for successful retrievals
Zotero_Lookup.toggleProgress(true);
for (let identifier of identifiers) {
var translate = new Zotero.Translate.Search();
translate.setIdentifier(identifier);
// be lenient about translators
let translators = yield translate.getTranslators();
translate.setTranslator(translators);
try {
let newItems = yield translate.translate({
libraryID,
collections: collection ? [collection.id] : false
});
successful++;
}
// Continue with other ids on failure
catch (e) {
Zotero.logError(e);
}
}
Zotero_Lookup.toggleProgress(false);
// TODO: Give indication if some failed
if (successful) {
document.getElementById("zotero-lookup-panel").hidePopup();
}
else {
Zotero.alert(
window,
Zotero.getString("lookup.failure.title"),
Zotero.getString("lookup.failure.description")
);
}
2009-04-11 04:03:23 +00:00
return false;
});
this.showPanel = function (button) {
var panel = document.getElementById('zotero-lookup-panel');
panel.openPopup(button, "after_start", 16, -2, false, false);
}
/**
* Focuses the field
*/
this.onShowing = function (event) {
// Ignore context menu
if (event.originalTarget.id != 'zotero-lookup-panel') return;
document.getElementById("zotero-lookup-panel").style.padding = "10px";
this.getActivePanel().getElementsByTagName('textbox')[0].focus();
}
/**
* Cancels the popup and resets fields
*/
this.onHidden = function (event) {
// Ignore context menu
if (event.originalTarget.id != 'zotero-lookup-panel') return;
document.getElementById("zotero-lookup-textbox").value = "";
document.getElementById("zotero-lookup-multiline-textbox").value = "";
Zotero_Lookup.toggleProgress(false);
}
this.getActivePanel = function() {
var mlPanel = document.getElementById("zotero-lookup-multiline");
if (mlPanel.collapsed) return document.getElementById("zotero-lookup-singleLine");
return mlPanel;
}
2011-07-24 17:49:16 +00:00
/**
* Handles a key press
*/
this.onKeyPress = function(event, textBox) {
2011-07-24 17:49:16 +00:00
var keyCode = event.keyCode;
//use enter to start search, shift+enter to insert a new line. Flipped in multiline mode
var multiline = textBox.getAttribute('multiline');
var search = multiline ? event.shiftKey : !event.shiftKey;
2011-07-24 17:49:16 +00:00
if(keyCode === 13 || keyCode === 14) {
if(search) {
Zotero_Lookup.accept(textBox);
event.stopImmediatePropagation();
} else if(!multiline) { //switch to multiline
var mlTextbox = Zotero_Lookup.toggleMultiline(true);
mlTextbox.value = mlTextbox.value + '\n';
}
2011-07-24 17:49:16 +00:00
} else if(keyCode == event.DOM_VK_ESCAPE) {
document.getElementById("zotero-lookup-panel").hidePopup();
}
return true;
}
this.onInput = function (event, textbox) {
this.adjustTextbox(textbox);
};
/**
* Converts the textbox to multiline if newlines are detected
*/
this.adjustTextbox = function (textbox) {
if (textbox.value.trim().match(/[\r\n]/)) {
Zotero_Lookup.toggleMultiline(true);
}
// Since we ignore trailing and leading newlines, we should also trim them for display
// can't use trim, because then we cannot add leading/trailing spaces to the single line textbox
else {
textbox.value = textbox.value.replace(/^([ \t]*[\r\n]+[ \t]*)+|([ \t]*[\r\n]+[ \t]*)+$/g,"");
}
}
/**
* Performs the switch to multiline textbox and returns that textbox
*/
this.toggleMultiline = function(on) {
var mlPanel = document.getElementById("zotero-lookup-multiline");
var mlTxtBox = document.getElementById("zotero-lookup-multiline-textbox");
var slPanel = document.getElementById("zotero-lookup-singleLine");
var slTxtBox = document.getElementById("zotero-lookup-textbox");
var source = on ? slTxtBox : mlTxtBox;
var dest = on ? mlTxtBox : slTxtBox;
if((mlPanel.collapsed && !on) || (!mlPanel.collapsed && on)) return dest;
//copy over the value
dest.value = source.value;
//switch textboxes
mlPanel.setAttribute("collapsed", !on);
slPanel.setAttribute("collapsed", !!on);
// Resize arrow box to fit content
if(Zotero.isMac) {
var panel = document.getElementById("zotero-lookup-panel");
var box = panel.firstChild;
panel.sizeTo(box.scrollWidth, box.scrollHeight);
}
dest.focus();
return dest;
}
this.toggleProgress = function(on) {
// In Firefox 52.6.0, progressmeters burn CPU at idle on Linux when undetermined, even
// if they're hidden. (Being hidden is enough on macOS.)
var mode = on ? 'undetermined' : 'determined';
//single line
var txtBox = document.getElementById("zotero-lookup-textbox");
txtBox.style.opacity = on ? 0.5 : 1;
txtBox.disabled = !!on;
var p1 = document.getElementById("zotero-lookup-progress");
p1.mode = mode;
//multiline
document.getElementById("zotero-lookup-multiline-textbox").disabled = !!on;
var p2 = document.getElementById("zotero-lookup-multiline-progress");
p2.mode = mode;
}
2009-04-11 04:03:23 +00:00
}