1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2004, 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) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * @overview
 26  */
 27 
 28 /**
 29  * Creates a new tag dialog.
 30  * @class
 31  * This class represents a new tag dialog.
 32  * 
 33  * @param	{DwtControl}	parent		the parent
 34  * @param	{String}	className		the class name
 35  * 
 36  * @extends		ZmDialog
 37  */
 38 ZmNewTagDialog = function(parent, className) {
 39 	ZmDialog.call(this, {parent:parent, className:className, title:ZmMsg.createNewTag, id:"CreateTagDialog"});
 40 
 41 	this._setNameField(this._htmlElId+"_name");
 42 	this._setTagColorMenu();
 43 	this._setAccountMenu();
 44 	DBG.timePt("set content");
 45 };
 46 
 47 ZmNewTagDialog.prototype = new ZmDialog;
 48 ZmNewTagDialog.prototype.constructor = ZmNewTagDialog;
 49 
 50 ZmNewTagDialog.prototype.toString = 
 51 function() {
 52 	return "ZmNewTagDialog";
 53 };
 54 
 55 /**
 56  * Pops-up the dialog.
 57  * 
 58  * @param	{ZmOrganizer}	org		the organizer
 59  * @param	{ZmAccount}		account	the account
 60  */
 61 ZmNewTagDialog.prototype.popup =
 62 function(org, account) {
 63 	if (this._accountSelect) {
 64 		var acct = account || appCtxt.getActiveAccount();
 65 		this._accountSelect.setSelectedValue(acct.id);
 66 	}
 67 
 68 	ZmDialog.prototype.popup.call(this);
 69 };
 70 
 71 ZmNewTagDialog.prototype.cleanup =
 72 function(bPoppedUp) {
 73 	DwtDialog.prototype.cleanup.call(this, bPoppedUp);
 74 	var color = this._getNextColor();
 75 	this._setColorButton(color, ZmOrganizer.COLOR_TEXT[color], ZmTag.getIcon(color));
 76 };
 77 
 78 ZmNewTagDialog.prototype._colorListener = 
 79 function(ev) {
 80 	var color = ev.item.getData(ZmOperation.MENUITEM_ID);
 81 	this._setColorButton(color, ZmOrganizer.COLOR_TEXT[color], ZmTag.getIcon(color));
 82 };
 83 
 84 ZmNewTagDialog.prototype._setTagColorMenu =
 85 function() {
 86 	var fieldId = this._htmlElId + "_tagColor";
 87 	this._colorButton = new DwtButton({parent:this, parentElement:fieldId, id:"ZmTagColorMenu"});
 88 	this._colorButton.noMenuBar = true;
 89 
 90 	var menu = ZmOperation.addColorMenu(this._colorButton, true);
 91 
 92 	var color = ZmOrganizer.DEFAULT_COLOR[ZmOrganizer.TAG];
 93 	this._setColorButton(color, ZmOrganizer.COLOR_TEXT[color], ZmTag.getIcon(color));
 94 
 95 	this._tagColorListener = new AjxListener(this, this._colorListener);
 96     menu.addSelectionListener(this._tagColorListener);
 97 };
 98 
 99 ZmNewTagDialog.prototype._setAccountMenu =
100 function() {
101 	if (!appCtxt.multiAccounts) { return; }
102 
103 	var fieldId = this._htmlElId + "_account";
104 	this._accountSelect = new DwtSelect({parent:this, parentElement:fieldId});
105 
106 	var accounts = appCtxt.accountList.visibleAccounts;
107 	for (var i = 0; i < accounts.length; i++) {
108 		var acct = accounts[i];
109 		if (appCtxt.get(ZmSetting.TAGGING_ENABLED, null, acct)) {
110 			var o = new DwtSelectOption(acct.id, null, acct.getDisplayName(), null, null, acct.getIcon());
111 			this._accountSelect.addOption(o);
112 		}
113 	}
114 };
115 
116 ZmNewTagDialog.prototype._setColorButton =
117 function(color, text, image) {
118 	this._colorButton.setData(ZmOperation.MENUITEM_ID, color);
119 	this._colorButton.setText(text || ZmMsg.custom);
120 	this._colorButton.setImage(image);
121 };
122 
123 ZmNewTagDialog.prototype._contentHtml = 
124 function() {
125 	return AjxTemplate.expand("share.Dialogs#ZmNewTagDialog", {id:this._htmlElId});
126 };
127 
128 ZmNewTagDialog.prototype._okButtonListener =
129 function(ev) {
130 	var results = this._getTagData();
131 	if (results) {
132 		DwtDialog.prototype._buttonListener.call(this, ev, results);
133 	}
134 };
135 
136 ZmNewTagDialog.prototype._getTagData =
137 function() {
138 	var acctId = this._accountSelect && this._accountSelect.getValue();
139 	var account = acctId && appCtxt.accountList.getAccount(acctId);
140 
141 	// check name for presence and validity
142 	var name = AjxStringUtil.trim(this._nameField.value);
143 	var msg = ZmTag.checkName(name);
144 
145 	// make sure tag doesn't already exist
146 	var tagTree = appCtxt.getTagTree(account);
147 	if (!msg && tagTree && tagTree.getByName(name)) {
148 		msg = ZmMsg.tagNameExists;
149 	}
150 
151 	if (msg) return this._showError(msg);
152     var color = this._colorButton.getData(ZmOperation.MENUITEM_ID);
153     var data = {name:name, color:color, accountName:(account && account.name)};
154     if (String(color).match(/^#/)) {
155         data.rgb = color;
156         delete data.color;
157     }
158     return data;
159 };
160 
161 ZmNewTagDialog.prototype._enterListener =
162 function(ev) {
163 	var results = this._getTagData();
164 	if (results) {
165 		this._runEnterCallback(results);
166 	}
167 };
168 
169 ZmNewTagDialog.prototype._getNextColor =
170 function() {
171 	var colorUsed = {};
172 	var tagTree = appCtxt.getTagTree();
173 	if (!tagTree) {
174 		return ZmOrganizer.DEFAULT_COLOR[ZmOrganizer.TAG];
175 	}
176 
177 	var tags = tagTree.root.children.getArray();
178 	if (!(tags && tags.length)) {
179 		return ZmOrganizer.DEFAULT_COLOR[ZmOrganizer.TAG];
180 	}
181 
182 	for (var i = 0; i < tags.length; i++) {
183 		colorUsed[tags[i].color] = true;
184 	}
185 	for (var i = 0; i < ZmTagTree.COLOR_LIST.length; i++) {
186 		var color = ZmTagTree.COLOR_LIST[i];
187 		if (!colorUsed[color]) {
188 			return color;
189 		}
190 	}
191 
192 	return ZmOrganizer.DEFAULT_COLOR[ZmOrganizer.TAG];
193 };
194 
195 ZmNewTagDialog.prototype._getTabGroupMembers =
196 function() {
197 	return [this._nameField, this._colorButton];
198 };
199