www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

groups.js (4393B)


      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 Zotero.Groups = new function () {
     28 	Zotero.defineProperty(this, 'addGroupURL', {
     29 		value: ZOTERO_CONFIG.WWW_BASE_URL + 'groups/new/'
     30 	});
     31 	
     32 	this._cache = null;
     33 	
     34 	this._makeCache = function() {
     35 		return {
     36 			groupIDByLibraryID: {},
     37 			libraryIDByGroupID: {}
     38 		};
     39 	}
     40 	
     41 	this.register = function (group) {
     42 		if (!this._cache) throw new Error("Zotero.Groups cache is not initialized");
     43 		Zotero.debug("Registering group " + group.id + " (" + group.libraryID + ")", 5);
     44 		this._addToCache(this._cache, group);
     45 	}
     46 	
     47 	this._addToCache = function (cache, group) {
     48 		cache.libraryIDByGroupID[group.id] = group.libraryID;
     49 		cache.groupIDByLibraryID[group.libraryID] = group.id;
     50 	}
     51 	
     52 	this.unregister = function (groupID) {
     53 		if (!this._cache) throw new Error("Zotero.Groups cache is not initialized");
     54 		let libraryID = this._cache.libraryIDByGroupID[groupID];
     55 		Zotero.debug("Unregistering group " + groupID + " (" + libraryID + ")", 5);
     56 		delete this._cache.groupIDByLibraryID[libraryID];
     57 		delete this._cache.libraryIDByGroupID[groupID];
     58 	}
     59 	
     60 	this.init = Zotero.Promise.method(function() {
     61 		// Cache initialized in Zotero.Libraries
     62 	})
     63 	
     64 	/**
     65 	 * @param {Integer} id - Group id
     66 	 * @return {Zotero.Group}
     67 	 */
     68 	this.get = function (id) {
     69 		return Zotero.Libraries.get(this.getLibraryIDFromGroupID(id));
     70 	}
     71 	
     72 	
     73 	/**
     74 	 * Get all groups, sorted by name
     75 	 *
     76 	 * @return {Zotero.Group[]}
     77 	 */
     78 	this.getAll = function () {
     79 		if (!this._cache) throw new Error("Zotero.Groups cache is not initialized");
     80 		
     81 		var groups = Object.keys(this._cache.groupIDByLibraryID)
     82 			.map(id => Zotero.Libraries.get(id));
     83 		var collation = Zotero.getLocaleCollation();
     84 		groups.sort(function(a, b) {
     85 			return collation.compareString(1, a.name, b.name);
     86 		});
     87 		return groups;
     88 	}
     89 	
     90 	
     91 	this.getByLibraryID = function (libraryID) {
     92 		return Zotero.Libraries.get(libraryID);
     93 	}
     94 	
     95 	
     96 	this.exists = function (groupID) {
     97 		if (!this._cache) throw new Error("Zotero.Groups cache is not initialized");
     98 		
     99 		return !!this._cache.libraryIDByGroupID[groupID];
    100 	}
    101 	
    102 	
    103 	this.getGroupIDFromLibraryID = function (libraryID) {
    104 		if (!this._cache) throw new Error("Zotero.Groups cache is not initialized");
    105 		
    106 		var groupID = this._cache.groupIDByLibraryID[libraryID];
    107 		if (!groupID) {
    108 			throw new Error("Group with libraryID " + libraryID + " does not exist");
    109 		}
    110 		return groupID;
    111 	}
    112 	
    113 	
    114 	this.getLibraryIDFromGroupID = function (groupID) {
    115 		if (!this._cache) throw new Error("Zotero.Groups cache is not initialized");
    116 		
    117 		return this._cache.libraryIDByGroupID[groupID] || false;
    118 	}
    119 	
    120 	
    121 	this.getPermissionsFromJSON = function (json, userID) {
    122 		if (!json.owner) throw new Error("Invalid JSON provided for group data");
    123 		if (!userID) throw new Error("userID not provided");
    124 		
    125 		var editable = false;
    126 		var filesEditable = false;
    127 		// If user is owner or admin, make library editable, and make files editable unless they're
    128 		// disabled altogether
    129 		if (json.owner == userID || (json.admins && json.admins.indexOf(userID) != -1)) {
    130 			editable = true;
    131 			if (json.fileEditing != 'none') {
    132 				filesEditable = true;
    133 			}
    134 		}
    135 		// If user is member, make library and files editable if they're editable by all members
    136 		else if (json.members && json.members.indexOf(userID) != -1) {
    137 			if (json.libraryEditing == 'members') {
    138 				editable = true;
    139 				if (json.fileEditing == 'members') {
    140 					filesEditable = true;
    141 				}
    142 			}
    143 		}
    144 		return { editable, filesEditable };
    145 	};
    146 }