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