1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 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, 2011, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * Creates a new controller to show mail send confirmation.
 26  * @class
 27  * 
 28  * @param {DwtControl}	parent		the element that created this view
 29  * @param {ZmController}	controller	the controller managing this view
 30  * 
 31  * @extends		DwtComposite
 32  * 
 33  * @private
 34  */
 35 ZmMailConfirmView = function(parent, controller) {
 36 
 37 	this._view = controller.getCurrentViewId();
 38 	DwtComposite.call(this, {parent:parent, className:"ZmMailConfirmView", posStyle:Dwt.ABSOLUTE_STYLE,
 39 							 id:ZmId.getViewId(this._view)});
 40 
 41 	this.setVisible(false);
 42 	this._controller = controller;
 43 	this._tabGroup = new DwtTabGroup(this._htmlElId);
 44 	this._createHtmlFromTemplate("mail.Message#ZmMailConfirmView", { id: this._htmlElId } );
 45 	var buttonArgs = {
 46 		parent: this,
 47 		parentElement: this._htmlElId + "_addButton"
 48 	};
 49 	this._addContactsButton = new DwtButton(buttonArgs);
 50 	this._addContactsButton.setText(ZmMsg.ok);
 51 	this._addContactsButton.addSelectionListener(new AjxListener(this, this._addContactsListener));
 52 
 53 	this._summaryFormat = new AjxMessageFormat(ZmMsg.confirmSummary);
 54 };
 55 
 56 ZmMailConfirmView.prototype = new DwtComposite;
 57 ZmMailConfirmView.prototype.constructor = ZmMailConfirmView;
 58 
 59 ZmMailConfirmView.prototype.toString = function() {
 60 	return "ZmMailConfirmView";
 61 };
 62 
 63 /**
 64  * Adds a listener for the Create Contacts button.
 65  * 
 66  * @param	{AjxListener}	listener		the listener
 67  */
 68 ZmMailConfirmView.prototype.addNewContactsListener =
 69 function(listener) {
 70 	this.addListener(DwtEvent.ACTION, listener);
 71 };
 72 
 73 /**
 74  * Shows confirmation that the message was sent.
 75  *
 76  * @param {ZmMailMsg}	msg			the message that was sent
 77  */
 78 ZmMailConfirmView.prototype.showConfirmation =
 79 function(msg) {
 80 	this._tabGroup.removeAllMembers();
 81 	
 82 	this._showLoading(true);
 83 	var addresses = msg.getAddresses(AjxEmailAddress.TO).getArray().concat(msg.getAddresses(AjxEmailAddress.CC).getArray());
 84 
 85 	if (!appCtxt.get(ZmSetting.CONTACTS_ENABLED) || appCtxt.get(ZmSetting.AUTO_ADD_ADDRESS)) {
 86 		this._setView(msg, [], [], addresses);
 87 	} else {
 88 		var callback = new AjxCallback(this, this._handleResponseGetContacts, [msg]);
 89 		appCtxt.getApp(ZmId.APP_CONTACTS).getContactsByEmails(addresses, callback);
 90 	}
 91 
 92 	appCtxt.notifyZimlets("onMailConfirm", [this, msg, Dwt.byId(this._htmlElId + "_ad")]);
 93 };
 94 
 95 ZmMailConfirmView.prototype.getController =
 96 function() {
 97 	return this._controller;
 98 };
 99 
100 ZmMailConfirmView.prototype.getTitle =
101 function() {
102 	return [ZmMsg.zimbraTitle, ZmMsg.messageSent].join(": ");
103 };
104 
105 ZmMailConfirmView.prototype.getTabGroupMember = function() {
106 	return this._tabGroup;
107 };
108 
109 ZmMailConfirmView.prototype.getDefaultFocusItem =
110 function() {
111 	return this._addContactsButton;
112 };
113 
114 ZmMailConfirmView.prototype._handleResponseGetContacts =
115 function(msg, contacts) {
116 	var newAddresses = [],
117 		existingContacts = [];
118 	for (var i = 0, count = contacts.length; i < count; i++) {
119 		if (contacts[i].contact) {
120 			existingContacts.push(contacts[i]);
121 		} else {
122 			newAddresses.push(contacts[i].address);
123 		}
124 	}
125 	this._setView(msg, newAddresses, existingContacts, []);
126 };
127 
128 ZmMailConfirmView.prototype._setView =
129 function(msg, newAddresses, existingContacts, displayAddresses) {
130 	this._showLoading(false);
131 	Dwt.byId(this._htmlElId + "_summary").innerHTML = AjxStringUtil.htmlEncode(this._summaryFormat.format(msg.subject));
132 	this._showNewAddresses(newAddresses);
133 	this._showExistingContacts(existingContacts);
134 	this._showDisplayAddresses(displayAddresses);
135 };
136 
137 ZmMailConfirmView.prototype._showLoading =
138 function(loading) {
139 	Dwt.setVisible(Dwt.byId(this._htmlElId + "_loading"), loading);
140 	Dwt.setVisible(Dwt.byId(this._htmlElId + "_notLoading"), !loading);
141 	this.setVisible(true);	
142 };
143 
144 ZmMailConfirmView.prototype._showNewAddresses =
145 function(newAddresses) {
146 	var visible = newAddresses.length;
147 	Dwt.setVisible(Dwt.byId(this._htmlElId + "_newAddresses"), visible);
148 	this._addContactsButton.setVisible(visible);
149 	if (this._newAddressForms) {
150 		for (var i = 0, count = this._newAddressForms.length; i < count; i++) {
151 			this._newAddressForms[i].dispose();
152 		}
153 		this._newAddressForms = [];
154 	}
155 	this._newAddressForms = [];
156 	var newAddressBox = Dwt.byId(this._htmlElId + "_newAddressBox");
157 	for (var i = 0, count = newAddresses.length; i < count; i++) {
158 		var div = document.createElement("DIV");
159 		newAddressBox.appendChild(div);
160 		var address = newAddresses[i],
161 			first = "",
162 			last = "";
163 		var name = address.getName();
164 		if (name) {
165 			var index = name.lastIndexOf(" ");
166 			if (index != -1) {
167 				first = name.substring(0, index);
168 				last = name.substring(index + 1);
169 			} else {
170 				first = name;
171 			}
172 		}
173 		var args = { parent: this, parentElement: div };
174 		args.form = {
175 			items: [
176 				// default items
177 				{ id: "CHECKBOX", type: "DwtCheckbox", label: address.getAddress() },
178 				{ id: "FIRST", type: "DwtInputField", value: first, hint: ZmMsg.AB_FIELD_firstName },
179 				{ id: "LAST", type: "DwtInputField", value: last, hint: ZmMsg.AB_FIELD_lastName }
180 			],
181 			template: "mail.Message#ZmMailConfirmViewNewAddress"
182 		};
183 		var form = new DwtForm(args);
184 		this._newAddressForms.push(form);
185 		this._tabGroup.addMember(form.getTabGroupMember());
186 	}
187 	if (visible) {
188 		this._tabGroup.addMember(this._addContactsButton);
189 	}
190 };
191 
192 ZmMailConfirmView.prototype._showExistingContacts =
193 function(existingContacts) {
194 	Dwt.setVisible(Dwt.byId(this._htmlElId + "_existingContacts"), existingContacts.length);
195 	var existingContactBox = Dwt.byId(this._htmlElId + "_existingContactBox");
196 	existingContactBox.innerHTML = "";
197 	for (var i = 0, count = existingContacts.length; i < count; i++) {
198 		var div = document.createElement("DIV");
199 		existingContactBox.appendChild(div);
200 		var data = existingContacts[i];
201 		var display;
202 		var fullName = data.contact.getFullName();
203 		if (fullName) {
204 			display = [fullName, " <", data.address.getAddress(), ">"].join("");
205 		} else {
206 			display = data.address.getAddress();
207 		}
208 		div.innerHTML = AjxTemplate.expand("mail.Message#ZmMailConfirmViewExistingContact", { text: AjxStringUtil.htmlEncode(display) });
209 	}
210 };
211 
212 ZmMailConfirmView.prototype._showDisplayAddresses =
213 function(displayAddresses) {
214 	Dwt.setVisible(Dwt.byId(this._htmlElId + "_displayAddresses"), displayAddresses.length);
215 	var displayAddressBox = Dwt.byId(this._htmlElId + "_displayAddressBox");
216 	displayAddressBox.innerHTML = "";
217 	for (var i = 0, count = displayAddresses.length; i < count; i++) {
218 		var div = document.createElement("DIV");
219 		displayAddressBox.appendChild(div);
220 		var address = displayAddresses[i].toString();
221 		div.innerHTML = AjxTemplate.expand("mail.Message#ZmMailConfirmViewExistingContact", { text: AjxStringUtil.htmlEncode(address) });
222 	}
223 };
224 
225 ZmMailConfirmView.prototype._addContactsListener =
226 function() {
227 	var newAddresses = [];
228 	for (var i = 0, count = this._newAddressForms.length; i < count; i++) {
229 		var form = this._newAddressForms[i];
230 		if (form.getValue("CHECKBOX")) {
231 			var data = {};
232 			data[ZmContact.F_email] = form.getControl("CHECKBOX").getText();
233 			data[ZmContact.F_firstName] = form.getValue("FIRST");
234 			data[ZmContact.F_lastName] = form.getValue("LAST");
235 			newAddresses.push(data);
236 		}
237 	}
238 	this.notifyListeners(DwtEvent.ACTION, newAddresses);
239 };
240 
241 ZmMailConfirmView.prototype.deactivate =
242 function() {
243 	this._controller.inactive = true;
244 };