xref: /plugin/davcal/script.js (revision a495d34c25233615fa25fba79abef99239c1dd50)
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 datetimepicker-2.4.5/jquery.datetimepicker.js */
6
7jQuery(function() {
8    // Redefine functions for using moment.js with datetimepicker
9
10    Date.parseDate = function( input, format ){
11      return moment(input,format).toDate();
12    };
13    Date.prototype.dateFormat = function( format ){
14      return moment(this).format(format);
15    };
16
17    // Attach to event links
18
19    jQuery('div.fullCalendarSettings a').each(function() {
20        var $link = jQuery(this);
21        var href = $link.attr('href');
22        if (!href) return;
23
24        $link.click(
25            function(e) {
26                dw_davcal__modals.showSettingsDialog();
27                e.preventDefault();
28                return '';
29            }
30        );
31        }
32    );
33
34    var postArray = { };
35    jQuery.post(
36        DOKU_BASE + 'lib/exe/ajax.php',
37        {
38            call: 'plugin_davcal',
39            id: JSINFO.id,
40            action: 'getSettings',
41            params: postArray
42        },
43        function(data)
44        {
45            var result = data['result'];
46            if(result === true)
47            {
48                dw_davcal__modals.settings = data['settings'];
49                var wknum = false;
50                var tz = false;
51                var we = true;
52                if(data['settings']['weeknumbers'] == 1)
53                    wknum = true;
54                if(data['settings']['timezne'] !== '')
55                    tz = data['settings']['timezone'];
56                if(data['settings']['workweek'] == 1)
57                    we = false;
58                // Initialize the davcal popup
59                var res = jQuery('#fullCalendar').fullCalendar({
60                    dayClick: function(date, jsEvent, view) {
61                        dw_davcal__modals.showNewEventDialog(date);
62                    },
63                    eventClick: function(calEvent, jsEvent, view) {
64                        dw_davcal__modals.showEditEventDialog(calEvent);
65                    },
66                    events: {
67                        url: DOKU_BASE + 'lib/exe/ajax.php',
68                        type: 'POST',
69                        data: {
70                            call: 'plugin_davcal',
71                            action: 'getEvents',
72                            id: JSINFO.id
73                        },
74                        error: function() {
75                            //alert('there was an error retrieving calendar data');
76                        }
77                    },
78                    header: {
79                        left: 'title',
80                        center: 'today prev,next',
81                        right: 'month,agendaWeek,agendaDay'
82                    },
83                    lang: JSINFO.plugin.davcal['language'],
84                    weekNumbers: wknum,
85                    timeZone: tz,
86                    weekends: we,
87                });
88            }
89        }
90    );
91});
92
93var dw_davcal__modals = {
94    $newEventDialog : null,
95    $editEventDialog: null,
96    $infoDialog: null,
97    $confirmDialog: null,
98    $settingsDialog: null,
99    msg: null,
100    completeCb: null,
101    action: null,
102    uid: null,
103    settings: null,
104
105    showSettingsDialog : function() {
106        if(dw_davcal__modals.$settingsDialog)
107            return;
108
109        var dialogButtons = {};
110        dialogButtons[LANG.plugins.davcal['save']] = function() {
111            var postArray = { };
112            jQuery("input[class=dw_davcal__settings]").each(function() {
113              if(jQuery(this).attr('type') == 'checkbox')
114              {
115                  postArray[jQuery(this).prop('name')] = jQuery(this).prop('checked') ? 1 : 0;
116              }
117              else
118              {
119                  postArray[jQuery(this).prop('name')] = jQuery(this).val();
120              }
121            });
122            jQuery('#dw_davcal__ajaxsettings').html('<img src="'+DOKU_BASE+'lib/images/throbber.gif" alt="" width="16" height="16" />');
123            jQuery.post(
124                DOKU_BASE + 'lib/exe/ajax.php',
125                {
126                    call: 'plugin_davcal',
127                    id: JSINFO.id,
128                    action: 'saveSettings',
129                    params: postArray
130                },
131                function(data)
132                {
133                    var result = data['result'];
134                    var html = data['html'];
135                    jQuery('#dw_davcal__ajaxsettings').html(html);
136                    if(result === true)
137                    {
138                        location.reload();
139                    }
140                }
141            );
142        };
143        dialogButtons[LANG.plugins.davcal['cancel']] = function () {
144            dw_davcal__modals.hideSettingsDialog();
145        };
146
147        dw_davcal__modals.$settingsDialog = jQuery(document.createElement('div'))
148       .dialog({
149           autoOpen: false,
150           draggable: true,
151           title: LANG.plugins.davcal['settings'],
152           resizable: true,
153           buttons: dialogButtons,
154       })
155       .html(
156            '<div><table>' +
157            //'<tr><td>' + LANG.plugins.davcal['use_lang_tz'] + '</td><td><input type="checkbox" name="use_lang_tz" id+"dw_davcal__settings_use_lang_tz" class="dw_davcal__settings"></td></tr>' +
158            '<tr><td>' + LANG.plugins.davcal['timezone'] + '</td><td>Timezone Dropdown</td></tr>' +
159            '<tr><td>' + LANG.plugins.davcal['weeknumbers'] + '</td><td><input type="checkbox" name="weeknumbers" id="dw_davcal__settings_weeknumbers" class="dw_davcal__settings"></td></tr>' +
160            '<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>' +
161            '</table>' +
162            '</div>' +
163            '<div id="dw_davcal__ajaxsettings"></div>'
164            )
165       .parent()
166       .attr('id','dw_davcal__settings')
167       .show()
168       .appendTo('.dokuwiki:first');
169
170           // attach event handlers
171        jQuery('#dw_davcal__settings .ui-dialog-titlebar-close').click(function(){
172          dw_davcal__modals.hideSettingsDialog();
173        });
174        if(dw_davcal__modals.settings)
175        {
176            if(dw_davcal__modals.settings['weeknumbers'] == 1)
177                jQuery('#dw_davcal__settings_weeknumbers').prop('checked', true);
178            else
179                jQuery('#dw_davcal__settings_weeknumbers').prop('checked', false);
180
181            if(dw_davcal__modals.settings['workweek'] == 1)
182                jQuery('#dw_davcal__settings_workweek').prop('checked', true);
183            else
184                jQuery('#dw_davcal__settings_workweek').prop('checked', false);
185        }
186    },
187
188    showEditEventDialog : function(calEvent) {
189        if(dw_davcal__modals.$editEventDialog)
190            return;
191
192        var dialogButtons = {};
193        dialogButtons[LANG.plugins.davcal['edit']] = function() {
194            var postArray = { };
195            jQuery("input[class=dw_davcal__editevent]").each(function() {
196              if(jQuery(this).attr('type') == 'checkbox')
197              {
198                  postArray[jQuery(this).prop('name')] = jQuery(this).prop('checked') ? 1 : 0;
199              }
200              else
201              {
202                  postArray[jQuery(this).prop('name')] = jQuery(this).val();
203              }
204            });
205            jQuery('#dw_davcal__ajaxedit').html('<img src="'+DOKU_BASE+'lib/images/throbber.gif" alt="" width="16" height="16" />');
206            jQuery.post(
207                DOKU_BASE + 'lib/exe/ajax.php',
208                {
209                    call: 'plugin_davcal',
210                    id: JSINFO.id,
211                    action: 'editEvent',
212                    params: postArray
213                },
214                function(data)
215                {
216                    var result = data['result'];
217                    var html = data['html'];
218                    jQuery('#dw_davcal__ajaxedit').html(html);
219                    if(result === true)
220                    {
221                        jQuery('#fullCalendar').fullCalendar('refetchEvents');
222                        dw_davcal__modals.hideEditEventDialog();
223                    }
224                }
225            );
226        };
227        dialogButtons[LANG.plugins.davcal['cancel']] = function() {
228            dw_davcal__modals.hideEditEventDialog();
229        };
230        dialogButtons[LANG.plugins.davcal['delete']] = function() {
231            dw_davcal__modals.action = 'deleteEvent';
232            dw_davcal__modals.msg = LANG.plugins.davcal['really_delete_this_event'];
233            dw_davcal__modals.completeCb = function(data) {
234                if(data.result == false)
235                {
236                    dw_davcal__modals.msg = data.errmsg;
237                    dw_davcal__modals.showInfoDialog();
238                }
239                else
240                {
241                    jQuery('#fullCalendar').fullCalendar('refetchEvents');
242                    dw_davcal__modals.hideEditEventDialog();
243                }
244            };
245            dw_davcal__modals.showConfirmDialog();
246        };
247        dw_davcal__modals.uid = calEvent.id;
248        dw_davcal__modals.$editEventDialog = jQuery(document.createElement('div'))
249       .dialog({
250           autoOpen: false,
251           draggable: true,
252           title: LANG.plugins.davcal['edit_event'],
253           resizable: true,
254           buttons: dialogButtons,
255       })
256       .html(
257            '<div><table><tr><td>' + LANG.plugins.davcal['title'] + '</td><td><input type="text" id="dw_davcal__eventname_edit" name="eventname" class="dw_davcal__editevent"></td></tr>' +
258            '<tr><td>' + LANG.plugins.davcal['from'] + '</td><td><input type="text" name="eventfrom" id="dw_davcal__eventfrom_edit" class="dw_davcal__editevent"></td></tr>' +
259            '<tr><td>' + LANG.plugins.davcal['to'] + '</td><td><input type="text" name="eventto" id="dw_davcal__eventto_edit" class="dw_davcal__editevent"></td></tr>' +
260            '<tr><td colspan="2"><input type="checkbox" name="allday" id="dw_davcal__allday_edit" class="dw_davcal__editevent">' + LANG.plugins.davcal['allday'] + '</td></tr>' +
261            '</table>' +
262            '<input type="hidden" name="uid" id="dw_davcal__uid_edit" class="dw_davcal__editevent">' +
263            '</div>' +
264            '<div id="dw_davcal__ajaxedit"></div>'
265            )
266       .parent()
267       .attr('id','dw_davcal__edit')
268       .show()
269       .appendTo('.dokuwiki:first');
270
271       jQuery('#dw_davcal__uid_edit').val(calEvent.id);
272       jQuery('#dw_davcal__eventname_edit').val(calEvent.title);
273       jQuery('#dw_davcal__eventfrom_edit').val(calEvent.start.format('YYYY-MM-DD HH:mm'));
274       jQuery('#dw_davcal__eventto_edit').val(calEvent.end.format('YYYY-MM-DD HH:mm'));
275       jQuery('#dw_davcal__allday_edit').prop('checked', calEvent.allDay);
276
277           // attach event handlers
278        jQuery('#dw_davcal__edit .ui-dialog-titlebar-close').click(function(){
279          dw_davcal__modals.hideEditEventDialog();
280        });
281        jQuery('#dw_davcal__eventfrom_edit').datetimepicker({format:'YYYY-MM-DD HH:mm',
282                                                      formatTime:'HH:mm',
283                                                      formatDate:'YYYY-MM-DD',
284                                                      step: 15});
285        jQuery('#dw_davcal__eventto_edit').datetimepicker({format:'YYYY-MM-DD HH:mm',
286                                                      formatTime:'HH:mm',
287                                                      formatDate:'YYYY-MM-DD',
288                                                      step: 15});
289        jQuery('#dw_davcal__allday_edit').change(function() {
290            if(jQuery(this).is(":checked"))
291            {
292                jQuery('#dw_davcal__eventfrom_edit').datetimepicker({timepicker: false});
293                jQuery('#dw_davcal__eventto_edit').datetimepicker({timepicker: false});
294            }
295            else
296            {
297                jQuery('#dw_davcal__eventfrom_edit').datetimepicker({timepicker: true});
298                jQuery('#dw_davcal__eventto_edit').datetimepicker({timepicker: true});
299            }
300        });
301    },
302
303    showNewEventDialog : function(date) {
304        if(dw_davcal__modals.$newEventDialog)
305            return;
306        var dialogButtons = {};
307        dialogButtons[LANG.plugins.davcal['create']] = function() {
308            var postArray = { };
309            jQuery("input[class=dw_davcal__newevent]").each(function() {
310              if(jQuery(this).attr('type') == 'checkbox')
311              {
312                  postArray[jQuery(this).prop('name')] = jQuery(this).prop('checked') ? 1 : 0;
313              }
314              else
315              {
316                  postArray[jQuery(this).prop('name')] = jQuery(this).val();
317              }
318            });
319            jQuery('#dw_davcal__ajaxnew').html('<img src="'+DOKU_BASE+'lib/images/throbber.gif" alt="" width="16" height="16" />');
320            jQuery.post(
321                DOKU_BASE + 'lib/exe/ajax.php',
322                {
323                    call: 'plugin_davcal',
324                    id: JSINFO.id,
325                    action: 'newEvent',
326                    params: postArray
327                },
328                function(data)
329                {
330                    var result = data['result'];
331                    var html = data['html'];
332                    jQuery('#dw_davcal__ajaxnew').html(html);
333                    if(result === true)
334                    {
335                        jQuery('#fullCalendar').fullCalendar('refetchEvents');
336                        dw_davcal__modals.hideNewEventDialog();
337                    }
338                }
339            );
340        };
341        dialogButtons[LANG.plugins.davcal['cancel']] = function() {
342            dw_davcal__modals.hideNewEventDialog();
343        };
344        dw_davcal__modals.$newEventDialog = jQuery(document.createElement('div'))
345       .dialog({
346           autoOpen: false,
347           draggable: true,
348           title: LANG.plugins.davcal['create_new_event'],
349           resizable: true,
350           buttons: dialogButtons,
351       })
352       .html(
353            '<div><table><tr><td>' + LANG.plugins.davcal['title'] + '</td><td><input type="text" name="eventname" class="dw_davcal__newevent"></td></tr>' +
354            '<tr><td>' + LANG.plugins.davcal['from'] + '</td><td><input type="text" value="' + date.format('YYYY-MM-DD') + ' 12:00" name="eventfrom" id="dw_davcal__eventfrom" class="dw_davcal__newevent"></td></tr>' +
355            '<tr><td>' + LANG.plugins.davcal['to'] + '</td><td><input type="text" name="eventto"  value="' + date.format('YYYY-MM-DD') + ' 12:00" id="dw_davcal__eventto" class="dw_davcal__newevent"></td></tr>' +
356            '<tr><td colspan="2"><input type="checkbox" name="allday" id="dw_davcal__allday" class="dw_davcal__newevent">' + LANG.plugins.davcal['allday'] + '</td></tr>' +
357            '</table>' +
358            '</div>' +
359            '<div id="dw_davcal__ajaxnew"></div>'
360            )
361       .parent()
362       .attr('id','dw_davcal__createnew')
363       .show()
364       .appendTo('.dokuwiki:first');
365
366           // attach event handlers
367        jQuery('#dw_davcal__createnew .ui-dialog-titlebar-close').click(function(){
368          dw_davcal__modals.hideNewEventDialog();
369        });
370        jQuery('#dw_davcal__eventfrom').datetimepicker({format:'YYYY-MM-DD HH:mm',
371                                                      formatTime:'HH:mm',
372                                                      formatDate:'YYYY-MM-DD',
373                                                      step: 15});
374        jQuery('#dw_davcal__eventto').datetimepicker({format:'YYYY-MM-DD HH:mm',
375                                                      formatTime:'HH:mm',
376                                                      formatDate:'YYYY-MM-DD',
377                                                      step: 15});
378        jQuery('#dw_davcal__allday').change(function() {
379            if(jQuery(this).is(":checked"))
380            {
381                jQuery('#dw_davcal__eventfrom').datetimepicker({timepicker: false});
382                jQuery('#dw_davcal__eventto').datetimepicker({timepicker: false});
383            }
384            else
385            {
386                jQuery('#dw_davcal__eventfrom').datetimepicker({timepicker: true});
387                jQuery('#dw_davcal__eventto').datetimepicker({timepicker: true});
388            }
389        });
390    },
391
392    showConfirmDialog : function()
393    {
394        if(dw_davcal__modals.$confirmDialog)
395            return;
396        var dialogButtons = {};
397        dialogButtons[LANG.plugins.davcal['yes']] =  function() {
398                        jQuery.post(
399                            DOKU_BASE + 'lib/exe/ajax.php',
400                            {
401                                call: 'plugin_davcal',
402                                id: JSINFO.id,
403                                action: dw_davcal__modals.action,
404                                params: {
405                                    uid: dw_davcal__modals.uid
406                                }
407                            },
408                            function(data)
409                            {
410                                dw_davcal__modals.completeCb(data);
411                            }
412                        );
413                        dw_davcal__modals.hideConfirmDialog();
414                };
415        dialogButtons[LANG.plugins.tagrevisions['cancel']] = function() {
416                        dw_davcal__modals.hideConfirmDialog();
417                };
418        dw_davcal__modals.$confirmDialog = jQuery(document.createElement('div'))
419            .dialog({
420                autoOpen: false,
421                draggable: true,
422                title: LANG.plugins.tagrevisions['confirmation'],
423                resizable: true,
424                buttons: dialogButtons,
425            })
426            .html(
427                '<div>' + dw_davcal__modals.msg + '</div>'
428            )
429            .parent()
430            .attr('id','dw_davcal__confirm')
431            .show()
432            .appendTo('.dokuwiki:first');
433
434                 // attach event handlers
435            jQuery('#dw_davcal__confirm .ui-dialog-titlebar-close').click(function(){
436                dw_davcal__modals.hideConfirmDialog();
437            });
438    },
439
440    showInfoDialog : function() {
441       if(dw_davcal__modal.$infoDialog)
442            return;
443       var dialogButtons = {};
444       dialogButtons[LANG.plugins.davcal['ok']] = function() {
445                 dw_davcal__modals.hideInfoDialog();
446           };
447       dw_davcal__modals.$infoDialog = jQuery(document.createElement('div'))
448       .dialog({
449           autoOpen: false,
450           draggable: true,
451           title: LANG.plugins.davcal['info'],
452           resizable: true,
453           buttons: dialogButtons,
454       })
455       .html(
456            '<div>' + dw_davcal__modals.msg + '</div>'
457            )
458       .parent()
459       .attr('id','dw_davcal__info')
460       .show()
461       .appendTo('.dokuwiki:first');
462
463           // attach event handlers
464        jQuery('#dw_davcal__info .ui-dialog-titlebar-close').click(function(){
465          dw_davcal__modals.hideInfoDialog();
466        });
467    },
468
469    hideNewEventDialog : function() {
470        dw_davcal__modals.$newEventDialog.empty();
471        dw_davcal__modals.$newEventDialog.remove();
472        dw_davcal__modals.$newEventDialog = null;
473    },
474
475    hideEditEventDialog : function() {
476        dw_davcal__modals.$editEventDialog.empty();
477        dw_davcal__modals.$editEventDialog.remove();
478        dw_davcal__modals.$editEventDialog = null;
479    },
480
481    hideInfoDialog : function() {
482        dw_davcal__modals.$infoDialog.empty();
483        dw_davcal__modals.$infoDialog.remove();
484        dw_davcal__modals.$infoDialog = null;
485    },
486
487    hideConfirmDialog: function() {
488        dw_davcal__modals.$confirmDialog.empty();
489        dw_davcal__modals.$confirmDialog.remove();
490        dw_davcal__modals.$confirmDialog = null;
491    },
492
493    hideSettingsDialog: function() {
494        dw_davcal__modals.$settingsDialog.empty();
495        dw_davcal__modals.$settingsDialog.remove();
496        dw_davcal__modals.$settingsDialog = null;
497    }
498};
499