1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2007, 2008, 2009, 2010, 2013, 2014, 2016 Synacor, Inc.
  5  *
  6  * The contents of this file are subject to the Common Public Attribution License Version 1.0 (the "License");
  7  * you may not use this file except in compliance with the License.
  8  * You may obtain a copy of the License at: https://www.zimbra.com/license
  9  * The License is based on the Mozilla Public License Version 1.1 but Sections 14 and 15
 10  * have been added to cover use of software over a computer network and provide for limited attribution
 11  * for the Original Developer. In addition, Exhibit A has been modified to be consistent with Exhibit B.
 12  *
 13  * Software distributed under the License is distributed on an "AS IS" basis,
 14  * WITHOUT WARRANTY OF ANY KIND, either express or implied.
 15  * See the License for the specific language governing rights and limitations under the License.
 16  * The Original Code is Zimbra Open Source Web Client.
 17  * The Initial Developer of the Original Code is Zimbra, Inc.  All rights to the Original Code were
 18  * transferred by Zimbra, Inc. to Synacor, Inc. on September 14, 2015.
 19  *
 20  * All portions of the code are Copyright (C) 2007, 2008, 2009, 2010, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * Creates a signature collection.
 26  * @class
 27  * This class represents a signature collection.
 28  * 
 29  * @extends		ZmModel
 30  */
 31 ZmSignatureCollection = function() {
 32 	ZmModel.call(this, ZmEvent.S_SIGNATURE);
 33 	this._idMap = {};
 34 	this._nameMap= {};
 35 	this._size = 0;
 36 };
 37 
 38 ZmSignatureCollection.prototype = new ZmModel;
 39 ZmSignatureCollection.prototype.constructor = ZmSignatureCollection;
 40 
 41 ZmSignatureCollection.prototype.toString =
 42 function() {
 43 	return "ZmSignatureCollection";
 44 };
 45 
 46 //
 47 // Public methods
 48 //
 49 /**
 50  * Adds the signature.
 51  * 
 52  * @param	{ZmSignature}	signature		the signature to add
 53  */
 54 ZmSignatureCollection.prototype.add =
 55 function(signature) {
 56 	if (!this._idMap[signature.id]) {
 57 		this._idMap[signature.id] = signature;
 58 		this._nameMap[signature.name] = signature;
 59 		this._size++;
 60 		this._notify(ZmEvent.E_CREATE, { item: signature });
 61 	}
 62 };
 63 
 64 /**
 65  * Removes the signature.
 66  * 
 67  * @param	{ZmSignature}	signature		the signature to remove
 68  */
 69 ZmSignatureCollection.prototype.remove =
 70 function(signature) {
 71 	if (this._idMap[signature.id]) {
 72 		delete this._idMap[signature.id];
 73 		delete this._nameMap[signature.name];
 74 		this._size--;
 75 		this._notify(ZmEvent.E_DELETE, { item: signature });
 76 	}
 77 };
 78 
 79 /**
 80  * Gets the count of signatures.
 81  * 
 82  * @return	{int}		the size
 83  */
 84 ZmSignatureCollection.prototype.getSize =
 85 function() {
 86 	return this._size;
 87 };
 88 
 89 /**
 90  * Gets the signatures.
 91  * 
 92  * @return	{Array}	an array of {@link ZmSignature} objects
 93  */
 94 ZmSignatureCollection.prototype.getSignatures =
 95 function(sort) {
 96 
 97 	var signatures = AjxUtil.values(this._idMap);
 98 	if (sort) {
 99 		signatures.sort(ZmSignatureCollection.BY_NAME);
100 	}
101 	return signatures;
102 };
103 
104 ZmSignatureCollection.prototype.getSignatureOptions =
105 function() {
106 	// collect signatures
107 	var signatures = [];
108 	for (var id in this._idMap) {
109 		signatures.push(this._idMap[id]);
110 	}
111 	signatures.sort(ZmSignatureCollection.BY_NAME);
112 
113 	// create options
114 	var options = [];
115 	//In Web Client offline mode signature having vCard will be suppressed
116 	var isWebClientOffline = appCtxt.isWebClientOffline();
117 	for (var i = 0; i < signatures.length; i++) {
118 		var signature = signatures[i];
119 		if (isWebClientOffline && signature.contactId) {
120 			continue;
121 		}
122 		options.push(new DwtSelectOptionData(signature.id, signature.name));
123 	}
124 	options.push(new DwtSelectOptionData("", ZmMsg.signatureDoNotAttach));
125 	return options;
126 };
127 
128 /**
129  * Gets the signature by id.
130  * 
131  * @param	{String}	id		the signature
132  * @return	{ZmSignature} the signature
133  */
134 ZmSignatureCollection.prototype.getById =
135 function(id) {
136 	return this._idMap[id];
137 };
138 
139 /**
140  * Gets the signature by name.
141  * 
142  * @param	{String}	name		the signature
143  * @return	{ZmSignature} the signature
144  */
145 ZmSignatureCollection.prototype.getByName =
146 function(name) {
147 	var lname = name.toLowerCase();
148 	for (var key in this._nameMap) {
149 		if (key.toLowerCase() == lname) {
150 			return this._nameMap[key];
151 		}
152 	}
153 };
154 
155 ZmSignatureCollection.prototype.initialize =
156 function(data) {
157 	if (this._size) return;
158 
159 	var signatures = data.signature;
160 	if (!signatures) return;
161 
162 	for (var i = 0; i < signatures.length; i++) {
163 		var signature = ZmSignature.createFromJson(signatures[i]);
164 		this.add(signature);
165 	}
166 };
167 
168 //
169 // Static functions
170 //
171 
172 ZmSignatureCollection.BY_NAME =
173 function(a, b) {
174 	return a.name.localeCompare(b.name);
175 };
176