1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2011, 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) 2011, 2012, 2013, 2014, 2015, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 ZmExternalCalendarDialog = function(params) {
 25     var back = new DwtDialog_ButtonDescriptor(ZmExternalCalendarDialog.BACK_BUTTON, ZmMsg.back , DwtDialog.ALIGN_LEFT);
 26     var next = new DwtDialog_ButtonDescriptor(ZmExternalCalendarDialog.NEXT_BUTTON, ZmMsg.next, DwtDialog.ALIGN_RIGHT);
 27     var cancel = new DwtDialog_ButtonDescriptor(ZmExternalCalendarDialog.SHARE_CANCEL_BUTTON, ZmMsg.cancel, DwtDialog.ALIGN_RIGHT);
 28     var parent = params.parent || appCtxt.getShell();
 29     this._controller = params.controller;
 30     ZmDialog.call(this, {parent:parent, className:"ZmExternalCalendarDialog", standardButtons:[DwtDialog.NO_BUTTONS], extraButtons: [back, next, cancel], id:'ADD_EXTERNAL_CAL_DIALOG'});
 31 
 32 	this.setButtonListener(ZmExternalCalendarDialog.BACK_BUTTON, new AjxListener(this, this._backButtonListener));
 33 	this.setButtonListener(ZmExternalCalendarDialog.NEXT_BUTTON, new AjxListener(this, this._nextButtonListener));
 34 	this.setButtonListener(ZmExternalCalendarDialog.SHARE_CANCEL_BUTTON, new AjxListener(this, this._cancelButtonListener));
 35 
 36     this.getButton(ZmExternalCalendarDialog.BACK_BUTTON).setVisibility(false);
 37 	//var title = ZmMsg.addSharedCalendar;
 38 	var type = ZmOrganizer.CALENDAR;
 39     this.setTitle(ZmMsg.addSharedCalendar);
 40 	this.setContent(this.getDefaultContent());
 41     this._viewsLoaded = {};
 42     //this._viewsLoaded[ZmExternalCalendarDialog.FIRST_VIEW] = true;
 43     this.currentView = ZmExternalCalendarDialog.FIRST_VIEW;
 44     this.getViews();
 45 };
 46 
 47 ZmExternalCalendarDialog.prototype = new ZmDialog;
 48 ZmExternalCalendarDialog.prototype.constructor = ZmExternalCalendarDialog;
 49 
 50 ZmExternalCalendarDialog.FIRST_VIEW = 1;
 51 ZmExternalCalendarDialog.SECOND_VIEW = 2;
 52 ZmExternalCalendarDialog.THIRD_VIEW = 3;
 53 
 54 ZmExternalCalendarDialog.FIRST_VIEW_ID = "_shareCalendarView1";
 55 ZmExternalCalendarDialog.SECOND_VIEW_ID = "_shareCalendarView2";
 56 ZmExternalCalendarDialog.THIRD_VIEW_ID = "_shareCalendarView3";
 57 
 58 ZmExternalCalendarDialog.SYNC_TYPE_ICAL = "EXT_CAL_SYNCTYPE_DIALOG_ical";
 59 ZmExternalCalendarDialog.SYNC_TYPE_CALDAV = "EXT_CAL_DIALOG_SYNCTYPE_caldav";
 60 
 61 ZmExternalCalendarDialog.TYPE_YAHOO = "Yahoo";
 62 ZmExternalCalendarDialog.TYPE_OTHER = "Other";
 63 
 64 ZmExternalCalendarDialog.TEMPLATE = "calendar.Calendar#SharedCalendarDialog";
 65 ZmExternalCalendarDialog.BACK_BUTTON = ++DwtDialog.LAST_BUTTON;
 66 ZmExternalCalendarDialog.NEXT_BUTTON = ++DwtDialog.LAST_BUTTON;
 67 ZmExternalCalendarDialog.SHARE_CANCEL_BUTTON = ++DwtDialog.LAST_BUTTON;
 68 
 69 
 70 ZmExternalCalendarDialog.prototype.toString =
 71 function() {
 72 	return "ZmExternalCalendarDialog";
 73 };
 74 
 75 ZmExternalCalendarDialog.prototype.getDefaultContent =
 76 function() {
 77     var html = AjxTemplate.expand(ZmExternalCalendarDialog.TEMPLATE, {id: this._htmlElId});
 78     return html;
 79 };
 80 
 81 ZmExternalCalendarDialog.prototype.popup =
 82 function() {
 83     this.showView(ZmExternalCalendarDialog.FIRST_VIEW, ZmExternalCalendarDialog.TYPE_YAHOO);
 84     ZmDialog.prototype.popup.call(this);
 85 };
 86 
 87 ZmExternalCalendarDialog.prototype.popdown =
 88 function() {
 89     this.clearControls();
 90     ZmDialog.prototype.popdown.call(this);
 91 };
 92 
 93 ZmExternalCalendarDialog.prototype._nextButtonListener =
 94 function(ev) {
 95     var id = this._htmlElId;
 96     switch(this.currentView) {
 97         case ZmExternalCalendarDialog.FIRST_VIEW :
 98             /*var _shareRadioPublic = document.getElementById(id + '_shareRadioPublic');
 99             if(_shareRadioPublic && _shareRadioPublic.checked) {
100                 this._showSharePublicView();
101             }*/
102             var shareRadioYahoo = document.getElementById(id + '_shareRadioYahoo'),
103                 shareRadioOther = document.getElementById(id + '_shareRadioOther');
104 
105             if(shareRadioYahoo && shareRadioYahoo.checked) {
106                 this.showView(ZmExternalCalendarDialog.SECOND_VIEW, ZmExternalCalendarDialog.TYPE_YAHOO);
107             }
108             if(shareRadioOther && shareRadioOther.checked) {
109                 this.showView(ZmExternalCalendarDialog.SECOND_VIEW, ZmExternalCalendarDialog.TYPE_OTHER);
110             }
111             this.showIcalView(false);
112             this._syncTypeSelect.setSelectedValue(ZmExternalCalendarDialog.SYNC_TYPE_CALDAV);
113         break;
114 
115         case ZmExternalCalendarDialog.SECOND_VIEW :
116             var syncType = this._syncTypeSelect.getValue();
117             if (!this.validate(syncType)) {
118                 return false;
119             }
120             var extCalData = {};
121             if(syncType == ZmExternalCalendarDialog.SYNC_TYPE_CALDAV) {
122                 extCalData = {
123                     calDav : {
124                         userName : this._userNameInput.getValue(),
125                         password : this._passwordInput.getValue(),
126                         hostUrl : this._urlInput.getValue()
127                     },
128                     iCal : null
129                 };
130             }
131             else {
132                 extCalData = {
133                     calDav : null,
134                     iCal : {
135                         url : this._icsUrlInput.getValue()
136                     }
137                 };
138             }
139             this._controller.setExternalCalendarData(extCalData);
140             // Fix for Bug: 85158 and regression due to Bug: 82811. Passing isExternalCalendar => true
141             this._controller._newListener(ev, null, true);
142         break;
143 
144         case ZmExternalCalendarDialog.THIRD_VIEW :
145         break;
146     }
147 };
148 ZmExternalCalendarDialog.prototype._backButtonListener =
149 function() {
150     var id = this._htmlElId;
151     switch(this.currentView) {
152         case ZmExternalCalendarDialog.FIRST_VIEW :
153             //this.showView(ZmExternalCalendarDialog.FIRST_VIEW);
154         break;
155 
156         case ZmExternalCalendarDialog.SECOND_VIEW :
157             this.showView(ZmExternalCalendarDialog.FIRST_VIEW);
158         break;
159 
160         case ZmExternalCalendarDialog.THIRD_VIEW :
161             this.showView(ZmExternalCalendarDialog.SECOND_VIEW);
162         break;
163     }
164 };
165 
166 ZmExternalCalendarDialog.prototype.validate =
167 function(syncType) {
168     var msg = "";
169     if(syncType == ZmExternalCalendarDialog.SYNC_TYPE_CALDAV) {
170         var userName = this._userNameInput.getValue(),
171             password = this._passwordInput.getValue(),
172             hostUrl = AjxStringUtil.trim(this._urlInput.getValue()),
173             url;
174         if(userName.indexOf('@') === -1) {
175             if (hostUrl.indexOf(ZmMsg.sharedCalCalDAVServerYahoo) !== -1){
176                 //yahoo selected
177                 userName += "@yahoo.com";
178             }
179         }
180         if(!AjxEmailAddress.isValid(userName)) {
181             msg = ZmMsg.errorInvalidEmail2;
182         }
183         if(!msg && AjxStringUtil.trim(password) == "") {
184             msg = ZmMsg.errorMissingPass;
185         }
186         if(!msg && !hostUrl) {
187             msg = ZmMsg.errorMissingUrl;
188         }
189     }
190     else {
191         url = this._icsUrlInput.getValue();
192         msg = ZmOrganizer.checkUrl(url);
193     }
194     if(msg) {
195         this._showError(msg);
196         return false;
197     }
198     return true;
199 };
200 
201 ZmExternalCalendarDialog.prototype._showSharePublicView =
202 function() {
203     //var psd = this._publichShareDialog || this.createPublicShareDialog();
204     //psd.popup();
205 };
206 
207 ZmExternalCalendarDialog.prototype.getViews =
208 function() {
209     var id = this._htmlElId;
210     this._views = {};
211     this._views[ZmExternalCalendarDialog.FIRST_VIEW] = document.getElementById(id + ZmExternalCalendarDialog.FIRST_VIEW_ID);
212     this._views[ZmExternalCalendarDialog.SECOND_VIEW] = document.getElementById(id + ZmExternalCalendarDialog.SECOND_VIEW_ID);
213     this._views[ZmExternalCalendarDialog.THIRD_VIEW] = document.getElementById(id + ZmExternalCalendarDialog.THIRD_VIEW_ID);
214 };
215 
216 ZmExternalCalendarDialog.prototype.hideAllViews =
217 function() {
218     for (var id in this._views) {
219         var view = this._views[id];
220         if(view) {
221             Dwt.setDisplay(view, Dwt.DISPLAY_NONE);
222         }
223     }
224 };
225 
226 ZmExternalCalendarDialog.prototype._changeCalType =
227 function() {
228     if(this.currentView != ZmExternalCalendarDialog.SECOND_VIEW) {
229         return;
230     }
231     var calType = this._syncTypeSelect.getValue();
232     this.showIcalView(ZmExternalCalendarDialog.SYNC_TYPE_ICAL == calType);
233 };
234 
235 ZmExternalCalendarDialog.prototype.showIcalView =
236 function(isIcal) {
237     var id = this._htmlElId,
238         el,
239         syncUserNameContainer = document.getElementById(id + "_syncUserNameContainer"),
240         syncPasswordContainer = document.getElementById(id + "_syncPasswordContainer"),
241         syncUrlContainer = document.getElementById(id + "_syncUrlContainer"),
242         syncIcsUrlContainer = document.getElementById(id + "_syncIcsUrlContainer"),
243         syncMsgContainer = document.getElementById(id + "_syncMsgContainer");
244 
245     Dwt.setVisible(syncUserNameContainer, !isIcal);
246     Dwt.setVisible(syncPasswordContainer, !isIcal);
247     Dwt.setVisible(syncUrlContainer, !isIcal);
248     //Dwt.setVisible(syncMsgContainer, !isIcal);
249     Dwt.setVisible(syncIcsUrlContainer, isIcal);
250     /*el = isIcal ? this._icsUrlInput.getInputElement() : this._userNameInput.getInputElement();
251     if(el) {
252         el.focus();
253     }*/
254 };
255 
256 ZmExternalCalendarDialog.prototype.showView =
257 function(viewId, type) {
258     viewId = viewId || ZmExternalCalendarDialog.FIRST_VIEW;
259     this.hideAllViews();
260     this.currentView = viewId;
261 
262     if(!this.isViewLoaded(viewId)) {
263         this.loadView(viewId);
264     }
265 
266     switch(viewId) {
267         case ZmExternalCalendarDialog.FIRST_VIEW :
268             this.getButton(ZmExternalCalendarDialog.BACK_BUTTON).setVisibility(false);
269             this.setTitle(ZmMsg.addExternalCalendar);
270         break;
271 
272         case ZmExternalCalendarDialog.SECOND_VIEW :
273             this.getButton(ZmExternalCalendarDialog.BACK_BUTTON).setVisibility(true);
274             if(type == ZmExternalCalendarDialog.TYPE_YAHOO) {
275                 this._userNameInput.setHint(ZmMsg.sharedCalUserNameYahooHint);
276                 this._urlInput._hideHint(ZmMsg.sharedCalCalDAVServerYahoo);
277                 this._urlInput.setEnabled(false);
278                 this._syncMsg.innerHTML = ZmMsg.sharedCalSyncMsgYahoo;
279                 this.setTitle(ZmMsg.sharedCalTitleYahoo);
280             }
281             else {
282                 this._userNameInput.setHint(ZmMsg.sharedCalUserNameHint);
283                 this._urlInput.setEnabled(true);
284                 this._urlInput.setValue("");
285                 this._urlInput._showHint();
286                 this._syncMsg.innerHTML = "";
287                 this.setTitle(ZmMsg.sharedCalTitleOther);
288             }
289         break;
290 
291         case ZmExternalCalendarDialog.THIRD_VIEW :
292             this.getButton(ZmExternalCalendarDialog.BACK_BUTTON).setVisibility(true);
293         break;
294 
295     }
296     Dwt.setDisplay(this._views[viewId], Dwt.DISPLAY_BLOCK);
297     this._setSecondViewTabGroup();
298 };
299 
300 ZmExternalCalendarDialog.prototype.loadView =
301 function(viewId) {
302     var id = this._htmlElId;
303 
304     switch(viewId) {
305         case ZmExternalCalendarDialog.FIRST_VIEW :
306             this._viewsLoaded[ZmExternalCalendarDialog.FIRST_VIEW] = true;
307         break;
308 
309         case ZmExternalCalendarDialog.SECOND_VIEW :
310             var syncTypeSelect = new DwtSelect({parent:this, parentElement: id + '_syncType'});
311             syncTypeSelect.addOption(ZmMsg.sharedCalTypeCalDAV, true, ZmExternalCalendarDialog.SYNC_TYPE_CALDAV);
312             syncTypeSelect.addOption(ZmMsg.sharedCalTypeICal, false, ZmExternalCalendarDialog.SYNC_TYPE_ICAL);
313             syncTypeSelect.addChangeListener(new AjxListener(this, this._changeCalType));
314             this._syncTypeSelect = syncTypeSelect;
315 
316             this._userNameInput = new DwtInputField({parent:this, parentElement: id + '_syncUserName', hint: ZmMsg.sharedCalUserNameHint, inputId:id + '_syncUserNameInput'});
317             this._passwordInput = new DwtInputField({parent:this, parentElement: id + '_syncPassword', type: DwtInputField.PASSWORD, inputId: id + '_syncPasswordInput'});
318             this._urlInput = new DwtInputField({parent:this, parentElement: id + '_syncUrl', hint: ZmMsg.sharedCalCalDAVServerHint, inputId: id+ 'syncUrlInput'});
319             this._icsUrlInput = new DwtInputField({parent:this, parentElement: id + '_syncIcsUrl', hint: ZmMsg.sharedCalIcsUrlHint, inputId: id+ '_syncIcsUrlInput'});
320             this._syncMsg = document.getElementById(id + '_syncMsg');
321             this._viewsLoaded[ZmExternalCalendarDialog.SECOND_VIEW] = true;
322 
323             this._userNameInput._showHint();
324             this._icsUrlInput._showHint();
325         break;
326 
327         case ZmExternalCalendarDialog.THIRD_VIEW :
328             this.getButton(ZmExternalCalendarDialog.BACK_BUTTON).setVisibility(true);
329             this._viewsLoaded[ZmExternalCalendarDialog.THIRD_VIEW] = true;
330         break;
331 
332     }
333 };
334 
335 ZmExternalCalendarDialog.prototype._setSecondViewTabGroup =
336 function() {
337     if(!this.isViewLoaded(ZmExternalCalendarDialog.SECOND_VIEW)) {
338         return false;
339     }
340     var members = [this._syncTypeSelect, this._userNameInput, this._passwordInput, this._urlInput, this._icsUrlInput];
341     for (var i = 0; i < members.length; i++) {
342         this._tabGroup.addMember(members[i], i);
343     }
344 	this._tabGroup.setFocusMember(this._syncTypeSelect);
345 };
346 
347 ZmExternalCalendarDialog.prototype.isViewLoaded =
348 function(viewId) {
349     return this._viewsLoaded[viewId] ? this._viewsLoaded[viewId] : false;
350 };
351 
352 ZmExternalCalendarDialog.prototype.clearControls =
353 function() {
354     if(this.isViewLoaded(ZmExternalCalendarDialog.SECOND_VIEW)) {
355         this._userNameInput.setValue("");
356         this._passwordInput.setValue("");
357         this._urlInput.setValue("");
358         this._icsUrlInput.setValue("");
359     }
360 };
361 
362 ZmExternalCalendarDialog.prototype.createPublicShareDialog =
363 function() {
364     /*var dialog = new ZmSharedCalendarSearchDialog({id:"ZmSharedCalendarSearchDialog"});
365     this._publichShareDialog = dialog;
366     return dialog;*/
367 };
368 
369 ZmExternalCalendarDialog.prototype._cancelButtonListener =
370 function() {
371     // reset the caldav object
372     this._controller.setExternalCalendarData(null);
373     this.popdown();
374 };
375 
376