1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2006, 2007, 2008, 2009, 2010, 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) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 ZmUploadConflictDialog = function(shell, className) {
 25 	className = className || "ZmUploadConflictDialog";
 26 	var title = ZmMsg.uploadConflict;
 27 	var standardButtons = [ DwtDialog.OK_BUTTON, DwtDialog.CANCEL_BUTTON ];
 28 	DwtDialog.call(this, {parent:shell, className:className, title:title, standardButtons:standardButtons});
 29 	this.setButtonListener(DwtDialog.OK_BUTTON, new AjxListener(this, this._resolve));
 30 
 31 	this._mineId = this._htmlElId+"_mine";
 32 	this._theirsId = this._htmlElId+"_theirs";
 33 	this._viewId = this._htmlElId+"_view";
 34 
 35 	this._createUploadHtml();
 36 }
 37 ZmUploadConflictDialog.prototype = new DwtDialog;
 38 ZmUploadConflictDialog.prototype.constructor = ZmUploadConflictDialog;
 39 
 40 ZmUploadConflictDialog.prototype.toString = function() {
 41 	return "ZmUploadConflictDialog";
 42 };
 43 
 44 // Constants
 45 
 46 ZmUploadConflictDialog._MINE = "mine";
 47 ZmUploadConflictDialog._THEIRS = "theirs";
 48 
 49 // Data
 50 
 51 ZmUploadConflictDialog.prototype._mineId;
 52 ZmUploadConflictDialog.prototype._theirsId;
 53 ZmUploadConflictDialog.prototype._viewId;
 54 
 55 ZmUploadConflictDialog.prototype._table;
 56 
 57 ZmUploadConflictDialog.prototype._conflicts;
 58 ZmUploadConflictDialog.prototype._conflictCallback;
 59 
 60 // Public methods
 61 
 62 ZmUploadConflictDialog.prototype.popup = function(folder, conflicts, callback, loc) {
 63 	// save data
 64 	this._uploadFolder = folder;
 65 	this._conflicts = conflicts;
 66 	this._conflictCallback = callback;
 67 
 68 	// setup dialog
 69 	var table = this._table;
 70 	for (var i = table.rows.length - 1; i > 0; i--) {
 71 		table.deleteRow(i);
 72 	}
 73 
 74 	for (var i = 0; i < conflicts.length; i++) {
 75 		var conflict = conflicts[i];
 76 		this.__addFileRow(table, conflict);
 77 	}
 78 
 79 	// show
 80 	DwtDialog.prototype.popup.call(this, loc);
 81 };
 82 
 83 ZmUploadConflictDialog.prototype.popdown = function() {
 84 	DwtDialog.prototype.popdown.call(this);
 85 	this._conflictCallback = null;
 86 };
 87 
 88 // Protected methods
 89 
 90 ZmUploadConflictDialog.prototype._resolve = function(){
 91 	var conflicts = this._conflicts;
 92 	var callback = this._conflictCallback;
 93 	this.popdown();
 94 	if (callback) {
 95 		callback.run(conflicts);
 96 	}
 97 };
 98 
 99 ZmUploadConflictDialog.prototype._selectAll = function(mineOrTheirs) {
100 	var element = this.getHtmlElement();
101 	var radios = element.getElementsByTagName("INPUT");
102 	for (var i = 0; i < radios.length; i++) {
103 		var radio = radios[i];
104 		if (radio.type != "radio") continue;
105 		radio.checked = radio.value == mineOrTheirs;
106 		ZmUploadConflictDialog.__setFileDone(radio);
107 	}
108 };
109 
110 // handlers
111 
112 ZmUploadConflictDialog._handleMine = function(event) {
113 	var target = DwtUiEvent.getTarget(event);
114 	var dialog = Dwt.getObjectFromElement(target);
115 	dialog._selectAll(ZmUploadConflictDialog._MINE);
116 };
117 
118 ZmUploadConflictDialog._handleTheirs = function(event) {
119 	var target = DwtUiEvent.getTarget(event);
120 	var dialog = Dwt.getObjectFromElement(target);
121 	dialog._selectAll(ZmUploadConflictDialog._THEIRS);
122 };
123 
124 ZmUploadConflictDialog._handleRadio = function(event) {
125 	var target = DwtUiEvent.getTarget(event);
126 	ZmUploadConflictDialog.__setFileDone(target);
127 };
128 ZmUploadConflictDialog.__setFileDone = function(radio) {
129 	var file = Dwt.getObjectFromElement(radio);
130 	file.done = radio.value == ZmUploadConflictDialog._THEIRS;
131 };
132 
133 ZmUploadConflictDialog._handleViewTheirs = function(event) {
134 	var target = DwtUiEvent.getTarget(event);
135 	var object = Dwt.getObjectFromElement(target);
136 	var dialog = object.dialog;
137 	var file = object.file;
138 
139     //module shared by docs edited in new window - use restUrl directly
140 	var winurl = [
141 		dialog._uploadFolder.restUrl || dialog._uploadFolder.getRestUrl(),
142 		"/",
143 		AjxStringUtil.urlComponentEncode(file.name)
144 	].join("");
145 	var winname = "_new";
146 	var winfeatures = [
147 		"width=",(window.outerWidth || 640),",",
148 		"height=",(window.outerHeight || 480),",",
149 		"location,menubar,",
150 		"resizable,scrollbars,status,toolbar"
151 	].join("");
152 
153 	var win = open(winurl, winname, winfeatures);
154 };
155 
156 ZmUploadConflictDialog._handleLinkOver = function(event) {
157 	this.style.cursor= "pointer";
158 };
159 ZmUploadConflictDialog._handleLinkOut = function(event) {
160 	this.style.cursor= "default";
161 };
162 
163 // view creation
164 
165 ZmUploadConflictDialog.prototype._createUploadHtml = function() {
166 	var div = document.createElement("DIV");
167 	div.innerHTML = ZmMsg.uploadConflictDesc;
168 	div.style.marginBottom = "0.5em";
169 
170 	var table = this._table = document.createElement("TABLE");
171 	table.border = 0;
172 	table.cellPadding = 0;
173 	table.cellSpacing = 3;
174 
175 	var row = table.insertRow(-1);
176 
177 	var cell = row.insertCell(-1);
178 	var id = this._mineId;
179 	var text = ZmMsg._new;
180 	var handler = ZmUploadConflictDialog._handleMine;
181 	cell.appendChild(this.__createLink(id, text, handler));
182 
183 	var cell = row.insertCell(-1);
184 	var id = this._theirsId;
185 	var text = ZmMsg.old;
186 	var handler = ZmUploadConflictDialog._handleTheirs;
187 	cell.appendChild(this.__createLink(id, text, handler));
188 
189 	var element = this._getContentDiv();
190 	element.appendChild(div);
191 	element.appendChild(table);
192 };
193 
194 // Private methods
195 
196 ZmUploadConflictDialog.prototype.__addFileRow = function(table, file) {
197 	var handler = ZmUploadConflictDialog._handleRadio;
198 
199 	var row = table.insertRow(-1);
200 
201 	var cell = row.insertCell(-1);
202 	var value = ZmUploadConflictDialog._MINE;
203 	cell.appendChild(this.__createRadio(file.name, value, true, handler, file));
204 
205 	var cell = row.insertCell(-1);
206 	var value = ZmUploadConflictDialog._THEIRS;
207 	cell.appendChild(this.__createRadio(file.name, value, false, handler, file));
208 
209 	var cell = row.insertCell(-1);
210 	cell.style.paddingLeft = "1em";
211 	cell.innerHTML = AjxStringUtil.htmlEncode(file.name);
212 
213 	var cell = row.insertCell(-1);
214 	cell.style.paddingLeft = "2em";
215 	var id = this._viewId+(table.rows.length-1);
216 	var text = ZmMsg.viewOld;
217 	var handler = ZmUploadConflictDialog._handleViewTheirs;
218 	var object = { dialog: this, file: file };
219 	cell.appendChild(this.__createLink(id, text, handler, object));
220 };
221 
222 ZmUploadConflictDialog.prototype.__createRadio =
223 function(name, value, checked, handler, object) {
224 	var radio;
225 	if (AjxEnv.isIE) {
226         try {
227             // NOTE: This has to be done because IE doesn't recognize the name
228             //       attribute if set programmatically.
229             var html = [];
230             var i = 0;
231             html[i++] = "<INPUT type=radio name='";
232             html[i++] = name;
233             html[i++] = "'";
234             if (checked) {
235                 html[i++] = " checked"
236             }
237             html[i++] = ">";
238             radio = document.createElement(html.join(""));
239         } catch (e) {
240             // But the above throws an exception for IE9+, non-quirks mode
241             radio = this.__createRadio1(checked, name);
242         }
243 	}
244 	else {
245 		radio = this.__createRadio1(checked, name);
246 	}
247 	radio.value = value;
248 
249 	if (handler) {
250 		Dwt.setHandler(radio, DwtEvent.ONCLICK, handler);
251 		Dwt.associateElementWithObject(radio, object || this);
252 	}
253 
254 	return radio;
255 };
256 
257 ZmUploadConflictDialog.prototype.__createRadio1 =
258 function(checked, name) {
259     var radio     = document.createElement("INPUT");
260     radio.type    = 'radio';
261     radio.checked = checked;
262     radio.name    = name;
263     return radio;
264 }
265 
266 
267 ZmUploadConflictDialog.prototype.__createLink =
268 function(id, text, handler, object) {
269 	var element = document.createElement("SPAN");
270 	element.id = id;
271 	element.style.color = "blue";
272 	element.style.textDecoration = "underline";
273 	element.style.padding = "3px";
274 	element.innerHTML = text;
275 
276 	Dwt.setHandler(element, DwtEvent.ONMOUSEOVER, ZmUploadConflictDialog._handleLinkOver);
277 	Dwt.setHandler(element, DwtEvent.ONMOUSEOUT, ZmUploadConflictDialog._handleLinkOut);
278 	Dwt.setHandler(element, DwtEvent.ONCLICK, handler);
279 	Dwt.associateElementWithObject(element, object || this);
280 
281 	return element;
282 };
283