1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2005, 2006, 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) 2005, 2006, 2007, 2008, 2009, 2010, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * @overview
 26  * This file contains the zimlet class.
 27  */
 28 
 29 /**
 30  * Creates the zimlet
 31  * @class
 32  * This class represents a zimlet.
 33  * 
 34  * @param	{String}	id		the id
 35  * @param	{String}	name	the name
 36  * @param	{Object}	parent	the parent
 37  * @param	{ZmTree}	tree	the tree
 38  * @param	{String}	color	the color
 39  * @extends		ZmOrganizer
 40  */
 41 ZmZimlet = function(id, name, parent, tree, color) {
 42 	ZmOrganizer.call(this, {type: ZmOrganizer.ZIMLET, id: id, name: name, parent: parent, tree: tree});
 43 };
 44 
 45 ZmZimlet.prototype = new ZmOrganizer();
 46 ZmZimlet.prototype.constructor = ZmZimlet;
 47 
 48 // test hack 
 49 ZmZimlet.actionMenus = {};
 50 ZmZimlet.actionMenus["ZmCalViewController"] = [];
 51 ZmZimlet.listeners = {};
 52 ZmZimlet.listeners["ZmCalViewController"] = {};
 53 
 54 // Constants
 55 ZmZimlet.ID_ZIMLET = ZmOrganizer.ID_ZIMLET;
 56 ZmZimlet.ID_ZIMLET_ROOT = ZmZimlet.ID_ZIMLET + "_root";
 57 
 58 /**
 59  * Returns a string representation of the object.
 60  * 
 61  * @return		{String}		a string representation of the object
 62  */
 63 ZmZimlet.prototype.toString =
 64 function() {
 65 	return "ZmZimlet - " + this.name;
 66 };
 67 
 68 /**
 69  * Sets the name
 70  * 
 71  * @param	{String}	name		the name
 72  */
 73 ZmZimlet.prototype.setName =
 74 function(name) {
 75 	this.name = name;
 76 };
 77 
 78 // Static methods
 79 /**
 80  * @private
 81  */
 82 ZmZimlet.createFromJs =
 83 function(parent, obj, tree, link) {
 84 	if (!obj && obj.length < 1) {return null;}
 85 
 86 	// create zimlet root
 87 	var zimletRoot = new ZmZimlet(ZmZimlet.ID_ZIMLET_ROOT, ZmMsg.zimlets, parent, tree, null, null);
 88 	if (obj && obj.length) {
 89 		var id = ZmZimlet.ID_ZIMLET;
 90 		for (var i = 0; i < obj.length; i++) {
 91 			var lbl = obj[i].processMessage(obj[i].zimletPanelItem.label);
 92 			// bug fix #23860 - unique-ify zimlet ID's so they dont conflict!
 93 			var zimletId = (++id) + "_z";
 94 			var childZimlet = new ZmZimlet(zimletId, lbl, zimletRoot, tree, null, null);
 95 			zimletRoot.children.add(childZimlet);
 96 			// WARNING: it's a bit unorthodox to do this linkage
 97 			// here, but we really do need these objects know about
 98 			// each other.
 99 			childZimlet._zimletContext = obj[i];
100 			childZimlet._zimletContext._id = zimletId;
101 			childZimlet._toolTip = obj[i].zimletPanelItem.toolTipText;
102 			obj[i]._organizer = childZimlet;
103 		}
104 	}
105 	return zimletRoot;
106 };
107 
108 /**
109  * Compares and sorts the zimlets by name (case-insensitive).
110  * 
111  * @param	{ZmZimlet}	zimletA		the zimlet
112  * @param	{ZmZimlet}	zimletB		the zimlet
113  * @return	{int}	0 if the zimlets match; 1 if "a" is before "b"; -1 if "b" is before "a"
114  */
115 ZmZimlet.sortCompare =
116 function(zimletA, zimletB) {
117 	var check = ZmOrganizer.checkSortArgs(zimletA, zimletB);
118 	if (!check) {return check;}
119 
120 	// sort by name
121 	var zimletAName = zimletA.name.toLowerCase();
122 	var zimletBName = zimletB.name.toLowerCase();
123 	if (zimletAName < zimletBName) {return -1;}
124 	if (zimletAName > zimletBName) {return 1;}
125 	return 0;
126 };
127 
128 /**
129  * Checks the name.
130  * 
131  * @param	{String}	name		the name
132  * @return	{String}	the name
133  * @see		ZmOrganizer.checkName()
134  */
135 ZmZimlet.checkName =
136 function(name) {
137 	return ZmOrganizer.checkName(name);
138 };
139 
140 // Public methods
141 /**
142  * Resets the names.
143  * 
144  */
145 ZmZimlet.prototype.resetNames =
146 function() {
147 	var oldName = this.name;
148 	var oldToolTip = this._toolTip;
149 	if(this._zimletContext && this._toolTip) {
150 		this._toolTip = this._zimletContext.processMessage(this._toolTip);
151 	}
152 	if(this._zimletContext && this.name) {
153 		this.name = this._zimletContext.processMessage(this.name);
154 	}
155 	// Update only if there was a change
156 	if((oldName != this.name) || (oldToolTip != this._toolTip)) {
157 		var fields = {};
158 		fields[ZmOrganizer.F_NAME] = true;
159 		var details = {};
160 		details.fields = fields;
161 		this._notify(ZmEvent.E_MODIFY, details);
162 	}
163 };
164 
165 /**
166  * Sets the tool tip text on the control.
167  * 
168  * @param	{DwtControl}	control		the control
169  */
170 ZmZimlet.prototype.setToolTipText =
171 function(control) {
172 	control.setToolTipContent(this._toolTip);
173 };
174 
175 /**
176  * Gets the icon.
177  * 
178  * @return	{String}	the icon
179  */
180 ZmZimlet.prototype.getIcon =
181 function() {
182 	return (this.id == ZmZimlet.ID_ZIMLET_ROOT) ? null : this._zimletContext.icon;
183 };
184 
185 /**
186  * Gets the zimlet context.
187  * 
188  * @return	{ZmZimletContext}	the context
189  */
190 ZmZimlet.prototype.getZimletContext =
191 function() {
192 	return this._zimletContext;
193 };
194 
195 /**
196  * Checks if the tag supports sharing.
197  * 
198  * @return	{Boolean}	always returns <code>false</code>. Zimlets cannot be shared.
199  */
200 ZmZimlet.prototype.supportsSharing =
201 function() {
202 	// zimlets cannot be shared
203 	return false;
204 };
205