1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2010, 2012, 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) 2010, 2012, 2013, 2014, 2015, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25 * show history of the status window
 26 * @param parent			the element that created this view
 27  * @private
 28 */
 29 ZmQuickReminderDialog = function(parent, reminderController, calController) {
 30 
 31 	// init custom buttons
 32 	var selectId = Dwt.getNextId();
 33 	// call base class
 34 	DwtDialog.call(this, {parent:parent, standardButtons: [DwtDialog.OK_BUTTON]});
 35 
 36 	this._reminderController = reminderController;
 37 	this._calController = calController;
 38 
 39 	this.setContent(this._contentHtml(selectId));
 40 	this.setTitle(ZmMsg.currentMeetings);
 41 };
 42 
 43 ZmQuickReminderDialog.prototype = new DwtDialog;
 44 ZmQuickReminderDialog.prototype.constructor = ZmQuickReminderDialog;
 45 
 46 
 47 ZmQuickReminderDialog.SOON = -AjxDateUtil.MSEC_PER_FIFTEEN_MINUTES;
 48 
 49 
 50 // Public methods
 51 
 52 ZmQuickReminderDialog.prototype.toString = 
 53 function() {
 54 	return "ZmQuickReminderDialog";
 55 };
 56 
 57 ZmQuickReminderDialog.prototype.popup =
 58 function() {
 59 	DwtDialog.prototype.popup.call(this);
 60 };
 61 
 62 ZmQuickReminderDialog.prototype.initialize =
 63 function(list) {
 64 	this._list = list.clone();
 65 	this._apptData = {};
 66 
 67 	var html = [];
 68 	var idx = 0;
 69 	var size = list.size();
 70 
 71 	html[idx++] = "<table cellpadding=0 cellspacing=0 border=0 width=100%>";
 72 	for (var i = 0; i < size; i++) {
 73 		var appt = list.get(i);
 74 		var uid = appt.getUniqueId(true);
 75 		var data = this._apptData[uid] = {appt:appt};
 76 		idx = this._addAppt(html, idx, appt, data, (i === size - 1));
 77 	}
 78     if(size == 0) {
 79         html[idx++] = '<tr name="rdsep">';
 80 		html[idx++] = '<td colspan=3><div>' + ZmMsg.noMeetingsFound + '</div></td>';
 81 		html[idx++] = '</tr>';
 82     }
 83 	html[idx++] = "</table>";
 84 
 85 	
 86 	if (this._openButtons) {
 87 		for (var id in this._openButtons) {
 88 			this._openButtons[id].dispose();
 89 		}
 90 	}
 91 	this._openButtons = {};
 92 
 93 	var openListener = new AjxListener(this, this._openButtonListener);
 94 	var div = document.getElementById(this._listId);
 95 	div.innerHTML = html.join("");
 96 
 97 	for (var i = 0; i < size; i++) {
 98 		var appt = list.get(i);
 99 		var uid = appt.getUniqueId(true);
100 		var data = this._apptData[uid];
101 
102 		// open button
103 		var openBtn = this._openButtons[data.openBtnId] = new DwtLinkButton({id: "openBtn_" + id, parent: this, parentElement: data.openLinkId, noDropDown: true});
104 		openBtn.setText(appt.getName());
105 		openBtn.addSelectionListener(openListener);
106 		openBtn.apptUid = uid;
107 
108 		this._updateDelta(data);
109 	}
110 };
111 
112 ZmQuickReminderDialog.prototype._contentHtml =
113 function(selectId) {
114 	this._listId = Dwt.getNextId();
115 	return ["<div class='ZmQuickReminderDialog' id='", this._listId, "'>"].join("");
116 };
117 
118 ZmQuickReminderDialog.prototype._updateDelta = 
119 function(data) {
120 	var td = document.getElementById(data.deltaId);
121 	if (td) {
122 		var startDelta = this._computeDelta(data.appt);
123 
124 		if (startDelta >= 0) 							td.className = 'ZmReminderOverdue';
125 		else if (startDelta > ZmQuickReminderDialog.SOON)	td.className = 'ZmReminderSoon';
126 		else											td.className = 'ZmReminderFuture';
127 
128 		td.innerHTML = ZmReminderDialog.formatDeltaString(startDelta);
129 	}
130 };
131 
132 ZmQuickReminderDialog.prototype._addAppt =
133 function(html, idx, appt, data, noSep) {
134 
135 	data.openLinkId = Dwt.getNextId();
136 	data.deltaId = Dwt.getNextId();
137 	data.rowId = Dwt.getNextId();
138 
139 	var calName = (appt.folderId != ZmOrganizer.ID_CALENDAR && this._calController)
140 		? this._calController.getCalendarName(appt.folderId) : null;
141 
142 	var calendar = appCtxt.getById(appt.folderId);
143 
144     var apptLabel = appt.isUpcomingEvent ? " (" + ZmMsg.upcoming + ")" : ""
145 
146 	var params = {
147 		noSep: noSep,
148 		rowId: data.rowId,
149 		calName: calName,
150 		accountName: (appCtxt.multiAccounts && calendar && calendar.getAccount().getDisplayName()),
151 		location: appt.getLocation(),
152 		apptIconHtml: (AjxImg.getImageHtml(appt.otherAttendees ? "ApptMeeting" : "Appointment")),
153 		organizer: appt.otherAtt ? appt.organizer : null,
154 		reminderName: (AjxStringUtil.htmlEncode(appt.name + apptLabel)),
155 		durationText: (AjxStringUtil.trim(this._getDurationText(appt))),
156 		deltaId: data.deltaId,
157 		openLinkId: data.openLinkId
158 	};
159 	html[idx++] = AjxTemplate.expand("calendar.Calendar#ReminderDialogRow", params);
160 	return idx;
161 };
162 
163 ZmQuickReminderDialog.prototype._openButtonListener =
164 function(ev) {
165 
166 	var obj = DwtControl.getTargetControl(ev);
167 	var data = this._apptData[obj.apptUid];
168 	var appt = data ? data.appt : null;
169 	if (appt) {
170 		AjxDispatcher.require(["MailCore", "CalendarCore", "Calendar"]);
171 
172 		var cc = AjxDispatcher.run("GetCalController");
173 
174 		// the give appt object is a ZmCalBaseItem. We need a ZmAppt
175 		var newAppt = new ZmAppt();
176 		for (var i in appt) {
177 			if (!AjxUtil.isFunction(appt[i])) {
178 				newAppt[i] = appt[i];
179 			}
180 		}
181 		var callback = new AjxCallback(cc, cc._showAppointmentDetails, newAppt);
182 		newAppt.getDetails(null, callback, null, null, true);
183         this.popdown();
184 	}
185 };
186 
187 ZmQuickReminderDialog.prototype._getDurationText =
188 function(appt) {
189 	var isMultiDay = appt.isMultiDay();
190 	var start = appt.startDate;
191 	var endTime = appt.getEndTime();
192 	var end = new Date(endTime);
193 
194 	if (appt.isAllDayEvent()) {
195 		end = new Date(endTime - (isMultiDay ? 2 * AjxDateUtil.MSEC_PER_HOUR : 0));
196 		var pattern = isMultiDay ? ZmMsg.apptTimeAllDayMulti : ZmMsg.apptTimeAllDay;
197 		return AjxMessageFormat.format(pattern, [start, end]);
198 	}
199 	var pattern = isMultiDay ? ZmMsg.apptTimeInstanceMulti : ZmMsg.apptTimeInstance;
200 	return AjxMessageFormat.format(pattern, [start, end, ""]);
201 };
202 
203 ZmQuickReminderDialog.prototype._computeDelta =
204 function(appt) {
205 	return ((new Date()).getTime() - appt.getStartTime());
206 };
207