lookup.js (6609B)
1 /* 2 ***** BEGIN LICENSE BLOCK ***** 3 4 Copyright © 2009-2011 Center for History and New Media 5 George Mason University, Fairfax, Virginia, USA 6 http://zotero.org 7 8 This file is part of Zotero. 9 10 Zotero is free software: you can redistribute it and/or modify 11 it under the terms of the GNU Affero General Public License as published by 12 the Free Software Foundation, either version 3 of the License, or 13 (at your option) any later version. 14 15 Zotero is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 GNU Affero General Public License for more details. 19 20 You should have received a copy of the GNU Affero General Public License 21 along with Zotero. If not, see <http://www.gnu.org/licenses/>. 22 23 ***** END LICENSE BLOCK ***** 24 */ 25 26 /** 27 * Handles UI for lookup panel 28 * @namespace 29 */ 30 var Zotero_Lookup = new function () { 31 /** 32 * Performs a lookup by DOI, PMID, or ISBN 33 */ 34 this.accept = Zotero.Promise.coroutine(function* (textBox) { 35 var identifiers = Zotero.Utilities.Internal.extractIdentifiers(textBox.value); 36 if (!identifiers.length) { 37 Zotero.alert( 38 window, 39 Zotero.getString("lookup.failure.title"), 40 Zotero.getString("lookup.failureToID.description") 41 ); 42 return false; 43 } 44 45 var libraryID = false; 46 var collection = false; 47 try { 48 libraryID = ZoteroPane_Local.getSelectedLibraryID(); 49 collection = ZoteroPane_Local.getSelectedCollection(); 50 } catch(e) { 51 /** TODO: handle this **/ 52 } 53 54 var successful = 0; //counter for successful retrievals 55 56 Zotero_Lookup.toggleProgress(true); 57 58 for (let identifier of identifiers) { 59 var translate = new Zotero.Translate.Search(); 60 translate.setIdentifier(identifier); 61 62 // be lenient about translators 63 let translators = yield translate.getTranslators(); 64 translate.setTranslator(translators); 65 66 try { 67 yield translate.translate({ 68 libraryID, 69 collections: collection ? [collection.id] : false 70 }) 71 successful++; 72 } 73 // Continue with other ids on failure 74 catch (e) { 75 Zotero.logError(e); 76 } 77 } 78 79 Zotero_Lookup.toggleProgress(false); 80 // TODO: Give indication if some failed 81 if (successful) { 82 document.getElementById("zotero-lookup-panel").hidePopup(); 83 } 84 else { 85 Zotero.alert( 86 window, 87 Zotero.getString("lookup.failure.title"), 88 Zotero.getString("lookup.failure.description") 89 ); 90 } 91 92 return false; 93 }); 94 95 96 this.showPanel = function (button) { 97 var panel = document.getElementById('zotero-lookup-panel'); 98 panel.openPopup(button, "after_start", 16, -2, false, false); 99 } 100 101 102 /** 103 * Focuses the field 104 */ 105 this.onShowing = function (event) { 106 // Ignore context menu 107 if (event.originalTarget.id != 'zotero-lookup-panel') return; 108 109 document.getElementById("zotero-lookup-panel").style.padding = "10px"; 110 this.getActivePanel().getElementsByTagName('textbox')[0].focus(); 111 } 112 113 114 /** 115 * Cancels the popup and resets fields 116 */ 117 this.onHidden = function (event) { 118 // Ignore context menu 119 if (event.originalTarget.id != 'zotero-lookup-panel') return; 120 121 document.getElementById("zotero-lookup-textbox").value = ""; 122 document.getElementById("zotero-lookup-multiline-textbox").value = ""; 123 Zotero_Lookup.toggleProgress(false); 124 } 125 126 127 this.getActivePanel = function() { 128 var mlPanel = document.getElementById("zotero-lookup-multiline"); 129 if (mlPanel.collapsed) return document.getElementById("zotero-lookup-singleLine"); 130 return mlPanel; 131 } 132 133 134 /** 135 * Handles a key press 136 */ 137 this.onKeyPress = function(event, textBox) { 138 var keyCode = event.keyCode; 139 //use enter to start search, shift+enter to insert a new line. Flipped in multiline mode 140 var multiline = textBox.getAttribute('multiline'); 141 var search = multiline ? event.shiftKey : !event.shiftKey; 142 if(keyCode === 13 || keyCode === 14) { 143 if(search) { 144 Zotero_Lookup.accept(textBox); 145 event.stopImmediatePropagation(); 146 } else if(!multiline) { //switch to multiline 147 var mlTextbox = Zotero_Lookup.toggleMultiline(true); 148 mlTextbox.value = mlTextbox.value + '\n'; 149 } 150 } else if(keyCode == event.DOM_VK_ESCAPE) { 151 document.getElementById("zotero-lookup-panel").hidePopup(); 152 } 153 return true; 154 } 155 156 157 this.onInput = function (event, textbox) { 158 this.adjustTextbox(textbox); 159 }; 160 161 162 /** 163 * Converts the textbox to multiline if newlines are detected 164 */ 165 this.adjustTextbox = function (textbox) { 166 if (textbox.value.trim().match(/[\r\n]/)) { 167 Zotero_Lookup.toggleMultiline(true); 168 } 169 // Since we ignore trailing and leading newlines, we should also trim them for display 170 // can't use trim, because then we cannot add leading/trailing spaces to the single line textbox 171 else { 172 textbox.value = textbox.value.replace(/^([ \t]*[\r\n]+[ \t]*)+|([ \t]*[\r\n]+[ \t]*)+$/g,""); 173 } 174 } 175 176 177 /** 178 * Performs the switch to multiline textbox and returns that textbox 179 */ 180 this.toggleMultiline = function(on) { 181 var mlPanel = document.getElementById("zotero-lookup-multiline"); 182 var mlTxtBox = document.getElementById("zotero-lookup-multiline-textbox"); 183 var slPanel = document.getElementById("zotero-lookup-singleLine"); 184 var slTxtBox = document.getElementById("zotero-lookup-textbox"); 185 var source = on ? slTxtBox : mlTxtBox; 186 var dest = on ? mlTxtBox : slTxtBox; 187 188 if((mlPanel.collapsed && !on) || (!mlPanel.collapsed && on)) return dest; 189 190 //copy over the value 191 dest.value = source.value; 192 193 //switch textboxes 194 mlPanel.setAttribute("collapsed", !on); 195 slPanel.setAttribute("collapsed", !!on); 196 197 // Resize arrow box to fit content 198 if(Zotero.isMac) { 199 var panel = document.getElementById("zotero-lookup-panel"); 200 var box = panel.firstChild; 201 panel.sizeTo(box.scrollWidth, box.scrollHeight); 202 } 203 204 dest.focus(); 205 return dest; 206 } 207 208 this.toggleProgress = function(on) { 209 // In Firefox 52.6.0, progressmeters burn CPU at idle on Linux when undetermined, even 210 // if they're hidden. (Being hidden is enough on macOS.) 211 var mode = on ? 'undetermined' : 'determined'; 212 213 //single line 214 var txtBox = document.getElementById("zotero-lookup-textbox"); 215 txtBox.style.opacity = on ? 0.5 : 1; 216 txtBox.disabled = !!on; 217 var p1 = document.getElementById("zotero-lookup-progress"); 218 p1.mode = mode; 219 220 //multiline 221 document.getElementById("zotero-lookup-multiline-textbox").disabled = !!on; 222 var p2 = document.getElementById("zotero-lookup-multiline-progress"); 223 p2.mode = mode; 224 } 225 }