xref: /plugin/davcal/script.js (revision 9f1dab8c22c579ae1903b00ba4d7de46f7cbdea4)
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           title: LANG.plugins.davcal['settings'],
238           resizable: true,
239           buttons: dialogButtons,
240       })
241       .html(
242           settingsHtml
243            )
244       .parent()
245       .attr('id','dw_davcal__settings')
246       .show()
247       .appendTo('.dokuwiki:first');
248
249       jQuery('#dw_davcal__settings').position({
250           my: "center",
251           at: "center",
252           of: window
253       });
254
255       // Initialize current settings
256
257        if(!JSINFO.plugin.davcal['disable_settings'])
258        {
259            var $tzdropdown = jQuery('#dw_davcal__settings_timezone');
260            jQuery('#fullCalendarTimezoneList option').each(function() {
261                jQuery('<option />', {value: jQuery(this).val(),
262                        text: jQuery(this).text()}).appendTo($tzdropdown);
263            });
264
265            var $tfdropdown = jQuery('#dw_davcal__settings_timeformat');
266            jQuery('<option />', {value: 'lang', text: LANG.plugins.davcal['language_specific']}).appendTo($tfdropdown);
267            jQuery('<option />', {value: '24h', text: '24h'}).appendTo($tfdropdown);
268            jQuery('<option />', {value: '12h', text: '12h'}).appendTo($tfdropdown);
269
270            if(!JSINFO.plugin.davcal['disable_sync'])
271            {
272                jQuery('#dw_davcal__settings_syncurl_edit').on('click', function() {
273                    jQuery(this).select();
274                });
275                jQuery('#dw_davcal__settings_principalurl_edit').on('click', function() {
276                    jQuery(this).select();
277                });
278            }
279
280            if(!JSINFO.plugin.davcal['disable_ics'])
281            {
282                jQuery('#dw_davcal__settings_privateurl_edit').on('click', function() {
283                    jQuery(this).select();
284                });
285            }
286
287            if(dw_davcal__modals.settings)
288            {
289                if(dw_davcal__modals.settings['timeformat'] !== '')
290                    jQuery('#dw_davcal__settings_timeformat').val(dw_davcal__modals.settings['timeformat']);
291                if(dw_davcal__modals.settings['timezone'] !== '')
292                    jQuery('#dw_davcal__settings_timezone').val(dw_davcal__modals.settings['timezone']);
293                if(dw_davcal__modals.settings['weeknumbers'] == 1)
294                    jQuery('#dw_davcal__settings_weeknumbers').prop('checked', true);
295                else
296                    jQuery('#dw_davcal__settings_weeknumbers').prop('checked', false);
297
298                if(dw_davcal__modals.settings['workweek'] == 1)
299                    jQuery('#dw_davcal__settings_workweek').prop('checked', true);
300                else
301                    jQuery('#dw_davcal__settings_workweek').prop('checked', false);
302
303                if(dw_davcal__modals.settings['monday'] == 1)
304                    jQuery('#dw_davcal__settings_monday').prop('checked', true);
305                else
306                    jQuery('#dw_davcal__settings_monday').prop('checked', false);
307                if(dw_davcal__modals.settings['meta']['forcetimezone'] !== 'no')
308                    jQuery('#dw_davcal__settings_timezone').prop('disabled', true);
309                if(dw_davcal__modals.settings['meta']['forcetimeformat'] !== 'no')
310                    jQuery('#dw_davcal__settings_timeformat').prop('disabled', true);
311            }
312        }
313
314        // attach event handlers
315        jQuery('#dw_davcal__settings .ui-dialog-titlebar-close').click(function(){
316          dw_davcal__modals.hideSettingsDialog();
317        });
318    },
319
320    /**
321     * Sanity-check our events.
322     *
323     * @return boolean false on failure, otherwise true
324     */
325    checkEvents : function() {
326        // Retrieve dates
327        var allDay = jQuery('#dw_davcal__allday_edit').prop('checked');
328        var startDate = moment(jQuery('#dw_davcal__eventfrom_edit').val(), 'YYYY-MM-DD');
329        var endDate = moment(jQuery('#dw_davcal__eventto_edit').val(), 'YYYY-MM-DD');
330
331        // Do the checking
332        if(!allDay)
333        {
334            var startTime = moment.duration(jQuery('#dw_davcal__eventfromtime_edit').val());
335            var endTime = moment.duration(jQuery('#dw_davcal__eventtotime_edit').val());
336            startDate.add(startTime);
337            endDate.add(endTime);
338        }
339        if(!startDate.isValid())
340        {
341            dw_davcal__modals.msg = LANG.plugins.davcal['start_date_invalid'];
342            dw_davcal__modals.showDialog(false);
343            return false;
344        }
345        if(!endDate.isValid())
346        {
347            dw_davcal__modals.msg = LANG.plugins.davcal['end_date_invalid'];
348            dw_davcal__modals.showDialog(false);
349            return false;
350        }
351        if(endDate.isBefore(startDate))
352        {
353            dw_davcal__modals.msg = LANG.plugins.davcal['end_date_before_start_date'];
354            dw_davcal__modals.showDialog(false);
355            return false;
356        }
357        if(!allDay && endDate.isSame(startDate))
358        {
359            dw_davcal__modals.msg = LANG.plugins.davcal['end_date_is_same_as_start_date'];
360            dw_davcal__modals.showDialog(false);
361            return false;
362        }
363        return true;
364    },
365
366    /**
367     * Show the edit event dialog, which is also used to create new events
368     * @param {Object} event The event to create, that is the date or the calEvent
369     * @param {Object} edit  Whether we edit (true) or create a new event (false)
370     */
371    showEditEventDialog : function(event, edit) {
372        if(dw_davcal__modals.$editEventDialog)
373            return;
374
375        var readonly = dw_davcal__modals.settings['readonly'];
376        var title = '';
377        var dialogButtons = {};
378        var calEvent = [];
379        var recurringWarning = '';
380        // Buttons are dependent on edit or create
381        // Several possibilities:
382        //
383        // 1) Somebody tries to edit, it is not recurring and not readonly -> show
384        // 2) Somebody tries to edit, it is recurring and not readonly -> message
385        // 3) Somebody tries to edit, it is readonly -> message
386        // 4) Somebody tries to create and it is readonly -> message
387        // 5) Somebody tries to create -> show
388        if(edit && (event.recurring != true) && (readonly === false))
389        {
390            calEvent = event;
391            title = LANG.plugins.davcal['edit_event'];
392            dialogButtons[LANG.plugins.davcal['edit']] = function() {
393                if(!dw_davcal__modals.checkEvents())
394                  return;
395                var postArray = { };
396                var attachArr = new Array();
397                var pageid = dw_davcal__modals.page;
398                if(dw_davcal__modals.settings['multi'])
399                {
400                    pageid = jQuery("#dw_davcal__editevent_calendar option:selected").val();
401                }
402                jQuery('.dw_davcal__editevent_attachment_link').each(function() {
403                    var attachment = jQuery(this).attr('href');
404                    if(attachment != undefined)
405                    {
406                        attachArr.push(attachment);
407                    }
408                });
409                postArray['attachments'] = attachArr;
410                jQuery("input.dw_davcal__editevent, textarea.dw_davcal__editevent").each(function() {
411                  if(jQuery(this).attr('type') == 'checkbox')
412                  {
413                      postArray[jQuery(this).prop('name')] = jQuery(this).prop('checked') ? 1 : 0;
414                  }
415                  else
416                  {
417                      postArray[jQuery(this).prop('name')] = jQuery(this).val();
418                  }
419                });
420                jQuery('#dw_davcal__ajaxedit').html('<img src="'+DOKU_BASE+'lib/images/throbber.gif" alt="" width="16" height="16" />');
421                jQuery.post(
422                    DOKU_BASE + 'lib/exe/ajax.php',
423                    {
424                        call: 'plugin_davcal',
425                        id: pageid,
426                        page: dw_davcal__modals.page,
427                        action: 'editEvent',
428                        params: postArray,
429                        sectok: JSINFO.plugin.davcal['sectok']
430                    },
431                    function(data)
432                    {
433                        var result = data['result'];
434                        var html = data['html'];
435                        jQuery('#dw_davcal__ajaxedit').html(html);
436                        if(result === true)
437                        {
438                            jQuery('#fullCalendar').fullCalendar('refetchEvents');
439                            dw_davcal__modals.hideEditEventDialog();
440                        }
441                    }
442                );
443            };
444            dialogButtons[LANG.plugins.davcal['delete']] = function() {
445                dw_davcal__modals.action = 'deleteEvent';
446                dw_davcal__modals.msg = LANG.plugins.davcal['really_delete_this_event'];
447                dw_davcal__modals.completeCb = function(data) {
448                    var result = data['result'];
449                    var html = data['html'];
450                    jQuery('#dw_davcal__ajaxedit').html(html);
451                    if(result === true)
452                    {
453                        jQuery('#fullCalendar').fullCalendar('refetchEvents');
454                        dw_davcal__modals.hideEditEventDialog();
455                    }
456                };
457                dw_davcal__modals.showDialog(true);
458            };
459        }
460        else if(edit && (event.recurring == true) && (readonly === false))
461        {
462            calEvent = event;
463            title = LANG.plugins.davcal['edit_event'];
464            recurringWarning = LANG.plugins.davcal['recurring_cant_edit'];
465        }
466        else if(edit && (readonly === true))
467        {
468            calEvent = event;
469            title = LANG.plugins.davcal['edit_event'];
470            recurringWarning = LANG.plugins.davcal['no_permission'];
471        }
472        else if(readonly === true)
473        {
474            calEvent.start = event;
475            calEvent.end = moment(event);
476            calEvent.start.hour(12);
477            calEvent.start.minute(0);
478            calEvent.end.hour(13);
479            calEvent.end.minute(0);
480            calEvent.allDay = false;
481            calEvent.recurring = false;
482            calEvent.title = '';
483            calEvent.description = '';
484            calEvent.id = '0';
485            calEvent.page = dw_davcal__modals.page;
486            title = LANG.plugins.davcal['create_new_event'];
487            recurringWarning = LANG.plugins.davcal['no_permission'];
488        }
489        else
490        {
491            calEvent.start = event;
492            calEvent.end = moment(event);
493            calEvent.start.hour(12);
494            calEvent.start.minute(0);
495            calEvent.end.hour(13);
496            calEvent.end.minute(0);
497            calEvent.allDay = false;
498            calEvent.recurring = false;
499            calEvent.title = '';
500            calEvent.description = '';
501            calEvent.id = '0';
502            calEvent.page = dw_davcal__modals.settings['calids'][0]['page'];
503            title = LANG.plugins.davcal['create_new_event'];
504            dialogButtons[LANG.plugins.davcal['create']] = function() {
505                if(!dw_davcal__modals.checkEvents())
506                  return;
507
508                var postArray = { };
509                var pageid = dw_davcal__modals.page;
510                var attachArr = new Array();
511                if(dw_davcal__modals.settings['multi'])
512                {
513                    pageid = jQuery("#dw_davcal__editevent_calendar option:selected").val();
514                }
515                jQuery("input.dw_davcal__editevent, textarea.dw_davcal__editevent").each(function() {
516                  if(jQuery(this).attr('type') == 'checkbox')
517                  {
518                      postArray[jQuery(this).prop('name')] = jQuery(this).prop('checked') ? 1 : 0;
519                  }
520                  else
521                  {
522                      postArray[jQuery(this).prop('name')] = jQuery(this).val();
523                  }
524                });
525                jQuery('.dw_davcal__editevent_attachment_link').each(function() {
526                    var attachment = jQuery(this).attr('href');
527                    if(attachment != undefined)
528                    {
529                        attachArr.push(attachment);
530                    }
531                });
532                postArray['attachments'] = attachArr;
533                jQuery('#dw_davcal__ajaxedit').html('<img src="'+DOKU_BASE+'lib/images/throbber.gif" alt="" width="16" height="16" />');
534                jQuery.post(
535                    DOKU_BASE + 'lib/exe/ajax.php',
536                    {
537                        call: 'plugin_davcal',
538                        id: pageid,
539                        page: dw_davcal__modals.page,
540                        action: 'newEvent',
541                        params: postArray,
542                        sectok: JSINFO.plugin.davcal['sectok']
543                    },
544                    function(data)
545                    {
546                        var result = data['result'];
547                        var html = data['html'];
548                        jQuery('#dw_davcal__ajaxedit').html(html);
549                        if(result === true)
550                        {
551                            jQuery('#fullCalendar').fullCalendar('refetchEvents');
552                            dw_davcal__modals.hideEditEventDialog();
553                        }
554                    }
555                );
556            };
557        }
558        dialogButtons[LANG.plugins.davcal['cancel']] = function() {
559            dw_davcal__modals.hideEditEventDialog();
560        };
561        dw_davcal__modals.uid = calEvent.id;
562        dw_davcal__modals.$editEventDialog = jQuery(document.createElement('div'))
563       .dialog({
564           autoOpen: false,
565           draggable: true,
566           title: title,
567           resizable: true,
568           buttons: dialogButtons,
569       })
570       .html(
571            '<div><table>' +
572            '<tr><td>' + LANG.plugins.davcal['calendar'] + '</td><td><select id="dw_davcal__editevent_calendar"></select></td></tr>' +
573            '<tr><td>' + LANG.plugins.davcal['title'] + '</td><td><input type="text" id="dw_davcal__eventname_edit" name="eventname" class="dw_davcal__editevent"></td></tr>' +
574            '<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>' +
575            '<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>' +
576            '<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>' +
577            '<tr><td colspan="2"><input type="checkbox" name="allday" id="dw_davcal__allday_edit" class="dw_davcal__editevent">' + LANG.plugins.davcal['allday'] + '</td></tr>' +
578            '<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>' +
579            '</table>' +
580            recurringWarning +
581            '<input type="hidden" name="uid" id="dw_davcal__uid_edit" class="dw_davcal__editevent">' +
582            '<input type="hidden" name="detectedtz" id="dw_davcal__tz_edit" class="dw_davcal__editevent">' +
583            '<input type="hidden" name="currenttz" id="dw_davcal__currenttz_edit" class="dw_davcal__editevent">' +
584            '</div>' +
585            '<div id="dw_davcal__ajaxedit"></div>'
586            )
587       .parent()
588       .attr('id','dw_davcal__edit')
589       .show()
590       .appendTo('.dokuwiki:first');
591
592       jQuery('#dw_davcal__edit').position({
593           my: "center",
594           at: "center",
595           of: window
596       });
597
598       // Populate calendar dropdown
599       var $dropdown = jQuery("#dw_davcal__editevent_calendar");
600       for(var i=0; i<dw_davcal__modals.settings['calids'].length; i++)
601       {
602           var sel = '';
603           // When creating an event, do not show read-only calendars
604           if(!edit && (dw_davcal__modals.settings['calids'][i]['write'] === false))
605             continue;
606           if(calEvent.page == dw_davcal__modals.settings['calids'][i]['page'])
607             sel = ' selected="selected"';
608           $dropdown.append('<option value="' + dw_davcal__modals.settings['calids'][i]['page'] + '"' + sel + '>' + dw_davcal__modals.settings['calids'][i]['name'] + '</option>');
609       }
610       if(edit || (dw_davcal__modals.settings['calids'].length < 1))
611       {
612           $dropdown.prop('disabled', true);
613       }
614
615       // Set up existing/predefined values
616       jQuery('#dw_davcal__tz_edit').val(dw_davcal__modals.detectedTz);
617       jQuery('#dw_davcal__currenttz_edit').val(dw_davcal__modals.currentTz);
618       jQuery('#dw_davcal__uid_edit').val(calEvent.id);
619       jQuery('#dw_davcal__eventname_edit').val(calEvent.title);
620       jQuery('#dw_davcal__eventfrom_edit').val(calEvent.start.format('YYYY-MM-DD'));
621       jQuery('#dw_davcal__eventfromtime_edit').val(calEvent.start.format('HH:mm'));
622       jQuery('#dw_davcal__eventdescription_edit').val(calEvent.description);
623       if(calEvent.attachments && (calEvent.attachments !== null))
624       {
625           for(var i=0; i<calEvent.attachments.length; i++)
626           {
627               var url = calEvent.attachments[i];
628               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>';
629               jQuery('#dw_davcal__editevent_attachments > tbody:last').append(row);
630
631           }
632       }
633       dw_davcal__modals.attachAttachmentDeleteHandlers();
634       jQuery('#dw_davcal__editevent_attach').on("click", function(e)
635       {
636           e.preventDefault();
637           var url = jQuery('#dw_davcal__editevent_attachment').val();
638           jQuery('#dw_davcal__editevent_attachment').val('http://');
639           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>';
640           jQuery('#dw_davcal__editevent_attachments > tbody:last').append(row);
641           dw_davcal__modals.attachAttachmentDeleteHandlers();
642           return false;
643       });
644       if(calEvent.allDay && (calEvent.end === null))
645       {
646           jQuery('#dw_davcal__eventto_edit').val(calEvent.start.format('YYYY-MM-DD'));
647           jQuery('#dw_davcal__eventtotime_edit').val(calEvent.start.format('HH:mm'));
648       }
649       else if(calEvent.allDay)
650       {
651           endEvent = moment(calEvent.end);
652           endEvent.subtract(1, 'days');
653           jQuery('#dw_davcal__eventto_edit').val(endEvent.format('YYYY-MM-DD'));
654           jQuery('#dw_davcal__eventotime_edit').val(endEvent.format('HH:mm'));
655       }
656       else
657       {
658           jQuery('#dw_davcal__eventto_edit').val(calEvent.end.format('YYYY-MM-DD'));
659           jQuery('#dw_davcal__eventtotime_edit').val(calEvent.end.format('HH:mm'));
660       }
661       jQuery('#dw_davcal__allday_edit').prop('checked', calEvent.allDay);
662
663        // attach event handlers
664        jQuery('#dw_davcal__edit .ui-dialog-titlebar-close').click(function(){
665          dw_davcal__modals.hideEditEventDialog();
666        });
667        jQuery('#dw_davcal__eventfrom_edit').datetimepicker({format:'YYYY-MM-DD',
668                                                      formatDate:'YYYY-MM-DD',
669                                                      datepicker: true,
670                                                      timepicker: false,
671                                                      });
672        jQuery('#dw_davcal__eventfromtime_edit').datetimepicker({format:'HH:mm',
673                                                      formatTime:'HH:mm',
674                                                      datepicker: false,
675                                                      timepicker: true,
676                                                      step: 15});
677        jQuery('#dw_davcal__eventto_edit').datetimepicker({format:'YYYY-MM-DD',
678                                                      formatDate:'YYYY-MM-DD',
679                                                      datepicker: true,
680                                                      timepicker: false,
681                                                      });
682        jQuery('#dw_davcal__eventtotime_edit').datetimepicker({format:'HH:mm',
683                                                      formatTime:'HH:mm',
684                                                      datepicker: false,
685                                                      timepicker: true,
686                                                      step:15});
687        jQuery('#dw_davcal__allday_edit').change(function() {
688            if(jQuery(this).is(":checked"))
689            {
690                jQuery('#dw_davcal__eventfromtime_edit').prop('readonly', true);
691                jQuery('#dw_davcal__eventtotime_edit').prop('readonly', true);
692            }
693            else
694            {
695                jQuery('#dw_davcal__eventfromtime_edit').prop('readonly', false);
696                jQuery('#dw_davcal__eventtotime_edit').prop('readonly', false);
697            }
698        });
699        jQuery('#dw_davcal__allday_edit').change();
700    },
701
702    /**
703     * Attach handles to delete the attachments to all 'delete' links
704     */
705    attachAttachmentDeleteHandlers: function()
706    {
707       jQuery("#dw_davcal__editevent_attachments .deleteLink").on("click", function(e)
708       {
709            e.preventDefault();
710            var tr = jQuery(this).closest('tr');
711            tr.css("background-color", "#FF3700");
712            tr.fadeOut(400, function()
713            {
714                tr.remove();
715            });
716            return false;
717       });
718    },
719
720    /**
721     * Show an info/confirmation dialog
722     * @param {Object} confirm Whether a confirmation dialog (true) or an info dialog (false) is requested
723     */
724    showDialog : function(confirm)
725    {
726        if(dw_davcal__modals.$confirmDialog)
727            return;
728        var dialogButtons = {};
729        var title = '';
730        if(confirm)
731        {
732            title = LANG.plugins.davcal['confirmation'];
733            var pageid = dw_davcal__modals.page;
734            if(dw_davcal__modals.settings['multi'])
735            {
736                pageid = jQuery("#dw_davcal__editevent_calendar option:selected").val();
737            }
738            dialogButtons[LANG.plugins.davcal['yes']] =  function() {
739                            jQuery.post(
740                                DOKU_BASE + 'lib/exe/ajax.php',
741                                {
742                                    call: 'plugin_davcal',
743                                    id: pageid,
744                                    page: dw_davcal__modals.page,
745                                    action: dw_davcal__modals.action,
746                                    params: {
747                                        uid: dw_davcal__modals.uid
748                                    },
749                                    sectok: JSINFO.plugin.davcal['sectok']
750                                },
751                                function(data)
752                                {
753                                    dw_davcal__modals.completeCb(data);
754                                }
755                            );
756                            dw_davcal__modals.hideDialog();
757                    };
758            dialogButtons[LANG.plugins.davcal['cancel']] = function() {
759                            dw_davcal__modals.hideDialog();
760                    };
761        }
762        else
763        {
764            title = LANG.plugins.davcal['info'];
765            dialogButtons[LANG.plugins.davcal['ok']] = function() {
766                 dw_davcal__modals.hideDialog();
767            };
768        }
769        dw_davcal__modals.$dialog = jQuery(document.createElement('div'))
770            .dialog({
771                autoOpen: false,
772                draggable: true,
773                title: title,
774                resizable: true,
775                buttons: dialogButtons,
776            })
777            .html(
778                '<div>' + dw_davcal__modals.msg + '</div>'
779            )
780            .parent()
781            .attr('id','dw_davcal__confirm')
782            .show()
783            .appendTo('.dokuwiki:first');
784
785            jQuery('#dw_davcal__confirm').position({
786                my: "center",
787                at: "center",
788                of: window
789            });
790                 // attach event handlers
791            jQuery('#dw_davcal__confirm .ui-dialog-titlebar-close').click(function(){
792                dw_davcal__modals.hideDialog();
793            });
794    },
795
796    /**
797     * Hide the edit event dialog
798     */
799    hideEditEventDialog : function() {
800        dw_davcal__modals.$editEventDialog.empty();
801        dw_davcal__modals.$editEventDialog.remove();
802        dw_davcal__modals.$editEventDialog = null;
803    },
804
805    /**
806     * Hide the confirm/info dialog
807     */
808    hideDialog: function() {
809        dw_davcal__modals.$dialog.empty();
810        dw_davcal__modals.$dialog.remove();
811        dw_davcal__modals.$dialog = null;
812    },
813
814    /**
815     * Hide the settings dialog
816     */
817    hideSettingsDialog: function() {
818        dw_davcal__modals.$settingsDialog.empty();
819        dw_davcal__modals.$settingsDialog.remove();
820        dw_davcal__modals.$settingsDialog = null;
821    }
822};
823