1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 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) 2010, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * Creates a dialog with a list of visible accounts to choose from
 26  * @class
 27  * This class represents choose account dialog.
 28  *
 29  * @param	{DwtControl}	parent		the parent, usually the global DwtShell
 30  *
 31  * @extends		ZmDialog
 32  */
 33 ZmChooseAccountDialog = function(parent) {
 34 	ZmDialog.call(this, {parent:parent});
 35 	this._createControls();
 36 };
 37 
 38 ZmChooseAccountDialog.prototype = new ZmDialog;
 39 ZmChooseAccountDialog.prototype.constructor = ZmChooseAccountDialog;
 40 
 41 ZmChooseAccountDialog.prototype.toString =
 42 function() {
 43 	return "ZmChooseAccountDialog";
 44 };
 45 
 46 /**
 47  * Pops up this dialog
 48  *
 49  * @param selectedAccount	{ZmZimbraAccount}	Optional. The account to have initially "selected". Otherwise, the active account is selected.
 50  * @param accountType		{String}			Optional. Only offer accounts of this type. Otherwise, all visible accounts are offered.
 51  * @param chooserMessage	{String}			Optional. The message to prompt user with. A default message is used if none provided.
 52  * @param title				{String}			Optional. Dialog title.
 53  */
 54 ZmChooseAccountDialog.prototype.popup =
 55 function(selectedAccount, accountType, chooserMessage, title) {
 56 	this.setTitle(title || ZmMsg.chooseAccount);
 57 
 58 	this._chooseMessageEl.innerHTML = chooserMessage || ZmMsg.chooseAccount;
 59 
 60 	var activeAcct = selectedAccount || appCtxt.getActiveAccount();
 61 	var accounts = appCtxt.accountList.visibleAccounts;
 62 
 63 	var html = [];
 64 	var idx = 0;
 65 
 66 	html[idx++] = "<table border=0 cellpadding=1 cellspacing=1>";
 67 	for (var i = 0; i < accounts.length; i++) {
 68 		var acct = accounts[i];
 69 		if (appCtxt.isOffline && acct.isMain) { continue; }
 70 		if (accountType && acct.type != accountType) { continue; }
 71 
 72 		var icon = appCtxt.isOffline ? acct.getIcon() : null;
 73 		var inputId = Dwt.getNextId();
 74 
 75 		html[idx++] = "<tr><td><input type='checkbox' name='";
 76 		html[idx++] = this._inputName;
 77 		html[idx++] = "'";
 78 		if (acct == activeAcct) {
 79 			html[idx++] = " checked";
 80 		}
 81 		html[idx++] = " _acctId='";
 82 		html[idx++] = acct.id;
 83 		html[idx++] = "' id='";
 84 		html[idx++] = inputId;
 85 		html[idx++] = "'></td>";
 86 		if (icon) {
 87 			html[idx++] = "<td>";
 88 			html[idx++] = AjxImg.getImageHtml(icon);
 89 			html[idx++] = "</td>";
 90 		}
 91 		html[idx++] = "<td><label for='";
 92 		html[idx++] = inputId;
 93 		html[idx++] = "'>";
 94 		html[idx++] = acct.getDisplayName();
 95 		html[idx++] = "</label></td></tr>";
 96 	}
 97 	html[idx++] = "</table>";
 98 	this._accountSelectEl.innerHTML = html.join("");
 99 
100 	ZmDialog.prototype.popup.call(this);
101 };
102 
103 ZmChooseAccountDialog.prototype._okButtonListener =
104 function(ev) {
105 	var selected = document.getElementsByName(this._inputName);
106 	var accountIds = [];
107 	for (var i = 0; i < selected.length; i++) {
108 		if (selected[i].checked) {
109 			accountIds.push(selected[i].getAttribute("_acctId"));
110 		}
111 	}
112 	DwtDialog.prototype._buttonListener.call(this, ev, [accountIds]);
113 };
114 
115 ZmChooseAccountDialog.prototype._enterListener =
116 function(ev) {
117 	this._okButtonListener.call(this, ev);
118 };
119 
120 ZmChooseAccountDialog.prototype._contentHtml =
121 function() {
122 	return AjxTemplate.expand("share.Widgets#ZmChooseAccountDialog", {id:this._htmlElId});
123 };
124 
125 ZmChooseAccountDialog.prototype._createControls =
126 function() {
127 	this._accountSelectEl = document.getElementById(this._htmlElId+"_accountSelectId");
128 	this._chooseMessageEl = document.getElementById(this._htmlElId+"_chooseAccountMsg");
129 	this._inputName = this._htmlElId + "_accountCheckbox";
130 };
131