1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013, 2014, 2015, 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, 2011, 2013, 2014, 2015, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * @overview
 26  */
 27 
 28 /**
 29  * Creates an "accept share" dialog.
 30  * @class
 31  * This class represents an "accept share" dialog.
 32  * 
 33  * @param	{DwtControl}	parent		the parent
 34  * @param	{String}	className		the class name
 35  * 
 36  * @extends		DwtDialog
 37  */
 38 ZmAcceptShareDialog = function(parent, className) {
 39 	className = className || "ZmAcceptShareDialog";
 40 	DwtDialog.call(this, {parent:parent, className:className, title:ZmMsg.acceptShare,
 41 						  standardButtons:[DwtDialog.YES_BUTTON, DwtDialog.NO_BUTTON], id: "ZmAcceptShare"});
 42 	this.setButtonListener(DwtDialog.YES_BUTTON, new AjxListener(this, this._handleYesButton));
 43 	this.setButtonListener(DwtDialog.NO_BUTTON, new AjxListener(this, this._handleNoButton));
 44 	
 45 	this.setView(this._createView());
 46 	
 47 	// create formatters
 48 	this._headerFormatter = new AjxMessageFormat(ZmMsg.acceptShareHeader);
 49 	this._detailsFormatter = new AjxMessageFormat(ZmMsg.acceptShareDetails);
 50 };
 51 
 52 ZmAcceptShareDialog.prototype = new DwtDialog;
 53 ZmAcceptShareDialog.prototype.constructor = ZmAcceptShareDialog;
 54 
 55 // Constants
 56 
 57 ZmAcceptShareDialog._ACTIONS = {};
 58 ZmAcceptShareDialog._ACTIONS[ZmShare.ROLE_NONE]		= ZmMsg.acceptShareDetailsNone;
 59 ZmAcceptShareDialog._ACTIONS[ZmShare.ROLE_VIEWER]	= ZmMsg.acceptShareDetailsViewer;
 60 ZmAcceptShareDialog._ACTIONS[ZmShare.ROLE_MANAGER]	= ZmMsg.acceptShareDetailsManager;
 61 ZmAcceptShareDialog._ACTIONS[ZmShare.ROLE_ADMIN]	= ZmMsg.acceptShareDetailsAdmin;
 62 
 63 // Public methods
 64 
 65 /**
 66  * Pops-up the dialog.
 67  * 
 68  * @param	{ZmShare}		share		the share
 69  * @param	{String}		fromAddr	the from address
 70  */
 71 ZmAcceptShareDialog.prototype.popup =
 72 function(share, fromAddr) {
 73 
 74 	this._share = share;
 75 	this._fromAddr = fromAddr;
 76 	this._headerEl.innerHTML = this._headerFormatter.format([AjxStringUtil.htmlEncode(share.grantor.name) || share.grantor.email, AjxStringUtil.htmlEncode(share.link.name)]);
 77 
 78 	var role = ZmShare._getRoleFromPerm(share.link.perm);
 79 	var params = [
 80 		ZmShare.getRoleName(role),
 81 		ZmAcceptShareDialog._ACTIONS[role]   // TODO: Be able to generate custom perms list
 82 	];
 83 	this._detailsEl.innerHTML = this._detailsFormatter.format(params);
 84 	this._questionEl.innerHTML = "<b>" + ZmMsg.acceptShareQuestion + "</b>";
 85 
 86 	var namePart = share.grantor.name || (share.grantor.email && share.grantor.email.substr(0, share.grantor.email.indexOf('@')));
 87 	this._nameEl.value = ZmShare.getDefaultMountpointName(namePart, share.link.name);
 88 
 89 	this._reply.setReplyType(ZmShareReply.NONE);
 90 	this._reply.setReplyNote("");
 91 
 92 	var orgType = ZmOrganizer.TYPE[share.link.view];
 93 	var icon = null;
 94 	var orgClass = ZmOrganizer.ORG_CLASS[orgType];
 95 	if (orgClass) {
 96 		var orgPackage = ZmOrganizer.ORG_PACKAGE[orgType];
 97 		if (orgPackage) {
 98 			AjxDispatcher.require(orgPackage);
 99 			//to fix bug 55320 - got rid of the calling getIcon on the prototype hack - that caused isRemote to set _isRemote on the prototype thus causing every object to have it by default set.
100 			var sample = new window[orgClass]({}); //get a sample object just for the icon
101 			sample._isRemote = true; //hack - so it would get the remote version of the icon
102 			icon = sample.getIcon();
103 		}
104 	}
105 	this._color.setImage(icon);
106 	this._color.setValue(ZmOrganizer.DEFAULT_COLOR[orgType]);
107 	
108 	DwtDialog.prototype.popup.call(this);
109 };
110 
111 /**
112  * Sets the accept listener.
113  * 
114  * @param	{AjxListener}		listener		the listener
115  */
116 ZmAcceptShareDialog.prototype.setAcceptListener =
117 function(listener) {
118 	this.removeAllListeners(ZmAcceptShareDialog.ACCEPT);
119 	if (listener) {
120 		this.addListener(ZmAcceptShareDialog.ACCEPT, listener);
121 	}
122 };
123 
124 // Protected methods
125 
126 ZmAcceptShareDialog.prototype._handleYesButton =
127 function(ev) {
128 	var replyType = this._reply.getReplyType();
129 	var notes = (replyType == ZmShareReply.QUICK) ? this._reply.getReplyNote(): "";
130 	var callback = new AjxCallback(this, this._yesButtonCallback, [ev]);
131 	this._share.accept(this._nameEl.value, this._color.getValue(), replyType, notes, callback, this._fromAddr);
132 };
133 
134 ZmAcceptShareDialog.prototype._yesButtonCallback =
135 function(ev) {
136 	// notify accept listener and clear
137 	this.notifyListeners(ZmAcceptShareDialog.ACCEPT, ev);
138 	this.setAcceptListener(null);
139 	this.popdown();
140 };
141 
142 ZmAcceptShareDialog.prototype._handleNoButton =
143 function(ev) {
144 	this.popdown();
145 };
146 
147 ZmAcceptShareDialog.prototype._getSeparatorTemplate =
148 function() {
149 	return "";
150 };
151 
152 ZmAcceptShareDialog.prototype._createView =
153 function() {
154 	var view = new DwtComposite(this);
155 
156 	this._headerEl = document.createElement("DIV");
157 	this._headerEl.style.marginBottom = "0.5em";
158 	this._detailsEl = document.createElement("DIV");
159 	this._detailsEl.style.marginBottom = "1em";
160 	this._detailsEl.id = "ZmAcceptShare_details";
161 	this._questionEl = document.createElement("DIV");
162 	this._questionEl.style.marginBottom = "0.5em";
163 	this._questionEl.id = "ZmAcceptShare_questions";
164 	this._nameEl = document.createElement("INPUT");
165 	this._nameEl.style.width = "20em";
166 	this._nameEl.id = "ZmAcceptShare_name";
167 	var nameElement = this._nameEl;
168 
169 	this._color = new ZmColorButton({parent:this, id: "ZmAcceptShare_color"});
170 
171 	var props = this._propSheet = new DwtPropertySheet(view);
172 	var propsEl = props.getHtmlElement();
173 	propsEl.style.marginBottom = "0.5em";
174 	propsEl.id = "ZmAcceptShare_props";
175 	props.addProperty(ZmMsg.nameLabel, nameElement);
176 	props.addProperty(ZmMsg.colorLabel, this._color);
177 
178 	this._reply = new ZmShareReply(view);
179 
180 	var settings = document.createElement("DIV");
181 	settings.style.marginLeft = "1.5em";
182 	settings.id = "ZmAcceptShare_settings";
183 	settings.appendChild(propsEl);
184 	settings.appendChild(this._reply.getHtmlElement());	
185 
186 	var el = view.getHtmlElement();
187 	el.appendChild(this._headerEl);
188 	el.appendChild(this._detailsEl);
189 	el.appendChild(this._questionEl);
190 	el.appendChild(settings);
191 
192 	this._tabGroup.addMember(this._color.getTabGroupMember());
193 	this._tabGroup.addMember(this._propSheet.getTabGroupMember());
194 	this._tabGroup.addMember(this._reply.getTabGroupMember());
195 
196 	return view;
197 };
198