2009-12-28 09:47:49 +00:00
|
|
|
/*
|
|
|
|
***** BEGIN LICENSE BLOCK *****
|
|
|
|
|
2011-07-24 17:41:48 +00:00
|
|
|
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
|
2011-05-18 18:34:22 +00:00
|
|
|
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
|
2011-05-18 18:34:22 +00:00
|
|
|
GNU Affero General Public License for more details.
|
2009-12-28 09:47:49 +00:00
|
|
|
|
2011-05-18 18:34:22 +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 *****
|
|
|
|
*/
|
|
|
|
|
2011-07-24 17:41:48 +00:00
|
|
|
/**
|
|
|
|
* Handles UI for lookup panel
|
|
|
|
* @namespace
|
|
|
|
*/
|
2009-04-11 04:03:23 +00:00
|
|
|
const Zotero_Lookup = new function () {
|
2011-07-24 17:41:48 +00:00
|
|
|
/**
|
|
|
|
* Performs a lookup by DOI, PMID, or ISBN
|
|
|
|
*/
|
2012-09-22 06:32:18 -05:00
|
|
|
this.accept = function(textBox) {
|
2012-11-24 00:18:42 -06:00
|
|
|
var foundIDs = []; //keep track of identifiers to avoid duplicates
|
2012-09-22 06:32:18 -05:00
|
|
|
var identifier = textBox.value;
|
|
|
|
//first look for DOIs
|
|
|
|
var ids = identifier.split(/[\s\u00A0]+/); //whitespace + non-breaking space
|
|
|
|
var items = [], doi;
|
|
|
|
for(var i=0, n=ids.length; i<n; i++) {
|
2012-11-24 00:18:42 -06:00
|
|
|
if((doi = Zotero.Utilities.cleanDOI(ids[i])) && foundIDs.indexOf(doi) == -1) {
|
2012-09-22 06:32:18 -05:00
|
|
|
items.push({itemType:"journalArticle", DOI:doi});
|
2012-11-24 00:18:42 -06:00
|
|
|
foundIDs.push(doi);
|
2009-04-11 04:03:23 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-22 06:32:18 -05:00
|
|
|
|
|
|
|
//then try ISBNs
|
|
|
|
if(!items.length) {
|
|
|
|
//first try replacing dashes
|
2012-11-23 23:07:20 -06:00
|
|
|
ids = identifier.replace(/[\u002D\u00AD\u2010-\u2015\u2212]+/g, "") //hyphens and dashes
|
|
|
|
.toUpperCase();
|
2012-09-22 06:32:18 -05:00
|
|
|
|
2012-11-23 23:07:20 -06:00
|
|
|
var ISBN_RE = /(?:\D|^)(97[89]\d{10}|\d{9}[\dX])(?!\d)/g;
|
2012-09-22 06:32:18 -05:00
|
|
|
var isbn;
|
|
|
|
|
|
|
|
while(isbn = ISBN_RE.exec(ids)) {
|
2012-11-23 23:07:20 -06:00
|
|
|
isbn = Zotero.Utilities.cleanISBN(isbn[1]);
|
2012-11-24 00:18:42 -06:00
|
|
|
if(isbn && foundIDs.indexOf(isbn) == -1) {
|
|
|
|
items.push({itemType:"book", ISBN:isbn});
|
|
|
|
foundIDs.push(isbn);
|
|
|
|
}
|
2009-04-11 04:03:23 +00:00
|
|
|
}
|
2012-09-22 06:32:18 -05:00
|
|
|
|
|
|
|
//now try spaces
|
|
|
|
if(!items.length) {
|
|
|
|
ids = ids.replace(/[ \u00A0]+/g, ""); //space + non-breaking space
|
|
|
|
while(isbn = ISBN_RE.exec(ids)) {
|
2012-11-23 23:07:20 -06:00
|
|
|
isbn = Zotero.Utilities.cleanISBN(isbn[1]);
|
2012-11-24 00:18:42 -06:00
|
|
|
if(isbn && foundIDs.indexOf(isbn) == -1) {
|
|
|
|
items.push({itemType:"book", ISBN:isbn});
|
|
|
|
foundIDs.push(isbn);
|
|
|
|
}
|
2012-09-22 06:32:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//finally try for PMID
|
|
|
|
if(!items.length) {
|
2013-04-04 22:23:56 -04:00
|
|
|
// PMID; right now, the longest PMIDs are 8 digits, so it doesn't
|
|
|
|
// seem like we will need to discriminate for a fairly long time
|
|
|
|
var PMID_RE = /(?:\D|^)(\d{1,9})(?!\d)/g;
|
2012-09-22 06:32:18 -05:00
|
|
|
var pmid;
|
2012-11-24 00:18:42 -06:00
|
|
|
while((pmid = PMID_RE.exec(identifier)) && foundIDs.indexOf(pmid) == -1) {
|
2012-09-22 06:32:18 -05:00
|
|
|
items.push({itemType:"journalArticle", contextObject:"rft_id=info:pmid/"+pmid[1]});
|
2012-11-24 00:18:42 -06:00
|
|
|
foundIDs.push(pmid);
|
2012-09-22 06:32:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!items.length) {
|
|
|
|
var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
|
|
|
|
.getService(Components.interfaces.nsIPromptService);
|
|
|
|
prompts.alert(window, Zotero.getString("lookup.failure.title"),
|
|
|
|
Zotero.getString("lookup.failureToID.description"));
|
|
|
|
return 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 libraryID = null;
|
|
|
|
var collection = false;
|
2009-04-11 04:03:23 +00:00
|
|
|
try {
|
2011-08-25 11:58:13 +00:00
|
|
|
libraryID = ZoteroPane_Local.getSelectedLibraryID();
|
|
|
|
collection = ZoteroPane_Local.getSelectedCollection();
|
2012-11-23 23:07:20 -06:00
|
|
|
} catch(e) {
|
|
|
|
/** TODO: handle this **/
|
|
|
|
}
|
|
|
|
|
|
|
|
var notDone = items.length; //counter for asynchronous fetching
|
|
|
|
var successful = 0; //counter for successful retrievals
|
2012-09-22 06:32:18 -05:00
|
|
|
|
2012-11-08 05:58:43 -06:00
|
|
|
Zotero_Lookup.toggleProgress(true);
|
2012-09-22 06:32:18 -05:00
|
|
|
|
|
|
|
var item;
|
|
|
|
while(item = items.pop()) {
|
|
|
|
(function(item) {
|
2012-11-23 23:07:20 -06:00
|
|
|
var translate = new Zotero.Translate.Search();
|
2012-09-22 06:32:18 -05:00
|
|
|
translate.setSearch(item);
|
|
|
|
|
|
|
|
// be lenient about translators
|
|
|
|
var translators = translate.getTranslators();
|
|
|
|
translate.setTranslator(translators);
|
|
|
|
|
|
|
|
translate.setHandler("done", function(translate, success) {
|
|
|
|
notDone--;
|
|
|
|
successful += success;
|
|
|
|
|
|
|
|
if(!notDone) { //i.e. done
|
2012-11-08 05:58:43 -06:00
|
|
|
Zotero_Lookup.toggleProgress(false);
|
2012-09-22 06:32:18 -05:00
|
|
|
if(successful) {
|
|
|
|
document.getElementById("zotero-lookup-panel").hidePopup();
|
|
|
|
} else {
|
|
|
|
var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
|
|
|
|
.getService(Components.interfaces.nsIPromptService);
|
|
|
|
prompts.alert(window, Zotero.getString("lookup.failure.title"),
|
|
|
|
Zotero.getString("lookup.failure.description"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
translate.setHandler("itemDone", function(obj, item) {
|
|
|
|
if(collection) collection.addItem(item.id);
|
|
|
|
});
|
|
|
|
|
|
|
|
translate.translate(libraryID);
|
|
|
|
})(item);
|
|
|
|
}
|
|
|
|
|
2009-04-11 04:03:23 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-07-24 17:41:48 +00:00
|
|
|
|
2011-07-24 17:49:16 +00:00
|
|
|
/**
|
|
|
|
* Handles a key press
|
|
|
|
*/
|
2012-09-22 06:32:18 -05:00
|
|
|
this.onKeyPress = function(event, textBox) {
|
2011-07-24 17:49:16 +00:00
|
|
|
var keyCode = event.keyCode;
|
2012-09-22 06:32:18 -05:00
|
|
|
//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) {
|
2012-09-22 06:32:18 -05:00
|
|
|
if(search) {
|
|
|
|
Zotero_Lookup.accept(textBox);
|
2012-11-08 05:58:43 -06:00
|
|
|
event.stopImmediatePropagation();
|
2012-09-22 06:32:18 -05:00
|
|
|
} else if(!multiline) { //switch to multiline
|
2012-11-08 05:58:43 -06:00
|
|
|
var mlTextbox = Zotero_Lookup.toggleMultiline(true);
|
2012-09-22 06:32:18 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-07-24 17:41:48 +00:00
|
|
|
/**
|
|
|
|
* Focuses the field
|
|
|
|
*/
|
|
|
|
this.onShowing = function() {
|
2012-02-09 03:56:30 -05:00
|
|
|
document.getElementById("zotero-lookup-panel").style.padding = "10px";
|
2013-06-30 01:46:50 -04:00
|
|
|
|
|
|
|
// Workaround for field being truncated in middle
|
|
|
|
// https://github.com/zotero/zotero/issues/343
|
|
|
|
this.toggleMultiline(true);
|
|
|
|
|
2012-11-08 05:58:43 -06:00
|
|
|
var identifierElement = Zotero_Lookup.toggleMultiline(false);
|
|
|
|
Zotero_Lookup.toggleProgress(false);
|
2012-01-18 23:50:21 -05:00
|
|
|
identifierElement.focus();
|
2011-07-24 17:41:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancels the popup and resets fields
|
|
|
|
*/
|
2012-09-22 06:32:18 -05:00
|
|
|
this.onHidden = function() {
|
2012-11-08 05:58:43 -06:00
|
|
|
var txtBox = Zotero_Lookup.toggleMultiline(false);
|
|
|
|
var mlTextbox = document.getElementById("zotero-lookup-multiline-textbox");
|
2012-09-22 06:32:18 -05:00
|
|
|
txtBox.value = "";
|
|
|
|
mlTextbox.value = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the textbox to multiline if newlines are detected
|
|
|
|
*/
|
|
|
|
this.adjustTextbox = function(txtBox) {
|
|
|
|
if(txtBox.value.trim().match(/[\r\n]/)) {
|
2012-11-08 05:58:43 -06:00
|
|
|
Zotero_Lookup.toggleMultiline(true);
|
2012-09-22 06:32:18 -05:00
|
|
|
} else {
|
|
|
|
//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
|
|
|
|
txtBox.value = txtBox.value.replace(/^([ \t]*[\r\n]+[ \t]*)+|([ \t]*[\r\n]+[ \t]*)+$/g,"");
|
2012-11-01 04:23:43 -04:00
|
|
|
}
|
2011-07-24 17:41:48 +00:00
|
|
|
}
|
2012-09-22 06:32:18 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs the switch to multiline textbox and returns that textbox
|
|
|
|
*/
|
2012-11-08 05:58:43 -06:00
|
|
|
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;
|
|
|
|
|
2012-09-22 06:32:18 -05:00
|
|
|
//copy over the value
|
2012-11-08 05:58:43 -06:00
|
|
|
dest.value = source.value;
|
|
|
|
|
2012-09-22 06:32:18 -05:00
|
|
|
//switch textboxes
|
2012-11-08 05:58:43 -06:00
|
|
|
mlPanel.setAttribute("collapsed", !on);
|
|
|
|
slPanel.setAttribute("collapsed", !!on);
|
2014-04-27 20:29:22 -04:00
|
|
|
|
|
|
|
// Resize arrow box to fit content
|
2014-04-28 19:50:31 -04:00
|
|
|
if(Zotero.isMac) {
|
|
|
|
var panel = document.getElementById("zotero-lookup-panel");
|
|
|
|
var box = panel.firstChild;
|
|
|
|
panel.sizeTo(box.scrollWidth, box.scrollHeight);
|
|
|
|
}
|
2014-04-27 20:29:22 -04:00
|
|
|
|
2012-11-08 05:58:43 -06:00
|
|
|
dest.focus();
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.toggleProgress = function(on) {
|
|
|
|
//single line
|
|
|
|
var txtBox = document.getElementById("zotero-lookup-textbox");
|
|
|
|
txtBox.style.opacity = on ? 0.5 : 1;
|
|
|
|
txtBox.disabled = !!on;
|
|
|
|
document.getElementById("zotero-lookup-progress").setAttribute("collapsed", !on);
|
|
|
|
|
|
|
|
//multiline
|
|
|
|
document.getElementById("zotero-lookup-multiline-textbox").disabled = !!on;
|
|
|
|
document.getElementById("zotero-lookup-multiline-progress").setAttribute("collapsed", !on);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.getActivePanel = function() {
|
|
|
|
var mlPanel = document.getElementById("zotero-lookup-multiline");
|
|
|
|
if(mlPanel.collapsed) return document.getElementById("zotero-lookup-singleLine");
|
|
|
|
|
|
|
|
return mlPanel;
|
2012-09-22 06:32:18 -05:00
|
|
|
}
|
2009-04-11 04:03:23 +00:00
|
|
|
}
|