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