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