Mercurial > hg > rc1
annotate plugins/calendar/calendar_ui.js @ 17:3bd5fe8166b8
switch to using vurl as well for better display, and it gets saved
author | Charlie Root |
---|---|
date | Sat, 13 Jan 2018 17:06:41 -0500 |
parents | ff1f27bd05ad |
children | e8f197ce1af3 |
rev | line source |
---|---|
3 | 1 /** |
2 * Client UI Javascript for the Calendar plugin | |
3 * | |
4 * @author Lazlo Westerhof <hello@lazlo.me> | |
5 * @author Thomas Bruederli <bruederli@kolabsys.com> | |
6 * | |
7 * @licstart The following is the entire license notice for the | |
8 * JavaScript code in this file. | |
9 * | |
10 * Copyright (C) 2010, Lazlo Westerhof <hello@lazlo.me> | |
11 * Copyright (C) 2014-2015, Kolab Systems AG <contact@kolabsys.com> | |
12 * | |
13 * This program is free software: you can redistribute it and/or modify | |
14 * it under the terms of the GNU Affero General Public License as | |
15 * published by the Free Software Foundation, either version 3 of the | |
16 * License, or (at your option) any later version. | |
17 * | |
18 * This program is distributed in the hope that it will be useful, | |
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 * GNU Affero General Public License for more details. | |
22 * | |
23 * You should have received a copy of the GNU Affero General Public License | |
24 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
25 * | |
26 * @licend The above is the entire license notice | |
27 * for the JavaScript code in this file. | |
28 */ | |
29 | |
30 // Roundcube calendar UI client class | |
31 function rcube_calendar_ui(settings) | |
32 { | |
33 // extend base class | |
34 rcube_calendar.call(this, settings); | |
35 | |
36 /*** member vars ***/ | |
37 this.is_loading = false; | |
38 this.selected_event = null; | |
39 this.selected_calendar = null; | |
40 this.search_request = null; | |
41 this.saving_lock = null; | |
42 this.calendars = {}; | |
43 this.quickview_sources = []; | |
44 | |
45 | |
46 /*** private vars ***/ | |
47 var DAY_MS = 86400000; | |
48 var HOUR_MS = 3600000; | |
49 var me = this; | |
50 var day_clicked = day_clicked_ts = 0; | |
51 var ignore_click = false; | |
52 var event_defaults = { free_busy:'busy', alarms:'' }; | |
53 var event_attendees = []; | |
54 var calendars_list; | |
55 var calenders_search_list; | |
56 var calenders_search_container; | |
57 var search_calendars = {}; | |
58 var attendees_list; | |
59 var resources_list; | |
60 var resources_treelist; | |
61 var resources_data = {}; | |
62 var resources_index = []; | |
63 var resource_owners = {}; | |
64 var resources_events_source = { url:null, editable:false }; | |
65 var freebusy_ui = { workinhoursonly:false, needsupdate:false }; | |
66 var freebusy_data = {}; | |
67 var current_view = null; | |
68 var count_sources = []; | |
69 var exec_deferred = bw.ie6 ? 5 : 1; | |
70 var sensitivitylabels = { 'public':rcmail.gettext('public','calendar'), 'private':rcmail.gettext('private','calendar'), 'confidential':rcmail.gettext('confidential','calendar') }; | |
71 var ui_loading = rcmail.set_busy(true, 'loading'); | |
72 | |
73 // general datepicker settings | |
74 var datepicker_settings = { | |
75 // translate from fullcalendar format to datepicker format | |
76 dateFormat: settings['date_format'].replace(/M/g, 'm').replace(/mmmmm/, 'MM').replace(/mmm/, 'M').replace(/dddd/, 'DD').replace(/ddd/, 'D').replace(/yy/g, 'y'), | |
77 firstDay : settings['first_day'], | |
78 dayNamesMin: settings['days_short'], | |
79 monthNames: settings['months'], | |
80 monthNamesShort: settings['months'], | |
81 changeMonth: false, | |
82 showOtherMonths: true, | |
83 selectOtherMonths: true | |
84 }; | |
85 | |
86 // global fullcalendar settings | |
87 var fullcalendar_defaults = { | |
88 aspectRatio: 1, | |
89 ignoreTimezone: true, // will treat the given date strings as in local (browser's) timezone | |
90 monthNames : settings.months, | |
91 monthNamesShort : settings.months_short, | |
92 dayNames : settings.days, | |
93 dayNamesShort : settings.days_short, | |
94 firstDay : settings.first_day, | |
95 firstHour : settings.first_hour, | |
96 slotMinutes : 60/settings.timeslots, | |
97 timeFormat: { | |
98 '': settings.time_format, | |
99 agenda: settings.time_format + '{ - ' + settings.time_format + '}', | |
100 list: settings.time_format + '{ - ' + settings.time_format + '}', | |
101 table: settings.time_format + '{ - ' + settings.time_format + '}' | |
102 }, | |
103 axisFormat : settings.time_format, | |
104 columnFormat: { | |
105 month: 'ddd', // Mon | |
106 week: 'ddd ' + settings.date_short, // Mon 9/7 | |
107 day: 'dddd ' + settings.date_short, // Monday 9/7 | |
108 table: settings.date_agenda | |
109 }, | |
110 titleFormat: { | |
111 month: 'MMMM yyyy', | |
112 week: settings.dates_long, | |
113 day: 'dddd ' + settings['date_long'], | |
114 table: settings.dates_long | |
115 }, | |
116 listPage: 7, // advance one week in agenda view | |
117 listRange: settings.agenda_range, | |
118 listSections: settings.agenda_sections, | |
119 tableCols: ['handle', 'date', 'time', 'title', 'location'], | |
120 defaultView: rcmail.env.view || settings.default_view, | |
121 allDayText: rcmail.gettext('all-day', 'calendar'), | |
122 buttonText: { | |
123 prev: ' ◄ ', | |
124 next: ' ► ', | |
125 today: settings['today'], | |
126 day: rcmail.gettext('day', 'calendar'), | |
127 week: rcmail.gettext('week', 'calendar'), | |
128 month: rcmail.gettext('month', 'calendar'), | |
129 table: rcmail.gettext('agenda', 'calendar') | |
130 }, | |
131 listTexts: { | |
132 until: rcmail.gettext('until', 'calendar'), | |
133 past: rcmail.gettext('pastevents', 'calendar'), | |
134 today: rcmail.gettext('today', 'calendar'), | |
135 tomorrow: rcmail.gettext('tomorrow', 'calendar'), | |
136 thisWeek: rcmail.gettext('thisweek', 'calendar'), | |
137 nextWeek: rcmail.gettext('nextweek', 'calendar'), | |
138 thisMonth: rcmail.gettext('thismonth', 'calendar'), | |
139 nextMonth: rcmail.gettext('nextmonth', 'calendar'), | |
140 future: rcmail.gettext('futureevents', 'calendar'), | |
141 week: rcmail.gettext('weekofyear', 'calendar') | |
142 }, | |
143 currentTimeIndicator: settings.time_indicator, | |
144 // event rendering | |
145 eventRender: function(event, element, view) { | |
146 if (view.name != 'list' && view.name != 'table') { | |
147 var prefix = event.sensitivity && event.sensitivity != 'public' ? String(sensitivitylabels[event.sensitivity]).toUpperCase()+': ' : ''; | |
148 element.attr('title', prefix + event.title); | |
149 } | |
150 if (view.name != 'month') { | |
151 if (event.location) { | |
152 element.find('div.fc-event-title').after('<div class="fc-event-location">@ ' + Q(event.location) + '</div>'); | |
153 } | |
154 if (event.sensitivity && event.sensitivity != 'public') | |
155 element.find('div.fc-event-time').append('<i class="fc-icon-sensitive"></i>'); | |
156 if (event.recurrence) | |
157 element.find('div.fc-event-time').append('<i class="fc-icon-recurring"></i>'); | |
158 if (event.alarms || (event.valarms && event.valarms.length)) | |
159 element.find('div.fc-event-time').append('<i class="fc-icon-alarms"></i>'); | |
160 } | |
161 if (event.status) { | |
162 element.addClass('cal-event-status-' + String(event.status).toLowerCase()); | |
163 } | |
164 | |
165 element.attr('aria-label', event.title + ', ' + me.event_date_text(event, true)); | |
166 }, | |
167 // render element indicating more (invisible) events | |
168 overflowRender: function(data, element) { | |
169 element.html(rcmail.gettext('andnmore', 'calendar').replace('$nr', data.count)) | |
170 .click(function(e){ me.fisheye_view(data.date); }); | |
171 }, | |
172 // callback when a specific event is clicked | |
173 eventClick: function(event, ev, view) { | |
174 if (!event.temp && String(event.className).indexOf('fc-type-freebusy') < 0) | |
175 event_show_dialog(event, ev); | |
176 } | |
177 }; | |
178 | |
179 /*** imports ***/ | |
180 var Q = this.quote_html; | |
181 var text2html = this.text2html; | |
182 var event_date_text = this.event_date_text; | |
183 var parse_datetime = this.parse_datetime; | |
184 var date2unixtime = this.date2unixtime; | |
185 var fromunixtime = this.fromunixtime; | |
186 var parseISO8601 = this.parseISO8601; | |
187 var date2servertime = this.date2ISO8601; | |
188 var render_message_links = this.render_message_links; | |
189 | |
190 | |
191 /*** private methods ***/ | |
192 | |
193 // same as str.split(delimiter) but it ignores delimiters within quoted strings | |
194 var explode_quoted_string = function(str, delimiter) | |
195 { | |
196 var result = [], | |
197 strlen = str.length, | |
198 q, p, i, chr, last; | |
199 | |
200 for (q = p = i = 0; i < strlen; i++) { | |
201 chr = str.charAt(i); | |
202 if (chr == '"' && last != '\\') { | |
203 q = !q; | |
204 } | |
205 else if (!q && chr == delimiter) { | |
206 result.push(str.substring(p, i)); | |
207 p = i + 1; | |
208 } | |
209 last = chr; | |
210 } | |
211 | |
212 result.push(str.substr(p)); | |
213 return result; | |
214 }; | |
215 | |
216 // Change the first charcter to uppercase | |
217 var ucfirst = function(str) | |
218 { | |
219 return str.charAt(0).toUpperCase() + str.substr(1); | |
220 }; | |
221 | |
222 // clone the given date object and optionally adjust time | |
223 var clone_date = function(date, adjust) | |
224 { | |
225 var d = new Date(date.getTime()); | |
226 | |
227 // set time to 00:00 | |
228 if (adjust == 1) { | |
229 d.setHours(0); | |
230 d.setMinutes(0); | |
231 } | |
232 // set time to 23:59 | |
233 else if (adjust == 2) { | |
234 d.setHours(23); | |
235 d.setMinutes(59); | |
236 } | |
237 | |
238 return d; | |
239 }; | |
240 | |
241 // fix date if jumped over a DST change | |
242 var fix_date = function(date) | |
243 { | |
244 if (date.getHours() == 23) | |
245 date.setTime(date.getTime() + HOUR_MS); | |
246 else if (date.getHours() > 0) | |
247 date.setHours(0); | |
248 }; | |
249 | |
250 var date2timestring = function(date, dateonly) | |
251 { | |
252 return date2servertime(date).replace(/[^0-9]/g, '').substr(0, (dateonly ? 8 : 14)); | |
253 } | |
254 | |
255 var format_datetime = function(date, mode, voice) | |
256 { | |
257 return me.format_datetime(date, mode, voice); | |
258 } | |
259 | |
260 var render_link = function(url) | |
261 { | |
262 var islink = false, href = url; | |
263 if (url.match(/^[fhtpsmailo]+?:\/\//i)) { | |
264 islink = true; | |
265 } | |
266 else if (url.match(/^[a-z0-9.-:]+(\/|$)/i)) { | |
267 islink = true; | |
268 href = 'http://' + url; | |
269 } | |
270 return islink ? '<a href="' + Q(href) + '" target="_blank">' + Q(url) + '</a>' : Q(url); | |
271 } | |
272 | |
273 // determine whether the given date is on a weekend | |
274 var is_weekend = function(date) | |
275 { | |
276 return date.getDay() == 0 || date.getDay() == 6; | |
277 }; | |
278 | |
279 var is_workinghour = function(date) | |
280 { | |
281 if (settings['work_start'] > settings['work_end']) | |
282 return date.getHours() >= settings['work_start'] || date.getHours() < settings['work_end']; | |
283 else | |
284 return date.getHours() >= settings['work_start'] && date.getHours() < settings['work_end']; | |
285 }; | |
286 | |
287 var load_attachment = function(event, att) | |
288 { | |
289 var query = { _id: att.id, _event: event.recurrence_id || event.id, _cal:event.calendar, _frame: 1 }; | |
290 if (event.rev) | |
291 query._rev = event.rev; | |
292 | |
293 if (event.calendar == "--invitation--itip") | |
294 $.extend(query, {_uid: event._uid, _part: event._part, _mbox: event._mbox}); | |
295 | |
296 // open attachment in frame if it's of a supported mimetype | |
297 if (id && att.mimetype && $.inArray(att.mimetype, settings.mimetypes)>=0) { | |
298 if (rcmail.open_window(rcmail.url('get-attachment', query), true, true)) { | |
299 return; | |
300 } | |
301 } | |
302 | |
303 query._frame = null; | |
304 query._download = 1; | |
305 rcmail.goto_url('get-attachment', query, false); | |
306 }; | |
307 | |
308 // build event attachments list | |
309 var event_show_attachments = function(list, container, event, edit) | |
310 { | |
311 var i, id, len, img, content, li, elem, | |
312 ul = document.createElement('UL'); | |
313 ul.className = 'attachmentslist'; | |
314 | |
315 for (i=0, len=list.length; i<len; i++) { | |
316 elem = list[i]; | |
317 li = document.createElement('LI'); | |
318 li.className = elem.classname; | |
319 | |
320 if (edit) { | |
321 rcmail.env.attachments[elem.id] = elem; | |
322 // delete icon | |
323 content = $('<a href="#delete" />') | |
324 .attr('title', rcmail.gettext('delete')) | |
325 .attr('aria-label', rcmail.gettext('delete') + ' ' + Q(elem.name)) | |
326 .addClass('delete') | |
327 .click({id: elem.id}, function(e) { remove_attachment(this, e.data.id); return false; }); | |
328 | |
329 if (!rcmail.env.deleteicon) | |
330 content.html(rcmail.gettext('delete')); | |
331 else { | |
332 img = document.createElement('IMG'); | |
333 img.src = rcmail.env.deleteicon; | |
334 img.alt = rcmail.gettext('delete'); | |
335 content.append(img); | |
336 } | |
337 | |
338 content.appendTo(li); | |
339 } | |
340 | |
341 // name/link | |
342 content = $('<a href="#load" />') | |
343 .html(Q(elem.name)) | |
344 .addClass('file') | |
345 .click({event: event, att: elem}, function(e) { | |
346 load_attachment(e.data.event, e.data.att); | |
347 return false; | |
348 }) | |
349 .appendTo(li); | |
350 | |
351 ul.appendChild(li); | |
352 } | |
353 | |
354 if (edit && rcmail.gui_objects.attachmentlist) { | |
355 ul.id = rcmail.gui_objects.attachmentlist.id; | |
356 rcmail.gui_objects.attachmentlist = ul; | |
357 } | |
358 | |
359 container.empty().append(ul); | |
360 }; | |
361 | |
362 var remove_attachment = function(elem, id) | |
363 { | |
364 $(elem.parentNode).hide(); | |
365 rcmail.env.deleted_attachments.push(id); | |
366 delete rcmail.env.attachments[id]; | |
367 }; | |
368 | |
369 // event details dialog (show only) | |
370 var event_show_dialog = function(event, ev, temp) | |
371 { | |
372 var $dialog = $("#eventshow"); | |
373 var calendar = event.calendar && me.calendars[event.calendar] ? me.calendars[event.calendar] : { editable:false, rights:'lrs' }; | |
374 | |
375 if (!temp) | |
376 me.selected_event = event; | |
377 | |
378 if ($dialog.is(':ui-dialog')) | |
379 $dialog.dialog('close'); | |
380 | |
381 // remove status-* and sensitivity-* classes | |
382 $dialog.removeClass(function(i, oldclass) { | |
383 var oldies = String(oldclass).split(' '); | |
384 return $.grep(oldies, function(cls) { return cls.indexOf('status-') === 0 || cls.indexOf('sensitivity-') === 0 }).join(' '); | |
385 }); | |
386 | |
387 // convert start/end dates if not done yet by fullcalendar | |
388 if (typeof event.start == 'string') | |
389 event.start = parseISO8601(event.start); | |
390 if (typeof event.end == 'string') | |
391 event.end = parseISO8601(event.end); | |
392 | |
393 // allow other plugins to do actions when event form is opened | |
394 rcmail.triggerEvent('calendar-event-init', {o: event}); | |
395 | |
396 $dialog.find('div.event-section, div.event-line').hide(); | |
397 $('#event-title').html(Q(event.title)).show(); | |
398 | |
399 if (event.location) | |
400 $('#event-location').html('@ ' + text2html(event.location)).show(); | |
401 if (event.description) | |
17
3bd5fe8166b8
switch to using vurl as well for better display, and it gets saved
Charlie Root
parents:
12
diff
changeset
|
402 $('#event-description').show().children('.event-text').html(text2html(event.description, 300, 6)); |
3 | 403 if (event.vurl) |
404 $('#event-url').show().children('.event-text').html(render_link(event.vurl)); | |
405 | |
406 // render from-to in a nice human-readable way | |
407 // -> now shown in dialog title | |
408 // $('#event-date').html(Q(me.event_date_text(event))).show(); | |
409 | |
410 if (event.recurrence && event.recurrence_text) | |
411 $('#event-repeat').show().children('.event-text').html(Q(event.recurrence_text)); | |
412 | |
413 if (event.valarms && event.alarms_text) | |
414 $('#event-alarm').show().children('.event-text').html(Q(event.alarms_text).replace(',', ',<br>')); | |
415 | |
416 if (calendar.name) | |
417 $('#event-calendar').show().children('.event-text').html(Q(calendar.name)).attr('class', 'event-text cal-'+calendar.id).css('color', calendar.textColor || calendar.color || ''); | |
418 if (event.categories) | |
419 $('#event-category').show().children('.event-text').html(Q(event.categories)).attr('class', 'event-text cat-'+String(event.categories).toLowerCase().replace(rcmail.identifier_expr, '')); | |
420 if (event.free_busy) | |
421 $('#event-free-busy').show().children('.event-text').html(Q(rcmail.gettext(event.free_busy, 'calendar'))); | |
422 if (event.priority > 0) { | |
423 var priolabels = [ '', rcmail.gettext('highest'), rcmail.gettext('high'), '', '', rcmail.gettext('normal'), '', '', rcmail.gettext('low'), rcmail.gettext('lowest') ]; | |
424 $('#event-priority').show().children('.event-text').html(Q(event.priority+' '+priolabels[event.priority])); | |
425 } | |
426 | |
427 if (event.status) { | |
428 var status_lc = String(event.status).toLowerCase(); | |
429 $('#event-status').show().children('.event-text').text(rcmail.gettext('status-'+status_lc,'calendar')); | |
430 $('#event-status-badge > span').text(rcmail.gettext('status-'+status_lc,'calendar')); | |
431 $dialog.addClass('status-'+status_lc); | |
432 } | |
433 if (event.sensitivity && event.sensitivity != 'public') { | |
434 $('#event-sensitivity').show().children('.event-text').text(sensitivitylabels[event.sensitivity]); | |
435 $('#event-status-badge > span').text(sensitivitylabels[event.sensitivity]); | |
436 $dialog.addClass('sensitivity-'+event.sensitivity); | |
437 } | |
438 if (event.created || event.changed) { | |
439 var created = parseISO8601(event.created), | |
440 changed = parseISO8601(event.changed) | |
441 $('#event-created-changed .event-created').html(Q(created ? format_datetime(created) : rcmail.gettext('unknown','calendar'))) | |
442 $('#event-created-changed .event-changed').html(Q(changed ? format_datetime(changed) : rcmail.gettext('unknown','calendar'))) | |
443 $('#event-created-changed').show() | |
444 } | |
445 | |
446 // create attachments list | |
447 if ($.isArray(event.attachments)) { | |
448 event_show_attachments(event.attachments, $('#event-attachments').children('.event-text'), event); | |
449 if (event.attachments.length > 0) { | |
450 $('#event-attachments').show(); | |
451 } | |
452 } | |
453 else if (calendar.attachments) { | |
454 // fetch attachments, some drivers doesn't set 'attachments' prop of the event? | |
455 } | |
456 | |
457 // build attachments list | |
458 $('#event-links').hide(); | |
459 if ($.isArray(event.links) && event.links.length) { | |
460 render_message_links(event.links || [], $('#event-links').children('.event-text'), false, 'calendar'); | |
461 $('#event-links').show(); | |
462 } | |
463 | |
464 // list event attendees | |
465 if (calendar.attendees && event.attendees) { | |
466 // sort resources to the end | |
467 event.attendees.sort(function(a,b) { | |
468 var j = a.cutype == 'RESOURCE' ? 1 : 0, | |
469 k = b.cutype == 'RESOURCE' ? 1 : 0; | |
470 return (j - k); | |
471 }); | |
472 | |
473 var data, mystatus = null, rsvp, line, morelink, html = '', overflow = '', | |
474 organizer = me.is_organizer(event); | |
475 | |
476 for (var j=0; j < event.attendees.length; j++) { | |
477 data = event.attendees[j]; | |
478 if (data.email) { | |
479 if (data.role != 'ORGANIZER' && settings.identity.emails.indexOf(';'+data.email) >= 0) { | |
480 mystatus = data.status.toLowerCase(); | |
481 if (data.status == 'NEEDS-ACTION' || data.status == 'TENTATIVE' || data.rsvp) | |
482 rsvp = mystatus; | |
483 } | |
484 } | |
485 | |
486 line = rcube_libcalendaring.attendee_html(data); | |
487 | |
488 if (morelink) | |
489 overflow += ' ' + line; | |
490 else | |
491 html += ' ' + line; | |
492 | |
493 // stop listing attendees | |
494 if (j == 7 && event.attendees.length >= 7) { | |
495 morelink = $('<a href="#more" class="morelink"></a>').html(rcmail.gettext('andnmore', 'calendar').replace('$nr', event.attendees.length - j - 1)); | |
496 } | |
497 } | |
498 | |
499 if (html && (event.attendees.length > 1 || !organizer)) { | |
500 $('#event-attendees').show() | |
501 .children('.event-text') | |
502 .html(html) | |
503 .find('a.mailtolink').click(event_attendee_click); | |
504 | |
505 // display all attendees in a popup when clicking the "more" link | |
506 if (morelink) { | |
507 $('#event-attendees .event-text').append(morelink); | |
508 morelink.click(function(e){ | |
509 rcmail.show_popup_dialog( | |
510 '<div id="all-event-attendees" class="event-attendees">' + html + overflow + '</div>', | |
511 rcmail.gettext('tabattendees','calendar'), | |
512 null, | |
513 { width:450, modal:false }); | |
514 $('#all-event-attendees a.mailtolink').click(event_attendee_click); | |
515 return false; | |
516 }) | |
517 } | |
518 } | |
519 | |
520 if (mystatus && !rsvp) { | |
521 $('#event-partstat').show().children('.changersvp') | |
522 .removeClass('accepted tentative declined delegated needs-action') | |
523 .addClass(mystatus) | |
524 .children('.event-text') | |
525 .text(rcmail.gettext('status' + mystatus, 'libcalendaring')); | |
526 } | |
527 | |
528 var show_rsvp = rsvp && !organizer && event.status != 'CANCELLED' && me.has_permission(calendar, 'v'); | |
529 $('#event-rsvp')[(show_rsvp ? 'show' : 'hide')](); | |
530 $('#event-rsvp .rsvp-buttons input').prop('disabled', false).filter('input[rel='+mystatus+']').prop('disabled', true); | |
531 | |
532 if (show_rsvp && event.comment) | |
533 $('#event-rsvp-comment').show().children('.event-text').html(Q(event.comment)); | |
534 | |
535 $('#event-rsvp a.reply-comment-toggle').show(); | |
536 $('#event-rsvp .itip-reply-comment textarea').hide().val(''); | |
537 | |
538 if (event.recurrence && event.id) { | |
539 var sel = event._savemode || (event.thisandfuture ? 'future' : (event.isexception ? 'current' : 'all')); | |
540 $('#event-rsvp .rsvp-buttons').addClass('recurring'); | |
541 } | |
542 else { | |
543 $('#event-rsvp .rsvp-buttons').removeClass('recurring'); | |
544 } | |
545 } | |
546 | |
547 var buttons = []; | |
548 if (!temp && calendar.editable && event.editable !== false) { | |
549 buttons.push({ | |
550 text: rcmail.gettext('edit', 'calendar'), | |
551 click: function() { | |
552 event_edit_dialog('edit', event); | |
553 } | |
554 }); | |
555 } | |
556 if (!temp && me.has_permission(calendar, 'td') && event.editable !== false) { | |
557 buttons.push({ | |
558 text: rcmail.gettext('delete', 'calendar'), | |
559 'class': 'delete', | |
560 click: function() { | |
561 me.delete_event(event); | |
562 $dialog.dialog('close'); | |
563 } | |
564 }); | |
565 } | |
566 | |
567 if (!buttons.length) { | |
568 buttons.push({ | |
569 text: rcmail.gettext('close', 'calendar'), | |
570 click: function(){ | |
571 $dialog.dialog('close'); | |
572 } | |
573 }); | |
574 } | |
575 | |
576 // open jquery UI dialog | |
577 $dialog.dialog({ | |
578 modal: false, | |
579 resizable: !bw.ie6, | |
580 closeOnEscape: (!bw.ie6 && !bw.ie7), // disable for performance reasons | |
581 title: me.event_date_text(event), | |
582 open: function() { | |
583 $dialog.attr('aria-hidden', 'false'); | |
584 setTimeout(function(){ | |
585 $dialog.parent().find('.ui-button:not(.ui-dialog-titlebar-close)').first().focus(); | |
586 }, 5); | |
587 }, | |
588 close: function() { | |
589 $dialog.dialog('destroy').attr('aria-hidden', 'true').hide(); | |
590 rcmail.command('menu-close','eventoptionsmenu'); | |
591 $('.libcal-rsvp-replymode').hide(); | |
592 }, | |
593 dragStart: function() { | |
594 rcmail.command('menu-close','eventoptionsmenu'); | |
595 $('.libcal-rsvp-replymode').hide(); | |
596 }, | |
597 resizeStart: function() { | |
598 rcmail.command('menu-close','eventoptionsmenu'); | |
599 $('.libcal-rsvp-replymode').hide(); | |
600 }, | |
601 buttons: buttons, | |
602 minWidth: 320, | |
603 width: 420 | |
604 }).show(); | |
605 | |
606 // remember opener element (to be focused on close) | |
607 $dialog.data('opener', ev && rcube_event.is_keyboard(ev) ? ev.target : null); | |
608 | |
609 // set voice title on dialog widget | |
610 $dialog.dialog('widget').removeAttr('aria-labelledby') | |
611 .attr('aria-label', me.event_date_text(event, true) + ', ', event.title); | |
612 | |
613 // set dialog size according to content | |
614 me.dialog_resize($dialog.get(0), $dialog.height(), 420); | |
615 | |
616 // add link for "more options" drop-down | |
617 if (!temp && !event.temporary && event.calendar != '_resource') { | |
618 $('<a>') | |
619 .attr('href', '#') | |
620 .html(rcmail.gettext('eventoptions','calendar')) | |
621 .addClass('dropdown-link') | |
622 .click(function(e) { | |
623 return rcmail.command('menu-open','eventoptionsmenu', this, e) | |
624 }) | |
625 .appendTo($dialog.parent().find('.ui-dialog-buttonset')); | |
626 } | |
627 | |
628 rcmail.enable_command('event-history', calendar.history) | |
629 | |
630 rcmail.triggerEvent('calendar-event-dialog', {dialog: $dialog}); | |
631 }; | |
632 | |
633 // event handler for clicks on an attendee link | |
634 var event_attendee_click = function(e) | |
635 { | |
636 var cutype = $(this).attr('data-cutype'), | |
637 mailto = this.href.substr(7); | |
638 if (rcmail.env.calendar_resources && cutype == 'RESOURCE') { | |
639 event_resources_dialog(mailto); | |
640 } | |
641 else { | |
642 rcmail.command('compose', mailto, e ? e.target : null, e); | |
643 } | |
644 return false; | |
645 }; | |
646 | |
647 // bring up the event dialog (jquery-ui popup) | |
648 var event_edit_dialog = function(action, event) | |
649 { | |
650 // copy opener element from show dialog | |
651 var op_elem = $("#eventshow:ui-dialog").data('opener'); | |
652 | |
653 // close show dialog first | |
654 $("#eventshow:ui-dialog").data('opener', null).dialog('close'); | |
655 | |
656 var $dialog = $('<div>'); | |
657 var calendar = event.calendar && me.calendars[event.calendar] ? me.calendars[event.calendar] : { editable:true, rights: action=='new' ? 'lrwitd' : 'lrs' }; | |
658 me.selected_event = $.extend($.extend({}, event_defaults), event); // clone event object (with defaults) | |
659 event = me.selected_event; // change reference to clone | |
660 freebusy_ui.needsupdate = false; | |
661 | |
662 // reset dialog first | |
663 $('#eventtabs').get(0).reset(); | |
664 $('#event-panel-recurrence input, #event-panel-recurrence select, #event-panel-attachments input').prop('disabled', false); | |
665 $('#event-panel-recurrence, #event-panel-attachments').removeClass('disabled'); | |
666 | |
667 // allow other plugins to do actions when event form is opened | |
668 rcmail.triggerEvent('calendar-event-init', {o: event}); | |
669 | |
670 // event details | |
671 var title = $('#edit-title').val(event.title || ''); | |
672 var location = $('#edit-location').val(event.location || ''); | |
17
3bd5fe8166b8
switch to using vurl as well for better display, and it gets saved
Charlie Root
parents:
12
diff
changeset
|
673 var description = $('#edit-description').text(event.description || ''); |
3 | 674 var vurl = $('#edit-url').val(event.vurl || ''); |
675 var categories = $('#edit-categories').val(event.categories); | |
676 var calendars = $('#edit-calendar').val(event.calendar); | |
677 var eventstatus = $('#edit-event-status').val(event.status); | |
678 var freebusy = $('#edit-free-busy').val(event.free_busy); | |
679 var priority = $('#edit-priority').val(event.priority); | |
680 var sensitivity = $('#edit-sensitivity').val(event.sensitivity); | |
681 | |
682 var duration = Math.round((event.end.getTime() - event.start.getTime()) / 1000); | |
683 var startdate = $('#edit-startdate').val($.fullCalendar.formatDate(event.start, settings['date_format'])).data('duration', duration); | |
684 var starttime = $('#edit-starttime').val($.fullCalendar.formatDate(event.start, settings['time_format'])).show(); | |
685 var enddate = $('#edit-enddate').val($.fullCalendar.formatDate(event.end, settings['date_format'])); | |
686 var endtime = $('#edit-endtime').val($.fullCalendar.formatDate(event.end, settings['time_format'])).show(); | |
687 var allday = $('#edit-allday').get(0); | |
688 var notify = $('#edit-attendees-donotify').get(0); | |
689 var invite = $('#edit-attendees-invite').get(0); | |
690 var comment = $('#edit-attendees-comment'); | |
691 | |
692 // make sure any calendar is selected | |
693 if (!calendars.val()) | |
694 calendars.val($('option:first', calendars).attr('value')); | |
695 | |
696 invite.checked = settings.itip_notify & 1 > 0; | |
697 notify.checked = me.has_attendees(event) && invite.checked; | |
698 | |
699 if (event.allDay) { | |
700 starttime.val("12:00").hide(); | |
701 endtime.val("13:00").hide(); | |
702 allday.checked = true; | |
703 } | |
704 else { | |
705 allday.checked = false; | |
706 } | |
707 | |
708 // set calendar selection according to permissions | |
709 calendars.find('option').each(function(i, opt) { | |
710 var cal = me.calendars[opt.value] || {}; | |
711 $(opt).prop('disabled', !(cal.editable || (action == 'new' && me.has_permission(cal, 'i')))) | |
712 }); | |
713 | |
714 // set alarm(s) | |
715 me.set_alarms_edit('#edit-alarms', action != 'new' && event.valarms && calendar.alarms ? event.valarms : []); | |
716 | |
717 // enable/disable alarm property according to backend support | |
718 $('#edit-alarms')[(calendar.alarms ? 'show' : 'hide')](); | |
719 | |
720 // check categories drop-down: add value if not exists | |
721 if (event.categories && !categories.find("option[value='"+event.categories+"']").length) { | |
722 $('<option>').attr('value', event.categories).text(event.categories).appendTo(categories).prop('selected', true); | |
723 } | |
724 | |
725 if ($.isArray(event.links) && event.links.length) { | |
17
3bd5fe8166b8
switch to using vurl as well for better display, and it gets saved
Charlie Root
parents:
12
diff
changeset
|
726 render_message_links(event.links, $('#edit-event-links .event-text'), true, 'calendar'); |
3bd5fe8166b8
switch to using vurl as well for better display, and it gets saved
Charlie Root
parents:
12
diff
changeset
|
727 $('#edit-event-links').show(); |
3 | 728 } |
729 else { | |
17
3bd5fe8166b8
switch to using vurl as well for better display, and it gets saved
Charlie Root
parents:
12
diff
changeset
|
730 $('#edit-event-links').hide(); |
3 | 731 } |
732 | |
733 // show warning if editing a recurring event | |
734 if (event.id && event.recurrence) { | |
735 var sel = event._savemode || (event.thisandfuture ? 'future' : (event.isexception ? 'current' : 'all')); | |
736 $('#edit-recurring-warning').show(); | |
737 $('input.edit-recurring-savemode[value="'+sel+'"]').prop('checked', true).change(); | |
738 } | |
739 else | |
740 $('#edit-recurring-warning').hide(); | |
741 | |
742 // init attendees tab | |
743 var organizer = !event.attendees || me.is_organizer(event), | |
744 allow_invitations = organizer || (calendar.owner && calendar.owner == 'anonymous') || settings.invite_shared; | |
745 event_attendees = []; | |
746 attendees_list = $('#edit-attendees-table > tbody').html(''); | |
747 resources_list = $('#edit-resources-table > tbody').html(''); | |
748 $('#edit-attendees-notify')[(action != 'new' && allow_invitations && me.has_attendees(event) && (settings.itip_notify & 2) ? 'show' : 'hide')](); | |
749 $('#edit-localchanges-warning')[(action != 'new' && me.has_attendees(event) && !(allow_invitations || (calendar.owner && me.is_organizer(event, calendar.owner))) ? 'show' : 'hide')](); | |
750 | |
751 var load_attendees_tab = function() | |
752 { | |
753 var j, data, organizer_attendee, reply_selected = 0; | |
754 if (event.attendees) { | |
755 for (j=0; j < event.attendees.length; j++) { | |
756 data = event.attendees[j]; | |
757 // reset attendee status | |
758 if (event._savemode == 'new' && data.role != 'ORGANIZER') { | |
759 data.status = 'NEEDS-ACTION'; | |
760 delete data.noreply; | |
761 } | |
762 add_attendee(data, !allow_invitations); | |
763 if (allow_invitations && data.role != 'ORGANIZER' && !data.noreply) | |
764 reply_selected++; | |
765 | |
766 if (data.role == 'ORGANIZER') | |
767 organizer_attendee = data; | |
768 } | |
769 } | |
770 | |
771 // make sure comment box is visible if at least one attendee has reply enabled | |
772 // or global "send invitations" checkbox is checked | |
773 $('#eventedit .attendees-commentbox')[(reply_selected || invite.checked ? 'show' : 'hide')](); | |
774 | |
775 // select the correct organizer identity | |
776 var identity_id = 0; | |
777 $.each(settings.identities, function(i,v){ | |
778 if (organizer && typeof organizer == 'object' && v == organizer.email) { | |
779 identity_id = i; | |
780 return false; | |
781 } | |
782 }); | |
783 | |
784 // In case the user is not the (shared) event organizer we'll add the organizer to the selection list | |
785 if (!identity_id && !organizer && organizer_attendee) { | |
786 var organizer_name = organizer_attendee.email; | |
787 if (organizer_attendee.name) | |
788 organizer_name = '"' + organizer_attendee.name + '" <' + organizer_name + '>'; | |
789 $('#edit-identities-list').append($('<option value="0">').text(organizer_name)); | |
790 } | |
791 | |
792 $('#edit-identities-list').val(identity_id); | |
793 $('#edit-attendees-form')[(allow_invitations?'show':'hide')](); | |
794 $('#edit-attendee-schedule')[(calendar.freebusy?'show':'hide')](); | |
795 }; | |
796 | |
797 // attachments | |
798 var load_attachments_tab = function() | |
799 { | |
800 rcmail.enable_command('remove-attachment', calendar.editable && !event.recurrence_id); | |
801 rcmail.env.deleted_attachments = []; | |
802 // we're sharing some code for uploads handling with app.js | |
803 rcmail.env.attachments = []; | |
804 rcmail.env.compose_id = event.id; // for rcmail.async_upload_form() | |
805 | |
806 if ($.isArray(event.attachments)) { | |
807 event_show_attachments(event.attachments, $('#edit-attachments'), event, true); | |
808 } | |
809 else { | |
810 $('#edit-attachments > ul').empty(); | |
811 // fetch attachments, some drivers doesn't set 'attachments' array for event? | |
812 } | |
813 }; | |
814 | |
815 // init dialog buttons | |
816 var buttons = []; | |
817 | |
818 // save action | |
819 buttons.push({ | |
820 text: rcmail.gettext('save', 'calendar'), | |
821 'class': 'mainaction', | |
822 click: function() { | |
823 var start = parse_datetime(allday.checked ? '12:00' : starttime.val(), startdate.val()); | |
824 var end = parse_datetime(allday.checked ? '13:00' : endtime.val(), enddate.val()); | |
825 | |
826 // basic input validatetion | |
827 if (start.getTime() > end.getTime()) { | |
828 alert(rcmail.gettext('invalideventdates', 'calendar')); | |
829 return false; | |
830 } | |
831 | |
832 // post data to server | |
833 var data = { | |
834 calendar: event.calendar, | |
835 start: date2servertime(start), | |
836 end: date2servertime(end), | |
837 allday: allday.checked?1:0, | |
838 title: title.val(), | |
12
ff1f27bd05ad
partial success in getting back_link to message when making event from mail
Charlie Root
parents:
3
diff
changeset
|
839 description: description.html(), |
3 | 840 location: location.val(), |
841 categories: categories.val(), | |
842 vurl: vurl.val(), | |
843 free_busy: freebusy.val(), | |
844 priority: priority.val(), | |
845 sensitivity: sensitivity.val(), | |
846 status: eventstatus.val(), | |
847 recurrence: me.serialize_recurrence(endtime.val()), | |
848 valarms: me.serialize_alarms('#edit-alarms'), | |
849 attendees: event_attendees, | |
850 links: me.selected_event.links, | |
851 deleted_attachments: rcmail.env.deleted_attachments, | |
852 attachments: [] | |
853 }; | |
854 | |
855 // uploaded attachments list | |
856 for (var i in rcmail.env.attachments) | |
857 if (i.match(/^rcmfile(.+)/)) | |
858 data.attachments.push(RegExp.$1); | |
859 | |
860 // read attendee roles | |
861 $('select.edit-attendee-role').each(function(i, elem){ | |
862 if (data.attendees[i]) | |
863 data.attendees[i].role = $(elem).val(); | |
864 }); | |
865 | |
866 if (organizer) | |
867 data._identity = $('#edit-identities-list option:selected').val(); | |
868 | |
869 // per-attendee notification suppression | |
870 var need_invitation = false; | |
871 if (allow_invitations) { | |
872 $.each(data.attendees, function (i, v) { | |
873 if (v.role != 'ORGANIZER') { | |
874 if ($('input.edit-attendee-reply[value="' + v.email + '"]').prop('checked') || v.cutype == 'RESOURCE') { | |
875 need_invitation = true; | |
876 delete data.attendees[i]['noreply']; | |
877 } | |
878 else if (settings.itip_notify > 0) { | |
879 data.attendees[i].noreply = 1; | |
880 } | |
881 } | |
882 }); | |
883 } | |
884 | |
885 // tell server to send notifications | |
886 if ((data.attendees.length || (event.id && event.attendees.length)) && allow_invitations && (notify.checked || invite.checked || need_invitation)) { | |
887 data._notify = settings.itip_notify; | |
888 data._comment = comment.val(); | |
889 } | |
890 | |
891 data.calendar = calendars.val(); | |
892 | |
893 if (event.id) { | |
894 data.id = event.id; | |
895 if (event.recurrence) | |
896 data._savemode = $('input.edit-recurring-savemode:checked').val(); | |
897 if (data.calendar && data.calendar != event.calendar) | |
898 data._fromcalendar = event.calendar; | |
899 } | |
900 | |
901 update_event(action, data); | |
902 $dialog.dialog("close"); | |
903 } // end click: | |
904 }); | |
905 | |
906 if (event.id) { | |
907 buttons.push({ | |
908 text: rcmail.gettext('delete', 'calendar'), | |
909 'class': 'delete', | |
910 click: function() { | |
911 me.delete_event(event); | |
912 $dialog.dialog('close'); | |
913 } | |
914 }); | |
915 } | |
916 | |
917 buttons.push({ | |
918 text: rcmail.gettext('cancel', 'calendar'), | |
919 click: function() { | |
920 $dialog.dialog("close"); | |
921 } | |
922 }); | |
923 | |
924 // show/hide tabs according to calendar's feature support | |
925 $('#edit-tab-attendees')[(calendar.attendees?'show':'hide')](); | |
926 $('#edit-tab-resources')[(rcmail.env.calendar_resources?'show':'hide')](); | |
927 $('#edit-tab-attachments')[(calendar.attachments?'show':'hide')](); | |
928 | |
929 // activate the first tab | |
930 $('#eventtabs').tabs('option', 'active', 0); | |
931 | |
932 // hack: set task to 'calendar' to make all dialog actions work correctly | |
933 var comm_path_before = rcmail.env.comm_path; | |
934 rcmail.env.comm_path = comm_path_before.replace(/_task=[a-z]+/, '_task=calendar'); | |
935 | |
936 var editform = $("#eventedit"); | |
937 | |
938 // open jquery UI dialog | |
939 $dialog.dialog({ | |
940 modal: true, | |
941 resizable: (!bw.ie6 && !bw.ie7), // disable for performance reasons | |
942 closeOnEscape: false, | |
943 title: rcmail.gettext((action == 'edit' ? 'edit_event' : 'new_event'), 'calendar'), | |
944 open: function() { | |
945 editform.attr('aria-hidden', 'false'); | |
946 }, | |
947 close: function() { | |
948 editform.hide().attr('aria-hidden', 'true').appendTo(document.body); | |
949 $dialog.dialog("destroy").remove(); | |
950 rcmail.ksearch_blur(); | |
951 freebusy_data = {}; | |
952 rcmail.env.comm_path = comm_path_before; // restore comm_path | |
953 if (op_elem) | |
954 $(op_elem).focus(); | |
955 }, | |
956 buttons: buttons, | |
957 minWidth: 500, | |
958 width: 600 | |
959 }).append(editform.show()); // adding form content AFTERWARDS massively speeds up opening on IE6 | |
960 | |
961 // set dialog size according to form content | |
962 me.dialog_resize($dialog.get(0), editform.height() + (bw.ie ? 20 : 0), 550); | |
963 | |
964 title.select(); | |
965 | |
966 // init other tabs asynchronously | |
967 window.setTimeout(function(){ me.set_recurrence_edit(event); }, exec_deferred); | |
968 if (calendar.attendees) | |
969 window.setTimeout(load_attendees_tab, exec_deferred); | |
970 if (calendar.attachments) | |
971 window.setTimeout(load_attachments_tab, exec_deferred); | |
972 | |
973 rcmail.triggerEvent('calendar-event-dialog', {dialog: $dialog}); | |
974 }; | |
975 | |
976 // show event changelog in a dialog | |
977 var event_history_dialog = function(event) | |
978 { | |
979 if (!event.id || !window.libkolab_audittrail) | |
980 return false | |
981 | |
982 // render dialog | |
983 var $dialog = libkolab_audittrail.object_history_dialog({ | |
984 module: 'calendar', | |
985 container: '#eventhistory', | |
986 title: rcmail.gettext('objectchangelog','calendar') + ' - ' + event.title + ', ' + me.event_date_text(event), | |
987 | |
988 // callback function for list actions | |
989 listfunc: function(action, rev) { | |
990 me.loading_lock = rcmail.set_busy(true, 'loading', me.loading_lock); | |
991 rcmail.http_post('event', { action:action, e:{ id:event.id, calendar:event.calendar, rev: rev } }, me.loading_lock); | |
992 }, | |
993 | |
994 // callback function for comparing two object revisions | |
995 comparefunc: function(rev1, rev2) { | |
996 me.loading_lock = rcmail.set_busy(true, 'loading', me.loading_lock); | |
997 rcmail.http_post('event', { action:'diff', e:{ id:event.id, calendar:event.calendar, rev1: rev1, rev2: rev2 } }, me.loading_lock); | |
998 } | |
999 }); | |
1000 | |
1001 $dialog.data('event', event); | |
1002 | |
1003 // fetch changelog data | |
1004 me.loading_lock = rcmail.set_busy(true, 'loading', me.loading_lock); | |
1005 rcmail.http_post('event', { action:'changelog', e:{ id:event.id, calendar:event.calendar } }, me.loading_lock); | |
1006 }; | |
1007 | |
1008 // callback from server with changelog data | |
1009 var render_event_changelog = function(data) | |
1010 { | |
1011 var $dialog = $('#eventhistory'), | |
1012 event = $dialog.data('event'); | |
1013 | |
1014 if (data === false || !data.length || !event) { | |
1015 // display 'unavailable' message | |
1016 $('<div class="notfound-message event-dialog-message warning">' + rcmail.gettext('objectchangelognotavailable','calendar') + '</div>') | |
1017 .insertBefore($dialog.find('.changelog-table').hide()); | |
1018 return; | |
1019 } | |
1020 | |
1021 data.module = 'calendar'; | |
1022 libkolab_audittrail.render_changelog(data, event, me.calendars[event.calendar]); | |
1023 | |
1024 // set dialog size according to content | |
1025 me.dialog_resize($dialog.get(0), $dialog.height(), 600); | |
1026 }; | |
1027 | |
1028 // callback from server with event diff data | |
1029 var event_show_diff = function(data) | |
1030 { | |
1031 var event = me.selected_event, | |
1032 $dialog = $("#eventdiff"); | |
1033 | |
1034 $dialog.find('div.event-section, div.event-line, h1.event-title-new').hide().data('set', false).find('.index').html(''); | |
1035 $dialog.find('div.event-section.clone, div.event-line.clone').remove(); | |
1036 | |
1037 // always show event title and date | |
1038 $('.event-title', $dialog).text(event.title).removeClass('event-text-old').show(); | |
1039 $('.event-date', $dialog).text(me.event_date_text(event)).show(); | |
1040 | |
1041 // show each property change | |
1042 $.each(data.changes, function(i,change) { | |
1043 var prop = change.property, r2, html = false, | |
1044 row = $('div.event-' + prop, $dialog).first(); | |
1045 | |
1046 // special case: title | |
1047 if (prop == 'title') { | |
1048 $('.event-title', $dialog).addClass('event-text-old').text(change['old'] || '--'); | |
1049 $('.event-title-new', $dialog).text(change['new'] || '--').show(); | |
1050 } | |
1051 | |
1052 // no display container for this property | |
1053 if (!row.length) { | |
1054 return true; | |
1055 } | |
1056 | |
1057 // clone row if already exists | |
1058 if (row.data('set')) { | |
1059 r2 = row.clone().addClass('clone').insertAfter(row); | |
1060 row = r2; | |
1061 } | |
1062 | |
1063 // format dates | |
1064 if (['start','end','changed'].indexOf(prop) >= 0) { | |
1065 if (change['old']) change.old_ = me.format_datetime(parseISO8601(change['old'])); | |
1066 if (change['new']) change.new_ = me.format_datetime(parseISO8601(change['new'])); | |
1067 } | |
1068 // render description text | |
1069 else if (prop == 'description') { | |
1070 // TODO: show real text diff | |
1071 if (!change.diff_ && change['old']) change.old_ = text2html(change['old']); | |
1072 if (!change.diff_ && change['new']) change.new_ = text2html(change['new']); | |
1073 html = true; | |
1074 } | |
1075 // format attendees struct | |
1076 else if (prop == 'attendees') { | |
1077 if (change['old']) change.old_ = rcube_libcalendaring.attendee_html(change['old']); | |
1078 if (change['new']) change.new_ = rcube_libcalendaring.attendee_html($.extend({}, change['old'] || {}, change['new'])); | |
1079 html = true; | |
1080 } | |
1081 // localize priority values | |
1082 else if (prop == 'priority') { | |
1083 var priolabels = [ '', rcmail.gettext('highest'), rcmail.gettext('high'), '', '', rcmail.gettext('normal'), '', '', rcmail.gettext('low'), rcmail.gettext('lowest') ]; | |
1084 if (change['old']) change.old_ = change['old'] + ' ' + (priolabels[change['old']] || ''); | |
1085 if (change['new']) change.new_ = change['new'] + ' ' + (priolabels[change['new']] || ''); | |
1086 } | |
1087 // localize status | |
1088 else if (prop == 'status') { | |
1089 var status_lc = String(event.status).toLowerCase(); | |
1090 if (change['old']) change.old_ = rcmail.gettext(String(change['old']).toLowerCase(), 'calendar'); | |
1091 if (change['new']) change.new_ = rcmail.gettext(String(change['new']).toLowerCase(), 'calendar'); | |
1092 } | |
1093 | |
1094 // format attachments struct | |
1095 if (prop == 'attachments') { | |
1096 if (change['old']) event_show_attachments([change['old']], row.children('.event-text-old'), event, false); | |
1097 else row.children('.event-text-old').text('--'); | |
1098 if (change['new']) event_show_attachments([$.extend({}, change['old'] || {}, change['new'])], row.children('.event-text-new'), event, false); | |
1099 else row.children('.event-text-new').text('--'); | |
1100 // remove click handler as we're currentyl not able to display the according attachment contents | |
1101 $('.attachmentslist li a', row).unbind('click').removeAttr('href'); | |
1102 } | |
1103 else if (change.diff_) { | |
1104 row.children('.event-text-diff').html(change.diff_); | |
1105 row.children('.event-text-old, .event-text-new').hide(); | |
1106 } | |
1107 else { | |
1108 if (!html) { | |
1109 // escape HTML characters | |
1110 change.old_ = Q(change.old_ || change['old'] || '--') | |
1111 change.new_ = Q(change.new_ || change['new'] || '--') | |
1112 } | |
1113 row.children('.event-text-old').html(change.old_ || change['old'] || '--'); | |
1114 row.children('.event-text-new').html(change.new_ || change['new'] || '--'); | |
1115 } | |
1116 | |
1117 // display index number | |
1118 if (typeof change.index != 'undefined') { | |
1119 row.find('.index').html('(' + change.index + ')'); | |
1120 } | |
1121 | |
1122 row.show().data('set', true); | |
1123 | |
1124 // hide event-date line | |
1125 if (prop == 'start' || prop == 'end') | |
1126 $('.event-date', $dialog).hide(); | |
1127 }); | |
1128 | |
1129 var buttons = {}; | |
1130 buttons[rcmail.gettext('close', 'calendar')] = function() { | |
1131 $dialog.dialog('close'); | |
1132 }; | |
1133 | |
1134 // open jquery UI dialog | |
1135 $dialog.dialog({ | |
1136 modal: false, | |
1137 resizable: true, | |
1138 closeOnEscape: true, | |
1139 title: rcmail.gettext('objectdiff','calendar').replace('$rev1', data.rev1).replace('$rev2', data.rev2) + ' - ' + event.title, | |
1140 open: function() { | |
1141 $dialog.attr('aria-hidden', 'false'); | |
1142 setTimeout(function(){ | |
1143 $dialog.parent().find('.ui-button:not(.ui-dialog-titlebar-close)').first().focus(); | |
1144 }, 5); | |
1145 }, | |
1146 close: function() { | |
1147 $dialog.dialog('destroy').attr('aria-hidden', 'true').hide(); | |
1148 }, | |
1149 buttons: buttons, | |
1150 minWidth: 320, | |
1151 width: 450 | |
1152 }).show(); | |
1153 | |
1154 // set dialog size according to content | |
1155 me.dialog_resize($dialog.get(0), $dialog.height(), 400); | |
1156 }; | |
1157 | |
1158 // close the event history dialog | |
1159 var close_history_dialog = function() | |
1160 { | |
1161 $('#eventhistory, #eventdiff').each(function(i, elem) { | |
1162 var $dialog = $(elem); | |
1163 if ($dialog.is(':ui-dialog')) | |
1164 $dialog.dialog('close'); | |
1165 }); | |
1166 } | |
1167 | |
1168 // exports | |
1169 this.event_show_diff = event_show_diff; | |
1170 this.event_show_dialog = event_show_dialog; | |
1171 this.event_history_dialog = event_history_dialog; | |
1172 this.render_event_changelog = render_event_changelog; | |
1173 this.close_history_dialog = close_history_dialog; | |
1174 | |
1175 // open a dialog to display detailed free-busy information and to find free slots | |
1176 var event_freebusy_dialog = function() | |
1177 { | |
1178 var $dialog = $('#eventfreebusy'), | |
1179 event = me.selected_event; | |
1180 | |
1181 if ($dialog.is(':ui-dialog')) | |
1182 $dialog.dialog('close'); | |
1183 | |
1184 if (!event_attendees.length) | |
1185 return false; | |
1186 | |
1187 // set form elements | |
1188 var allday = $('#edit-allday').get(0); | |
1189 var duration = Math.round((event.end.getTime() - event.start.getTime()) / 1000); | |
1190 freebusy_ui.startdate = $('#schedule-startdate').val($.fullCalendar.formatDate(event.start, settings['date_format'])).data('duration', duration); | |
1191 freebusy_ui.starttime = $('#schedule-starttime').val($.fullCalendar.formatDate(event.start, settings['time_format'])).show(); | |
1192 freebusy_ui.enddate = $('#schedule-enddate').val($.fullCalendar.formatDate(event.end, settings['date_format'])); | |
1193 freebusy_ui.endtime = $('#schedule-endtime').val($.fullCalendar.formatDate(event.end, settings['time_format'])).show(); | |
1194 | |
1195 if (allday.checked) { | |
1196 freebusy_ui.starttime.val("12:00").hide(); | |
1197 freebusy_ui.endtime.val("13:00").hide(); | |
1198 event.allDay = true; | |
1199 } | |
1200 | |
1201 // read attendee roles from drop-downs | |
1202 $('select.edit-attendee-role').each(function(i, elem){ | |
1203 if (event_attendees[i]) | |
1204 event_attendees[i].role = $(elem).val(); | |
1205 }); | |
1206 | |
1207 // render time slots | |
1208 var now = new Date(), fb_start = new Date(), fb_end = new Date(); | |
1209 fb_start.setTime(event.start); | |
1210 fb_start.setHours(0); fb_start.setMinutes(0); fb_start.setSeconds(0); fb_start.setMilliseconds(0); | |
1211 fb_end.setTime(fb_start.getTime() + DAY_MS); | |
1212 | |
1213 freebusy_data = { required:{}, all:{} }; | |
1214 freebusy_ui.loading = 1; // prevent render_freebusy_grid() to load data yet | |
1215 freebusy_ui.numdays = Math.max(allday.checked ? 14 : 1, Math.ceil(duration * 2 / 86400)); | |
1216 freebusy_ui.interval = allday.checked ? 1440 : (60 / (settings.timeslots || 1)); | |
1217 freebusy_ui.start = fb_start; | |
1218 freebusy_ui.end = new Date(freebusy_ui.start.getTime() + DAY_MS * freebusy_ui.numdays); | |
1219 render_freebusy_grid(0); | |
1220 | |
1221 // render list of attendees | |
1222 freebusy_ui.attendees = {}; | |
1223 var domid, dispname, data, role_html, list_html = ''; | |
1224 for (var i=0; i < event_attendees.length; i++) { | |
1225 data = event_attendees[i]; | |
1226 dispname = Q(data.name || data.email); | |
1227 domid = String(data.email).replace(rcmail.identifier_expr, ''); | |
1228 role_html = '<a class="attendee-role-toggle" id="rcmlia' + domid + '" title="' + Q(rcmail.gettext('togglerole', 'calendar')) + '"> </a>'; | |
1229 list_html += '<div class="attendee ' + String(data.role).toLowerCase() + '" id="rcmli' + domid + '">' + role_html + dispname + '</div>'; | |
1230 | |
1231 // clone attendees data for local modifications | |
1232 freebusy_ui.attendees[i] = freebusy_ui.attendees[domid] = $.extend({}, data); | |
1233 } | |
1234 | |
1235 // add total row | |
1236 list_html += '<div class="attendee spacer"> </div>'; | |
1237 list_html += '<div class="attendee total">' + rcmail.gettext('reqallattendees','calendar') + '</div>'; | |
1238 | |
1239 $('#schedule-attendees-list').html(list_html) | |
1240 .unbind('click.roleicons') | |
1241 .bind('click.roleicons', function(e){ | |
1242 // toggle attendee status upon click on icon | |
1243 if (e.target.id && e.target.id.match(/rcmlia(.+)/)) { | |
1244 var attendee, domid = RegExp.$1, | |
1245 roles = [ 'REQ-PARTICIPANT', 'OPT-PARTICIPANT', 'NON-PARTICIPANT', 'CHAIR' ]; | |
1246 if ((attendee = freebusy_ui.attendees[domid]) && attendee.role != 'ORGANIZER') { | |
1247 var req = attendee.role != 'OPT-PARTICIPANT' && attendee.role != 'NON-PARTICIPANT'; | |
1248 var j = $.inArray(attendee.role, roles); | |
1249 j = (j+1) % roles.length; | |
1250 attendee.role = roles[j]; | |
1251 $(e.target).parent().attr('class', 'attendee '+String(attendee.role).toLowerCase()); | |
1252 | |
1253 // update total display if required-status changed | |
1254 if (req != (roles[j] != 'OPT-PARTICIPANT' && roles[j] != 'NON-PARTICIPANT')) { | |
1255 compute_freebusy_totals(); | |
1256 update_freebusy_display(attendee.email); | |
1257 } | |
1258 } | |
1259 } | |
1260 | |
1261 return false; | |
1262 }); | |
1263 | |
1264 // enable/disable buttons | |
1265 $('#shedule-find-prev').button('option', 'disabled', (fb_start.getTime() < now.getTime())); | |
1266 | |
1267 // dialog buttons | |
1268 var buttons = {}; | |
1269 | |
1270 buttons[rcmail.gettext('select', 'calendar')] = function() { | |
1271 $('#edit-startdate').val(freebusy_ui.startdate.val()); | |
1272 $('#edit-starttime').val(freebusy_ui.starttime.val()); | |
1273 $('#edit-enddate').val(freebusy_ui.enddate.val()); | |
1274 $('#edit-endtime').val(freebusy_ui.endtime.val()); | |
1275 | |
1276 // write role changes back to main dialog | |
1277 $('select.edit-attendee-role').each(function(i, elem){ | |
1278 if (event_attendees[i] && freebusy_ui.attendees[i]) { | |
1279 event_attendees[i].role = freebusy_ui.attendees[i].role; | |
1280 $(elem).val(event_attendees[i].role); | |
1281 } | |
1282 }); | |
1283 | |
1284 if (freebusy_ui.needsupdate) | |
1285 update_freebusy_status(me.selected_event); | |
1286 freebusy_ui.needsupdate = false; | |
1287 $dialog.dialog("close"); | |
1288 }; | |
1289 | |
1290 buttons[rcmail.gettext('cancel', 'calendar')] = function() { | |
1291 $dialog.dialog("close"); | |
1292 }; | |
1293 | |
1294 $dialog.dialog({ | |
1295 modal: true, | |
1296 resizable: true, | |
1297 closeOnEscape: (!bw.ie6 && !bw.ie7), | |
1298 title: rcmail.gettext('scheduletime', 'calendar'), | |
1299 open: function() { | |
1300 rcmail.ksearch_blur(); | |
1301 $dialog.attr('aria-hidden', 'false').find('#shedule-find-next, #shedule-find-prev').not(':disabled').first().focus(); | |
1302 }, | |
1303 close: function() { | |
1304 if (bw.ie6) | |
1305 $("#edit-attendees-table").css('visibility','visible'); | |
1306 $dialog.dialog("destroy").attr('aria-hidden', 'true').hide(); | |
1307 // TODO: focus opener button | |
1308 }, | |
1309 resizeStop: function() { | |
1310 render_freebusy_overlay(); | |
1311 }, | |
1312 buttons: buttons, | |
1313 minWidth: 640, | |
1314 width: 850 | |
1315 }).show(); | |
1316 | |
1317 // hide edit dialog on IE6 because of drop-down elements | |
1318 if (bw.ie6) | |
1319 $("#edit-attendees-table").css('visibility','hidden'); | |
1320 | |
1321 // adjust dialog size to fit grid without scrolling | |
1322 var gridw = $('#schedule-freebusy-times').width(); | |
1323 var overflow = gridw - $('#attendees-freebusy-table td.times').width(); | |
1324 me.dialog_resize($dialog.get(0), $dialog.height() + (bw.ie ? 20 : 0), 800 + Math.max(0, overflow)); | |
1325 | |
1326 // fetch data from server | |
1327 freebusy_ui.loading = 0; | |
1328 load_freebusy_data(freebusy_ui.start, freebusy_ui.interval); | |
1329 }; | |
1330 | |
1331 // render an HTML table showing free-busy status for all the event attendees | |
1332 var render_freebusy_grid = function(delta) | |
1333 { | |
1334 if (delta) { | |
1335 freebusy_ui.start.setTime(freebusy_ui.start.getTime() + DAY_MS * delta); | |
1336 fix_date(freebusy_ui.start); | |
1337 | |
1338 // skip weekends if in workinhoursonly-mode | |
1339 if (Math.abs(delta) == 1 && freebusy_ui.workinhoursonly) { | |
1340 while (is_weekend(freebusy_ui.start)) | |
1341 freebusy_ui.start.setTime(freebusy_ui.start.getTime() + DAY_MS * delta); | |
1342 fix_date(freebusy_ui.start); | |
1343 } | |
1344 | |
1345 freebusy_ui.end = new Date(freebusy_ui.start.getTime() + DAY_MS * freebusy_ui.numdays); | |
1346 } | |
1347 | |
1348 var dayslots = Math.floor(1440 / freebusy_ui.interval); | |
1349 var date_format = 'ddd '+ (dayslots <= 2 ? settings.date_short : settings.date_format); | |
1350 var lastdate, datestr, css, | |
1351 curdate = new Date(), | |
1352 allday = (freebusy_ui.interval == 1440), | |
1353 interval = allday ? 1440 : (freebusy_ui.interval * (settings.timeslots || 1)); | |
1354 times_css = (allday ? 'allday ' : ''), | |
1355 dates_row = '<tr class="dates">', | |
1356 times_row = '<tr class="times">', | |
1357 slots_row = ''; | |
1358 | |
1359 for (var s = 0, t = freebusy_ui.start.getTime(); t < freebusy_ui.end.getTime(); s++) { | |
1360 curdate.setTime(t); | |
1361 datestr = fc.fullCalendar('formatDate', curdate, date_format); | |
1362 if (datestr != lastdate) { | |
1363 if (lastdate && !allday) break; | |
1364 dates_row += '<th colspan="' + dayslots + '" class="boxtitle date' + $.fullCalendar.formatDate(curdate, 'ddMMyyyy') + '">' + Q(datestr) + '</th>'; | |
1365 lastdate = datestr; | |
1366 } | |
1367 | |
1368 // set css class according to working hours | |
1369 css = is_weekend(curdate) || (freebusy_ui.interval <= 60 && !is_workinghour(curdate)) ? 'offhours' : 'workinghours'; | |
1370 times_row += '<td class="' + times_css + css + '" id="t-' + Math.floor(t/1000) + '">' + Q(allday ? rcmail.gettext('all-day','calendar') : $.fullCalendar.formatDate(curdate, settings['time_format'])) + '</td>'; | |
1371 slots_row += '<td class="' + css + '"> </td>'; | |
1372 | |
1373 t += interval * 60000; | |
1374 } | |
1375 dates_row += '</tr>'; | |
1376 times_row += '</tr>'; | |
1377 | |
1378 // render list of attendees | |
1379 var domid, data, list_html = '', times_html = ''; | |
1380 for (var i=0; i < event_attendees.length; i++) { | |
1381 data = event_attendees[i]; | |
1382 domid = String(data.email).replace(rcmail.identifier_expr, ''); | |
1383 times_html += '<tr id="fbrow' + domid + '">' + slots_row + '</tr>'; | |
1384 } | |
1385 | |
1386 // add line for all/required attendees | |
1387 times_html += '<tr class="spacer"><td colspan="' + (dayslots * freebusy_ui.numdays) + '"></td>'; | |
1388 times_html += '<tr id="fbrowall">' + slots_row + '</tr>'; | |
1389 | |
1390 var table = $('#schedule-freebusy-times'); | |
1391 table.children('thead').html(dates_row + times_row); | |
1392 table.children('tbody').html(times_html); | |
1393 | |
1394 // initialize event handlers on grid | |
1395 if (!freebusy_ui.grid_events) { | |
1396 freebusy_ui.grid_events = true; | |
1397 table.children('thead').click(function(e){ | |
1398 // move event to the clicked date/time | |
1399 if (e.target.id && e.target.id.match(/t-(\d+)/)) { | |
1400 var newstart = new Date(RegExp.$1 * 1000); | |
1401 // set time to 00:00 | |
1402 if (me.selected_event.allDay) { | |
1403 newstart.setMinutes(0); | |
1404 newstart.setHours(0); | |
1405 } | |
1406 update_freebusy_dates(newstart, new Date(newstart.getTime() + freebusy_ui.startdate.data('duration') * 1000)); | |
1407 render_freebusy_overlay(); | |
1408 } | |
1409 }); | |
1410 } | |
1411 | |
1412 // if we have loaded free-busy data, show it | |
1413 if (!freebusy_ui.loading) { | |
1414 if (freebusy_ui.start < freebusy_data.start || freebusy_ui.end > freebusy_data.end || freebusy_ui.interval != freebusy_data.interval) { | |
1415 load_freebusy_data(freebusy_ui.start, freebusy_ui.interval); | |
1416 } | |
1417 else { | |
1418 for (var email, i=0; i < event_attendees.length; i++) { | |
1419 if ((email = event_attendees[i].email)) | |
1420 update_freebusy_display(email); | |
1421 } | |
1422 } | |
1423 } | |
1424 | |
1425 // render current event date/time selection over grid table | |
1426 // use timeout to let the dom attributes (width/height/offset) be set first | |
1427 window.setTimeout(function(){ render_freebusy_overlay(); }, 10); | |
1428 }; | |
1429 | |
1430 // render overlay element over the grid to visiualize the current event date/time | |
1431 var render_freebusy_overlay = function() | |
1432 { | |
1433 var overlay = $('#schedule-event-time'); | |
1434 if (me.selected_event.end.getTime() <= freebusy_ui.start.getTime() || me.selected_event.start.getTime() >= freebusy_ui.end.getTime()) { | |
1435 overlay.hide(); | |
1436 if (overlay.data('isdraggable')) | |
1437 overlay.draggable('disable'); | |
1438 } | |
1439 else { | |
1440 var i, n, table = $('#schedule-freebusy-times'), | |
1441 width = 0, | |
1442 pos = { top:table.children('thead').height(), left:0 }, | |
1443 eventstart = date2unixtime(clone_date(me.selected_event.start, me.selected_event.allDay?1:0)), | |
1444 eventend = date2unixtime(clone_date(me.selected_event.end, me.selected_event.allDay?2:0)) - 60, | |
1445 slotstart = date2unixtime(freebusy_ui.start), | |
1446 slotsize = freebusy_ui.interval * 60, | |
1447 slotnum = freebusy_ui.interval > 60 ? 1 : (60 / freebusy_ui.interval), | |
1448 cells = table.children('thead').find('td'), | |
1449 cell_width = cells.first().get(0).offsetWidth, | |
1450 slotend; | |
1451 | |
1452 // iterate through slots to determine position and size of the overlay | |
1453 for (i=0; i < cells.length; i++) { | |
1454 for (n=0; n < slotnum; n++) { | |
1455 slotend = slotstart + slotsize - 1; | |
1456 // event starts in this slot: compute left | |
1457 if (eventstart >= slotstart && eventstart <= slotend) { | |
1458 pos.left = Math.round(i * cell_width + (cell_width / slotnum) * n); | |
1459 } | |
1460 // event ends in this slot: compute width | |
1461 if (eventend >= slotstart && eventend <= slotend) { | |
1462 width = Math.round(i * cell_width + (cell_width / slotnum) * (n + 1)) - pos.left; | |
1463 } | |
1464 slotstart += slotsize; | |
1465 } | |
1466 } | |
1467 | |
1468 if (!width) | |
1469 width = table.width() - pos.left; | |
1470 | |
1471 // overlay is visible | |
1472 if (width > 0) { | |
1473 overlay.css({ width: (width-4)+'px', height:(table.children('tbody').height() - 4)+'px', left:pos.left+'px', top:pos.top+'px' }).show(); | |
1474 | |
1475 // configure draggable | |
1476 if (!overlay.data('isdraggable')) { | |
1477 overlay.draggable({ | |
1478 axis: 'x', | |
1479 scroll: true, | |
1480 stop: function(e, ui){ | |
1481 // convert pixels to time | |
1482 var px = ui.position.left; | |
1483 var range_p = $('#schedule-freebusy-times').width(); | |
1484 var range_t = freebusy_ui.end.getTime() - freebusy_ui.start.getTime(); | |
1485 var newstart = new Date(freebusy_ui.start.getTime() + px * (range_t / range_p)); | |
1486 newstart.setSeconds(0); newstart.setMilliseconds(0); | |
1487 // snap to day boundaries | |
1488 if (me.selected_event.allDay) { | |
1489 if (newstart.getHours() >= 12) // snap to next day | |
1490 newstart.setTime(newstart.getTime() + DAY_MS); | |
1491 newstart.setMinutes(0); | |
1492 newstart.setHours(0); | |
1493 } | |
1494 else { | |
1495 // round to 5 minutes | |
1496 // @TODO: round to timeslots? | |
1497 var round = newstart.getMinutes() % 5; | |
1498 if (round > 2.5) newstart.setTime(newstart.getTime() + (5 - round) * 60000); | |
1499 else if (round > 0) newstart.setTime(newstart.getTime() - round * 60000); | |
1500 } | |
1501 // update event times and display | |
1502 update_freebusy_dates(newstart, new Date(newstart.getTime() + freebusy_ui.startdate.data('duration') * 1000)); | |
1503 if (me.selected_event.allDay) | |
1504 render_freebusy_overlay(); | |
1505 } | |
1506 }).data('isdraggable', true); | |
1507 } | |
1508 else | |
1509 overlay.draggable('enable'); | |
1510 } | |
1511 else | |
1512 overlay.draggable('disable').hide(); | |
1513 } | |
1514 }; | |
1515 | |
1516 // fetch free-busy information for each attendee from server | |
1517 var load_freebusy_data = function(from, interval) | |
1518 { | |
1519 var start = new Date(from.getTime() - DAY_MS * 2); // start 2 days before event | |
1520 fix_date(start); | |
1521 var end = new Date(start.getTime() + DAY_MS * Math.max(14, freebusy_ui.numdays + 7)); // load min. 14 days | |
1522 freebusy_ui.numrequired = 0; | |
1523 freebusy_data.all = []; | |
1524 freebusy_data.required = []; | |
1525 | |
1526 // load free-busy information for every attendee | |
1527 var domid, email; | |
1528 for (var i=0; i < event_attendees.length; i++) { | |
1529 if ((email = event_attendees[i].email)) { | |
1530 domid = String(email).replace(rcmail.identifier_expr, ''); | |
1531 $('#rcmli' + domid).addClass('loading'); | |
1532 freebusy_ui.loading++; | |
1533 | |
1534 $.ajax({ | |
1535 type: 'GET', | |
1536 dataType: 'json', | |
1537 url: rcmail.url('freebusy-times'), | |
1538 data: { email:email, start:date2servertime(clone_date(start, 1)), end:date2servertime(clone_date(end, 2)), interval:interval, _remote:1 }, | |
1539 success: function(data) { | |
1540 freebusy_ui.loading--; | |
1541 | |
1542 // find attendee | |
1543 var i, attendee = null; | |
1544 for (i=0; i < event_attendees.length; i++) { | |
1545 if (freebusy_ui.attendees[i].email == data.email) { | |
1546 attendee = freebusy_ui.attendees[i]; | |
1547 break; | |
1548 } | |
1549 } | |
1550 | |
1551 // copy data to member var | |
1552 var ts, status, | |
1553 req = attendee.role != 'OPT-PARTICIPANT', | |
1554 start = parseISO8601(data.start); | |
1555 | |
1556 freebusy_data.start = new Date(start); | |
1557 freebusy_data.end = parseISO8601(data.end); | |
1558 freebusy_data.interval = data.interval; | |
1559 freebusy_data[data.email] = {}; | |
1560 | |
1561 for (i=0; i < data.slots.length; i++) { | |
1562 ts = date2timestring(start, data.interval > 60); | |
1563 status = data.slots.charAt(i); | |
1564 freebusy_data[data.email][ts] = status | |
1565 start = new Date(start.getTime() + data.interval * 60000); | |
1566 | |
1567 // set totals | |
1568 if (!freebusy_data.required[ts]) | |
1569 freebusy_data.required[ts] = [0,0,0,0]; | |
1570 if (req) | |
1571 freebusy_data.required[ts][status]++; | |
1572 | |
1573 if (!freebusy_data.all[ts]) | |
1574 freebusy_data.all[ts] = [0,0,0,0]; | |
1575 freebusy_data.all[ts][status]++; | |
1576 } | |
1577 | |
1578 // hide loading indicator | |
1579 var domid = String(data.email).replace(rcmail.identifier_expr, ''); | |
1580 $('#rcmli' + domid).removeClass('loading'); | |
1581 | |
1582 // update display | |
1583 update_freebusy_display(data.email); | |
1584 } | |
1585 }); | |
1586 | |
1587 // count required attendees | |
1588 if (freebusy_ui.attendees[i].role != 'OPT-PARTICIPANT') | |
1589 freebusy_ui.numrequired++; | |
1590 } | |
1591 } | |
1592 }; | |
1593 | |
1594 // re-calculate total status after role change | |
1595 var compute_freebusy_totals = function() | |
1596 { | |
1597 freebusy_ui.numrequired = 0; | |
1598 freebusy_data.all = []; | |
1599 freebusy_data.required = []; | |
1600 | |
1601 var email, req, status; | |
1602 for (var i=0; i < event_attendees.length; i++) { | |
1603 if (!(email = event_attendees[i].email)) | |
1604 continue; | |
1605 | |
1606 req = freebusy_ui.attendees[i].role != 'OPT-PARTICIPANT'; | |
1607 if (req) | |
1608 freebusy_ui.numrequired++; | |
1609 | |
1610 for (var ts in freebusy_data[email]) { | |
1611 if (!freebusy_data.required[ts]) | |
1612 freebusy_data.required[ts] = [0,0,0,0]; | |
1613 if (!freebusy_data.all[ts]) | |
1614 freebusy_data.all[ts] = [0,0,0,0]; | |
1615 | |
1616 status = freebusy_data[email][ts]; | |
1617 freebusy_data.all[ts][status]++; | |
1618 | |
1619 if (req) | |
1620 freebusy_data.required[ts][status]++; | |
1621 } | |
1622 } | |
1623 }; | |
1624 | |
1625 // update free-busy grid with status loaded from server | |
1626 var update_freebusy_display = function(email) | |
1627 { | |
1628 var status_classes = ['unknown','free','busy','tentative','out-of-office']; | |
1629 var domid = String(email).replace(rcmail.identifier_expr, ''); | |
1630 var row = $('#fbrow' + domid); | |
1631 var rowall = $('#fbrowall').children(); | |
1632 var dateonly = freebusy_ui.interval > 60, | |
1633 t, ts = date2timestring(freebusy_ui.start, dateonly), | |
1634 curdate = new Date(), | |
1635 fbdata = freebusy_data[email]; | |
1636 | |
1637 if (fbdata && fbdata[ts] !== undefined && row.length) { | |
1638 t = freebusy_ui.start.getTime(); | |
1639 row.children().each(function(i, cell) { | |
1640 var j, n, attr, last, all_slots = [], slots = [], | |
1641 all_cell = rowall.get(i), | |
1642 cnt = dateonly ? 1 : (60 / freebusy_ui.interval), | |
1643 percent = (100 / cnt); | |
1644 | |
1645 for (n=0; n < cnt; n++) { | |
1646 curdate.setTime(t); | |
1647 ts = date2timestring(curdate, dateonly); | |
1648 attr = { | |
1649 'style': 'float:left; width:' + percent.toFixed(2) + '%', | |
1650 'class': fbdata[ts] ? status_classes[fbdata[ts]] : 'unknown' | |
1651 }; | |
1652 | |
1653 slots.push($('<div>').attr(attr)); | |
1654 | |
1655 // also update total row if all data was loaded | |
1656 if (!freebusy_ui.loading && freebusy_data.all[ts] && all_cell) { | |
1657 var all_status = freebusy_data.all[ts][2] ? 'busy' : 'unknown', | |
1658 req_status = freebusy_data.required[ts][2] ? 'busy' : 'free'; | |
1659 | |
1660 for (j=1; j < status_classes.length; j++) { | |
1661 if (freebusy_ui.numrequired && freebusy_data.required[ts][j] >= freebusy_ui.numrequired) | |
1662 req_status = status_classes[j]; | |
1663 if (freebusy_data.all[ts][j] == event_attendees.length) | |
1664 all_status = status_classes[j]; | |
1665 } | |
1666 | |
1667 attr['class'] = req_status + ' all-' + all_status; | |
1668 | |
1669 // these elements use some specific styling, so we want to minimize their number | |
1670 if (last && last.attr('class') == attr['class']) | |
1671 last.css('width', (percent + parseFloat(last.css('width').replace('%', ''))).toFixed(2) + '%'); | |
1672 else { | |
1673 last = $('<div>').attr(attr); | |
1674 all_slots.push(last); | |
1675 } | |
1676 } | |
1677 | |
1678 t += freebusy_ui.interval * 60000; | |
1679 } | |
1680 | |
1681 $(cell).html('').append(slots); | |
1682 if (all_slots.length) | |
1683 $(all_cell).html('').append(all_slots); | |
1684 }); | |
1685 } | |
1686 }; | |
1687 | |
1688 // write changed event date/times back to form fields | |
1689 var update_freebusy_dates = function(start, end) | |
1690 { | |
1691 // fix all-day evebt times | |
1692 if (me.selected_event.allDay) { | |
1693 var numdays = Math.floor((me.selected_event.end.getTime() - me.selected_event.start.getTime()) / DAY_MS); | |
1694 start.setHours(12); | |
1695 start.setMinutes(0); | |
1696 end.setTime(start.getTime() + numdays * DAY_MS); | |
1697 end.setHours(13); | |
1698 end.setMinutes(0); | |
1699 } | |
1700 me.selected_event.start = start; | |
1701 me.selected_event.end = end; | |
1702 freebusy_ui.startdate.val($.fullCalendar.formatDate(start, settings['date_format'])); | |
1703 freebusy_ui.starttime.val($.fullCalendar.formatDate(start, settings['time_format'])); | |
1704 freebusy_ui.enddate.val($.fullCalendar.formatDate(end, settings['date_format'])); | |
1705 freebusy_ui.endtime.val($.fullCalendar.formatDate(end, settings['time_format'])); | |
1706 freebusy_ui.needsupdate = true; | |
1707 }; | |
1708 | |
1709 // attempt to find a time slot where all attemdees are available | |
1710 var freebusy_find_slot = function(dir) | |
1711 { | |
1712 // exit if free-busy data isn't available yet | |
1713 if (!freebusy_data || !freebusy_data.start) | |
1714 return false; | |
1715 | |
1716 var event = me.selected_event, | |
1717 eventstart = clone_date(event.start, event.allDay ? 1 : 0).getTime(), // calculate with integers | |
1718 eventend = clone_date(event.end, event.allDay ? 2 : 0).getTime(), | |
1719 duration = eventend - eventstart - (event.allDay ? HOUR_MS : 0), /* make sure we don't cross day borders on DST change */ | |
1720 sinterval = freebusy_data.interval * 60000, | |
1721 intvlslots = 1, | |
1722 numslots = Math.ceil(duration / sinterval), | |
1723 fb_start = freebusy_data.start.getTime(), | |
1724 fb_end = freebusy_data.end.getTime(), | |
1725 checkdate, slotend, email, ts, slot, slotdate = new Date(), | |
1726 candidatecount = 0, candidatestart = false, success = false; | |
1727 | |
1728 // shift event times to next possible slot | |
1729 eventstart += sinterval * intvlslots * dir; | |
1730 eventend += sinterval * intvlslots * dir; | |
1731 | |
1732 // iterate through free-busy slots and find candidates | |
1733 for (slot = dir > 0 ? fb_start : fb_end - sinterval; | |
1734 (dir > 0 && slot < fb_end) || (dir < 0 && slot >= fb_start); | |
1735 slot += sinterval * dir | |
1736 ) { | |
1737 slotdate.setTime(slot); | |
1738 // fix slot if just crossed a DST change | |
1739 if (event.allDay) { | |
1740 fix_date(slotdate); | |
1741 slot = slotdate.getTime(); | |
1742 } | |
1743 slotend = slot + sinterval; | |
1744 | |
1745 if ((dir > 0 && slotend <= eventstart) || (dir < 0 && slot >= eventend)) // skip | |
1746 continue; | |
1747 | |
1748 // respect workinghours setting | |
1749 if (freebusy_ui.workinhoursonly) { | |
1750 if (is_weekend(slotdate) || (freebusy_data.interval <= 60 && !is_workinghour(slotdate))) { // skip off-hours | |
1751 candidatestart = false; | |
1752 candidatecount = 0; | |
1753 continue; | |
1754 } | |
1755 } | |
1756 | |
1757 if (!candidatestart) | |
1758 candidatestart = slot; | |
1759 | |
1760 ts = date2timestring(slotdate, freebusy_data.interval > 60); | |
1761 | |
1762 // check freebusy data for all attendees | |
1763 for (var i=0; i < event_attendees.length; i++) { | |
1764 if (freebusy_ui.attendees[i].role != 'OPT-PARTICIPANT' && (email = freebusy_ui.attendees[i].email) && freebusy_data[email] && freebusy_data[email][ts] > 1) { | |
1765 candidatestart = false; | |
1766 break; | |
1767 } | |
1768 } | |
1769 | |
1770 // occupied slot | |
1771 if (!candidatestart) { | |
1772 slot += Math.max(0, intvlslots - candidatecount - 1) * sinterval * dir; | |
1773 candidatecount = 0; | |
1774 continue; | |
1775 } | |
1776 else if (dir < 0) | |
1777 candidatestart = slot; | |
1778 | |
1779 candidatecount++; | |
1780 | |
1781 // if candidate is big enough, this is it! | |
1782 if (candidatecount == numslots) { | |
1783 event.start.setTime(candidatestart); | |
1784 event.end.setTime(candidatestart + duration); | |
1785 success = true; | |
1786 break; | |
1787 } | |
1788 } | |
1789 | |
1790 // update event date/time display | |
1791 if (success) { | |
1792 update_freebusy_dates(event.start, event.end); | |
1793 | |
1794 // move freebusy grid if necessary | |
1795 var offset = Math.ceil((event.start.getTime() - freebusy_ui.end.getTime()) / DAY_MS); | |
1796 if (event.start.getTime() >= freebusy_ui.end.getTime()) | |
1797 render_freebusy_grid(Math.max(1, offset)); | |
1798 else if (event.end.getTime() <= freebusy_ui.start.getTime()) | |
1799 render_freebusy_grid(Math.min(-1, offset)); | |
1800 else | |
1801 render_freebusy_overlay(); | |
1802 | |
1803 var now = new Date(); | |
1804 $('#shedule-find-prev').button('option', 'disabled', (event.start.getTime() < now.getTime())); | |
1805 | |
1806 // speak new selection | |
1807 rcmail.display_message(rcmail.gettext('suggestedslot', 'calendar') + ': ' + me.event_date_text(event, true), 'voice'); | |
1808 } | |
1809 else { | |
1810 alert(rcmail.gettext('noslotfound','calendar')); | |
1811 } | |
1812 }; | |
1813 | |
1814 // update event properties and attendees availability if event times have changed | |
1815 var event_times_changed = function() | |
1816 { | |
1817 if (me.selected_event) { | |
1818 var allday = $('#edit-allday').get(0); | |
1819 me.selected_event.allDay = allday.checked; | |
1820 me.selected_event.start = parse_datetime(allday.checked ? '12:00' : $('#edit-starttime').val(), $('#edit-startdate').val()); | |
1821 me.selected_event.end = parse_datetime(allday.checked ? '13:00' : $('#edit-endtime').val(), $('#edit-enddate').val()); | |
1822 if (event_attendees) | |
1823 freebusy_ui.needsupdate = true; | |
1824 $('#edit-startdate').data('duration', Math.round((me.selected_event.end.getTime() - me.selected_event.start.getTime()) / 1000)); | |
1825 } | |
1826 }; | |
1827 | |
1828 // add the given list of participants | |
1829 var add_attendees = function(names, params) | |
1830 { | |
1831 names = explode_quoted_string(names.replace(/,\s*$/, ''), ','); | |
1832 | |
1833 // parse name/email pairs | |
1834 var item, email, name, success = false; | |
1835 for (var i=0; i < names.length; i++) { | |
1836 email = name = ''; | |
1837 item = $.trim(names[i]); | |
1838 | |
1839 if (!item.length) { | |
1840 continue; | |
1841 } // address in brackets without name (do nothing) | |
1842 else if (item.match(/^<[^@]+@[^>]+>$/)) { | |
1843 email = item.replace(/[<>]/g, ''); | |
1844 } // address without brackets and without name (add brackets) | |
1845 else if (rcube_check_email(item)) { | |
1846 email = item; | |
1847 } // address with name | |
1848 else if (item.match(/([^\s<@]+@[^>]+)>*$/)) { | |
1849 email = RegExp.$1; | |
1850 name = item.replace(email, '').replace(/^["\s<>]+/, '').replace(/["\s<>]+$/, ''); | |
1851 } | |
1852 if (email) { | |
1853 add_attendee($.extend({ email:email, name:name }, params)); | |
1854 success = true; | |
1855 } | |
1856 else { | |
1857 alert(rcmail.gettext('noemailwarning')); | |
1858 } | |
1859 } | |
1860 | |
1861 return success; | |
1862 }; | |
1863 | |
1864 // add the given attendee to the list | |
1865 var add_attendee = function(data, readonly, before) | |
1866 { | |
1867 if (!me.selected_event) | |
1868 return false; | |
1869 | |
1870 // check for dupes... | |
1871 var exists = false; | |
1872 $.each(event_attendees, function(i, v){ exists |= (v.email == data.email); }); | |
1873 if (exists) | |
1874 return false; | |
1875 | |
1876 var calendar = me.selected_event && me.calendars[me.selected_event.calendar] ? me.calendars[me.selected_event.calendar] : me.calendars[me.selected_calendar]; | |
1877 | |
1878 var dispname = Q(data.name || data.email); | |
1879 if (data.email) | |
1880 dispname = '<a href="mailto:' + data.email + '" title="' + Q(data.email) + '" class="mailtolink" data-cutype="' + data.cutype + '">' + dispname + '</a>'; | |
1881 | |
1882 // role selection | |
1883 var organizer = data.role == 'ORGANIZER'; | |
1884 var opts = {}; | |
1885 if (organizer) | |
1886 opts.ORGANIZER = rcmail.gettext('calendar.roleorganizer'); | |
1887 opts['REQ-PARTICIPANT'] = rcmail.gettext('calendar.rolerequired'); | |
1888 opts['OPT-PARTICIPANT'] = rcmail.gettext('calendar.roleoptional'); | |
1889 opts['NON-PARTICIPANT'] = rcmail.gettext('calendar.rolenonparticipant'); | |
1890 | |
1891 if (data.cutype != 'RESOURCE') | |
1892 opts['CHAIR'] = rcmail.gettext('calendar.rolechair'); | |
1893 | |
1894 if (organizer && !readonly) | |
1895 dispname = rcmail.env['identities-selector']; | |
1896 | |
1897 var select = '<select class="edit-attendee-role"' + (organizer || readonly ? ' disabled="true"' : '') + ' aria-label="' + rcmail.gettext('role','calendar') + '">'; | |
1898 for (var r in opts) | |
1899 select += '<option value="'+ r +'" class="' + r.toLowerCase() + '"' + (data.role == r ? ' selected="selected"' : '') +'>' + Q(opts[r]) + '</option>'; | |
1900 select += '</select>'; | |
1901 | |
1902 // availability | |
1903 var avail = data.email ? 'loading' : 'unknown'; | |
1904 | |
1905 // delete icon | |
1906 var icon = rcmail.env.deleteicon ? '<img src="' + rcmail.env.deleteicon + '" alt="" />' : rcmail.gettext('delete'); | |
1907 var dellink = '<a href="#delete" class="iconlink delete deletelink" title="' + Q(rcmail.gettext('delete')) + '">' + icon + '</a>'; | |
1908 var tooltip = '', status = (data.status || '').toLowerCase(), | |
1909 status_label = rcmail.gettext('status' + status, 'libcalendaring'); | |
1910 | |
1911 // send invitation checkbox | |
1912 var invbox = '<input type="checkbox" class="edit-attendee-reply" value="' + Q(data.email) +'" title="' + Q(rcmail.gettext('calendar.sendinvitations')) + '" ' | |
1913 + (!data.noreply && settings.itip_notify & 1 ? 'checked="checked" ' : '') + '/>'; | |
1914 | |
1915 if (data['delegated-to']) | |
1916 tooltip = rcmail.gettext('libcalendaring.delegatedto') + ' ' + data['delegated-to']; | |
1917 else if (data['delegated-from']) | |
1918 tooltip = rcmail.gettext('libcalendaring.delegatedfrom') + ' ' + data['delegated-from']; | |
1919 else if (status) | |
1920 tooltip = status_label; | |
1921 | |
1922 // add expand button for groups | |
1923 if (data.cutype == 'GROUP') { | |
1924 dispname += ' <a href="#expand" data-email="' + Q(data.email) + '" class="iconbutton add expandlink" title="' + rcmail.gettext('expandattendeegroup','libcalendaring') + '">' + | |
1925 rcmail.gettext('expandattendeegroup','libcalendaring') + '</a>'; | |
1926 } | |
1927 | |
1928 var img_src = rcmail.assets_path('program/resources/blank.gif'); | |
1929 var html = '<td class="role">' + select + '</td>' + | |
1930 '<td class="name"><span class="attendee-name">' + dispname + '</span></td>' + | |
1931 '<td class="availability"><img src="' + img_src + '" class="availabilityicon ' + avail + '" data-email="' + data.email + '" alt="" /></td>' + | |
1932 '<td class="confirmstate"><span class="' + status + '" title="' + Q(tooltip) + '">' + Q(status ? status_label : '') + '</span></td>' + | |
1933 (data.cutype != 'RESOURCE' ? '<td class="invite">' + (organizer || readonly || !invbox ? '' : invbox) + '</td>' : '') + | |
1934 '<td class="options">' + (organizer || readonly ? '' : dellink) + '</td>'; | |
1935 | |
1936 var table = rcmail.env.calendar_resources && data.cutype == 'RESOURCE' ? resources_list : attendees_list; | |
1937 var tr = $('<tr>') | |
1938 .addClass(String(data.role).toLowerCase()) | |
1939 .html(html); | |
1940 | |
1941 if (before) | |
1942 tr.insertBefore(before) | |
1943 else | |
1944 tr.appendTo(table); | |
1945 | |
1946 tr.find('a.deletelink').click({ id:(data.email || data.name) }, function(e) { remove_attendee(this, e.data.id); return false; }); | |
1947 tr.find('a.mailtolink').click(event_attendee_click); | |
1948 tr.find('a.expandlink').click(data, function(e) { me.expand_attendee_group(e, add_attendee, remove_attendee); return false; }); | |
1949 tr.find('input.edit-attendee-reply').click(function() { | |
1950 var enabled = $('#edit-attendees-invite:checked').length || $('input.edit-attendee-reply:checked').length; | |
1951 $('#eventedit .attendees-commentbox')[enabled ? 'show' : 'hide'](); | |
1952 }); | |
1953 | |
1954 // select organizer identity | |
1955 if (data.identity_id) | |
1956 $('#edit-identities-list').val(data.identity_id); | |
1957 | |
1958 // check free-busy status | |
1959 if (avail == 'loading') { | |
1960 check_freebusy_status(tr.find('img.availabilityicon'), data.email, me.selected_event); | |
1961 } | |
1962 | |
1963 event_attendees.push(data); | |
1964 return true; | |
1965 }; | |
1966 | |
1967 // iterate over all attendees and update their free-busy status display | |
1968 var update_freebusy_status = function(event) | |
1969 { | |
1970 attendees_list.find('img.availabilityicon').each(function(i,v) { | |
1971 var email, icon = $(this); | |
1972 if (email = icon.attr('data-email')) | |
1973 check_freebusy_status(icon, email, event); | |
1974 }); | |
1975 | |
1976 freebusy_ui.needsupdate = false; | |
1977 }; | |
1978 | |
1979 // load free-busy status from server and update icon accordingly | |
1980 var check_freebusy_status = function(icon, email, event) | |
1981 { | |
1982 var calendar = event.calendar && me.calendars[event.calendar] ? me.calendars[event.calendar] : { freebusy:false }; | |
1983 if (!calendar.freebusy) { | |
1984 $(icon).attr('class', 'availabilityicon unknown'); | |
1985 return; | |
1986 } | |
1987 | |
1988 icon = $(icon).attr('class', 'availabilityicon loading'); | |
1989 | |
1990 $.ajax({ | |
1991 type: 'GET', | |
1992 dataType: 'html', | |
1993 url: rcmail.url('freebusy-status'), | |
1994 data: { email:email, start:date2servertime(clone_date(event.start, event.allDay?1:0)), end:date2servertime(clone_date(event.end, event.allDay?2:0)), _remote: 1 }, | |
1995 success: function(status){ | |
1996 var avail = String(status).toLowerCase(); | |
1997 icon.removeClass('loading').addClass(avail).attr('alt', rcmail.gettext('avail' + avail, 'calendar')); | |
1998 }, | |
1999 error: function(){ | |
2000 icon.removeClass('loading').addClass('unknown').attr('alt', rcmail.gettext('availunknown', 'calendar')); | |
2001 } | |
2002 }); | |
2003 }; | |
2004 | |
2005 // remove an attendee from the list | |
2006 var remove_attendee = function(elem, id) | |
2007 { | |
2008 $(elem).closest('tr').remove(); | |
2009 event_attendees = $.grep(event_attendees, function(data){ return (data.name != id && data.email != id) }); | |
2010 }; | |
2011 | |
2012 // open a dialog to display detailed free-busy information and to find free slots | |
2013 var event_resources_dialog = function(search) | |
2014 { | |
2015 var $dialog = $('#eventresourcesdialog'); | |
2016 | |
2017 if ($dialog.is(':ui-dialog')) | |
2018 $dialog.dialog('close'); | |
2019 | |
2020 // dialog buttons | |
2021 var buttons = {}; | |
2022 | |
2023 buttons[rcmail.gettext('addresource', 'calendar')] = function() { | |
2024 rcmail.command('add-resource'); | |
2025 }; | |
2026 | |
2027 buttons[rcmail.gettext('close')] = function() { | |
2028 $dialog.dialog("close"); | |
2029 }; | |
2030 | |
2031 // open jquery UI dialog | |
2032 $dialog.dialog({ | |
2033 modal: true, | |
2034 resizable: true, | |
2035 closeOnEscape: true, | |
2036 title: rcmail.gettext('findresources', 'calendar'), | |
2037 open: function() { | |
2038 rcmail.ksearch_blur(); | |
2039 $dialog.attr('aria-hidden', 'false'); | |
2040 }, | |
2041 close: function() { | |
2042 $dialog.dialog('destroy').attr('aria-hidden', 'true').hide(); | |
2043 }, | |
2044 resize: function(e) { | |
2045 var container = $(rcmail.gui_objects.resourceinfocalendar); | |
2046 container.fullCalendar('option', 'height', container.height() + 4); | |
2047 }, | |
2048 buttons: buttons, | |
2049 width: 900, | |
2050 height: 500 | |
2051 }).show(); | |
2052 | |
2053 // define add-button as main action | |
2054 $('.ui-dialog-buttonset .ui-button', $dialog.parent()).first().addClass('mainaction').attr('id', 'rcmbtncalresadd'); | |
2055 | |
2056 me.dialog_resize($dialog.get(0), 540, Math.min(1000, $(window).width() - 50)); | |
2057 | |
2058 // set search query | |
2059 $('#resourcesearchbox').val(search || ''); | |
2060 | |
2061 // initialize the treelist widget | |
2062 if (!resources_treelist) { | |
2063 resources_treelist = new rcube_treelist_widget(rcmail.gui_objects.resourceslist, { | |
2064 id_prefix: 'rcres', | |
2065 id_encode: rcmail.html_identifier_encode, | |
2066 id_decode: rcmail.html_identifier_decode, | |
2067 selectable: true, | |
2068 save_state: true | |
2069 }); | |
2070 resources_treelist.addEventListener('select', function(node) { | |
2071 if (resources_data[node.id]) { | |
2072 resource_showinfo(resources_data[node.id]); | |
2073 rcmail.enable_command('add-resource', me.selected_event && $("#eventedit").is(':visible') ? true : false); | |
2074 } | |
2075 else { | |
2076 rcmail.enable_command('add-resource', false); | |
2077 $(rcmail.gui_objects.resourceinfo).hide(); | |
2078 $(rcmail.gui_objects.resourceownerinfo).hide(); | |
2079 $(rcmail.gui_objects.resourceinfocalendar).fullCalendar('removeEventSource', resources_events_source); | |
2080 } | |
2081 }); | |
2082 | |
2083 // fetch (all) resource data from server | |
2084 me.loading_lock = rcmail.set_busy(true, 'loading', me.loading_lock); | |
2085 rcmail.http_request('resources-list', {}, me.loading_lock); | |
2086 | |
2087 // register button | |
2088 rcmail.register_button('add-resource', 'rcmbtncalresadd', 'uibutton'); | |
2089 | |
2090 // initialize resource calendar display | |
2091 var resource_cal = $(rcmail.gui_objects.resourceinfocalendar); | |
2092 resource_cal.fullCalendar($.extend({}, fullcalendar_defaults, { | |
2093 header: { left: '', center: '', right: '' }, | |
2094 height: resource_cal.height() + 4, | |
2095 defaultView: 'agendaWeek', | |
2096 eventSources: [], | |
2097 slotMinutes: 60, | |
2098 allDaySlot: false, | |
2099 eventRender: function(event, element, view) { | |
2100 var title = rcmail.get_label(event.status, 'calendar'); | |
2101 element.addClass('status-' + event.status); | |
2102 element.find('.fc-event-head').hide(); | |
2103 element.find('.fc-event-title').text(title); | |
2104 element.attr('aria-label', me.event_date_text(event, true) + ': ' + title); | |
2105 } | |
2106 })); | |
2107 | |
2108 $('#resource-calendar-prev').click(function(){ | |
2109 resource_cal.fullCalendar('prev'); | |
2110 return false; | |
2111 }); | |
2112 $('#resource-calendar-next').click(function(){ | |
2113 resource_cal.fullCalendar('next'); | |
2114 return false; | |
2115 }); | |
2116 } | |
2117 else if (search) { | |
2118 resource_search(); | |
2119 } | |
2120 else { | |
2121 resource_render_list(resources_index); | |
2122 } | |
2123 | |
2124 if (me.selected_event && me.selected_event.start) { | |
2125 $(rcmail.gui_objects.resourceinfocalendar).fullCalendar('gotoDate', me.selected_event.start); | |
2126 } | |
2127 }; | |
2128 | |
2129 // render the resource details UI box | |
2130 var resource_showinfo = function(resource) | |
2131 { | |
2132 // inline function to render a resource attribute | |
2133 function render_attrib(value) { | |
2134 if (typeof value == 'boolean') { | |
2135 return value ? rcmail.get_label('yes') : rcmail.get_label('no'); | |
2136 } | |
2137 | |
2138 return value; | |
2139 } | |
2140 | |
2141 if (rcmail.gui_objects.resourceinfo) { | |
2142 var tr, table = $(rcmail.gui_objects.resourceinfo).show().find('tbody').html(''), | |
2143 attribs = $.extend({ name:resource.name }, resource.attributes||{}) | |
2144 attribs.description = resource.description; | |
2145 | |
2146 for (var k in attribs) { | |
2147 if (typeof attribs[k] == 'undefined') | |
2148 continue; | |
2149 table.append($('<tr>').addClass(k) | |
2150 .append('<td class="title">' + Q(ucfirst(rcmail.get_label(k, 'calendar'))) + '</td>') | |
2151 .append('<td class="value">' + text2html(render_attrib(attribs[k])) + '</td>') | |
2152 ); | |
2153 } | |
2154 | |
2155 $(rcmail.gui_objects.resourceownerinfo).hide(); | |
2156 $(rcmail.gui_objects.resourceinfocalendar).fullCalendar('removeEventSource', resources_events_source); | |
2157 | |
2158 if (resource.owner) { | |
2159 // display cached data | |
2160 if (resource_owners[resource.owner]) { | |
2161 resource_owner_load(resource_owners[resource.owner]); | |
2162 } | |
2163 else { | |
2164 // fetch owner data from server | |
2165 me.loading_lock = rcmail.set_busy(true, 'loading', me.loading_lock); | |
2166 rcmail.http_request('resources-owner', { _id: resource.owner }, me.loading_lock); | |
2167 } | |
2168 } | |
2169 | |
2170 // load resource calendar | |
2171 resources_events_source.url = "./?_task=calendar&_action=resources-calendar&_id="+urlencode(resource.ID); | |
2172 $(rcmail.gui_objects.resourceinfocalendar).fullCalendar('addEventSource', resources_events_source); | |
2173 } | |
2174 }; | |
2175 | |
2176 // callback from server for resource listing | |
2177 var resource_data_load = function(data) | |
2178 { | |
2179 var resources_tree = {}; | |
2180 | |
2181 // store data by ID | |
2182 $.each(data, function(i, rec) { | |
2183 resources_data[rec.ID] = rec; | |
2184 | |
2185 // assign parent-relations | |
2186 if (rec.members) { | |
2187 $.each(rec.members, function(j, m){ | |
2188 resources_tree[m] = rec.ID; | |
2189 }); | |
2190 } | |
2191 }); | |
2192 | |
2193 // walk the parent-child tree to determine the depth of each node | |
2194 $.each(data, function(i, rec) { | |
2195 rec._depth = 0; | |
2196 if (resources_tree[rec.ID]) | |
2197 rec.parent_id = resources_tree[rec.ID]; | |
2198 | |
2199 var parent_id = resources_tree[rec.ID]; | |
2200 while (parent_id) { | |
2201 rec._depth++; | |
2202 parent_id = resources_tree[parent_id]; | |
2203 } | |
2204 }); | |
2205 | |
2206 // sort by depth, collection and name | |
2207 data.sort(function(a,b) { | |
2208 var j = a._type == 'collection' ? 1 : 0, | |
2209 k = b._type == 'collection' ? 1 : 0, | |
2210 d = a._depth - b._depth; | |
2211 if (!d) d = (k - j); | |
2212 if (!d) d = b.name < a.name ? 1 : -1; | |
2213 return d; | |
2214 }); | |
2215 | |
2216 $.each(data, function(i, rec) { | |
2217 resources_index.push(rec.ID); | |
2218 }); | |
2219 | |
2220 // apply search filter... | |
2221 if ($('#resourcesearchbox').val() != '') | |
2222 resource_search(); | |
2223 else // ...or render full list | |
2224 resource_render_list(resources_index); | |
2225 | |
2226 rcmail.set_busy(false, null, me.loading_lock); | |
2227 }; | |
2228 | |
2229 // renders the given list of resource records into the treelist | |
2230 var resource_render_list = function(index) { | |
2231 var rec, link; | |
2232 | |
2233 resources_treelist.reset(); | |
2234 | |
2235 $.each(index, function(i, dn) { | |
2236 if (rec = resources_data[dn]) { | |
2237 link = $('<a>').attr('href', '#') | |
2238 .attr('rel', rec.ID) | |
2239 .html(Q(rec.name)); | |
2240 | |
2241 resources_treelist.insert({ id:rec.ID, html:link, classes:[rec._type], collapsed:true }, rec.parent_id, false); | |
2242 } | |
2243 }); | |
2244 }; | |
2245 | |
2246 // callback from server for owner information display | |
2247 var resource_owner_load = function(data) | |
2248 { | |
2249 if (data) { | |
2250 // cache this! | |
2251 resource_owners[data.ID] = data; | |
2252 | |
2253 var table = $(rcmail.gui_objects.resourceownerinfo).find('tbody').html(''); | |
2254 | |
2255 for (var k in data) { | |
2256 if (k == 'event' || k == 'ID') | |
2257 continue; | |
2258 | |
2259 table.append($('<tr>').addClass(k) | |
2260 .append('<td class="title">' + Q(ucfirst(rcmail.get_label(k, 'calendar'))) + '</td>') | |
2261 .append('<td class="value">' + text2html(data[k]) + '</td>') | |
2262 ); | |
2263 } | |
2264 | |
2265 table.parent().show(); | |
2266 } | |
2267 } | |
2268 | |
2269 // quick-filter the loaded resource data | |
2270 var resource_search = function() | |
2271 { | |
2272 var dn, rec, dataset = [], | |
2273 q = $('#resourcesearchbox').val().toLowerCase(); | |
2274 | |
2275 if (q.length && resources_data) { | |
2276 // search by iterating over all resource records | |
2277 for (dn in resources_data) { | |
2278 rec = resources_data[dn]; | |
2279 if ((rec.name && String(rec.name).toLowerCase().indexOf(q) >= 0) | |
2280 || (rec.email && String(rec.email).toLowerCase().indexOf(q) >= 0) | |
2281 || (rec.description && String(rec.description).toLowerCase().indexOf(q) >= 0) | |
2282 ) { | |
2283 dataset.push(rec.ID); | |
2284 } | |
2285 } | |
2286 | |
2287 resource_render_list(dataset); | |
2288 | |
2289 // select single match | |
2290 if (dataset.length == 1) { | |
2291 resources_treelist.select(dataset[0]); | |
2292 } | |
2293 } | |
2294 else { | |
2295 $('#resourcesearchbox').val(''); | |
2296 } | |
2297 }; | |
2298 | |
2299 // | |
2300 var reset_resource_search = function() | |
2301 { | |
2302 $('#resourcesearchbox').val('').focus(); | |
2303 resource_render_list(resources_index); | |
2304 }; | |
2305 | |
2306 // | |
2307 var add_resource2event = function() | |
2308 { | |
2309 var resource = resources_data[resources_treelist.get_selection()]; | |
2310 if (resource) { | |
2311 if (add_attendee($.extend({ role:'REQ-PARTICIPANT', status:'NEEDS-ACTION', cutype:'RESOURCE' }, resource))) | |
2312 rcmail.display_message(rcmail.get_label('resourceadded', 'calendar'), 'confirmation'); | |
2313 } | |
2314 } | |
2315 | |
2316 // when the user accepts or declines an event invitation | |
2317 var event_rsvp = function(response, delegate, replymode) | |
2318 { | |
2319 var btn; | |
2320 if (typeof response == 'object') { | |
2321 btn = $(response); | |
2322 response = btn.attr('rel') | |
2323 } | |
2324 else { | |
2325 btn = $('#event-rsvp input.button[rel='+response+']'); | |
2326 } | |
2327 | |
2328 // show menu to select rsvp reply mode (current or all) | |
2329 if (me.selected_event && me.selected_event.recurrence && !replymode) { | |
2330 rcube_libcalendaring.itip_rsvp_recurring(btn, function(resp, mode) { | |
2331 event_rsvp(resp, null, mode); | |
2332 }); | |
2333 return; | |
2334 } | |
2335 | |
2336 if (me.selected_event && me.selected_event.attendees && response) { | |
2337 // bring up delegation dialog | |
2338 if (response == 'delegated' && !delegate) { | |
2339 rcube_libcalendaring.itip_delegate_dialog(function(data) { | |
2340 data.rsvp = data.rsvp ? 1 : ''; | |
2341 event_rsvp('delegated', data, replymode); | |
2342 }); | |
2343 return; | |
2344 } | |
2345 | |
2346 // update attendee status | |
2347 attendees = []; | |
2348 for (var data, i=0; i < me.selected_event.attendees.length; i++) { | |
2349 data = me.selected_event.attendees[i]; | |
2350 if (settings.identity.emails.indexOf(';'+String(data.email).toLowerCase()) >= 0) { | |
2351 data.status = response.toUpperCase(); | |
2352 data.rsvp = 0; // unset RSVP flag | |
2353 | |
2354 if (data.status == 'DELEGATED') { | |
2355 data['delegated-to'] = delegate.to; | |
2356 data.rsvp = delegate.rsvp | |
2357 } | |
2358 else { | |
2359 if (data['delegated-to']) { | |
2360 delete data['delegated-to']; | |
2361 if (data.role == 'NON-PARTICIPANT' && data.status != 'DECLINED') | |
2362 data.role = 'REQ-PARTICIPANT'; | |
2363 } | |
2364 } | |
2365 | |
2366 attendees.push(i) | |
2367 } | |
2368 else if (response != 'DELEGATED' && data['delegated-from'] && | |
2369 settings.identity.emails.indexOf(';'+String(data['delegated-from']).toLowerCase()) >= 0) { | |
2370 delete data['delegated-from']; | |
2371 } | |
2372 | |
2373 // set free_busy status to transparent if declined (#4425) | |
2374 if (data.status == 'DECLINED' || data.role == 'NON-PARTICIPANT') { | |
2375 me.selected_event.free_busy = 'free'; | |
2376 } | |
2377 else { | |
2378 me.selected_event.free_busy = 'busy'; | |
2379 } | |
2380 } | |
2381 | |
2382 // submit status change to server | |
2383 var submit_data = $.extend({}, me.selected_event, { source:null, comment:$('#reply-comment-event-rsvp').val(), _savemode: replymode || 'all' }, (delegate || {})), | |
2384 noreply = $('#noreply-event-rsvp:checked').length ? 1 : 0; | |
2385 | |
2386 // import event from mail (temporary iTip event) | |
2387 if (submit_data._mbox && submit_data._uid) { | |
2388 me.saving_lock = rcmail.set_busy(true, 'calendar.savingdata'); | |
2389 rcmail.http_post('mailimportitip', { | |
2390 _mbox: submit_data._mbox, | |
2391 _uid: submit_data._uid, | |
2392 _part: submit_data._part, | |
2393 _status: response, | |
2394 _to: (delegate ? delegate.to : null), | |
2395 _rsvp: (delegate && delegate.rsvp) ? 1 : 0, | |
2396 _noreply: noreply, | |
2397 _comment: submit_data.comment, | |
2398 _instance: submit_data._instance, | |
2399 _savemode: submit_data._savemode | |
2400 }); | |
2401 } | |
2402 else if (settings.invitation_calendars) { | |
2403 update_event('rsvp', submit_data, { status:response, noreply:noreply, attendees:attendees }); | |
2404 } | |
2405 else { | |
2406 me.saving_lock = rcmail.set_busy(true, 'calendar.savingdata'); | |
2407 rcmail.http_post('event', { action:'rsvp', e:submit_data, status:response, attendees:attendees, noreply:noreply }); | |
2408 } | |
2409 | |
2410 event_show_dialog(me.selected_event); | |
2411 } | |
2412 }; | |
2413 | |
2414 // add the given date to the RDATE list | |
2415 var add_rdate = function(date) | |
2416 { | |
2417 var li = $('<li>') | |
2418 .attr('data-value', date2servertime(date)) | |
2419 .html('<span>' + Q($.fullCalendar.formatDate(date, settings['date_format'])) + '</span>') | |
2420 .appendTo('#edit-recurrence-rdates'); | |
2421 | |
2422 $('<a>').attr('href', '#del') | |
2423 .addClass('iconbutton delete') | |
2424 .html(rcmail.get_label('delete', 'calendar')) | |
2425 .attr('title', rcmail.get_label('delete', 'calendar')) | |
2426 .appendTo(li); | |
2427 }; | |
2428 | |
2429 // re-sort the list items by their 'data-value' attribute | |
2430 var sort_rdates = function() | |
2431 { | |
2432 var mylist = $('#edit-recurrence-rdates'), | |
2433 listitems = mylist.children('li').get(); | |
2434 listitems.sort(function(a, b) { | |
2435 var compA = $(a).attr('data-value'); | |
2436 var compB = $(b).attr('data-value'); | |
2437 return (compA < compB) ? -1 : (compA > compB) ? 1 : 0; | |
2438 }) | |
2439 $.each(listitems, function(idx, item) { mylist.append(item); }); | |
2440 } | |
2441 | |
2442 // remove the link reference matching the given uri | |
2443 function remove_link(elem) | |
2444 { | |
2445 var $elem = $(elem), uri = $elem.attr('data-uri'); | |
2446 | |
2447 me.selected_event.links = $.grep(me.selected_event.links, function(link) { return link.uri != uri; }); | |
2448 | |
2449 // remove UI list item | |
2450 $elem.hide().closest('li').addClass('deleted'); | |
2451 } | |
2452 | |
2453 // post the given event data to server | |
2454 var update_event = function(action, data, add) | |
2455 { | |
2456 me.saving_lock = rcmail.set_busy(true, 'calendar.savingdata'); | |
2457 rcmail.http_post('calendar/event', $.extend({ action:action, e:data }, (add || {}))); | |
2458 | |
2459 // render event temporarily into the calendar | |
2460 if ((data.start && data.end) || data.id) { | |
2461 var event = data.id ? $.extend(fc.fullCalendar('clientEvents', function(e){ return e.id == data.id; })[0], data) : data; | |
2462 if (data.start) | |
2463 event.start = data.start; | |
2464 if (data.end) | |
2465 event.end = data.end; | |
2466 if (data.allday !== undefined) | |
2467 event.allDay = data.allday; | |
2468 event.editable = false; | |
2469 event.temp = true; | |
2470 event.className = 'fc-event-cal-'+data.calendar+' fc-event-temp'; | |
2471 fc.fullCalendar(data.id ? 'updateEvent' : 'renderEvent', event); | |
2472 | |
2473 // mark all recurring instances as temp | |
2474 if (event.recurrence || event.recurrence_id) { | |
2475 var base_id = event.recurrence_id ? event.recurrence_id : String(event.id).replace(/-\d+(T\d{6})?$/, ''); | |
2476 $.each(fc.fullCalendar('clientEvents', function(e){ return e.id == base_id || e.recurrence_id == base_id; }), function(i,ev) { | |
2477 ev.temp = true; | |
2478 ev.editable = false; | |
2479 event.className += ' fc-event-temp'; | |
2480 fc.fullCalendar('updateEvent', ev); | |
2481 }); | |
2482 } | |
2483 } | |
2484 }; | |
2485 | |
2486 // mouse-click handler to check if the show dialog is still open and prevent default action | |
2487 var dialog_check = function(e) | |
2488 { | |
2489 var showd = $("#eventshow"); | |
2490 if (showd.is(':visible') && !$(e.target).closest('.ui-dialog').length && !$(e.target).closest('.popupmenu').length) { | |
2491 showd.dialog('close'); | |
2492 e.stopImmediatePropagation(); | |
2493 ignore_click = true; | |
2494 return false; | |
2495 } | |
2496 else if (ignore_click) { | |
2497 window.setTimeout(function(){ ignore_click = false; }, 20); | |
2498 return false; | |
2499 } | |
2500 return true; | |
2501 }; | |
2502 | |
2503 // display confirm dialog when modifying/deleting an event | |
2504 var update_event_confirm = function(action, event, data) | |
2505 { | |
2506 // Allow other plugins to do actions here | |
2507 // E.g. when you move/resize the event init wasn't called | |
2508 // but we need it as some plugins may modify user identities | |
2509 // we depend on here (kolab_delegation) | |
2510 rcmail.triggerEvent('calendar-event-init', {o: event}); | |
2511 | |
2512 if (!data) data = event; | |
2513 var decline = false, notify = false, html = '', cal = me.calendars[event.calendar], | |
2514 _has_attendees = me.has_attendees(event), | |
2515 _is_attendee = _has_attendees && me.is_attendee(event), | |
2516 _is_organizer = me.is_organizer(event); | |
2517 | |
2518 // event has attendees, ask whether to notify them | |
2519 if (_has_attendees) { | |
2520 var checked = (settings.itip_notify & 1 ? ' checked="checked"' : ''); | |
2521 | |
2522 if (action == 'remove' && cal.group != 'shared' && !_is_organizer && _is_attendee) { | |
2523 decline = true; | |
2524 checked = event.status != 'CANCELLED' ? checked : ''; | |
2525 html += '<div class="message">' + | |
2526 '<label><input class="confirm-attendees-decline" type="checkbox"' + checked + ' value="1" name="decline" /> ' + | |
2527 rcmail.gettext('itipdeclineevent', 'calendar') + | |
2528 '</label></div>'; | |
2529 } | |
2530 else if (_is_organizer) { | |
2531 notify = true; | |
2532 if (settings.itip_notify & 2) { | |
2533 html += '<div class="message">' + | |
2534 '<label><input class="confirm-attendees-donotify" type="checkbox"' + checked + ' value="1" name="notify" /> ' + | |
2535 rcmail.gettext((action == 'remove' ? 'sendcancellation' : 'sendnotifications'), 'calendar') + | |
2536 '</label></div>'; | |
2537 } | |
2538 else { | |
2539 data._notify = settings.itip_notify; | |
2540 } | |
2541 } | |
2542 } | |
2543 | |
2544 // recurring event: user needs to select the savemode | |
2545 if (event.recurrence) { | |
2546 var future_disabled = '', message_label = (action == 'remove' ? 'removerecurringeventwarning' : 'changerecurringeventwarning'); | |
2547 | |
2548 // disable the 'future' savemode if I'm an attendee | |
2549 // reason: no calendaring system supports the thisandfuture range parameter in iTip REPLY | |
2550 if (action == 'remove' && !_is_organizer && _is_attendee) { | |
2551 future_disabled = ' disabled'; | |
2552 } | |
2553 | |
2554 html += '<div class="message"><span class="ui-icon ui-icon-alert"></span>' + | |
2555 rcmail.gettext(message_label, 'calendar') + '</div>' + | |
2556 '<div class="savemode">' + | |
2557 '<a href="#current" class="button">' + rcmail.gettext('currentevent', 'calendar') + '</a>' + | |
2558 '<a href="#future" class="button' + future_disabled + '">' + rcmail.gettext('futurevents', 'calendar') + '</a>' + | |
2559 '<a href="#all" class="button">' + rcmail.gettext('allevents', 'calendar') + '</a>' + | |
2560 (action != 'remove' ? '<a href="#new" class="button">' + rcmail.gettext('saveasnew', 'calendar') + '</a>' : '') + | |
2561 '</div>'; | |
2562 } | |
2563 | |
2564 // show dialog | |
2565 if (html) { | |
2566 var $dialog = $('<div>').html(html); | |
2567 | |
2568 $dialog.find('a.button').button().filter(':not(.disabled)').click(function(e) { | |
2569 data._savemode = String(this.href).replace(/.+#/, ''); | |
2570 data._notify = settings.itip_notify; | |
2571 | |
2572 // open event edit dialog when saving as new | |
2573 if (data._savemode == 'new') { | |
2574 event._savemode = 'new'; | |
2575 event_edit_dialog('edit', event); | |
2576 fc.fullCalendar('refetchEvents'); | |
2577 } | |
2578 else { | |
2579 if ($dialog.find('input.confirm-attendees-donotify').length) | |
2580 data._notify = $dialog.find('input.confirm-attendees-donotify').get(0).checked ? 1 : 0; | |
2581 if (decline) { | |
2582 data._decline = $dialog.find('input.confirm-attendees-decline:checked').length; | |
2583 data._notify = 0; | |
2584 } | |
2585 update_event(action, data); | |
2586 } | |
2587 | |
2588 $dialog.dialog("close"); | |
2589 return false; | |
2590 }); | |
2591 | |
2592 var buttons = []; | |
2593 | |
2594 if (!event.recurrence) { | |
2595 buttons.push({ | |
2596 text: rcmail.gettext((action == 'remove' ? 'delete' : 'save'), 'calendar'), | |
2597 click: function() { | |
2598 data._notify = notify && $dialog.find('input.confirm-attendees-donotify:checked').length ? 1 : 0; | |
2599 data._decline = decline && $dialog.find('input.confirm-attendees-decline:checked').length ? 1 : 0; | |
2600 update_event(action, data); | |
2601 $(this).dialog("close"); | |
2602 } | |
2603 }); | |
2604 } | |
2605 | |
2606 buttons.push({ | |
2607 text: rcmail.gettext('cancel', 'calendar'), | |
2608 click: function() { | |
2609 $(this).dialog("close"); | |
2610 } | |
2611 }); | |
2612 | |
2613 $dialog.dialog({ | |
2614 modal: true, | |
2615 width: 460, | |
2616 dialogClass: 'warning', | |
2617 title: rcmail.gettext((action == 'remove' ? 'removeeventconfirm' : 'changeeventconfirm'), 'calendar'), | |
2618 buttons: buttons, | |
2619 open: function() { | |
2620 setTimeout(function(){ | |
2621 $dialog.parent().find('.ui-button:not(.ui-dialog-titlebar-close)').first().focus(); | |
2622 }, 5); | |
2623 }, | |
2624 close: function(){ | |
2625 $dialog.dialog("destroy").remove(); | |
2626 if (!rcmail.busy) | |
2627 fc.fullCalendar('refetchEvents'); | |
2628 } | |
2629 }).addClass('event-update-confirm').show(); | |
2630 | |
2631 return false; | |
2632 } | |
2633 // show regular confirm box when deleting | |
2634 else if (action == 'remove' && !cal.undelete) { | |
2635 if (!confirm(rcmail.gettext('deleteventconfirm', 'calendar'))) | |
2636 return false; | |
2637 } | |
2638 | |
2639 // do update | |
2640 update_event(action, data); | |
2641 | |
2642 return true; | |
2643 }; | |
2644 | |
2645 var update_agenda_toolbar = function() | |
2646 { | |
2647 $('#agenda-listrange').val(fc.fullCalendar('option', 'listRange')); | |
2648 $('#agenda-listsections').val(fc.fullCalendar('option', 'listSections')); | |
2649 } | |
2650 | |
2651 | |
2652 /*** public methods ***/ | |
2653 | |
2654 /** | |
2655 * Remove saving lock and free the UI for new input | |
2656 */ | |
2657 this.unlock_saving = function() | |
2658 { | |
2659 if (me.saving_lock) | |
2660 rcmail.set_busy(false, null, me.saving_lock); | |
2661 }; | |
2662 | |
2663 // opens calendar day-view in a popup | |
2664 this.fisheye_view = function(date) | |
2665 { | |
2666 $('#fish-eye-view:ui-dialog').dialog('close'); | |
2667 | |
2668 // create list of active event sources | |
2669 var src, cals = {}, sources = []; | |
2670 for (var id in this.calendars) { | |
2671 src = $.extend({}, this.calendars[id]); | |
2672 src.editable = false; | |
2673 src.url = null; | |
2674 src.events = []; | |
2675 | |
2676 if (src.active) { | |
2677 cals[id] = src; | |
2678 sources.push(src); | |
2679 } | |
2680 } | |
2681 | |
2682 // copy events already loaded | |
2683 var events = fc.fullCalendar('clientEvents'); | |
2684 for (var event, i=0; i< events.length; i++) { | |
2685 event = events[i]; | |
2686 if (event.source && (src = cals[event.source.id])) { | |
2687 src.events.push(event); | |
2688 } | |
2689 } | |
2690 | |
2691 var h = $(window).height() - 50; | |
2692 var dialog = $('<div>') | |
2693 .attr('id', 'fish-eye-view') | |
2694 .dialog({ | |
2695 modal: true, | |
2696 width: 680, | |
2697 height: h, | |
2698 title: $.fullCalendar.formatDate(date, 'dddd ' + settings['date_long']), | |
2699 close: function(){ | |
2700 dialog.dialog("destroy"); | |
2701 me.fisheye_date = null; | |
2702 } | |
2703 }) | |
2704 .fullCalendar($.extend({}, fullcalendar_defaults, { | |
2705 defaultView: 'agendaDay', | |
2706 header: { left: '', center: '', right: '' }, | |
2707 height: h - 50, | |
2708 date: date.getDate(), | |
2709 month: date.getMonth(), | |
2710 year: date.getFullYear(), | |
2711 eventSources: sources | |
2712 })); | |
2713 | |
2714 this.fisheye_date = date; | |
2715 }; | |
2716 | |
2717 // opens the given calendar in a popup dialog | |
2718 this.quickview = function(id, shift) | |
2719 { | |
2720 var src, in_quickview = false; | |
2721 $.each(this.quickview_sources, function(i,cal) { | |
2722 if (cal.id == id) { | |
2723 in_quickview = true; | |
2724 src = cal; | |
2725 } | |
2726 }); | |
2727 | |
2728 // remove source from quickview | |
2729 if (in_quickview && shift) { | |
2730 this.quickview_sources = $.grep(this.quickview_sources, function(src) { return src.id != id; }); | |
2731 } | |
2732 else { | |
2733 if (!shift) { | |
2734 // remove all current quickview event sources | |
2735 if (this.quickview_active) { | |
2736 fc.fullCalendar('removeEventSources'); | |
2737 } | |
2738 | |
2739 this.quickview_sources = []; | |
2740 | |
2741 // uncheck all active quickview icons | |
2742 calendars_list.container.find('div.focusview') | |
2743 .add('#calendars .searchresults div.focusview') | |
2744 .removeClass('focusview') | |
2745 .find('a.quickview').attr('aria-checked', 'false'); | |
2746 } | |
2747 | |
2748 if (!in_quickview) { | |
2749 // clone and modify calendar properties | |
2750 src = $.extend({}, this.calendars[id]); | |
2751 src.url += '&_quickview=1'; | |
2752 this.quickview_sources.push(src); | |
2753 } | |
2754 } | |
2755 | |
2756 // disable quickview | |
2757 if (this.quickview_active && !this.quickview_sources.length) { | |
2758 // register regular calendar event sources | |
2759 $.each(this.calendars, function(k, cal) { | |
2760 if (cal.active) | |
2761 fc.fullCalendar('addEventSource', cal); | |
2762 }); | |
2763 | |
2764 this.quickview_active = false; | |
2765 $('body').removeClass('quickview-active'); | |
2766 | |
2767 // uncheck all active quickview icons | |
2768 calendars_list.container.find('div.focusview') | |
2769 .add('#calendars .searchresults div.focusview') | |
2770 .removeClass('focusview') | |
2771 .find('a.quickview').attr('aria-checked', 'false'); | |
2772 } | |
2773 // activate quickview | |
2774 else if (!this.quickview_active) { | |
2775 // remove regular calendar event sources | |
2776 fc.fullCalendar('removeEventSources'); | |
2777 | |
2778 // register quickview event sources | |
2779 $.each(this.quickview_sources, function(i, src) { | |
2780 fc.fullCalendar('addEventSource', src); | |
2781 }); | |
2782 | |
2783 this.quickview_active = true; | |
2784 $('body').addClass('quickview-active'); | |
2785 } | |
2786 // update quickview sources | |
2787 else if (in_quickview) { | |
2788 fc.fullCalendar('removeEventSource', src); | |
2789 } | |
2790 else if (src) { | |
2791 fc.fullCalendar('addEventSource', src); | |
2792 } | |
2793 | |
2794 // activate quickview icon | |
2795 if (this.quickview_active) { | |
2796 $(calendars_list.get_item(id)).find('.calendar').first() | |
2797 .add('#calendars .searchresults .cal-' + id) | |
2798 [in_quickview ? 'removeClass' : 'addClass']('focusview') | |
2799 .find('a.quickview').attr('aria-checked', in_quickview ? 'false' : 'true'); | |
2800 } | |
2801 }; | |
2802 | |
2803 // disable quickview mode | |
2804 function reset_quickview() | |
2805 { | |
2806 // remove all current quickview event sources | |
2807 if (me.quickview_active) { | |
2808 fc.fullCalendar('removeEventSources'); | |
2809 me.quickview_sources = []; | |
2810 } | |
2811 | |
2812 // register regular calendar event sources | |
2813 $.each(me.calendars, function(k, cal) { | |
2814 if (cal.active) | |
2815 fc.fullCalendar('addEventSource', cal); | |
2816 }); | |
2817 | |
2818 // uncheck all active quickview icons | |
2819 calendars_list.container.find('div.focusview') | |
2820 .add('#calendars .searchresults div.focusview') | |
2821 .removeClass('focusview') | |
2822 .find('a.quickview').attr('aria-checked', 'false'); | |
2823 | |
2824 me.quickview_active = false; | |
2825 $('body').removeClass('quickview-active'); | |
2826 }; | |
2827 | |
2828 //public method to show the print dialog. | |
2829 this.print_calendars = function(view) | |
2830 { | |
2831 if (!view) view = fc.fullCalendar('getView').name; | |
2832 var date = fc.fullCalendar('getDate') || new Date(); | |
2833 var range = fc.fullCalendar('option', 'listRange'); | |
2834 var sections = fc.fullCalendar('option', 'listSections'); | |
2835 rcmail.open_window(rcmail.url('print', { view: view, date: date2unixtime(date), range: range, sections: sections, search: this.search_query }), true, true); | |
2836 }; | |
2837 | |
2838 // public method to bring up the new event dialog | |
2839 this.add_event = function(templ) { | |
2840 if (this.selected_calendar) { | |
2841 var now = new Date(); | |
2842 var date = fc.fullCalendar('getDate'); | |
2843 if (typeof date != 'Date') | |
2844 date = now; | |
2845 date.setHours(now.getHours()+1); | |
2846 date.setMinutes(0); | |
2847 var end = new Date(date.getTime()); | |
2848 end.setHours(date.getHours()+1); | |
2849 event_edit_dialog('new', $.extend({ start:date, end:end, allDay:false, calendar:this.selected_calendar }, templ || {})); | |
2850 } | |
2851 }; | |
2852 | |
2853 // delete the given event after showing a confirmation dialog | |
2854 this.delete_event = function(event) { | |
2855 // show confirm dialog for recurring events, use jquery UI dialog | |
2856 return update_event_confirm('remove', event, { id:event.id, calendar:event.calendar, attendees:event.attendees }); | |
2857 }; | |
2858 | |
2859 // opens a jquery UI dialog with event properties (or empty for creating a new calendar) | |
2860 this.calendar_edit_dialog = function(calendar) | |
2861 { | |
2862 // close show dialog first | |
2863 var $dialog = $("#calendarform"); | |
2864 if ($dialog.is(':ui-dialog')) | |
2865 $dialog.dialog('close'); | |
2866 | |
2867 if (!calendar) | |
2868 calendar = { name:'', color:'cc0000', editable:true, showalarms:true }; | |
2869 | |
2870 var form, name, color, alarms; | |
2871 | |
2872 $dialog.html(rcmail.get_label('loading')); | |
2873 $.ajax({ | |
2874 type: 'GET', | |
2875 dataType: 'html', | |
2876 url: rcmail.url('calendar'), | |
2877 data: { action:(calendar.id ? 'form-edit' : 'form-new'), c:{ id:calendar.id } }, | |
2878 success: function(data) { | |
2879 $dialog.html(data); | |
2880 // resize and reposition dialog window | |
2881 form = $('#calendarpropform'); | |
2882 me.dialog_resize('#calendarform', form.height(), form.width()); | |
2883 name = $('#calendar-name').prop('disabled', !calendar.editable).val(calendar.editname || calendar.name); | |
2884 color = $('#calendar-color').val(calendar.color).miniColors({ value: calendar.color, colorValues:rcmail.env.mscolors }); | |
2885 alarms = $('#calendar-showalarms').prop('checked', calendar.showalarms).get(0); | |
2886 name.select(); | |
2887 } | |
2888 }); | |
2889 | |
2890 // dialog buttons | |
2891 var buttons = {}; | |
2892 | |
2893 buttons[rcmail.gettext('save', 'calendar')] = function() { | |
2894 // form is not loaded | |
2895 if (!form || !form.length) | |
2896 return; | |
2897 | |
2898 // TODO: do some input validation | |
2899 if (!name.val() || name.val().length < 2) { | |
2900 alert(rcmail.gettext('invalidcalendarproperties', 'calendar')); | |
2901 name.select(); | |
2902 return; | |
2903 } | |
2904 | |
2905 // post data to server | |
2906 var data = form.serializeJSON(); | |
2907 if (data.color) | |
2908 data.color = data.color.replace(/^#/, ''); | |
2909 if (calendar.id) | |
2910 data.id = calendar.id; | |
2911 if (alarms) | |
2912 data.showalarms = alarms.checked ? 1 : 0; | |
2913 | |
2914 me.saving_lock = rcmail.set_busy(true, 'calendar.savingdata'); | |
2915 rcmail.http_post('calendar', { action:(calendar.id ? 'edit' : 'new'), c:data }); | |
2916 $dialog.dialog("close"); | |
2917 }; | |
2918 | |
2919 buttons[rcmail.gettext('cancel', 'calendar')] = function() { | |
2920 $dialog.dialog("close"); | |
2921 }; | |
2922 | |
2923 // open jquery UI dialog | |
2924 $dialog.dialog({ | |
2925 modal: true, | |
2926 resizable: true, | |
2927 closeOnEscape: false, | |
2928 title: rcmail.gettext((calendar.id ? 'editcalendar' : 'createcalendar'), 'calendar'), | |
2929 open: function() { | |
2930 $dialog.parent().find('.ui-dialog-buttonset .ui-button').first().addClass('mainaction'); | |
2931 }, | |
2932 close: function() { | |
2933 $dialog.html('').dialog("destroy").hide(); | |
2934 }, | |
2935 buttons: buttons, | |
2936 minWidth: 400, | |
2937 width: 420 | |
2938 }).show(); | |
2939 | |
2940 }; | |
2941 | |
2942 this.calendar_remove = function(calendar) | |
2943 { | |
2944 this.calendar_destroy_source(calendar.id); | |
2945 rcmail.http_post('calendar', { action:'subscribe', c:{ id:calendar.id, active:0, permanent:0, recursive:1 } }); | |
2946 return true; | |
2947 }; | |
2948 | |
2949 this.calendar_delete = function(calendar) | |
2950 { | |
2951 if (confirm(rcmail.gettext(calendar.children ? 'deletecalendarconfirmrecursive' : 'deletecalendarconfirm', 'calendar'))) { | |
2952 rcmail.http_post('calendar', { action:'delete', c:{ id:calendar.id } }); | |
2953 return true; | |
2954 } | |
2955 return false; | |
2956 }; | |
2957 | |
2958 this.calendar_refresh_source = function(id) | |
2959 { | |
2960 // got race-conditions fc.currentFetchID when using refetchEvents, | |
2961 // so we remove and add the source instead | |
2962 // fc.fullCalendar('refetchEvents', me.calendars[id]); | |
2963 fc.fullCalendar('removeEventSource', me.calendars[id]); | |
2964 fc.fullCalendar('addEventSource', me.calendars[id]); | |
2965 }; | |
2966 | |
2967 this.calendar_destroy_source = function(id) | |
2968 { | |
2969 var delete_ids = []; | |
2970 | |
2971 if (this.calendars[id]) { | |
2972 // find sub-calendars | |
2973 if (this.calendars[id].children) { | |
2974 for (var child_id in this.calendars) { | |
2975 if (String(child_id).indexOf(id) == 0) | |
2976 delete_ids.push(child_id); | |
2977 } | |
2978 } | |
2979 else { | |
2980 delete_ids.push(id); | |
2981 } | |
2982 } | |
2983 | |
2984 // delete all calendars in the list | |
2985 for (var i=0; i < delete_ids.length; i++) { | |
2986 id = delete_ids[i]; | |
2987 calendars_list.remove(id); | |
2988 fc.fullCalendar('removeEventSource', this.calendars[id]); | |
2989 $('#edit-calendar option[value="'+id+'"]').remove(); | |
2990 delete this.calendars[id]; | |
2991 } | |
2992 }; | |
2993 | |
2994 // open a dialog to upload an .ics file with events to be imported | |
2995 this.import_events = function(calendar) | |
2996 { | |
2997 // close show dialog first | |
2998 var $dialog = $("#eventsimport"), | |
2999 form = rcmail.gui_objects.importform; | |
3000 | |
3001 if ($dialog.is(':ui-dialog')) | |
3002 $dialog.dialog('close'); | |
3003 | |
3004 if (calendar) | |
3005 $('#event-import-calendar').val(calendar.id); | |
3006 | |
3007 var buttons = {}; | |
3008 buttons[rcmail.gettext('import', 'calendar')] = function() { | |
3009 if (form && form.elements._data.value) { | |
3010 rcmail.async_upload_form(form, 'import_events', function(e) { | |
3011 rcmail.set_busy(false, null, me.saving_lock); | |
3012 $('.ui-dialog-buttonpane button', $dialog.parent()).button('enable'); | |
3013 | |
3014 // display error message if no sophisticated response from server arrived (e.g. iframe load error) | |
3015 if (me.import_succeeded === null) | |
3016 rcmail.display_message(rcmail.get_label('importerror', 'calendar'), 'error'); | |
3017 }); | |
3018 | |
3019 // display upload indicator (with extended timeout) | |
3020 var timeout = rcmail.env.request_timeout; | |
3021 rcmail.env.request_timeout = 600; | |
3022 me.import_succeeded = null; | |
3023 me.saving_lock = rcmail.set_busy(true, 'uploading'); | |
3024 $('.ui-dialog-buttonpane button', $dialog.parent()).button('disable'); | |
3025 | |
3026 // restore settings | |
3027 rcmail.env.request_timeout = timeout; | |
3028 } | |
3029 }; | |
3030 | |
3031 buttons[rcmail.gettext('cancel', 'calendar')] = function() { | |
3032 $dialog.dialog("close"); | |
3033 }; | |
3034 | |
3035 // open jquery UI dialog | |
3036 $dialog.dialog({ | |
3037 modal: true, | |
3038 resizable: false, | |
3039 closeOnEscape: false, | |
3040 title: rcmail.gettext('importevents', 'calendar'), | |
3041 open: function() { | |
3042 $dialog.parent().find('.ui-dialog-buttonset .ui-button').first().addClass('mainaction'); | |
3043 }, | |
3044 close: function() { | |
3045 $('.ui-dialog-buttonpane button', $dialog.parent()).button('enable'); | |
3046 $dialog.dialog("destroy").hide(); | |
3047 }, | |
3048 buttons: buttons, | |
3049 width: 520 | |
3050 }).show(); | |
3051 }; | |
3052 | |
3053 // callback from server if import succeeded | |
3054 this.import_success = function(p) | |
3055 { | |
3056 this.import_succeeded = true; | |
3057 $("#eventsimport:ui-dialog").dialog('close'); | |
3058 rcmail.set_busy(false, null, me.saving_lock); | |
3059 rcmail.gui_objects.importform.reset(); | |
3060 | |
3061 if (p.refetch) | |
3062 this.refresh(p); | |
3063 }; | |
3064 | |
3065 // callback from server to report errors on import | |
3066 this.import_error = function(p) | |
3067 { | |
3068 this.import_succeeded = false; | |
3069 rcmail.set_busy(false, null, me.saving_lock); | |
3070 rcmail.display_message(p.message || rcmail.get_label('importerror', 'calendar'), 'error'); | |
3071 } | |
3072 | |
3073 // open a dialog to select calendars for export | |
3074 this.export_events = function(calendar) | |
3075 { | |
3076 // close show dialog first | |
3077 var $dialog = $("#eventsexport"), | |
3078 form = rcmail.gui_objects.exportform; | |
3079 | |
3080 if ($dialog.is(':ui-dialog')) | |
3081 $dialog.dialog('close'); | |
3082 | |
3083 if (calendar) | |
3084 $('#event-export-calendar').val(calendar.id); | |
3085 | |
3086 $('#event-export-range').change(function(e){ | |
3087 var custom = $('option:selected', this).val() == 'custom', | |
3088 input = $('#event-export-startdate') | |
3089 input.parent()[(custom?'show':'hide')](); | |
3090 if (custom) | |
3091 input.select(); | |
3092 }) | |
3093 | |
3094 var buttons = {}; | |
3095 buttons[rcmail.gettext('export', 'calendar')] = function() { | |
3096 if (form) { | |
3097 var start = 0, range = $('#event-export-range option:selected', this).val(), | |
3098 source = $('#event-export-calendar option:selected').val(), | |
3099 attachmt = $('#event-export-attachments').get(0).checked; | |
3100 | |
3101 if (range == 'custom') | |
3102 start = date2unixtime(parse_datetime('00:00', $('#event-export-startdate').val())); | |
3103 else if (range > 0) | |
3104 start = 'today -' + range + ' months'; | |
3105 | |
3106 rcmail.goto_url('export_events', { source:source, start:start, attachments:attachmt?1:0 }); | |
3107 } | |
3108 $dialog.dialog("close"); | |
3109 }; | |
3110 | |
3111 buttons[rcmail.gettext('cancel', 'calendar')] = function() { | |
3112 $dialog.dialog("close"); | |
3113 }; | |
3114 | |
3115 // open jquery UI dialog | |
3116 $dialog.dialog({ | |
3117 modal: true, | |
3118 resizable: false, | |
3119 closeOnEscape: false, | |
3120 title: rcmail.gettext('exporttitle', 'calendar'), | |
3121 open: function() { | |
3122 $dialog.parent().find('.ui-dialog-buttonset .ui-button').first().addClass('mainaction'); | |
3123 }, | |
3124 close: function() { | |
3125 $('.ui-dialog-buttonpane button', $dialog.parent()).button('enable'); | |
3126 $dialog.dialog("destroy").hide(); | |
3127 }, | |
3128 buttons: buttons, | |
3129 width: 520 | |
3130 }).show(); | |
3131 }; | |
3132 | |
3133 // download the selected event as iCal | |
3134 this.event_download = function(event) | |
3135 { | |
3136 if (event && event.id) { | |
3137 rcmail.goto_url('export_events', { source:event.calendar, id:event.id, attachments:1 }); | |
3138 } | |
3139 }; | |
3140 | |
3141 // open the message compose step with a calendar_event parameter referencing the selected event. | |
3142 // the server-side plugin hook will pick that up and attach the event to the message. | |
3143 this.event_sendbymail = function(event, e) | |
3144 { | |
3145 if (event && event.id) { | |
3146 rcmail.command('compose', { _calendar_event:event._id }, e ? e.target : null, e); | |
3147 } | |
3148 }; | |
3149 | |
3150 // display the edit dialog, request 'new' action and pass the selected event | |
3151 this.event_copy = function(event) { | |
3152 if (event && event.id) { | |
3153 var copy = $.extend(true, {}, event); | |
3154 | |
3155 delete copy.id; | |
3156 delete copy._id; | |
3157 delete copy.created; | |
3158 delete copy.changed; | |
3159 delete copy.recurrence_id; | |
3160 delete copy.attachments; // @TODO | |
3161 | |
3162 $.each(copy.attendees, function (k, v) { | |
3163 if (v.role != 'ORGANIZER') { | |
3164 v.status = 'NEEDS-ACTION'; | |
3165 } | |
3166 }) | |
3167 | |
3168 event_edit_dialog('new', copy); | |
3169 } | |
3170 }; | |
3171 | |
3172 // show URL of the given calendar in a dialog box | |
3173 this.showurl = function(calendar) | |
3174 { | |
3175 var $dialog = $('#calendarurlbox'); | |
3176 | |
3177 if ($dialog.is(':ui-dialog')) | |
3178 $dialog.dialog('close'); | |
3179 | |
3180 if (calendar.feedurl) { | |
3181 if (calendar.caldavurl) { | |
3182 $('#caldavurl').val(calendar.caldavurl); | |
3183 $('#calendarcaldavurl').show(); | |
3184 } | |
3185 else { | |
3186 $('#calendarcaldavurl').hide(); | |
3187 } | |
3188 | |
3189 $dialog.dialog({ | |
3190 resizable: true, | |
3191 closeOnEscape: true, | |
3192 title: rcmail.gettext('showurl', 'calendar'), | |
3193 close: function() { | |
3194 $dialog.dialog("destroy").hide(); | |
3195 }, | |
3196 width: 520 | |
3197 }).show(); | |
3198 | |
3199 $('#calfeedurl').val(calendar.feedurl).select(); | |
3200 } | |
3201 }; | |
3202 | |
3203 // refresh the calendar view after saving event data | |
3204 this.refresh = function(p) | |
3205 { | |
3206 var source = me.calendars[p.source]; | |
3207 | |
3208 // helper function to update the given fullcalendar view | |
3209 function update_view(view, event, source) { | |
3210 var existing = view.fullCalendar('clientEvents', event._id); | |
3211 if (existing.length) { | |
3212 $.extend(existing[0], event); | |
3213 view.fullCalendar('updateEvent', existing[0]); | |
3214 // remove old recurrence instances | |
3215 if (event.recurrence && !event.recurrence_id) | |
3216 view.fullCalendar('removeEvents', function(e){ return e._id.indexOf(event._id+'-') == 0; }); | |
3217 } | |
3218 else { | |
3219 event.source = source; // link with source | |
3220 view.fullCalendar('renderEvent', event); | |
3221 } | |
3222 } | |
3223 | |
3224 // remove temp events | |
3225 fc.fullCalendar('removeEvents', function(e){ return e.temp; }); | |
3226 | |
3227 if (source && (p.refetch || (p.update && !source.active))) { | |
3228 // activate event source if new event was added to an invisible calendar | |
3229 if (this.quickview_active) { | |
3230 // map source to the quickview_sources equivalent | |
3231 $.each(this.quickview_sources, function(src) { | |
3232 if (src.id == source.id) { | |
3233 source = src; | |
3234 return false; | |
3235 } | |
3236 }); | |
3237 fc.fullCalendar('refetchEvents', source, true); | |
3238 } | |
3239 else if (!source.active) { | |
3240 source.active = true; | |
3241 fc.fullCalendar('addEventSource', source); | |
3242 $('#rcmlical' + source.id + ' input').prop('checked', true); | |
3243 } | |
3244 else | |
3245 fc.fullCalendar('refetchEvents', source, true); | |
3246 | |
3247 fetch_counts(); | |
3248 } | |
3249 // add/update single event object | |
3250 else if (source && p.update) { | |
3251 var event = p.update; | |
3252 event.temp = false; | |
3253 event.editable = 0; | |
3254 | |
3255 // update fish-eye view | |
3256 if (this.fisheye_date) | |
3257 update_view($('#fish-eye-view'), event, source); | |
3258 | |
3259 // update main view | |
3260 event.editable = source.editable; | |
3261 update_view(fc, event, source); | |
3262 | |
3263 // update the currently displayed event dialog | |
3264 if ($('#eventshow').is(':visible') && me.selected_event && me.selected_event.id == event.id) | |
3265 event_show_dialog(event) | |
3266 } | |
3267 // refetch all calendars | |
3268 else if (p.refetch) { | |
3269 fc.fullCalendar('refetchEvents', undefined, true); | |
3270 fetch_counts(); | |
3271 } | |
3272 }; | |
3273 | |
3274 // modify query parameters for refresh requests | |
3275 this.before_refresh = function(query) | |
3276 { | |
3277 var view = fc.fullCalendar('getView'); | |
3278 | |
3279 query.start = date2unixtime(view.visStart); | |
3280 query.end = date2unixtime(view.visEnd); | |
3281 | |
3282 if (this.search_query) | |
3283 query.q = this.search_query; | |
3284 | |
3285 return query; | |
3286 }; | |
3287 | |
3288 // callback from server providing event counts | |
3289 this.update_counts = function(p) | |
3290 { | |
3291 $.each(p.counts, function(cal, count) { | |
3292 var li = calendars_list.get_item(cal), | |
3293 bubble = $(li).children('.calendar').find('span.count'); | |
3294 | |
3295 if (!bubble.length && count > 0) { | |
3296 bubble = $('<span>') | |
3297 .addClass('count') | |
3298 .appendTo($(li).children('.calendar').first()) | |
3299 } | |
3300 | |
3301 if (count > 0) { | |
3302 bubble.text(count).show(); | |
3303 } | |
3304 else { | |
3305 bubble.text('').hide(); | |
3306 } | |
3307 }); | |
3308 }; | |
3309 | |
3310 // callback after an iTip message event was imported | |
3311 this.itip_message_processed = function(data) | |
3312 { | |
3313 // remove temporary iTip source | |
3314 fc.fullCalendar('removeEventSource', this.calendars['--invitation--itip']); | |
3315 | |
3316 $('#eventshow:ui-dialog').dialog('close'); | |
3317 this.selected_event = null; | |
3318 | |
3319 // refresh destination calendar source | |
3320 this.refresh({ source:data.calendar, refetch:true }); | |
3321 | |
3322 this.unlock_saving(); | |
3323 | |
3324 // process 'after_action' in mail task | |
3325 if (window.opener && window.opener.rcube_libcalendaring) | |
3326 window.opener.rcube_libcalendaring.itip_message_processed(data); | |
3327 }; | |
3328 | |
3329 // reload the calendar view by keeping the current date/view selection | |
3330 this.reload_view = function() | |
3331 { | |
3332 var query = { view: fc.fullCalendar('getView').name }, | |
3333 date = fc.fullCalendar('getDate'); | |
3334 if (date) | |
3335 query.date = date2unixtime(date); | |
3336 rcmail.redirect(rcmail.url('', query)); | |
3337 } | |
3338 | |
3339 // update browser location to remember current view | |
3340 this.update_state = function() | |
3341 { | |
3342 var query = { view: current_view }, | |
3343 date = fc.fullCalendar('getDate'); | |
3344 if (date) | |
3345 query.date = date2unixtime(date); | |
3346 | |
3347 if (window.history.replaceState) | |
3348 window.history.replaceState({}, document.title, rcmail.url('', query).replace('&_action=', '')); | |
3349 }; | |
3350 | |
3351 this.resource_search = resource_search; | |
3352 this.reset_resource_search = reset_resource_search; | |
3353 this.add_resource2event = add_resource2event; | |
3354 this.resource_data_load = resource_data_load; | |
3355 this.resource_owner_load = resource_owner_load; | |
3356 | |
3357 | |
3358 /*** event searching ***/ | |
3359 | |
3360 // execute search | |
3361 this.quicksearch = function() | |
3362 { | |
3363 if (rcmail.gui_objects.qsearchbox) { | |
3364 var q = rcmail.gui_objects.qsearchbox.value; | |
3365 if (q != '') { | |
3366 var id = 'search-'+q; | |
3367 var sources = []; | |
3368 | |
3369 if (me.quickview_active) | |
3370 reset_quickview(); | |
3371 | |
3372 if (this._search_message) | |
3373 rcmail.hide_message(this._search_message); | |
3374 | |
3375 for (var sid in this.calendars) { | |
3376 if (this.calendars[sid]) { | |
3377 this.calendars[sid].url = this.calendars[sid].url.replace(/&q=.+/, '') + '&q=' + urlencode(q); | |
3378 sources.push(sid); | |
3379 } | |
3380 } | |
3381 id += '@'+sources.join(','); | |
3382 | |
3383 // ignore if query didn't change | |
3384 if (this.search_request == id) { | |
3385 return; | |
3386 } | |
3387 // remember current view | |
3388 else if (!this.search_request) { | |
3389 this.default_view = fc.fullCalendar('getView').name; | |
3390 } | |
3391 | |
3392 this.search_request = id; | |
3393 this.search_query = q; | |
3394 | |
3395 // change to list view | |
3396 fc.fullCalendar('option', 'listSections', 'month') | |
3397 .fullCalendar('option', 'listRange', Math.max(60, settings['agenda_range'])) | |
3398 .fullCalendar('changeView', 'table'); | |
3399 | |
3400 update_agenda_toolbar(); | |
3401 | |
3402 // refetch events with new url (if not already triggered by changeView) | |
3403 if (!this.is_loading) | |
3404 fc.fullCalendar('refetchEvents'); | |
3405 } | |
3406 else // empty search input equals reset | |
3407 this.reset_quicksearch(); | |
3408 } | |
3409 }; | |
3410 | |
3411 // reset search and get back to normal event listing | |
3412 this.reset_quicksearch = function() | |
3413 { | |
3414 $(rcmail.gui_objects.qsearchbox).val(''); | |
3415 | |
3416 if (this._search_message) | |
3417 rcmail.hide_message(this._search_message); | |
3418 | |
3419 if (this.search_request) { | |
3420 // hide bottom links of agenda view | |
3421 fc.find('.fc-list-content > .fc-listappend').hide(); | |
3422 | |
3423 // restore original event sources and view mode from fullcalendar | |
3424 fc.fullCalendar('option', 'listSections', settings['agenda_sections']) | |
3425 .fullCalendar('option', 'listRange', settings['agenda_range']); | |
3426 | |
3427 update_agenda_toolbar(); | |
3428 | |
3429 for (var sid in this.calendars) { | |
3430 if (this.calendars[sid]) | |
3431 this.calendars[sid].url = this.calendars[sid].url.replace(/&q=.+/, ''); | |
3432 } | |
3433 if (this.default_view) | |
3434 fc.fullCalendar('changeView', this.default_view); | |
3435 | |
3436 if (!this.is_loading) | |
3437 fc.fullCalendar('refetchEvents'); | |
3438 | |
3439 this.search_request = this.search_query = null; | |
3440 } | |
3441 }; | |
3442 | |
3443 // callback if all sources have been fetched from server | |
3444 this.events_loaded = function(count) | |
3445 { | |
3446 var addlinks, append = ''; | |
3447 | |
3448 // enhance list view when searching | |
3449 if (this.search_request) { | |
3450 if (!count) { | |
3451 this._search_message = rcmail.display_message(rcmail.gettext('searchnoresults', 'calendar'), 'notice'); | |
3452 append = '<div class="message">' + rcmail.gettext('searchnoresults', 'calendar') + '</div>'; | |
3453 } | |
3454 append += '<div class="fc-bottomlinks formlinks"></div>'; | |
3455 addlinks = true; | |
3456 } | |
3457 | |
3458 if (fc.fullCalendar('getView').name == 'table') { | |
3459 var container = fc.find('.fc-list-content > .fc-listappend'); | |
3460 if (append) { | |
3461 if (!container.length) | |
3462 container = $('<div class="fc-listappend"></div>').appendTo(fc.find('.fc-list-content')); | |
3463 container.html(append).show(); | |
3464 } | |
3465 else if (container.length) | |
3466 container.hide(); | |
3467 | |
3468 // add links to adjust search date range | |
3469 if (addlinks) { | |
3470 var lc = container.find('.fc-bottomlinks'); | |
3471 $('<a>').attr('href', '#').html(rcmail.gettext('searchearlierdates', 'calendar')).appendTo(lc).click(function(){ | |
3472 fc.fullCalendar('incrementDate', 0, -1, 0); | |
3473 }); | |
3474 lc.append(" "); | |
3475 $('<a>').attr('href', '#').html(rcmail.gettext('searchlaterdates', 'calendar')).appendTo(lc).click(function(){ | |
3476 var range = fc.fullCalendar('option', 'listRange'); | |
3477 if (range < 90) { | |
3478 fc.fullCalendar('option', 'listRange', fc.fullCalendar('option', 'listRange') + 30).fullCalendar('render'); | |
3479 update_agenda_toolbar(); | |
3480 } | |
3481 else | |
3482 fc.fullCalendar('incrementDate', 0, 1, 0); | |
3483 }); | |
3484 } | |
3485 } | |
3486 | |
3487 if (this.fisheye_date) | |
3488 this.fisheye_view(this.fisheye_date); | |
3489 }; | |
3490 | |
3491 // adjust calendar view size | |
3492 this.view_resize = function() | |
3493 { | |
3494 var footer = fc.fullCalendar('getView').name == 'table' ? $('#agendaoptions').outerHeight() : 0; | |
3495 fc.fullCalendar('option', 'height', $('#calendar').height() - footer); | |
3496 }; | |
3497 | |
3498 // mark the given calendar folder as selected | |
3499 this.select_calendar = function(id, nolistupdate) | |
3500 { | |
3501 if (!nolistupdate) | |
3502 calendars_list.select(id); | |
3503 | |
3504 // trigger event hook | |
3505 rcmail.triggerEvent('selectfolder', { folder:id, prefix:'rcmlical' }); | |
3506 | |
3507 this.selected_calendar = id; | |
3508 | |
3509 rcmail.update_state({source: id}); | |
3510 }; | |
3511 | |
3512 // register the given calendar to the current view | |
3513 var add_calendar_source = function(cal) | |
3514 { | |
3515 var color, brightness, select, id = cal.id; | |
3516 | |
3517 me.calendars[id] = $.extend({ | |
3518 url: rcmail.url('calendar/load_events', { source: id }), | |
3519 className: 'fc-event-cal-'+id, | |
3520 id: id | |
3521 }, cal); | |
3522 | |
3523 // choose black text color when background is bright, white otherwise | |
3524 if (color = settings.event_coloring % 2 ? '' : '#' + cal.color) { | |
3525 if (/^#([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i.test(color)) { | |
3526 // use information about brightness calculation found at | |
3527 // http://javascriptrules.com/2009/08/05/css-color-brightness-contrast-using-javascript/ | |
3528 brightness = (parseInt(RegExp.$1, 16) * 299 + parseInt(RegExp.$2, 16) * 587 + parseInt(RegExp.$3, 16) * 114) / 1000; | |
3529 if (brightness > 125) | |
3530 me.calendars[id].textColor = 'black'; | |
3531 } | |
3532 | |
3533 me.calendars[id].color = color; | |
3534 } | |
3535 | |
3536 if (fc && (cal.active || cal.subscribed)) { | |
3537 if (cal.active) | |
3538 fc.fullCalendar('addEventSource', me.calendars[id]); | |
3539 | |
3540 var submit = { id: id, active: cal.active ? 1 : 0 }; | |
3541 if (cal.subscribed !== undefined) | |
3542 submit.permanent = cal.subscribed ? 1 : 0; | |
3543 rcmail.http_post('calendar', { action:'subscribe', c:submit }); | |
3544 } | |
3545 | |
3546 // insert to #calendar-select options if writeable | |
3547 select = $('#edit-calendar'); | |
3548 if (fc && me.has_permission(cal, 'i') && select.length && !select.find('option[value="'+id+'"]').length) { | |
3549 $('<option>').attr('value', id).html(cal.name).appendTo(select); | |
3550 } | |
3551 } | |
3552 | |
3553 // fetch counts for some calendars from the server | |
3554 var fetch_counts = function() | |
3555 { | |
3556 if (count_sources.length) { | |
3557 setTimeout(function() { | |
3558 rcmail.http_request('calendar/count', { source:count_sources }); | |
3559 }, 500); | |
3560 } | |
3561 }; | |
3562 | |
3563 | |
3564 /*** startup code ***/ | |
3565 | |
3566 // create list of event sources AKA calendars | |
3567 var id, cal, active, event_sources = []; | |
3568 for (id in rcmail.env.calendars) { | |
3569 cal = rcmail.env.calendars[id]; | |
3570 active = cal.active || false; | |
3571 add_calendar_source(cal); | |
3572 | |
3573 // check active calendars | |
3574 $('#rcmlical'+id+' > .calendar input').prop('checked', active); | |
3575 | |
3576 if (active) { | |
3577 event_sources.push(this.calendars[id]); | |
3578 } | |
3579 if (cal.counts) { | |
3580 count_sources.push(id); | |
3581 } | |
3582 | |
3583 if (cal.editable && !this.selected_calendar) { | |
3584 this.selected_calendar = id; | |
3585 rcmail.enable_command('addevent', true); | |
3586 } | |
3587 } | |
3588 | |
3589 // initialize treelist widget that controls the calendars list | |
3590 var widget_class = window.kolab_folderlist || rcube_treelist_widget; | |
3591 calendars_list = new widget_class(rcmail.gui_objects.calendarslist, { | |
3592 id_prefix: 'rcmlical', | |
3593 selectable: true, | |
3594 save_state: true, | |
3595 keyboard: false, | |
3596 searchbox: '#calendarlistsearch', | |
3597 search_action: 'calendar/calendar', | |
3598 search_sources: [ 'folders', 'users' ], | |
3599 search_title: rcmail.gettext('calsearchresults','calendar') | |
3600 }); | |
3601 calendars_list.addEventListener('select', function(node) { | |
3602 if (node && node.id && me.calendars[node.id]) { | |
3603 me.select_calendar(node.id, true); | |
3604 rcmail.enable_command('calendar-edit', 'calendar-showurl', true); | |
3605 rcmail.enable_command('calendar-delete', me.calendars[node.id].editable); | |
3606 rcmail.enable_command('calendar-remove', me.calendars[node.id] && me.calendars[node.id].removable); | |
3607 } | |
3608 }); | |
3609 calendars_list.addEventListener('insert-item', function(p) { | |
3610 var cal = p.data; | |
3611 if (cal && cal.id) { | |
3612 add_calendar_source(cal); | |
3613 | |
3614 // add css classes related to this calendar to document | |
3615 if (cal.css) { | |
3616 $('<style type="text/css"></style>') | |
3617 .html(cal.css) | |
3618 .appendTo('head'); | |
3619 } | |
3620 } | |
3621 }); | |
3622 calendars_list.addEventListener('subscribe', function(p) { | |
3623 var cal; | |
3624 if ((cal = me.calendars[p.id])) { | |
3625 cal.subscribed = p.subscribed || false; | |
3626 rcmail.http_post('calendar', { action:'subscribe', c:{ id:p.id, active:cal.active?1:0, permanent:cal.subscribed?1:0 } }); | |
3627 } | |
3628 }); | |
3629 calendars_list.addEventListener('remove', function(p) { | |
3630 if (me.calendars[p.id] && me.calendars[p.id].removable) { | |
3631 me.calendar_remove(me.calendars[p.id]); | |
3632 } | |
3633 }); | |
3634 calendars_list.addEventListener('search-complete', function(data) { | |
3635 if (data.length) | |
3636 rcmail.display_message(rcmail.gettext('nrcalendarsfound','calendar').replace('$nr', data.length), 'voice'); | |
3637 else | |
3638 rcmail.display_message(rcmail.gettext('nocalendarsfound','calendar'), 'info'); | |
3639 }); | |
3640 calendars_list.addEventListener('click-item', function(event) { | |
3641 // handle clicks on quickview icon: temprarily add this source and open in quickview | |
3642 if ($(event.target).hasClass('quickview') && event.data) { | |
3643 if (!me.calendars[event.data.id]) { | |
3644 event.data.readonly = true; | |
3645 event.data.active = false; | |
3646 event.data.subscribed = false; | |
3647 add_calendar_source(event.data); | |
3648 } | |
3649 me.quickview(event.data.id, event.shiftKey || event.metaKey || event.ctrlKey); | |
3650 return false; | |
3651 } | |
3652 }); | |
3653 | |
3654 // init (delegate) event handler on calendar list checkboxes | |
3655 $(rcmail.gui_objects.calendarslist).on('click', 'input[type=checkbox]', function(e) { | |
3656 e.stopPropagation(); | |
3657 | |
3658 if (me.quickview_active) { | |
3659 this.checked = !this.checked; | |
3660 return false; | |
3661 } | |
3662 | |
3663 var id = this.value; | |
3664 if (me.calendars[id]) { // add or remove event source on click | |
3665 var action; | |
3666 if (this.checked) { | |
3667 action = 'addEventSource'; | |
3668 me.calendars[id].active = true; | |
3669 } | |
3670 else { | |
3671 action = 'removeEventSource'; | |
3672 me.calendars[id].active = false; | |
3673 } | |
3674 | |
3675 // adjust checked state of original list item | |
3676 if (calendars_list.is_search()) { | |
3677 calendars_list.container.find('input[value="'+id+'"]').prop('checked', this.checked); | |
3678 } | |
3679 | |
3680 // add/remove event source | |
3681 fc.fullCalendar(action, me.calendars[id]); | |
3682 rcmail.http_post('calendar', { action:'subscribe', c:{ id:id, active:me.calendars[id].active?1:0 } }); | |
3683 } | |
3684 }) | |
3685 .on('keypress', 'input[type=checkbox]', function(e) { | |
3686 // select calendar on <Enter> | |
3687 if (e.keyCode == 13) { | |
3688 calendars_list.select(this.value); | |
3689 return rcube_event.cancel(e); | |
3690 } | |
3691 }) | |
3692 // init (delegate) event handler on quickview links | |
3693 .on('click', 'a.quickview', function(e) { | |
3694 var id = $(this).closest('li').attr('id').replace(/^rcmlical/, ''); | |
3695 | |
3696 if (calendars_list.is_search()) | |
3697 id = id.replace(/--xsR$/, ''); | |
3698 | |
3699 if (me.calendars[id]) | |
3700 me.quickview(id, e.shiftKey || e.metaKey || e.ctrlKey); | |
3701 | |
3702 if (!rcube_event.is_keyboard(e) && this.blur) | |
3703 this.blur(); | |
3704 | |
3705 e.stopPropagation(); | |
3706 return false; | |
3707 }); | |
3708 | |
3709 // register dbl-click handler to open calendar edit dialog | |
3710 $(rcmail.gui_objects.calendarslist).on('dblclick', ':not(.virtual) > .calname', function(e){ | |
3711 var id = $(this).closest('li').attr('id').replace(/^rcmlical/, ''); | |
3712 me.calendar_edit_dialog(me.calendars[id]); | |
3713 }); | |
3714 | |
3715 // select default calendar | |
3716 if (rcmail.env.source && this.calendars[rcmail.env.source]) | |
3717 this.selected_calendar = rcmail.env.source; | |
3718 else if (settings.default_calendar && this.calendars[settings.default_calendar] && this.calendars[settings.default_calendar].editable) | |
3719 this.selected_calendar = settings.default_calendar; | |
3720 | |
3721 if (this.selected_calendar) | |
3722 this.select_calendar(this.selected_calendar); | |
3723 | |
3724 var viewdate = new Date(); | |
3725 if (rcmail.env.date) | |
3726 viewdate.setTime(fromunixtime(rcmail.env.date)); | |
3727 | |
3728 // add source with iTip event data for rendering | |
3729 if (rcmail.env.itip_events && rcmail.env.itip_events.length) { | |
3730 me.calendars['--invitation--itip'] = { | |
3731 events: rcmail.env.itip_events, | |
3732 className: 'fc-event-cal---invitation--itip', | |
3733 color: '#fff', | |
3734 textColor: '#333', | |
3735 editable: false, | |
3736 rights: 'lrs', | |
3737 attendees: true | |
3738 }; | |
3739 event_sources.push(me.calendars['--invitation--itip']); | |
3740 } | |
3741 | |
3742 // initalize the fullCalendar plugin | |
3743 var fc = $('#calendar').fullCalendar($.extend({}, fullcalendar_defaults, { | |
3744 header: { | |
3745 right: 'prev,next today', | |
3746 center: 'title', | |
3747 left: 'agendaDay,agendaWeek,month,table' | |
3748 }, | |
3749 date: viewdate.getDate(), | |
3750 month: viewdate.getMonth(), | |
3751 year: viewdate.getFullYear(), | |
3752 height: $('#calendar').height(), | |
3753 eventSources: event_sources, | |
3754 selectable: true, | |
3755 selectHelper: false, | |
3756 loading: function(isLoading) { | |
3757 me.is_loading = isLoading; | |
3758 this._rc_loading = rcmail.set_busy(isLoading, 'loading', this._rc_loading); | |
3759 // trigger callback | |
3760 if (!isLoading) | |
3761 me.events_loaded($(this).fullCalendar('clientEvents').length); | |
3762 }, | |
3763 // callback for date range selection | |
3764 select: function(start, end, allDay, e, view) { | |
3765 var range_select = (!allDay || start.getDate() != end.getDate()) | |
3766 if (dialog_check(e) && range_select) | |
3767 event_edit_dialog('new', { start:start, end:end, allDay:allDay, calendar:me.selected_calendar }); | |
3768 if (range_select || ignore_click) | |
3769 view.calendar.unselect(); | |
3770 }, | |
3771 // callback for clicks in all-day box | |
3772 dayClick: function(date, allDay, e, view) { | |
3773 var now = new Date().getTime(); | |
3774 if (now - day_clicked_ts < 400 && day_clicked == date.getTime()) { // emulate double-click on day | |
3775 var enddate = new Date(); enddate.setTime(date.getTime() + DAY_MS - 60000); | |
3776 return event_edit_dialog('new', { start:date, end:enddate, allDay:allDay, calendar:me.selected_calendar }); | |
3777 } | |
3778 | |
3779 if (!ignore_click) { | |
3780 view.calendar.gotoDate(date); | |
3781 if (day_clicked && new Date(day_clicked).getMonth() != date.getMonth()) | |
3782 view.calendar.select(date, date, allDay); | |
3783 } | |
3784 day_clicked = date.getTime(); | |
3785 day_clicked_ts = now; | |
3786 }, | |
3787 // callback when an event was dragged and finally dropped | |
3788 eventDrop: function(event, dayDelta, minuteDelta, allDay, revertFunc) { | |
3789 if (event.end == null || event.end.getTime() < event.start.getTime()) { | |
3790 event.end = new Date(event.start.getTime() + (allDay ? DAY_MS : HOUR_MS)); | |
3791 } | |
3792 // moved to all-day section: set times to 12:00 - 13:00 | |
3793 if (allDay && !event.allDay) { | |
3794 event.start.setHours(12); | |
3795 event.start.setMinutes(0); | |
3796 event.start.setSeconds(0); | |
3797 event.end.setHours(13); | |
3798 event.end.setMinutes(0); | |
3799 event.end.setSeconds(0); | |
3800 } | |
3801 // moved from all-day section: set times to working hours | |
3802 else if (event.allDay && !allDay) { | |
3803 var newstart = event.start.getTime(); | |
3804 revertFunc(); // revert to get original duration | |
3805 var numdays = Math.max(1, Math.round((event.end.getTime() - event.start.getTime()) / DAY_MS)) - 1; | |
3806 event.start = new Date(newstart); | |
3807 event.end = new Date(newstart + numdays * DAY_MS); | |
3808 event.end.setHours(settings['work_end'] || 18); | |
3809 event.end.setMinutes(0); | |
3810 | |
3811 if (event.end.getTime() < event.start.getTime()) | |
3812 event.end = new Date(newstart + HOUR_MS); | |
3813 } | |
3814 | |
3815 // send move request to server | |
3816 var data = { | |
3817 id: event.id, | |
3818 calendar: event.calendar, | |
3819 start: date2servertime(event.start), | |
3820 end: date2servertime(event.end), | |
3821 allday: allDay?1:0 | |
3822 }; | |
3823 update_event_confirm('move', event, data); | |
3824 }, | |
3825 // callback for event resizing | |
3826 eventResize: function(event, delta) { | |
3827 // sanitize event dates | |
3828 if (event.allDay) | |
3829 event.start.setHours(12); | |
3830 if (!event.end || event.end.getTime() < event.start.getTime()) | |
3831 event.end = new Date(event.start.getTime() + HOUR_MS); | |
3832 | |
3833 // send resize request to server | |
3834 var data = { | |
3835 id: event.id, | |
3836 calendar: event.calendar, | |
3837 start: date2servertime(event.start), | |
3838 end: date2servertime(event.end) | |
3839 }; | |
3840 update_event_confirm('resize', event, data); | |
3841 }, | |
3842 viewDisplay: function(view) { | |
3843 $('#agendaoptions')[view.name == 'table' ? 'show' : 'hide'](); | |
3844 if (minical) { | |
3845 window.setTimeout(function(){ minical.datepicker('setDate', fc.fullCalendar('getDate')); }, exec_deferred); | |
3846 if (view.name != current_view) | |
3847 me.view_resize(); | |
3848 current_view = view.name; | |
3849 me.update_state(); | |
3850 } | |
3851 }, | |
3852 viewRender: function(view) { | |
3853 if (fc && view.name == 'month') | |
3854 fc.fullCalendar('option', 'maxHeight', Math.floor((view.element.parent().height()-18) / 6) - 35); | |
3855 } | |
3856 })); | |
3857 | |
3858 // if start date is changed, shift end date according to initial duration | |
3859 var shift_enddate = function(dateText) { | |
3860 var newstart = parse_datetime('0', dateText); | |
3861 var newend = new Date(newstart.getTime() + $('#edit-startdate').data('duration') * 1000); | |
3862 $('#edit-enddate').val($.fullCalendar.formatDate(newend, me.settings['date_format'])); | |
3863 event_times_changed(); | |
3864 }; | |
3865 | |
3866 // Set as calculateWeek to determine the week of the year based on the ISO 8601 definition. | |
3867 // Uses the default $.datepicker.iso8601Week() function but takes firstDay setting into account. | |
3868 // This is a temporary fix until http://bugs.jqueryui.com/ticket/8420 is resolved. | |
3869 var iso8601Week = datepicker_settings.calculateWeek = function(date) { | |
3870 var mondayOffset = Math.abs(1 - datepicker_settings.firstDay); | |
3871 return $.datepicker.iso8601Week(new Date(date.getTime() + mondayOffset * 86400000)); | |
3872 }; | |
3873 | |
3874 var minical; | |
3875 var init_calendar_ui = function() | |
3876 { | |
3877 // initialize small calendar widget using jQuery UI datepicker | |
3878 minical = $('#datepicker').datepicker($.extend(datepicker_settings, { | |
3879 inline: true, | |
3880 showWeek: true, | |
3881 changeMonth: true, | |
3882 changeYear: true, | |
3883 onSelect: function(dateText, inst) { | |
3884 ignore_click = true; | |
3885 var d = minical.datepicker('getDate'); //parse_datetime('0:0', dateText); | |
3886 fc.fullCalendar('gotoDate', d).fullCalendar('select', d, d, true); | |
3887 }, | |
3888 onChangeMonthYear: function(year, month, inst) { | |
3889 minical.data('year', year).data('month', month); | |
3890 }, | |
3891 beforeShowDay: function(date) { | |
3892 var view = fc.fullCalendar('getView'); | |
3893 var active = view.visStart && date.getTime() >= view.visStart.getTime() && date.getTime() < view.visEnd.getTime(); | |
3894 return [ true, (active ? 'ui-datepicker-activerange ui-datepicker-active-' + view.name : ''), '']; | |
3895 } | |
3896 })) // set event handler for clicks on calendar week cell of the datepicker widget | |
3897 .on('click', 'td.ui-datepicker-week-col', function(e) { | |
3898 var cell = $(e.target); | |
3899 if (e.target.tagName == 'TD') { | |
3900 var base_date = minical.datepicker('getDate'); | |
3901 if (minical.data('month')) | |
3902 base_date.setMonth(minical.data('month')-1); | |
3903 if (minical.data('year')) | |
3904 base_date.setYear(minical.data('year')); | |
3905 base_date.setHours(12); | |
3906 base_date.setDate(base_date.getDate() - ((base_date.getDay() + 6) % 7) + datepicker_settings.firstDay); | |
3907 var base_kw = iso8601Week(base_date), | |
3908 target_kw = parseInt(cell.html()), | |
3909 wdiff = target_kw - base_kw; | |
3910 if (wdiff > 10) // year jump | |
3911 base_date.setYear(base_date.getFullYear() - 1); | |
3912 else if (wdiff < -10) | |
3913 base_date.setYear(base_date.getFullYear() + 1); | |
3914 // select monday of the chosen calendar week | |
3915 var day_off = base_date.getDay() - datepicker_settings.firstDay, | |
3916 date = new Date(base_date.getTime() - day_off * DAY_MS + wdiff * 7 * DAY_MS); | |
3917 fc.fullCalendar('gotoDate', date).fullCalendar('setDate', date).fullCalendar('changeView', 'agendaWeek'); | |
3918 minical.datepicker('setDate', date); | |
3919 } | |
3920 }); | |
3921 | |
3922 minical.find('.ui-datepicker-inline').attr('aria-labelledby', 'aria-label-minical'); | |
3923 | |
3924 if (rcmail.env.date) { | |
3925 var viewdate = new Date(); | |
3926 viewdate.setTime(fromunixtime(rcmail.env.date)); | |
3927 minical.datepicker('setDate', viewdate); | |
3928 } | |
3929 | |
3930 // init event dialog | |
3931 $('#eventtabs').tabs({ | |
3932 activate: function(event, ui) { | |
3933 // newPanel.selector for jQuery-UI 1.10, newPanel.attr('id') for jQuery-UI 1.12 | |
3934 var tab = String(ui.newPanel.selector || ui.newPanel.attr('id')) | |
3935 .replace(/^#?event-panel-/, '').replace(/s$/, ''); | |
3936 | |
3937 var has_real_attendee = function(attendees) { | |
3938 for (var i=0; i < (attendees ? attendees.length : 0); i++) { | |
3939 if (attendees[i].cutype != 'RESOURCE') | |
3940 return true; | |
3941 } | |
3942 }; | |
3943 | |
3944 if (tab == 'attendee' || tab == 'resource') { | |
3945 if (!rcube_event.is_keyboard(event)) | |
3946 $('#edit-'+tab+'-name').select(); | |
3947 // update free-busy status if needed | |
3948 if (freebusy_ui.needsupdate && me.selected_event) | |
3949 update_freebusy_status(me.selected_event); | |
3950 // add current user as organizer if non added yet | |
3951 if (tab == 'attendee' && !has_real_attendee(event_attendees)) { | |
3952 add_attendee($.extend({ role:'ORGANIZER' }, settings.identity)); | |
3953 $('#edit-attendees-form .attendees-invitebox').show(); | |
3954 } | |
3955 } | |
3956 // reset autocompletion on tab change (#3389) | |
3957 rcmail.ksearch_blur(); | |
3958 } | |
3959 }); | |
3960 $('#edit-enddate').datepicker(datepicker_settings); | |
3961 $('#edit-startdate').datepicker(datepicker_settings).datepicker('option', 'onSelect', shift_enddate).change(function(){ shift_enddate(this.value); }); | |
3962 $('#edit-enddate').datepicker('option', 'onSelect', event_times_changed).change(event_times_changed); | |
3963 $('#edit-allday').click(function(){ $('#edit-starttime, #edit-endtime')[(this.checked?'hide':'show')](); event_times_changed(); }); | |
3964 | |
3965 // configure drop-down menu on time input fields based on jquery UI autocomplete | |
3966 $('#edit-starttime, #edit-endtime, #eventedit input.edit-alarm-time').each(function() { | |
3967 me.init_time_autocomplete(this, { | |
3968 container: '#eventedit', | |
3969 change: event_times_changed | |
3970 }); | |
3971 }); | |
3972 | |
3973 // adjust end time when changing start | |
3974 $('#edit-starttime').change(function(e) { | |
3975 var dstart = $('#edit-startdate'), | |
3976 newstart = parse_datetime(this.value, dstart.val()), | |
3977 newend = new Date(newstart.getTime() + dstart.data('duration') * 1000); | |
3978 $('#edit-endtime').val($.fullCalendar.formatDate(newend, me.settings['time_format'])); | |
3979 $('#edit-enddate').val($.fullCalendar.formatDate(newend, me.settings['date_format'])); | |
3980 event_times_changed(); | |
3981 }); | |
3982 | |
3983 // register events on alarms and recurrence fields | |
3984 me.init_alarms_edit('#edit-alarms'); | |
3985 me.init_recurrence_edit('#eventedit'); | |
3986 | |
3987 // reload free-busy status when changing the organizer identity | |
3988 $('#eventedit').on('change', '#edit-identities-list', function(e) { | |
3989 var email = settings.identities[$(this).val()], | |
3990 icon = $(this).closest('tr').find('img.availabilityicon'); | |
3991 | |
3992 if (email && icon.length) { | |
3993 icon.attr('data-email', email); | |
3994 check_freebusy_status(icon, email, me.selected_event); | |
3995 } | |
3996 }); | |
3997 | |
3998 $('#event-export-startdate').datepicker(datepicker_settings); | |
3999 | |
4000 // init attendees autocompletion | |
4001 var ac_props; | |
4002 // parallel autocompletion | |
4003 if (rcmail.env.autocomplete_threads > 0) { | |
4004 ac_props = { | |
4005 threads: rcmail.env.autocomplete_threads, | |
4006 sources: rcmail.env.autocomplete_sources | |
4007 }; | |
4008 } | |
4009 rcmail.init_address_input_events($('#edit-attendee-name'), ac_props); | |
4010 rcmail.addEventListener('autocomplete_insert', function(e) { | |
4011 var success = false; | |
4012 if (e.field.name == 'participant') { | |
4013 success = add_attendees(e.insert, { role:'REQ-PARTICIPANT', status:'NEEDS-ACTION', cutype:(e.data && e.data.type == 'group' ? 'GROUP' : 'INDIVIDUAL') }); | |
4014 } | |
4015 else if (e.field.name == 'resource' && e.data && e.data.email) { | |
4016 success = add_attendee($.extend(e.data, { role:'REQ-PARTICIPANT', status:'NEEDS-ACTION', cutype:'RESOURCE' })); | |
4017 } | |
4018 if (e.field && success) { | |
4019 e.field.value = ''; | |
4020 } | |
4021 }); | |
4022 | |
4023 $('#edit-attendee-add').click(function(){ | |
4024 var input = $('#edit-attendee-name'); | |
4025 rcmail.ksearch_blur(); | |
4026 if (add_attendees(input.val(), { role:'REQ-PARTICIPANT', status:'NEEDS-ACTION', cutype:'INDIVIDUAL' })) { | |
4027 input.val(''); | |
4028 } | |
4029 }); | |
4030 | |
4031 rcmail.init_address_input_events($('#edit-resource-name'), { action:'calendar/resources-autocomplete' }); | |
4032 | |
4033 $('#edit-resource-add').click(function(){ | |
4034 var input = $('#edit-resource-name'); | |
4035 rcmail.ksearch_blur(); | |
4036 if (add_attendees(input.val(), { role:'REQ-PARTICIPANT', status:'NEEDS-ACTION', cutype:'RESOURCE' })) { | |
4037 input.val(''); | |
4038 } | |
4039 }); | |
4040 | |
4041 $('#edit-resource-find').click(function(){ | |
4042 event_resources_dialog(); | |
4043 return false; | |
4044 }); | |
4045 | |
4046 // handle change of "send invitations" checkbox | |
4047 $('#edit-attendees-invite').change(function() { | |
4048 $('#edit-attendees-donotify,input.edit-attendee-reply').prop('checked', this.checked); | |
4049 // hide/show comment field | |
4050 $('#eventedit .attendees-commentbox')[this.checked ? 'show' : 'hide'](); | |
4051 }); | |
4052 | |
4053 // delegate change event to "send invitations" checkbox | |
4054 $('#edit-attendees-donotify').change(function() { | |
4055 $('#edit-attendees-invite').click(); | |
4056 return false; | |
4057 }); | |
4058 | |
4059 $('#edit-attendee-schedule').click(function(){ | |
4060 event_freebusy_dialog(); | |
4061 }); | |
4062 | |
4063 $('#shedule-freebusy-prev').html(bw.ie6 ? '<<' : '◄').button().click(function(){ render_freebusy_grid(-1); }); | |
4064 $('#shedule-freebusy-next').html(bw.ie6 ? '>>' : '►').button().click(function(){ render_freebusy_grid(1); }).parent().buttonset(); | |
4065 | |
4066 $('#shedule-find-prev').button().click(function(){ freebusy_find_slot(-1); }); | |
4067 $('#shedule-find-next').button().click(function(){ freebusy_find_slot(1); }); | |
4068 | |
4069 $('#schedule-freebusy-workinghours').click(function(){ | |
4070 freebusy_ui.workinhoursonly = this.checked; | |
4071 $('#workinghourscss').remove(); | |
4072 if (this.checked) | |
4073 $('<style type="text/css" id="workinghourscss"> td.offhours { opacity:0.3; filter:alpha(opacity=30) } </style>').appendTo('head'); | |
4074 }); | |
4075 | |
4076 $('#event-rsvp input.button').click(function(e) { | |
4077 event_rsvp(this) | |
4078 }); | |
4079 | |
4080 $('#eventedit input.edit-recurring-savemode').change(function(e) { | |
4081 var sel = $('input.edit-recurring-savemode:checked').val(), | |
4082 disabled = sel == 'current' || sel == 'future'; | |
4083 $('#event-panel-recurrence input, #event-panel-recurrence select, #event-panel-attachments input').prop('disabled', disabled); | |
4084 $('#event-panel-recurrence, #event-panel-attachments')[(disabled?'addClass':'removeClass')]('disabled'); | |
4085 }) | |
4086 | |
4087 $('#eventshow .changersvp').click(function(e) { | |
4088 var d = $('#eventshow'), | |
4089 h = -$(this).closest('.event-line').toggle().height(); | |
4090 $('#event-rsvp').slideDown(300, function() { | |
4091 h += $(this).height(); | |
4092 me.dialog_resize(d.get(0), d.height() + h, d.outerWidth() - 50); | |
4093 }); | |
4094 return false; | |
4095 }) | |
4096 | |
4097 // register click handler for message links | |
4098 $('#edit-event-links, #event-links').on('click', 'li a.messagelink', function(e) { | |
4099 rcmail.open_window(this.href); | |
4100 if (!rcube_event.is_keyboard(e) && this.blur) | |
4101 this.blur(); | |
4102 return false; | |
4103 }); | |
4104 | |
4105 // register click handler for message delete buttons | |
4106 $('#edit-event-links').on('click', 'li a.delete', function(e) { | |
4107 remove_link(e.target); | |
4108 return false; | |
4109 }); | |
4110 | |
4111 $('#agenda-listrange').change(function(e){ | |
4112 settings['agenda_range'] = parseInt($(this).val()); | |
4113 fc.fullCalendar('option', 'listRange', settings['agenda_range']).fullCalendar('render'); | |
4114 // TODO: save new settings in prefs | |
4115 }).val(settings['agenda_range']); | |
4116 | |
4117 $('#agenda-listsections').change(function(e){ | |
4118 settings['agenda_sections'] = $(this).val(); | |
4119 fc.fullCalendar('option', 'listSections', settings['agenda_sections']).fullCalendar('render'); | |
4120 // TODO: save new settings in prefs | |
4121 }).val(fc.fullCalendar('option', 'listSections')); | |
4122 | |
4123 // hide event dialog when clicking somewhere into document | |
4124 $(document).bind('mousedown', dialog_check); | |
4125 | |
4126 rcmail.set_busy(false, 'loading', ui_loading); | |
4127 } | |
4128 | |
4129 // initialize more UI elements (deferred) | |
4130 window.setTimeout(init_calendar_ui, exec_deferred); | |
4131 | |
4132 // fetch counts for some calendars | |
4133 fetch_counts(); | |
4134 | |
4135 // add proprietary css styles if not IE | |
4136 if (!bw.ie) | |
4137 $('div.fc-content').addClass('rcube-fc-content'); | |
4138 | |
4139 // IE supresses 2nd click event when double-clicking | |
4140 if (bw.ie && bw.vendver < 9) { | |
4141 $('div.fc-content').bind('dblclick', function(e){ | |
4142 if (!$(this).hasClass('fc-widget-header') && fc.fullCalendar('getView').name != 'table') { | |
4143 var date = fc.fullCalendar('getDate'); | |
4144 var enddate = new Date(); enddate.setTime(date.getTime() + DAY_MS - 60000); | |
4145 event_edit_dialog('new', { start:date, end:enddate, allDay:true, calendar:me.selected_calendar }); | |
4146 } | |
4147 }); | |
4148 } | |
4149 } // end rcube_calendar class | |
4150 | |
4151 | |
4152 /* calendar plugin initialization */ | |
4153 window.rcmail && rcmail.addEventListener('init', function(evt) { | |
4154 // configure toolbar buttons | |
4155 rcmail.register_command('addevent', function(){ cal.add_event(); }, true); | |
4156 rcmail.register_command('print', function(){ cal.print_calendars(); }, true); | |
4157 | |
4158 // configure list operations | |
4159 rcmail.register_command('calendar-create', function(){ cal.calendar_edit_dialog(null); }, true); | |
4160 rcmail.register_command('calendar-edit', function(){ cal.calendar_edit_dialog(cal.calendars[cal.selected_calendar]); }, false); | |
4161 rcmail.register_command('calendar-remove', function(){ cal.calendar_remove(cal.calendars[cal.selected_calendar]); }, false); | |
4162 rcmail.register_command('calendar-delete', function(){ cal.calendar_delete(cal.calendars[cal.selected_calendar]); }, false); | |
4163 rcmail.register_command('events-import', function(){ cal.import_events(cal.calendars[cal.selected_calendar]); }, true); | |
4164 rcmail.register_command('calendar-showurl', function(){ cal.showurl(cal.calendars[cal.selected_calendar]); }, false); | |
4165 rcmail.register_command('event-download', function(){ cal.event_download(cal.selected_event); }, true); | |
4166 rcmail.register_command('event-sendbymail', function(p, obj, e){ cal.event_sendbymail(cal.selected_event, e); }, true); | |
4167 rcmail.register_command('event-copy', function(){ cal.event_copy(cal.selected_event); }, true); | |
4168 rcmail.register_command('event-history', function(p, obj, e){ cal.event_history_dialog(cal.selected_event); }, false); | |
4169 | |
4170 // search and export events | |
4171 rcmail.register_command('export', function(){ cal.export_events(cal.calendars[cal.selected_calendar]); }, true); | |
4172 rcmail.register_command('search', function(){ cal.quicksearch(); }, true); | |
4173 rcmail.register_command('reset-search', function(){ cal.reset_quicksearch(); }, true); | |
4174 | |
4175 // resource invitation dialog | |
4176 rcmail.register_command('search-resource', function(){ cal.resource_search(); }, true); | |
4177 rcmail.register_command('reset-resource-search', function(){ cal.reset_resource_search(); }, true); | |
4178 rcmail.register_command('add-resource', function(){ cal.add_resource2event(); }, false); | |
4179 | |
4180 // register callback commands | |
4181 rcmail.addEventListener('plugin.refresh_source', function(data) { cal.calendar_refresh_source(data); }); | |
4182 rcmail.addEventListener('plugin.destroy_source', function(p){ cal.calendar_destroy_source(p.id); }); | |
4183 rcmail.addEventListener('plugin.unlock_saving', function(p){ cal.unlock_saving(); }); | |
4184 rcmail.addEventListener('plugin.refresh_calendar', function(p){ cal.refresh(p); }); | |
4185 rcmail.addEventListener('plugin.import_success', function(p){ cal.import_success(p); }); | |
4186 rcmail.addEventListener('plugin.import_error', function(p){ cal.import_error(p); }); | |
4187 rcmail.addEventListener('plugin.update_counts', function(p){ cal.update_counts(p); }); | |
4188 rcmail.addEventListener('plugin.reload_view', function(p){ cal.reload_view(p); }); | |
4189 rcmail.addEventListener('plugin.resource_data', function(p){ cal.resource_data_load(p); }); | |
4190 rcmail.addEventListener('plugin.resource_owner', function(p){ cal.resource_owner_load(p); }); | |
4191 rcmail.addEventListener('plugin.render_event_changelog', function(data){ cal.render_event_changelog(data); }); | |
4192 rcmail.addEventListener('plugin.event_show_diff', function(data){ cal.event_show_diff(data); }); | |
4193 rcmail.addEventListener('plugin.close_history_dialog', function(data){ cal.close_history_dialog(); }); | |
4194 rcmail.addEventListener('plugin.event_show_revision', function(data){ cal.event_show_dialog(data, null, true); }); | |
4195 rcmail.addEventListener('plugin.itip_message_processed', function(data){ cal.itip_message_processed(data); }); | |
4196 rcmail.addEventListener('requestrefresh', function(q){ return cal.before_refresh(q); }); | |
4197 | |
4198 // let's go | |
4199 var cal = new rcube_calendar_ui($.extend(rcmail.env.calendar_settings, rcmail.env.libcal_settings)); | |
4200 | |
4201 $(window).resize(function(e) { | |
4202 // check target due to bugs in jquery | |
4203 // http://bugs.jqueryui.com/ticket/7514 | |
4204 // http://bugs.jquery.com/ticket/9841 | |
4205 if (e.target == window) { | |
4206 cal.view_resize(); | |
4207 } | |
4208 }).resize(); | |
4209 | |
4210 // show calendars list when ready | |
4211 $('#calendars').css('visibility', 'inherit'); | |
4212 | |
4213 // show toolbar | |
4214 $('#toolbar').show(); | |
4215 | |
4216 }); |