1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 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) 2010, 2011, 2012, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 ZmTaskMultiView = function(params) {
 25 
 26 	if (arguments.length == 0) { return; }
 27 
 28     params.className = params.className || "ZmTaskMultiView";
 29     params.mode = ZmId.VIEW_TASKMULTI;
 30 
 31 	var view = params.controller.getCurrentViewId();
 32 	params.id = ZmId.getViewId(view);
 33 	DwtComposite.call(this, params);
 34 
 35 	this._controller = params.controller;
 36 
 37     this._taskListView = this._createTaskListView(params);
 38 
 39 	this._vertMsgSash = new DwtSash({parent:this, style:DwtSash.HORIZONTAL_STYLE, className:"AppSash-horiz",
 40 									 threshold:ZmTaskMultiView.SASH_THRESHOLD, posStyle:Dwt.ABSOLUTE_STYLE});
 41 	this._vertMsgSash.registerCallback(this._sashCallback, this);
 42 
 43 	this._horizMsgSash = new DwtSash({parent:this, style:DwtSash.VERTICAL_STYLE, className:"AppSash-vert",
 44 									  threshold:ZmTaskMultiView.SASH_THRESHOLD, posStyle:Dwt.ABSOLUTE_STYLE});
 45 	this._horizMsgSash.registerCallback(this._sashCallback, this);
 46 
 47 	this._taskView = new ZmTaskView(this, DwtControl.ABSOLUTE_STYLE, this._controller);
 48 
 49 	this.setReadingPane();
 50 }
 51 
 52 ZmTaskMultiView.prototype = new DwtComposite;
 53 ZmTaskMultiView.prototype.constructor = ZmTaskMultiView;
 54 
 55 ZmTaskMultiView.prototype.toString =
 56 function() {
 57 	return "ZmTaskMultiView";
 58 };
 59 
 60 // consts
 61 
 62 ZmTaskMultiView.SASH_THRESHOLD = 5;
 63 ZmTaskMultiView._TAG_IMG = "TI";
 64 
 65 // public methods
 66 
 67 ZmTaskMultiView.prototype.getController =
 68 function() {
 69 	return this._controller;
 70 };
 71 
 72 ZmTaskMultiView.prototype.getTitle =
 73 function() {
 74 	return this._taskListView.getTitle();
 75 };
 76 
 77 /**
 78  * Displays the reading pane, based on the current settings.
 79  */
 80 ZmTaskMultiView.prototype.setReadingPane =
 81 function() {
 82 
 83 	var tlv = this._taskListView, tv = this._taskView;
 84 	var readingPaneEnabled = this._controller.isReadingPaneOn();
 85     var readingPaneOnRight = this._controller.isReadingPaneOnRight();
 86 
 87     if (!readingPaneEnabled) {
 88 		tv.setVisible(false);
 89 		this._vertMsgSash.setVisible(false);
 90 		this._horizMsgSash.setVisible(false);
 91 	} else {
 92 		if (!tv.getVisible()) {
 93 			if (tlv.getSelectionCount() == 1) {
 94 				this._controller._setSelectedItem();
 95 			} else {
 96 				tv.reset();
 97 			}
 98 		}
 99 		tv.setVisible(true);
100 		var newSash = readingPaneOnRight ? this._vertMsgSash : this._horizMsgSash;
101 		var oldSash = readingPaneOnRight ? this._horizMsgSash : this._vertMsgSash;
102 		oldSash.setVisible(false);
103 		newSash.setVisible(true);
104 	}
105 
106 	tlv.reRenderListView();
107     if(readingPaneOnRight) {
108         tlv.setSortByAsc(ZmItem.F_SORTED_BY, true);
109     } else {
110         tlv.setSortByAsc(ZmItem.F_DATE, true);
111     }
112 
113     tv.noTab = !readingPaneEnabled || AjxEnv.isIE;
114 	var sz = this.getSize();
115 	this._resetSize(sz.x, sz.y, true);
116 };
117 
118 ZmTaskMultiView.prototype._createTaskListView =
119 function(params) {
120 	params.parent = this;
121 	params.posStyle = Dwt.ABSOLUTE_STYLE;
122 	params.id = DwtId.getListViewId(this._controller.getCurrentViewType());
123 	return new ZmTaskListView(this, this._controller, this._controller._dropTgt );
124 };
125 
126 ZmTaskMultiView.prototype.getTaskListView =
127 function() {
128 	return this._taskListView;
129 };
130 
131 ZmTaskMultiView.prototype.getTaskView =
132 function() {
133 	return this._taskView;
134 };
135 
136 ZmTaskMultiView.prototype.getSelectionCount =
137 function() {
138 	return this._taskListView.getSelectionCount();
139 };
140 
141 ZmTaskMultiView.prototype.getSelection =
142 function() {
143 	return this._taskListView.getSelection();
144 };
145 
146 ZmTaskMultiView.prototype.reset =
147 function() {
148 	this._taskListView.reset();
149 	this._taskView.reset();
150 };
151 
152 ZmTaskMultiView.prototype.getTask =
153 function() {
154 	return this._taskView.getTask();
155 };
156 
157 ZmTaskMultiView.prototype.setTask =
158 function(task) {
159 	this._taskView.set(task, ZmId.VIEW_TASK);
160 };
161 
162 ZmTaskMultiView.prototype.resetTask =
163 function(newTask) {
164 	this._taskView.resetMsg(newTask);
165 };
166 
167 ZmTaskMultiView.prototype.isTaskViewVisible =
168 function() {
169 	return this._taskView.getVisible();
170 };
171 
172 ZmTaskMultiView.prototype.setBounds =
173 function(x, y, width, height) {
174 	DwtComposite.prototype.setBounds.call(this, x, y, width, height);
175 	this._resetSize(width, height);
176 };
177 
178 ZmTaskMultiView.prototype._resetSize =
179 function(newWidth, newHeight, force) {
180 
181 
182 	if (newWidth <= 0 || newHeight <= 0) { return; }
183 	if (!force && newWidth == this._lastResetWidth && newHeight == this._lastResetHeight) { return; }
184 
185 	var readingPaneOnRight = this._controller.isReadingPaneOnRight();
186 
187 	if (this.isTaskViewVisible()) {
188 		var sash = this.getSash();
189 		var sashSize = sash.getSize();
190 		var sashThickness = readingPaneOnRight ? sashSize.x : sashSize.y;
191 		if (readingPaneOnRight) {
192 			var listViewWidth = this._vertSashX || (Number(ZmMsg.LISTVIEW_WIDTH)) || Math.floor(newWidth / 2.5);
193 			this._taskListView.resetSize(listViewWidth, newHeight);
194 			sash.setLocation(listViewWidth, 0);
195 			this._taskView.setBounds(listViewWidth + sashThickness, 0,
196 									newWidth - (listViewWidth + sashThickness), newHeight);
197 		} else {
198 			var listViewHeight = this._horizSashY || (Math.floor(newHeight / 2) - DwtListView.HEADERITEM_HEIGHT);
199 			this._taskListView.resetSize(newWidth, listViewHeight);
200 			sash.setLocation(0, listViewHeight);
201 			this._taskView.setBounds(0, listViewHeight + sashThickness, newWidth,
202 									newHeight - (listViewHeight + sashThickness));
203 		}
204 	} else {
205 		this._taskListView.resetSize(newWidth, newHeight);
206 	}
207 	this._taskListView._resetColWidth();
208 
209 	this._lastResetWidth = newWidth;
210 	this._lastResetHeight = newHeight;
211 };
212 
213 ZmTaskMultiView.prototype._sashCallback =
214 function(delta) {
215 
216 	var readingPaneOnRight = this._controller.isReadingPaneOnRight();
217 
218 	if (delta > 0) {
219 		if (readingPaneOnRight) {
220 			// moving sash right
221 			var minMsgViewWidth = this._taskView.getMinWidth();
222 			var currentMsgWidth = this._taskView.getSize().x;
223 			delta = Math.max(0, Math.min(delta, currentMsgWidth - minMsgViewWidth));
224 			var newListWidth = ((AjxEnv.isIE) ? this._vertMsgSash.getLocation().x : this._taskListView.getSize().x) + delta;
225 
226 			if (delta > 0) {
227 				this._taskListView.resetSize(newListWidth, Dwt.DEFAULT);
228 				this._taskView.setBounds(this._taskView.getLocation().x + delta, Dwt.DEFAULT,
229 										currentMsgWidth - delta, Dwt.DEFAULT);
230 			} else {
231 				delta = 0;
232 			}
233 		} else {
234 			// moving sash down
235 			var newMsgViewHeight = this._taskView.getSize().y - delta;
236 			var minMsgViewHeight = this._taskView.getMinHeight();
237 			if (newMsgViewHeight > minMsgViewHeight) {
238 				this._taskListView.resetSize(Dwt.DEFAULT, this._taskListView.getSize().y + delta);
239 				this._taskView.setBounds(Dwt.DEFAULT, this._taskView.getLocation().y + delta,
240 										Dwt.DEFAULT, newMsgViewHeight);
241 			} else {
242 				delta = 0;
243 			}
244 		}
245 	} else {
246 		var absDelta = Math.abs(delta);
247 
248 		if (readingPaneOnRight) {
249 			// moving sash left
250 			if (!this._minMLVWidth) {
251 				var firstHdr = this._taskListView._headerList[0];
252 				var hdrWidth = firstHdr._width;
253 				if (hdrWidth == "auto") {
254 					var header = Dwt.byId(firstHdr._id);
255 					hdrWidth = header && Dwt.getSize(header).x;
256 				}
257 				this._minMLVWidth = hdrWidth;
258 			}
259 
260 			var currentWidth = ((AjxEnv.isIE) ? this._vertMsgSash.getLocation().x : this._taskListView.getSize().x);
261 			absDelta = Math.max(0, Math.min(absDelta, currentWidth - this._minMLVWidth));
262 
263 			if (absDelta > 0) {
264 				delta = -absDelta;
265 				this._taskListView.resetSize(currentWidth - absDelta, Dwt.DEFAULT);
266 				this._taskView.setBounds(this._taskView.getLocation().x - absDelta, Dwt.DEFAULT,
267 										this._taskView.getSize().x + absDelta, Dwt.DEFAULT);
268 			} else {
269 				delta = 0;
270 			}
271 		} else {
272 			// moving sash up
273 			if (!this._minMLVHeight) {
274 				var list = this._taskListView.getList();
275 				if (list && list.size()) {
276 					var item = list.get(0);
277 					var div = document.getElementById(this._taskListView._getItemId(item));
278 					this._minMLVHeight = DwtListView.HEADERITEM_HEIGHT + (Dwt.getSize(div).y * 2);
279 				} else {
280 					this._minMLVHeight = DwtListView.HEADERITEM_HEIGHT;
281 				}
282 			}
283 
284 			if (this.getSash().getLocation().y - absDelta > this._minMLVHeight) {
285 				// moving sash up
286 				this._taskListView.resetSize(Dwt.DEFAULT, this._taskListView.getSize().y - absDelta);
287 				this._taskView.setBounds(Dwt.DEFAULT, this._taskView.getLocation().y - absDelta,
288 										Dwt.DEFAULT, this._taskView.getSize().y + absDelta);
289 			} else {
290 				delta = 0;
291 			}
292 		}
293 	}
294 
295 	if (delta) {
296 		this._taskListView._resetColWidth();
297 		if (readingPaneOnRight) {
298 			this._vertSashX = this._vertMsgSash.getLocation().x;
299 		} else {
300 			this._horizSashY = this._horizMsgSash.getLocation().y;
301 		}
302 	}
303 
304 	return delta;
305 };
306 
307 ZmTaskMultiView.prototype._selectFirstItem =
308 function() {
309 	var list = this._taskListView.getList();
310 	var selectedItem = list ? list.get(0) : null;
311 	if (selectedItem) {
312 		this._taskListView.setSelection(selectedItem, false);
313 	}
314 };
315 
316 ZmTaskMultiView.prototype.getSash =
317 function() {
318 	var readingPaneOnRight = this._controller.isReadingPaneOnRight();
319 	return readingPaneOnRight ? this._vertMsgSash : this._horizMsgSash;
320 };
321 
322 ZmTaskMultiView.prototype.getLimit =
323 function(offset) {
324 	return this._taskListView.getLimit(offset);
325 };
326 
327 ZmTaskMultiView.prototype.set =
328 function(list, sortField) { 
329 	this._taskListView.set(list, sortField);
330 	this.isStale = false;
331 };
332