Add support for multiple identifier lookup to the magic wand tool. Enable shift+enter for submitting input in multiline mode. Use shift+enter in single line mode to convert to multiline

This commit is contained in:
aurimasv 2012-09-22 06:32:18 -05:00
parent 4caae896cf
commit 1b9c89fd59
5 changed files with 158 additions and 57 deletions

View File

@ -31,71 +31,126 @@ 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 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])) {
items.push({itemType:"journalArticle", DOI: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
var ISBN_RE = /(?:\D|^)(\d{10}|\d{13})(?!\d)/g;
var isbn;
while(isbn = ISBN_RE.exec(ids)) {
items.push({itemType:"book", ISBN:isbn[1]});
}
});
//now try spaces
if(!items.length) {
ids = ids.replace(/[ \u00A0]+/g, ""); //space + non-breaking space
while(isbn = ISBN_RE.exec(ids)) {
items.push({itemType:"book", ISBN:isbn[1]});
}
}
}
//finally try for PMID
if(!items.length) {
// PMID; right now, PMIDs are 8 digits, so there 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)) {
items.push({itemType:"journalArticle", contextObject:"rft_id=info:pmid/"+pmid[1]});
}
}
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 notDone = items.length; //counter for asynchronous fetching
var successful = 0; //counter for successful retrievals
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);
textBox.style.opacity = 0.5;
textBox.disabled = true;
document.getElementById("zotero-lookup-progress").setAttribute("collapsed", false);
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
textBox.style.opacity = 1;
textBox.disabled = false;
document.getElementById("zotero-lookup-progress").setAttribute("collapsed", true);
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);
} else if(!multiline) { //switch to multiline
var mlTextbox = Zotero_Lookup.switchToMultiline(textBox);
mlTextbox.value = mlTextbox.value + '\n';
}
} else if(keyCode == event.DOM_VK_ESCAPE) {
document.getElementById("zotero-lookup-panel").hidePopup();
}
@ -118,9 +173,43 @@ const Zotero_Lookup = new function () {
/**
* 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 = document.getElementById("zotero-lookup-textbox");
var mlTextbox = document.getElementById("zotero-lookup-textbox-multiline");
txtBox.value = "";
mlTextbox.value = "";
//switch back to single line textbox
mlTextbox.setAttribute("collapsed", true);
document.getElementById("zotero-lookup-buttons").setAttribute("collapsed", true);
txtBox.setAttribute("collapsed", false);
txtBox.focus();
}
/**
* Converts the textbox to multiline if newlines are detected
*/
this.adjustTextbox = function(txtBox) {
if(txtBox.value.trim().match(/[\r\n]/)) {
Zotero_Lookup.switchToMultiline(txtBox);
} 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.switchToMultiline = function(txtBox) {
//copy over the value
var mlTextbox = document.getElementById("zotero-lookup-textbox-multiline");
mlTextbox.value = txtBox.value;
//switch textboxes
txtBox.setAttribute("collapsed", true);
mlTextbox.setAttribute("collapsed", false);
mlTextbox.focus();
document.getElementById("zotero-lookup-buttons").setAttribute("collapsed", false);
return mlTextbox;
}
}

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"/>
@ -146,9 +146,15 @@
<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"/>
<progressmeter id="zotero-lookup-progress" mode="undetermined" collapsed="true"/>
<vbox>
<textbox id="zotero-lookup-textbox" onkeypress="return Zotero_Lookup.onKeyPress(event, this)" oninput="Zotero_Lookup.adjustTextbox(this)" flex="1" newlines="pasteintact"/>
<textbox id="zotero-lookup-textbox-multiline" onkeypress="return Zotero_Lookup.onKeyPress(event, this)" multiline="true" rows="5" wrap="off" collapsed="true" flex="1"/>
</vbox>
</stack>
<hbox align="start" id="zotero-lookup-buttons" class="zotero-button-clear-image" collapsed="true">
<button label="&zotero.lookup.button.search;" align="start" oncommand="Zotero_Lookup.accept(document.getElementById('zotero-lookup-textbox-multiline'))"/>
</hbox>
</vbox>
</panel>
</toolbarbutton>

View File

@ -92,7 +92,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…">
@ -139,7 +139,8 @@
<!ENTITY zotero.tagSelector.renameTag "Rename Tag…">
<!ENTITY zotero.tagSelector.deleteTag "Delete Tag…">
<!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

@ -756,6 +756,7 @@ rtfScan.scannedFileSuffix = (Scanned)
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

@ -266,6 +266,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');