Merge remote-tracking branch 'aurimasv/multipleLookup'

Conflicts:
	chrome/locale/en-US/zotero/zotero.dtd
This commit is contained in:
Dan Stillman 2013-03-05 14:46:31 -05:00
commit 4496c8d9e9
5 changed files with 205 additions and 65 deletions

View file

@ -31,71 +31,137 @@ const Zotero_Lookup = new function () {
/**
* Performs a lookup by DOI, PMID, or ISBN
*/
this.accept = function() {
var identifierElement = document.getElementById("zotero-lookup-textbox");
var identifier = identifierElement.value;
var doi = Zotero.Utilities.cleanDOI(identifier);
if(doi) {
var item = {itemType:"journalArticle", DOI:doi};
} else {
identifier = identifier.trim().replace(/[\- ]/g, "");
if(identifier.length == 10 || identifier.length == 13) {
// ISBN
var item = {itemType:"book", ISBN:identifier};
} else {
// PMID; right now, PMIDs are 8 digits, so there doesn't seem like we will need to
// discriminate for a fairly long time
var item = {itemType:"journalArticle", contextObject:"rft_id=info:pmid/"+identifier};
this.accept = function(textBox) {
var foundIDs = []; //keep track of identifiers to avoid duplicates
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++) {
if((doi = Zotero.Utilities.cleanDOI(ids[i])) && foundIDs.indexOf(doi) == -1) {
items.push({itemType:"journalArticle", DOI:doi});
foundIDs.push(doi);
}
}
var translate = new Zotero.Translate("search");
translate.setSearch(item);
// be lenient about translators
var translators = translate.getTranslators();
translate.setTranslator(translators);
translate.setHandler("done", function(translate, success) {
identifierElement.style.opacity = 1;
identifierElement.disabled = false;
document.getElementById("zotero-lookup-progress").hidden = true;
if(success) {
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"));
//then try ISBNs
if(!items.length) {
//first try replacing dashes
ids = identifier.replace(/[\u002D\u00AD\u2010-\u2015\u2212]+/g, "") //hyphens and dashes
.toUpperCase();
var ISBN_RE = /(?:\D|^)(97[89]\d{10}|\d{9}[\dX])(?!\d)/g;
var isbn;
while(isbn = ISBN_RE.exec(ids)) {
isbn = Zotero.Utilities.cleanISBN(isbn[1]);
if(isbn && foundIDs.indexOf(isbn) == -1) {
items.push({itemType:"book", ISBN:isbn});
foundIDs.push(isbn);
}
}
});
//now try spaces
if(!items.length) {
ids = ids.replace(/[ \u00A0]+/g, ""); //space + non-breaking space
while(isbn = ISBN_RE.exec(ids)) {
isbn = Zotero.Utilities.cleanISBN(isbn[1]);
if(isbn && foundIDs.indexOf(isbn) == -1) {
items.push({itemType:"book", ISBN:isbn});
foundIDs.push(isbn);
}
}
}
}
//finally try for PMID
if(!items.length) {
// PMID; right now, 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{8})(?!\d)/g;
var pmid;
while((pmid = PMID_RE.exec(identifier)) && foundIDs.indexOf(pmid) == -1) {
items.push({itemType:"journalArticle", contextObject:"rft_id=info:pmid/"+pmid[1]});
foundIDs.push(pmid);
}
}
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;
}
var libraryID = null;
var collection = false;
try {
libraryID = ZoteroPane_Local.getSelectedLibraryID();
collection = ZoteroPane_Local.getSelectedCollection();
} catch(e) {}
translate.setHandler("itemDone", function(obj, item) {
if(collection) collection.addItem(item.id);
});
identifierElement.style.opacity = 0.5;
identifierElement.disabled = true;
document.getElementById("zotero-lookup-progress").hidden = false;
translate.translate(libraryID);
} catch(e) {
/** TODO: handle this **/
}
var notDone = items.length; //counter for asynchronous fetching
var successful = 0; //counter for successful retrievals
Zotero_Lookup.toggleProgress(true);
var item;
while(item = items.pop()) {
(function(item) {
var translate = new Zotero.Translate.Search();
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
Zotero_Lookup.toggleProgress(false);
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);
}
return false;
}
/**
* Handles a key press
*/
this.onKeyPress = function(event) {
this.onKeyPress = function(event, textBox) {
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;
if(keyCode === 13 || keyCode === 14) {
Zotero_Lookup.accept();
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';
}
} else if(keyCode == event.DOM_VK_ESCAPE) {
document.getElementById("zotero-lookup-panel").hidePopup();
}
@ -107,20 +173,74 @@ const Zotero_Lookup = new function () {
*/
this.onShowing = function() {
document.getElementById("zotero-lookup-panel").style.padding = "10px";
document.getElementById("zotero-lookup-progress").hidden = false;
var identifierElement = document.getElementById("zotero-lookup-textbox");
identifierElement.style.opacity = 1;
identifierElement.disabled = false;
var identifierElement = Zotero_Lookup.toggleMultiline(false);
Zotero_Lookup.toggleProgress(false);
identifierElement.focus();
}
/**
* Cancels the popup and resets fields
*/
this.onHidden = function(event) {
if (event.target.id == 'zotero-lookup-panel') {
document.getElementById("zotero-lookup-textbox").value = "";
this.onHidden = function() {
var txtBox = Zotero_Lookup.toggleMultiline(false);
var mlTextbox = document.getElementById("zotero-lookup-multiline-textbox");
txtBox.value = "";
mlTextbox.value = "";
}
/**
* Converts the textbox to multiline if newlines are detected
*/
this.adjustTextbox = function(txtBox) {
if(txtBox.value.trim().match(/[\r\n]/)) {
Zotero_Lookup.toggleMultiline(true);
} 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,"");
}
}
/**
* 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);
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;
}
}

View file

@ -1,4 +1,4 @@
<?xml version="1.0"?>
<?xml version="1.0"?>
<!--
***** BEGIN LICENSE BLOCK *****
@ -39,7 +39,7 @@
<script src="include.js"/>
<script src="zoteroPane.js" type="application/javascript;version=1.8"/>
<script src="fileInterface.js"/>
<script src="fileInterface.js"/>
<script src="reportInterface.js"/>
<script src="timelineInterface.js"/>
<script src="recognizePDF.js"/>
@ -144,10 +144,19 @@
onpopuphidden="Zotero_Lookup.onHidden(event)">
<vbox>
<description>&zotero.lookup.description;</description>
<stack>
<progressmeter id="zotero-lookup-progress" mode="undetermined" hidden="true"/>
<textbox id="zotero-lookup-textbox" onkeypress="return Zotero_Lookup.onKeyPress(event)" flex="1"/>
</stack>
<vbox id="zotero-lookup-singleLine">
<stack>
<progressmeter id="zotero-lookup-progress" mode="undetermined" collapsed="true"/>
<textbox id="zotero-lookup-textbox" onkeypress="return Zotero_Lookup.onKeyPress(event, this)" oninput="Zotero_Lookup.adjustTextbox(this)" flex="1" newlines="pasteintact"/>
</stack>
</vbox>
<vbox id="zotero-lookup-multiline" collapsed="true">
<textbox id="zotero-lookup-multiline-textbox" onkeypress="return Zotero_Lookup.onKeyPress(event, this)" multiline="true" rows="5" wrap="off" flex="1"/>
<hbox align="start" id="zotero-lookup-buttons" class="zotero-button-clear-image">
<button label="&zotero.lookup.button.search;" align="start" oncommand="Zotero_Lookup.accept(document.getElementById('zotero-lookup-multiline-textbox'))"/>
<progressmeter id="zotero-lookup-multiline-progress" mode="undetermined" collapsed="true" flex="1"/>
</hbox>
</vbox>
</vbox>
</panel>
</toolbarbutton>

View file

@ -94,7 +94,7 @@
<!ENTITY zotero.toolbar.newItem.label "New Item">
<!ENTITY zotero.toolbar.moreItemTypes.label "More">
<!ENTITY zotero.toolbar.newItemFromPage.label "Create Web Page Item from Current Page">
<!ENTITY zotero.toolbar.lookup.label "Add Item by Identifier">
<!ENTITY zotero.toolbar.lookup.label "Add Item(s) by Identifier">
<!ENTITY zotero.toolbar.removeItem.label "Remove Item…">
<!ENTITY zotero.toolbar.newCollection.label "New Collection…">
<!ENTITY zotero.toolbar.newGroup "New Group…">
@ -148,7 +148,8 @@
<!ENTITY zotero.tagColorChooser.setColor "Set Color">
<!ENTITY zotero.tagColorChooser.removeColor "Remove Color">
<!ENTITY zotero.lookup.description "Enter the ISBN, DOI, or PMID to look up in the box below.">
<!ENTITY zotero.lookup.description "Enter one or more ISBNs, DOIs, or PMIDs to look up in the box below.">
<!ENTITY zotero.lookup.button.search "Search">
<!ENTITY zotero.selectitems.title "Select Items">
<!ENTITY zotero.selectitems.intro.label "Select which items you'd like to add to your library">

View file

@ -894,6 +894,7 @@ file.accessError.showParentDir = Show Parent Directory
lookup.failure.title = Lookup Failed
lookup.failure.description = Zotero could not find a record for the specified identifier. Please verify the identifier and try again.
lookup.failureToID.description = Zotero could not find any identifiers in your input. Please verify your input and try again.
locate.online.label = View Online
locate.online.tooltip = Go to this item online

View file

@ -273,6 +273,10 @@
display: none;
}
.zotero-button-clear-image {
list-style-image: none
}
#zotero-tb-collection-add
{
list-style-image: url('chrome://zotero/skin/toolbar-collection-add.png');
@ -311,6 +315,11 @@
list-style-image: url('chrome://zotero/skin/toolbar-lookup.png');
}
#zotero-lookup-multiline-progress
{
height: 2em;
}
#zotero-tb-item-from-page
{
list-style-image: url('chrome://zotero/skin/toolbar-item-from-page.png');