xref: /plugin/davcal/script.js (revision 4ecb526c7347192ad8d51e77c5c0398be9f5d5af)
1/* DOKUWIKI:include_once fullcalendar-2.4.0/moment.js */
2/* DOKUWIKI:include_once fullcalendar-2.4.0/fullcalendar.js */
3/* DOKUWIKI:include_once fullcalendar-2.4.0/lang/de.js */
4/* DOKUWIKI:include_once fullcalendar-2.4.0/lang/en.js */
5/* DOKUWIKI:include_once fullcalendar-2.4.0/lang/nl.js */
6/* DOKUWIKI:include_once datetimepicker-2.4.5/jquery.datetimepicker.js */
7/* DOKUWIKI:include_once jstz.js */
8
9/**
10 * Initialize the DAVCal script, attaching some event handlers and triggering
11 * the initial load of the fullcalendar JS
12 */
13jQuery(function() {
14    // Redefine functions for using moment.js with datetimepicker
15
16    Date.parseDate = function( input, format ){
17      return moment(input,format).toDate();
18    };
19    Date.prototype.dateFormat = function( format ){
20      return moment(this).format(format);
21    };
22
23    // Attach to event links
24    var calendarpage = jQuery('#fullCalendar').data('calendarpage');
25    if(!calendarpage) return;
26    dw_davcal__modals.page = calendarpage;
27
28    jQuery('div.fullCalendarSettings a').each(function() {
29        var $link = jQuery(this);
30        var href = $link.attr('href');
31        if (!href) return;
32
33        $link.click(
34            function(e) {
35                dw_davcal__modals.showSettingsDialog();
36                e.preventDefault();
37                return '';
38            }
39        );
40        }
41    );
42
43    // First, retrieve the current settings.
44    // Upon success, initialize fullcalendar.
45    var postArray = { };
46    jQuery.post(
47        DOKU_BASE + 'lib/exe/ajax.php',
48        {
49            call: 'plugin_davcal',
50            id: dw_davcal__modals.page,
51            page: dw_davcal__modals.page,
52            action: 'getSettings',
53            params: postArray
54        },
55        function(data)
56        {
57            var result = data['result'];
58            if(result === true)
59            {
60                dw_davcal__modals.settings = data['settings'];
61                var wknum = false;
62                var tz = false;
63                var we = true;
64                var ro = false;
65                var firstday = 0;
66                var detectedTz = jstz.determine().name();
67                dw_davcal__modals.detectedTz = detectedTz;
68                if(data['settings']['weeknumbers'] == 1)
69                    wknum = true;
70                if(data['settings']['timezone'] !== '')
71                    tz = data['settings']['timezone'];
72                if(data['settings']['workweek'] == 1)
73                    we = false;
74                if(data['settings']['monday'] == 1)
75                    firstday = 1;
76                var defaultView = data['settings']['meta']['view'];
77                // Initialize the davcal popup
78                var res = jQuery('#fullCalendar').fullCalendar({
79                    dayClick: function(date, jsEvent, view) {
80                        dw_davcal__modals.showEditEventDialog(date, false);
81                    },
82                    eventClick: function(calEvent, jsEvent, view) {
83                        dw_davcal__modals.showEditEventDialog(calEvent, true);
84                    },
85                    events: {
86                        url: DOKU_BASE + 'lib/exe/ajax.php',
87                        type: 'POST',
88                        data: {
89                            call: 'plugin_davcal',
90                            action: 'getEvents',
91                            id: dw_davcal__modals.page,
92                            page: dw_davcal__modals.page
93                        },
94                        error: function() {
95                            dw_davcal__modals.msg = LANG.plugins.davcal['error_retrieving_data'];
96                            dw_davcal__modals.showDialog(false);
97                        }
98                    },
99                    header: {
100                        left: 'title',
101                        center: 'today prev,next',
102                        right: 'month,agendaWeek,agendaDay'
103                    },
104                    lang: JSINFO.plugin.davcal['language'],
105                    weekNumbers: wknum,
106                    timezone: tz,
107                    weekends: we,
108                    firstDay: firstday,
109                    defaultView: defaultView
110                });
111            }
112        }
113    );
114});
115
116/**
117 * This holds all modal windows that DAVCal uses.
118 */
119var dw_davcal__modals = {
120    $editEventDialog: null,
121    $dialog: null,
122    $settingsDialog: null,
123    $inputDialog: null,
124    msg: null,
125    completeCb: null,
126    action: null,
127    uid: null,
128    settings: null,
129    page: null,
130    detectedTz: null,
131
132    /**
133     * Show the settings dialog
134     */
135    // FIXME: Hide URLs for multi-calendar
136    showSettingsDialog : function() {
137        if(dw_davcal__modals.$settingsDialog)
138            return;
139
140        // Dialog buttons are language-dependent and defined here.
141        // Attach event handlers for save and cancel.
142        var dialogButtons = {};
143        if(!JSINFO.plugin.davcal['disable_settings'])
144        {
145            dialogButtons[LANG.plugins.davcal['save']] = function() {
146                var postArray = { };
147                jQuery("input[class=dw_davcal__settings], select[class=dw_davcal__settings]").each(function() {
148                  if(jQuery(this).attr('type') == 'checkbox')
149                  {
150                      postArray[jQuery(this).prop('name')] = jQuery(this).prop('checked') ? 1 : 0;
151                  }
152                  else
153                  {
154                      postArray[jQuery(this).prop('name')] = jQuery(this).val();
155                  }
156                });
157                jQuery('#dw_davcal__ajaxsettings').html('<img src="'+DOKU_BASE+'lib/images/throbber.gif" alt="" width="16" height="16" />');
158                jQuery.post(
159                    DOKU_BASE + 'lib/exe/ajax.php',
160                    {
161                        call: 'plugin_davcal',
162                        id: dw_davcal__modals.page,
163                        page: dw_davcal__modals.page,
164                        action: 'saveSettings',
165                        params: postArray
166                    },
167                    function(data)
168                    {
169                        var result = data['result'];
170                        var html = data['html'];
171                        jQuery('#dw_davcal__ajaxsettings').html(html);
172                        if(result === true)
173                        {
174                            location.reload();
175                        }
176                    }
177                );
178            };
179        }
180        dialogButtons[LANG.plugins.davcal['cancel']] = function () {
181            dw_davcal__modals.hideSettingsDialog();
182        };
183
184        var settingsHtml = '<div><table>';
185
186        if(JSINFO.plugin.davcal['disable_settings'] && JSINFO.plugin.davcal['disable_sync'] && JSINFO.plugin.davcal['disable_ics'])
187        {
188            settingsHtml += LANG.plugins.davcal['nothing_to_show'];
189        }
190
191        if(!JSINFO.plugin.davcal['disable_settings'])
192        {
193            settingsHtml += '<tr><td>' + LANG.plugins.davcal['timezone'] + '</td><td><select name="timezone" id="dw_davcal__settings_timezone" class="dw_davcal__settings"></select></td></tr>' +
194            '<tr><td>' + LANG.plugins.davcal['weeknumbers'] + '</td><td><input type="checkbox" name="weeknumbers" id="dw_davcal__settings_weeknumbers" class="dw_davcal__settings"></td></tr>' +
195            '<tr><td>' + LANG.plugins.davcal['only_workweek'] + '</td><td><input type="checkbox" name="workweek" id="dw_davcal__settings_workweek" class="dw_davcal__settings"></td></tr>' +
196            '<tr><td>' + LANG.plugins.davcal['start_monday'] + '</td><td><input type="checkbox" name="monday" id="dw_davcal__settings_monday" class="dw_davcal__settings"></td></tr>';
197         }
198
199         if(!JSINFO.plugin.davcal['disable_sync'])
200         {
201             settingsHtml += '<tr id="dw_davcal__settings_syncurl"><td>' + LANG.plugins.davcal['sync_url'] + '</td><td><input type="text" name="syncurl" readonly="readonly" id="dw_davcal__settings_syncurl" class="dw_davcal__text" value="' + dw_davcal__modals.settings['syncurl'] + '"></td></tr>';
202         }
203
204         if(!JSINFO.plugin.davcal['disable_ics'])
205         {
206             settingsHtml += '<tr id="dw_davcal__settings_privateurl"><td>' + LANG.plugins.davcal['private_url'] + '</td><td><input type="text" name="privateurl" readonly="readonly" id="dw_davcal__settings_privateurl" class="dw_davcal__text" value="' + dw_davcal__modals.settings['privateurl'] + '"></td></tr>';
207         }
208
209         settingsHtml += '</table>' +
210            '</div>' +
211            '<div id="dw_davcal__ajaxsettings"></div>';
212
213        dw_davcal__modals.$settingsDialog = jQuery(document.createElement('div'))
214       .dialog({
215           autoOpen: false,
216           draggable: true,
217           title: LANG.plugins.davcal['settings'],
218           resizable: true,
219           buttons: dialogButtons,
220       })
221       .html(
222           settingsHtml
223            )
224       .parent()
225       .attr('id','dw_davcal__settings')
226       .show()
227       .appendTo('.dokuwiki:first');
228
229       jQuery('#dw_davcal__settings').position({
230           my: "center",
231           at: "center",
232           of: window
233       });
234
235       // Initialize current settings
236
237        if(!JSINFO.plugin.davcal['disable_settings'])
238        {
239            var $tzdropdown = jQuery('#dw_davcal__settings_timezone');
240            jQuery('#fullCalendarTimezoneList option').each(function() {
241                jQuery('<option />', {value: jQuery(this).val(),
242                        text: jQuery(this).text()}).appendTo($tzdropdown);
243            });
244
245            if(dw_davcal__modals.settings)
246            {
247                if(dw_davcal__modals.settings['timezone'] !== '')
248                    jQuery('#dw_davcal__settings_timezone').val(dw_davcal__modals.settings['timezone']);
249                if(dw_davcal__modals.settings['weeknumbers'] == 1)
250                    jQuery('#dw_davcal__settings_weeknumbers').prop('checked', true);
251                else
252                    jQuery('#dw_davcal__settings_weeknumbers').prop('checked', false);
253
254                if(dw_davcal__modals.settings['workweek'] == 1)
255                    jQuery('#dw_davcal__settings_workweek').prop('checked', true);
256                else
257                    jQuery('#dw_davcal__settings_workweek').prop('checked', false);
258
259                if(dw_davcal__modals.settings['monday'] == 1)
260                    jQuery('#dw_davcal__settings_monday').prop('checked', true);
261                else
262                    jQuery('#dw_davcal__settings_monday').prop('checked', false);
263            }
264        }
265
266        // attach event handlers
267        jQuery('#dw_davcal__settings .ui-dialog-titlebar-close').click(function(){
268          dw_davcal__modals.hideSettingsDialog();
269        });
270    },
271
272    /**
273     * Sanity-check our events.
274     *
275     * @return boolean false on failure, otherwise true
276     */
277    checkEvents : function() {
278        // Retrieve dates
279        var allDay = jQuery('#dw_davcal__allday_edit').prop('checked');
280        var startDate = moment(jQuery('#dw_davcal__eventfrom_edit').val(), 'YYYY-MM-DD');
281        var endDate = moment(jQuery('#dw_davcal__eventto_edit').val(), 'YYYY-MM-DD');
282
283        // Do the checking
284        if(!allDay)
285        {
286            var startTime = moment.duration(jQuery('#dw_davcal__eventfromtime_edit').val());
287            var endTime = moment.duration(jQuery('#dw_davcal__eventtotime_edit').val());
288            startDate.add(startTime);
289            endDate.add(endTime);
290        }
291        if(!startDate.isValid())
292        {
293            dw_davcal__modals.msg = LANG.plugins.davcal['start_date_invalid'];
294            dw_davcal__modals.showDialog(false);
295            return false;
296        }
297        if(!endDate.isValid())
298        {
299            dw_davcal__modals.msg = LANG.plugins.davcal['end_date_invalid'];
300            dw_davcal__modals.showDialog(false);
301            return false;
302        }
303        if(endDate.isBefore(startDate))
304        {
305            dw_davcal__modals.msg = LANG.plugins.davcal['end_date_before_start_date'];
306            dw_davcal__modals.showDialog(false);
307            return false;
308        }
309        if(!allDay && endDate.isSame(startDate))
310        {
311            dw_davcal__modals.msg = LANG.plugins.davcal['end_date_is_same_as_start_date'];
312            dw_davcal__modals.showDialog(false);
313            return false;
314        }
315        return true;
316    },
317
318    /**
319     * Show the edit event dialog, which is also used to create new events
320     * @param {Object} event The event to create, that is the date or the calEvent
321     * @param {Object} edit  Whether we edit (true) or create a new event (false)
322     */
323    showEditEventDialog : function(event, edit) {
324        if(dw_davcal__modals.$editEventDialog)
325            return;
326
327        var readonly = dw_davcal__modals.settings['readonly'];
328        var title = '';
329        var dialogButtons = {};
330        var calEvent = [];
331        var recurringWarning = '';
332        // Buttons are dependent on edit or create
333        // Several possibilities:
334        //
335        // 1) Somebody tries to edit, it is not recurring and not readonly -> show
336        // 2) Somebody tries to edit, it is recurring and not readonly -> message
337        // 3) Somebody tries to edit, it is readonly -> message
338        // 4) Somebody tries to create and it is readonly -> message
339        // 5) Somebody tries to create -> show
340        if(edit && (event.recurring != true) && (readonly === false))
341        {
342            calEvent = event;
343            title = LANG.plugins.davcal['edit_event'];
344            dialogButtons[LANG.plugins.davcal['edit']] = function() {
345                if(!dw_davcal__modals.checkEvents())
346                  return;
347                var postArray = { };
348                var attachArr = new Array();
349                var pageid = dw_davcal__modals.page;
350                if(dw_davcal__modals.settings['multi'])
351                {
352                    pageid = jQuery("#dw_davcal__editevent_calendar option:selected").val();
353                }
354                jQuery('.dw_davcal__editevent_attachment_link').each(function() {
355                    var attachment = jQuery(this).attr('href');
356                    if(attachment != undefined)
357                    {
358                        attachArr.push(attachment);
359                    }
360                });
361                postArray['attachments'] = attachArr;
362                jQuery("input.dw_davcal__editevent, textarea.dw_davcal__editevent").each(function() {
363                  if(jQuery(this).attr('type') == 'checkbox')
364                  {
365                      postArray[jQuery(this).prop('name')] = jQuery(this).prop('checked') ? 1 : 0;
366                  }
367                  else
368                  {
369                      postArray[jQuery(this).prop('name')] = jQuery(this).val();
370                  }
371                });
372                jQuery('#dw_davcal__ajaxedit').html('<img src="'+DOKU_BASE+'lib/images/throbber.gif" alt="" width="16" height="16" />');
373                jQuery.post(
374                    DOKU_BASE + 'lib/exe/ajax.php',
375                    {
376                        call: 'plugin_davcal',
377                        id: pageid,
378                        page: dw_davcal__modals.page,
379                        action: 'editEvent',
380                        params: postArray
381                    },
382                    function(data)
383                    {
384                        var result = data['result'];
385                        var html = data['html'];
386                        jQuery('#dw_davcal__ajaxedit').html(html);
387                        if(result === true)
388                        {
389                            jQuery('#fullCalendar').fullCalendar('refetchEvents');
390                            dw_davcal__modals.hideEditEventDialog();
391                        }
392                    }
393                );
394            };
395            dialogButtons[LANG.plugins.davcal['delete']] = function() {
396                dw_davcal__modals.action = 'deleteEvent';
397                dw_davcal__modals.msg = LANG.plugins.davcal['really_delete_this_event'];
398                dw_davcal__modals.completeCb = function(data) {
399                    var result = data['result'];
400                    var html = data['html'];
401                    jQuery('#dw_davcal__ajaxedit').html(html);
402                    if(result === true)
403                    {
404                        jQuery('#fullCalendar').fullCalendar('refetchEvents');
405                        dw_davcal__modals.hideEditEventDialog();
406                    }
407                };
408                dw_davcal__modals.showDialog(true);
409            };
410        }
411        else if(edit && (event.recurring == true) && (readonly === false))
412        {
413            calEvent = event;
414            title = LANG.plugins.davcal['edit_event'];
415            recurringWarning = LANG.plugins.davcal['recurring_cant_edit'];
416        }
417        else if(edit && (readonly === true))
418        {
419            calEvent = event;
420            title = LANG.plugins.davcal['edit_event'];
421            recurringWarning = LANG.plugins.davcal['no_permission'];
422        }
423        else if(readonly === true)
424        {
425            calEvent.start = event;
426            calEvent.end = moment(event);
427            calEvent.start.hour(12);
428            calEvent.start.minute(0);
429            calEvent.end.hour(13);
430            calEvent.end.minute(0);
431            calEvent.allDay = false;
432            calEvent.recurring = false;
433            calEvent.title = '';
434            calEvent.description = '';
435            calEvent.id = '0';
436            calEvent.page = dw_davcal__modals.page;
437            title = LANG.plugins.davcal['create_new_event'];
438            recurringWarning = LANG.plugins.davcal['no_permission'];
439        }
440        else
441        {
442            calEvent.start = event;
443            calEvent.end = moment(event);
444            calEvent.start.hour(12);
445            calEvent.start.minute(0);
446            calEvent.end.hour(13);
447            calEvent.end.minute(0);
448            calEvent.allDay = false;
449            calEvent.recurring = false;
450            calEvent.title = '';
451            calEvent.description = '';
452            calEvent.id = '0';
453            calEvent.page = dw_davcal__modals.settings['calids'][0]['page'];
454            title = LANG.plugins.davcal['create_new_event'];
455            dialogButtons[LANG.plugins.davcal['create']] = function() {
456                if(!dw_davcal__modals.checkEvents())
457                  return;
458
459                var postArray = { };
460                var pageid = dw_davcal__modals.page;
461                var attachArr = new Array();
462                if(dw_davcal__modals.settings['multi'])
463                {
464                    pageid = jQuery("#dw_davcal__editevent_calendar option:selected").val();
465                }
466                jQuery("input.dw_davcal__editevent, textarea.dw_davcal__editevent").each(function() {
467                  if(jQuery(this).attr('type') == 'checkbox')
468                  {
469                      postArray[jQuery(this).prop('name')] = jQuery(this).prop('checked') ? 1 : 0;
470                  }
471                  else
472                  {
473                      postArray[jQuery(this).prop('name')] = jQuery(this).val();
474                  }
475                });
476                jQuery('.dw_davcal__editevent_attachment_link').each(function() {
477                    var attachment = jQuery(this).attr('href');
478                    if(attachment != undefined)
479                    {
480                        attachArr.push(attachment);
481                    }
482                });
483                postArray['attachments'] = attachArr;
484                jQuery('#dw_davcal__ajaxedit').html('<img src="'+DOKU_BASE+'lib/images/throbber.gif" alt="" width="16" height="16" />');
485                jQuery.post(
486                    DOKU_BASE + 'lib/exe/ajax.php',
487                    {
488                        call: 'plugin_davcal',
489                        id: pageid,
490                        page: dw_davcal__modals.page,
491                        action: 'newEvent',
492                        params: postArray
493                    },
494                    function(data)
495                    {
496                        var result = data['result'];
497                        var html = data['html'];
498                        jQuery('#dw_davcal__ajaxedit').html(html);
499                        if(result === true)
500                        {
501                            jQuery('#fullCalendar').fullCalendar('refetchEvents');
502                            dw_davcal__modals.hideEditEventDialog();
503                        }
504                    }
505                );
506            };
507        }
508        dialogButtons[LANG.plugins.davcal['cancel']] = function() {
509            dw_davcal__modals.hideEditEventDialog();
510        };
511        dw_davcal__modals.uid = calEvent.id;
512        dw_davcal__modals.$editEventDialog = jQuery(document.createElement('div'))
513       .dialog({
514           autoOpen: false,
515           draggable: true,
516           title: title,
517           resizable: true,
518           buttons: dialogButtons,
519       })
520       .html(
521            '<div><table>' +
522            '<tr><td>' + LANG.plugins.davcal['calendar'] + '</td><td><select id="dw_davcal__editevent_calendar"></select></td></tr>' +
523            '<tr><td>' + LANG.plugins.davcal['title'] + '</td><td><input type="text" id="dw_davcal__eventname_edit" name="eventname" class="dw_davcal__editevent"></td></tr>' +
524            '<tr><td>' + LANG.plugins.davcal['description'] + '</td><td><textarea name="eventdescription" id="dw_davcal__eventdescription_edit" class="dw_davcal__editevent dw_davcal__text"></textarea></td></tr>' +
525            '<tr><td>' + LANG.plugins.davcal['from'] + '</td><td><input type="text" name="eventfrom" id="dw_davcal__eventfrom_edit" class="dw_davcal__editevent dw_davcal__date"><input type="text" name="eventfromtime" id="dw_davcal__eventfromtime_edit" class="dw_davcal__editevent dw_davcal__time"></td></tr>' +
526            '<tr><td>' + LANG.plugins.davcal['to'] + '</td><td><input type="text" name="eventto" id="dw_davcal__eventto_edit" class="dw_davcal__editevent dw_davcal__date"><input type="text" name="eventtotime" id="dw_davcal__eventtotime_edit" class="dw_davcal__editevent dw_davcal__time"></td></tr>' +
527            '<tr><td colspan="2"><input type="checkbox" name="allday" id="dw_davcal__allday_edit" class="dw_davcal__editevent">' + LANG.plugins.davcal['allday'] + '</td></tr>' +
528            '<tr><td>' + LANG.plugins.davcal['attachments'] + '</td><td><table id="dw_davcal__editevent_attachments"><tbody><tr><td><input type="text" id="dw_davcal__editevent_attachment" value="http://"></td><td><a href="#" id="dw_davcal__editevent_attach">' + LANG.plugins.davcal['add_attachment'] + '</a></td></tr></tbody></table></td></tr>' +
529            '</table>' +
530            recurringWarning +
531            '<input type="hidden" name="uid" id="dw_davcal__uid_edit" class="dw_davcal__editevent">' +
532            '<input type="hidden" name="detectedtz" id="dw_davcal__tz_edit" class="dw_davcal__editevent">' +
533            '</div>' +
534            '<div id="dw_davcal__ajaxedit"></div>'
535            )
536       .parent()
537       .attr('id','dw_davcal__edit')
538       .show()
539       .appendTo('.dokuwiki:first');
540
541       jQuery('#dw_davcal__edit').position({
542           my: "center",
543           at: "center",
544           of: window
545       });
546
547       // Populate calendar dropdown
548       var $dropdown = jQuery("#dw_davcal__editevent_calendar");
549       for(var i=0; i<dw_davcal__modals.settings['calids'].length; i++)
550       {
551           var sel = '';
552           if(calEvent.page == dw_davcal__modals.settings['calids'][i]['page'])
553             sel = ' selected="selected"';
554           $dropdown.append('<option value="' + dw_davcal__modals.settings['calids'][i]['page'] + '"' + sel + '>' + dw_davcal__modals.settings['calids'][i]['name'] + '</option>');
555       }
556       if(edit || (dw_davcal__modals.settings['calids'].length < 1))
557       {
558           $dropdown.prop('disabled', true);
559       }
560
561       // Set up existing/predefined values
562       jQuery('#dw_davcal__tz_edit').val(dw_davcal__modals.detectedTz);
563       jQuery('#dw_davcal__uid_edit').val(calEvent.id);
564       jQuery('#dw_davcal__eventname_edit').val(calEvent.title);
565       jQuery('#dw_davcal__eventfrom_edit').val(calEvent.start.format('YYYY-MM-DD'));
566       jQuery('#dw_davcal__eventfromtime_edit').val(calEvent.start.format('HH:mm'));
567       jQuery('#dw_davcal__eventdescription_edit').val(calEvent.description);
568       if(calEvent.attachments && (calEvent.attachments !== null))
569       {
570           for(var i=0; i<calEvent.attachments.length; i++)
571           {
572               var url = calEvent.attachments[i];
573               var row = '<tr><td><a href="' + url + '" class="dw_davcal__editevent_attachment_link">' + url + '</a></td><td><a class="deleteLink" href="#">' + LANG.plugins.davcal['delete'] + '</a></td></tr>';
574               jQuery('#dw_davcal__editevent_attachments > tbody:last').append(row);
575
576           }
577       }
578       dw_davcal__modals.attachAttachmentDeleteHandlers();
579       jQuery('#dw_davcal__editevent_attach').on("click", function(e)
580       {
581           e.preventDefault();
582           var url = jQuery('#dw_davcal__editevent_attachment').val();
583           jQuery('#dw_davcal__editevent_attachment').val('http://');
584           var row = '<tr><td><a href="' + url + '" class="dw_davcal__editevent_attachment_link">' + url + '</a></td><td><a class="deleteLink" href="#">' + LANG.plugins.davcal['delete'] + '</a></td></tr>';
585           jQuery('#dw_davcal__editevent_attachments > tbody:last').append(row);
586           dw_davcal__modals.attachAttachmentDeleteHandlers();
587           return false;
588       });
589       if(calEvent.allDay && (calEvent.end === null))
590       {
591           jQuery('#dw_davcal__eventto_edit').val(calEvent.start.format('YYYY-MM-DD'));
592           jQuery('#dw_davcal__eventtotime_edit').val(calEvent.start.format('HH:mm'));
593       }
594       else if(calEvent.allDay)
595       {
596           endEvent = moment(calEvent.end);
597           endEvent.subtract(1, 'days');
598           jQuery('#dw_davcal__eventto_edit').val(endEvent.format('YYYY-MM-DD'));
599           jQuery('#dw_davcal__eventotime_edit').val(endEvent.format('HH:mm'));
600       }
601       else
602       {
603           jQuery('#dw_davcal__eventto_edit').val(calEvent.end.format('YYYY-MM-DD'));
604           jQuery('#dw_davcal__eventtotime_edit').val(calEvent.end.format('HH:mm'));
605       }
606       jQuery('#dw_davcal__allday_edit').prop('checked', calEvent.allDay);
607
608        // attach event handlers
609        jQuery('#dw_davcal__edit .ui-dialog-titlebar-close').click(function(){
610          dw_davcal__modals.hideEditEventDialog();
611        });
612        jQuery('#dw_davcal__eventfrom_edit').datetimepicker({format:'YYYY-MM-DD',
613                                                      formatDate:'YYYY-MM-DD',
614                                                      datepicker: true,
615                                                      timepicker: false,
616                                                      });
617        jQuery('#dw_davcal__eventfromtime_edit').datetimepicker({format:'HH:mm',
618                                                      formatTime:'HH:mm',
619                                                      datepicker: false,
620                                                      timepicker: true,
621                                                      step: 15});
622        jQuery('#dw_davcal__eventto_edit').datetimepicker({format:'YYYY-MM-DD',
623                                                      formatDate:'YYYY-MM-DD',
624                                                      datepicker: true,
625                                                      timepicker: false,
626                                                      });
627        jQuery('#dw_davcal__eventtotime_edit').datetimepicker({format:'HH:mm',
628                                                      formatTime:'HH:mm',
629                                                      datepicker: false,
630                                                      timepicker: true,
631                                                      step:15});
632        jQuery('#dw_davcal__allday_edit').change(function() {
633            if(jQuery(this).is(":checked"))
634            {
635                jQuery('#dw_davcal__eventfromtime_edit').prop('readonly', true);
636                jQuery('#dw_davcal__eventtotime_edit').prop('readonly', true);
637            }
638            else
639            {
640                jQuery('#dw_davcal__eventfromtime_edit').prop('readonly', false);
641                jQuery('#dw_davcal__eventtotime_edit').prop('readonly', false);
642            }
643        });
644        jQuery('#dw_davcal__allday_edit').change();
645    },
646
647    attachAttachmentDeleteHandlers: function()
648    {
649       jQuery("#dw_davcal__editevent_attachments .deleteLink").on("click", function(e)
650       {
651            e.preventDefault();
652            var tr = jQuery(this).closest('tr');
653            tr.css("background-color", "#FF3700");
654            tr.fadeOut(400, function()
655            {
656                tr.remove();
657            });
658            return false;
659       });
660    },
661
662    /**
663     * Show an info/confirmation dialog
664     * @param {Object} confirm Whether a confirmation dialog (true) or an info dialog (false) is requested
665     */
666    showDialog : function(confirm)
667    {
668        if(dw_davcal__modals.$confirmDialog)
669            return;
670        var dialogButtons = {};
671        var title = '';
672        if(confirm)
673        {
674            title = LANG.plugins.davcal['confirmation'];
675            var pageid = dw_davcal__modals.page;
676            if(dw_davcal__modals.settings['multi'])
677            {
678                pageid = jQuery("#dw_davcal__editevent_calendar option:selected").val();
679            }
680            dialogButtons[LANG.plugins.davcal['yes']] =  function() {
681                            jQuery.post(
682                                DOKU_BASE + 'lib/exe/ajax.php',
683                                {
684                                    call: 'plugin_davcal',
685                                    id: pageid,
686                                    page: dw_davcal__modals.page,
687                                    action: dw_davcal__modals.action,
688                                    params: {
689                                        uid: dw_davcal__modals.uid
690                                    }
691                                },
692                                function(data)
693                                {
694                                    dw_davcal__modals.completeCb(data);
695                                }
696                            );
697                            dw_davcal__modals.hideDialog();
698                    };
699            dialogButtons[LANG.plugins.davcal['cancel']] = function() {
700                            dw_davcal__modals.hideDialog();
701                    };
702        }
703        else
704        {
705            title = LANG.plugins.davcal['info'];
706            dialogButtons[LANG.plugins.davcal['ok']] = function() {
707                 dw_davcal__modals.hideDialog();
708            };
709        }
710        dw_davcal__modals.$dialog = jQuery(document.createElement('div'))
711            .dialog({
712                autoOpen: false,
713                draggable: true,
714                title: title,
715                resizable: true,
716                buttons: dialogButtons,
717            })
718            .html(
719                '<div>' + dw_davcal__modals.msg + '</div>'
720            )
721            .parent()
722            .attr('id','dw_davcal__confirm')
723            .show()
724            .appendTo('.dokuwiki:first');
725
726            jQuery('#dw_davcal__confirm').position({
727                my: "center",
728                at: "center",
729                of: window
730            });
731                 // attach event handlers
732            jQuery('#dw_davcal__confirm .ui-dialog-titlebar-close').click(function(){
733                dw_davcal__modals.hideDialog();
734            });
735    },
736
737    /**
738     * Hide the edit event dialog
739     */
740    hideEditEventDialog : function() {
741        dw_davcal__modals.$editEventDialog.empty();
742        dw_davcal__modals.$editEventDialog.remove();
743        dw_davcal__modals.$editEventDialog = null;
744    },
745
746    /**
747     * Hide the confirm/info dialog
748     */
749    hideDialog: function() {
750        dw_davcal__modals.$dialog.empty();
751        dw_davcal__modals.$dialog.remove();
752        dw_davcal__modals.$dialog = null;
753    },
754
755    /**
756     * Hide the settings dialog
757     */
758    hideSettingsDialog: function() {
759        dw_davcal__modals.$settingsDialog.empty();
760        dw_davcal__modals.$settingsDialog.remove();
761        dw_davcal__modals.$settingsDialog = null;
762    }
763};
764