api.js (5733B)
1 /* 2 ***** BEGIN LICENSE BLOCK ***** 3 4 Copyright © 2014 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 Zotero.API = { 27 parseParams: function (params) { 28 if (params.groupID) { 29 params.libraryID = Zotero.Groups.getLibraryIDFromGroupID(params.groupID); 30 } 31 32 if (typeof params.itemKey == 'string') { 33 params.itemKey = params.itemKey.split(','); 34 } 35 }, 36 37 38 getResultsFromParams: Zotero.Promise.coroutine(function* (params) { 39 if (!params.objectType) { 40 throw new Error("objectType not specified"); 41 } 42 43 var results; 44 45 if (params.objectType == 'item') { 46 switch (params.scopeObject) { 47 case 'collections': 48 if (params.scopeObjectKey) { 49 var col = Zotero.Collections.getByLibraryAndKey( 50 params.libraryID, params.scopeObjectKey 51 ); 52 } 53 else { 54 var col = Zotero.Collections.get(params.scopeObjectID); 55 } 56 if (!col) { 57 throw new Error('Invalid collection ID or key'); 58 } 59 results = col.getChildItems(); 60 break; 61 62 case 'searches': 63 if (params.scopeObjectKey) { 64 var s = Zotero.Searches.getByLibraryAndKey( 65 params.libraryID, params.scopeObjectKey 66 ); 67 } 68 else { 69 var s = Zotero.Searches.get(params.scopeObjectID); 70 } 71 if (!s) { 72 throw new Error('Invalid search ID or key'); 73 } 74 75 // FIXME: Hack to exclude group libraries for now 76 var s2 = new Zotero.Search(); 77 s2.setScope(s); 78 var groups = Zotero.Groups.getAll(); 79 for (let group of groups) { 80 s2.addCondition('libraryID', 'isNot', group.libraryID); 81 } 82 var ids = yield s2.search(); 83 break; 84 85 default: 86 if (params.scopeObject) { 87 throw new Error("Invalid scope object '" + params.scopeObject + "'"); 88 } 89 90 var s = new Zotero.Search; 91 if (params.libraryID !== undefined) { 92 s.addCondition('libraryID', 'is', params.libraryID); 93 } 94 95 if (params.objectKey) { 96 s.addCondition('key', 'is', params.objectKey); 97 } 98 else if (params.objectID) { 99 s.addCondition('itemID', 'is', params.objectID); 100 } 101 102 if (params.itemKey) { 103 for (let i=0; i<params.itemKey.length; i++) { 104 let itemKey = params.itemKey[i]; 105 s.addCondition('key', 'is', itemKey); 106 } 107 } 108 109 // Display all top-level items 110 /*if (params.onlyTopLevel) { 111 s.addCondition('noChildren', 'true'); 112 }*/ 113 114 var ids = yield s.search(); 115 } 116 117 if (results) { 118 // Filter results by item key 119 if (params.itemKey) { 120 results = results.filter(function (result) { 121 return params.itemKey.indexOf(result.key) !== -1; 122 }); 123 } 124 } 125 else if (ids) { 126 // Filter results by item key 127 if (params.itemKey) { 128 ids = ids.filter(function (id) { 129 var {libraryID, key} = Zotero.Items.getLibraryAndKeyFromID(id); 130 return params.itemKey.indexOf(key) !== -1; 131 }); 132 } 133 results = yield Zotero.Items.getAsync(ids); 134 } 135 } 136 else { 137 throw new Error("Unsupported object type '" + params.objectType + "'"); 138 } 139 140 return results; 141 }), 142 143 144 getLibraryPrefix: function (libraryID) { 145 var type = Zotero.Libraries.get(libraryID).libraryType; 146 switch (type) { 147 case 'user': 148 return 'library'; 149 150 case 'publications': 151 return 'publications'; 152 153 case 'group': 154 return 'groups/' + Zotero.Groups.getGroupIDFromLibraryID(libraryID); 155 156 default: 157 throw new Error(`Invalid type '${type}'`); 158 } 159 } 160 }; 161 162 Zotero.API.Data = { 163 /** 164 * Parse a relative URI path and return parameters for the request 165 */ 166 parsePath: function (path) { 167 var userLibraryID = Zotero.Libraries.userLibraryID; 168 var params = {}; 169 var router = new Zotero.Router(params); 170 171 // Top-level objects 172 router.add('library/:controller/top', function () { 173 params.libraryID = userLibraryID; 174 params.subset = 'top'; 175 }); 176 router.add('groups/:groupID/:controller/top', function () { 177 params.subset = 'top'; 178 }); 179 180 router.add('library/:scopeObject/:scopeObjectKey/items/:objectKey/:subset', function () { 181 params.libraryID = userLibraryID; 182 params.controller = 'items'; 183 }); 184 router.add('groups/:groupID/:scopeObject/:scopeObjectKey/items/:objectKey/:subset', function () { 185 params.controller = 'items'; 186 }); 187 188 // All objects 189 router.add('library/:controller', function () { 190 params.libraryID = userLibraryID; 191 }); 192 router.add('groups/:groupID/:controller', function () {}); 193 194 var parsed = router.run(path); 195 if (!parsed || !params.controller) { 196 throw new Zotero.Router.InvalidPathException(path); 197 } 198 199 if (params.groupID) { 200 params.libraryID = Zotero.Groups.getLibraryIDFromGroupID(params.groupID); 201 } 202 Zotero.Router.Utilities.convertControllerToObjectType(params); 203 204 return params; 205 }, 206 207 208 getGenerator: function (path) { 209 var params = this.parsePath(path); 210 //Zotero.debug(params); 211 212 return Zotero.DataObjectUtilities.getObjectsClassForObjectType(params.objectType) 213 .apiDataGenerator(params); 214 } 215 }; 216 217 218 219