advancedSearch.js (5226B)
1 /* 2 ***** BEGIN LICENSE BLOCK ***** 3 4 Copyright © 2009 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 var ZoteroAdvancedSearch = new function() { 28 this.onLoad = onLoad; 29 this.search = search; 30 this.clear = clear; 31 this.onDblClick = onDblClick; 32 this.onUnload = onUnload; 33 34 this.itemsView = false; 35 36 var _searchBox; 37 var _libraryID; 38 39 function onLoad() { 40 _searchBox = document.getElementById('zotero-search-box'); 41 42 // Set font size from pref 43 var sbc = document.getElementById('zotero-search-box-container'); 44 Zotero.setFontSize(sbc); 45 46 _searchBox.onLibraryChange = this.onLibraryChange; 47 var io = window.arguments[0]; 48 49 io.dataIn.search.loadPrimaryData() 50 .then(function () { 51 _searchBox.search = io.dataIn.search; 52 }); 53 } 54 55 56 function search() { 57 _searchBox.updateSearch(); 58 _searchBox.active = true; 59 60 // A minimal implementation of Zotero.CollectionTreeRow 61 var collectionTreeRow = { 62 view: {}, 63 ref: _searchBox.search, 64 isSearchMode: function() { return true; }, 65 getItems: Zotero.Promise.coroutine(function* () { 66 var search = _searchBox.search.clone(); 67 search.libraryID = _libraryID; 68 var ids = yield search.search(); 69 return Zotero.Items.get(ids); 70 }), 71 isLibrary: function () { return false; }, 72 isCollection: function () { return false; }, 73 isSearch: function () { return true; }, 74 isPublications: () => false, 75 isFeed: () => false, 76 isShare: function () { return false; }, 77 isTrash: function () { return false; } 78 } 79 80 if (this.itemsView) { 81 this.itemsView.unregister(); 82 } 83 84 this.itemsView = new Zotero.ItemTreeView(collectionTreeRow, false); 85 document.getElementById('zotero-items-tree').view = this.itemsView; 86 } 87 88 89 function clear() { 90 if (this.itemsView) { 91 this.itemsView.unregister(); 92 } 93 document.getElementById('zotero-items-tree').view = null; 94 95 var s = new Zotero.Search(); 96 // Don't clear the selected library 97 s.libraryID = _searchBox.search.libraryID; 98 s.addCondition('title', 'contains', '') 99 _searchBox.search = s; 100 _searchBox.active = false; 101 } 102 103 104 this.save = Zotero.Promise.coroutine(function* () { 105 _searchBox.updateSearch(); 106 107 var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"] 108 .getService(Components.interfaces.nsIPromptService); 109 110 var untitled = yield Zotero.DB.getNextName( 111 _searchBox.search.libraryID, 112 'savedSearches', 113 'savedSearchName', 114 Zotero.getString('pane.collections.untitled') 115 ); 116 117 var name = { value: untitled }; 118 var result = promptService.prompt(window, 119 Zotero.getString('pane.collections.newSavedSeach'), 120 Zotero.getString('pane.collections.savedSearchName'), name, "", {}); 121 122 if (!result) 123 { 124 return; 125 } 126 127 if (!name.value) 128 { 129 name.value = untitled; 130 } 131 132 var s = _searchBox.search.clone(); 133 s.name = name.value; 134 yield s.save(); 135 136 window.close() 137 }); 138 139 140 this.onLibraryChange = function (libraryID) { 141 _libraryID = libraryID; 142 var library = Zotero.Libraries.get(libraryID); 143 var isEditable = library.editable && library.libraryType != 'publications'; 144 document.getElementById('zotero-search-save').disabled = !isEditable; 145 } 146 147 148 // Adapted from: http://www.xulplanet.com/references/elemref/ref_tree.html#cmnote-9 149 function onDblClick(event, tree) 150 { 151 if (event && tree && event.type == "dblclick") 152 { 153 var row = {}, col = {}, obj = {}; 154 tree.treeBoxObject.getCellAt(event.clientX, event.clientY, row, col, obj); 155 // obj.value == cell/text/image 156 // TODO: handle collection double-click 157 if (obj.value && this.itemsView && this.itemsView.selection.currentIndex > -1) 158 { 159 var item = this.itemsView.getSelectedItems()[0]; 160 161 var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"] 162 .getService(Components.interfaces.nsIWindowMediator); 163 164 var lastWin = wm.getMostRecentWindow("navigator:browser"); 165 166 if (!lastWin) { 167 window.open(); 168 var newWindow = wm.getMostRecentWindow("navigator:browser"); 169 var b = newWindow.getBrowser(); 170 return; 171 } 172 173 if (lastWin.ZoteroOverlay) { 174 lastWin.ZoteroOverlay.toggleDisplay(true); 175 } 176 177 lastWin.ZoteroPane.selectItem(item.getID(), false, true); 178 lastWin.focus(); 179 } 180 } 181 } 182 183 184 function onUnload() { 185 // Unregister search from Notifier 186 if (this.itemsView) { 187 this.itemsView.unregister(); 188 } 189 } 190 }