1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2011, 2012, 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) 2011, 2012, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 ZmContactQuickAddDialog = function() {
 25 	ZmDialog.call(this, {parent:appCtxt.getShell(), className:"ZmContactQuickAddDialog", title:ZmMsg.quickAddContact,
 26 						  standardButtons:[DwtDialog.OK_BUTTON, DwtDialog.CANCEL_BUTTON]});
 27 
 28 	// set content
 29 	this.setContent(this._contentHtml());
 30 	this._initialize();
 31 	
 32 	this.setButtonListener(DwtDialog.OK_BUTTON, new AjxListener(this, this._saveListener));
 33 };
 34 
 35 ZmContactQuickAddDialog.prototype = new ZmDialog;
 36 ZmContactQuickAddDialog.prototype.constructor = ZmContactQuickAddDialog;
 37 
 38 ZmContactQuickAddDialog.prototype._contentHtml = 
 39 function() {   
 40 	var html = "<div style='width: 350px' id='CONTACT_QUICKADD_FORM'></div>";	
 41 	return html;			
 42 };
 43 
 44 ZmContactQuickAddDialog.prototype._initialize = 
 45 function() {
 46 	var params = {};
 47 	params.parent = this;
 48 	params.template = "abook.Contacts#QuickAddPrompt";
 49 	params.id = "ZmContactQuickAddDialog";
 50 	params.form = {
 51 		items: [
 52 			{ id: "FIRST_NAME", type: "DwtInputField", label: "First Name", value: "", cols: 35},
 53 			{ id: "LAST_NAME", type: "DwtInputField", label: "Last Name", value: "", cols: 35},
 54 			{ id: "EMAIL", type: "DwtInputField", label: "Email", value: "", cols: 35},
 55 			{ id: "ADDR_BOOK", type: "DwtSelect", items: []}
 56 		]
 57 	};
 58 	this._quickAddForm = new DwtForm(params);
 59 	var quickAddForm = document.getElementById("CONTACT_QUICKADD_FORM");
 60 	this._quickAddForm.appendElement(quickAddForm);
 61 	
 62 };
 63 
 64 /**
 65  * Popup quick add dialog
 66  */
 67 ZmContactQuickAddDialog.prototype.popup = 
 68 function(saveCallback) {
 69 	this._saveCallback = saveCallback;
 70 	this._updateAddressBooks();
 71 	ZmDialog.prototype.popup.call(this);
 72 	this._quickAddForm.getControl("FIRST_NAME").focus();
 73 };
 74 
 75 
 76 ZmContactQuickAddDialog.prototype.setFields = 
 77 function(email) {
 78 	if (this._quickAddForm) {
 79 		var emailField = this._quickAddForm.getControl("EMAIL");
 80 		emailField.setValue(email);
 81 		
 82 		var fnameField = this._quickAddForm.getControl("FIRST_NAME");
 83 		fnameField.setValue("");
 84 		
 85 		var lnameField = this._quickAddForm.getControl("LAST_NAME");
 86 		lnameField.setValue("");
 87 	}
 88 };
 89 
 90 ZmContactQuickAddDialog.prototype._saveListener =
 91 function() {
 92 	var firstName = this._quickAddForm.getControl("FIRST_NAME");
 93 	var lastName = this._quickAddForm.getControl("LAST_NAME");
 94 	var email = this._quickAddForm.getControl("EMAIL");
 95 	var addrBook = this._quickAddForm.getControl("ADDR_BOOK");
 96 	
 97 	var contact = new ZmContact(null, null, ZmItem.CONTACT);
 98 	var attr = {};
 99 	attr[ZmContact.F_firstName] = firstName.getValue();
100 	attr[ZmContact.F_lastName] = lastName.getValue();
101 	attr[ZmContact.F_email] = email.getValue();
102 	attr[ZmContact.F_folderId] = addrBook.getValue();
103 	var batchCommand = new ZmBatchCommand(false, null, true);
104 	batchCommand.add(new AjxCallback(contact, contact.create, [attr]));
105 	if (this._saveCallback) {
106 		batchCommand.run(this._saveCallback, [contact]);
107 	}
108 	this.popdown();
109 };
110 
111 ZmContactQuickAddDialog.prototype._getAddressBooks = 
112 function() {
113 	var folderTree = appCtxt.getFolderTree();
114 	var addrBooks = folderTree.getByType(ZmOrganizer.ADDRBOOK);
115 	for (var i=0; i<addrBooks.length; i++) {
116 		if (addrBooks[i].isReadOnly()) {
117 			addrBooks.splice(i,1); //if addrBook is read only do not add it to list
118 		}
119 	}
120 	return addrBooks;
121 };
122 
123 ZmContactQuickAddDialog.prototype._updateAddressBooks = 
124 function() {
125 	var select = this._quickAddForm.getControl("ADDR_BOOK");
126 	select.clearOptions();
127 	
128 	var addrBooks = this._getAddressBooks();
129 	for (var i=0; i<addrBooks.length; i++) {
130 		if (addrBooks[i].id == ZmFolder.ID_DLS) {
131 			continue;
132 		}
133 		var selectOption = new DwtSelectOption(addrBooks[i].nId, false, addrBooks[i].name);
134 		select.addOption(selectOption);	
135 	}
136 };
137 
138 ZmContactQuickAddDialog.prototype._getTabGroupMembers =
139 function() {
140 	var firstName = this._quickAddForm.getControl("FIRST_NAME");
141 	var lastName = this._quickAddForm.getControl("LAST_NAME");
142 	var email = this._quickAddForm.getControl("EMAIL");
143 	var addrBook = this._quickAddForm.getControl("ADDR_BOOK");
144 	return [firstName, lastName, email, addrBook];
145 };
146