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