forked from themouette/jquery-week-calendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.weekcalendar.js
2945 lines (2661 loc) · 115 KB
/
jquery.weekcalendar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* jQuery.weekCalendar v2.0-dev
*
* for support join us at the google group:
* - http://groups.google.com/group/jquery-week-calendar
* have a look to the wiki for documentation:
* - http://wiki.github.com/themouette/jquery-week-calendar/
* something went bad ? report an issue:
* - http://github.com/themouette/jquery-week-calendar/issues
* get the last version on github:
* - http://github.com/themouette/jquery-week-calendar
*
* Copyright (c) 2009 Rob Monie
* Copyright (c) 2010 Julien MUETTON
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* If you're after a monthly calendar plugin, check out this one :
* http://arshaw.com/fullcalendar/
*/
(function($) {
// check the jquery version
var _v = $.fn.jquery.split('.'),
_jQuery14OrLower = (10 * _v[0] + _v[1]) < 15;
$.widget('ui.weekCalendar', (function() {
var _currentAjaxCall, _hourLineTimeout;
return {
options: {
date: new Date(),
timeFormat: null,
dateFormat: 'M d, Y',
alwaysDisplayTimeMinutes: true,
use24Hour: false,
daysToShow: 7,
minBodyHeight: 100,
firstDayOfWeek: function(calendar) {
if ($(calendar).weekCalendar('option', 'daysToShow') != 5) {
return 0;
} else {
//workweek
return 1;
}
}, // 0 = Sunday, 1 = Monday, 2 = Tuesday, ... , 6 = Saturday
useShortDayNames: false,
timeSeparator: ' to ',
startParam: 'start',
endParam: 'end',
businessHours: {start: 8, end: 18, limitDisplay: false},
newEventText: 'New Event',
timeslotHeight: 20,
defaultEventLength: 2,
timeslotsPerHour: 4,
minDate: null,
maxDate: null,
showHeader: true,
buttons: true,
buttonText: {
today: 'today',
lastWeek: 'previous',
nextWeek: 'next'
},
switchDisplay: {},
scrollToHourMillis: 500,
allowEventDelete: false,
allowCalEventOverlap: false,
overlapEventsSeparate: false,
totalEventsWidthPercentInOneColumn: 100,
readonly: false,
allowEventCreation: true,
hourLine: false,
deletable: function(calEvent, element) {
return true;
},
draggable: function(calEvent, element) {
return true;
},
resizable: function(calEvent, element) {
return true;
},
eventClick: function(calEvent, element, dayFreeBusyManager,
calendar, clickEvent) {
},
eventRender: function(calEvent, element) {
return element;
},
eventAfterRender: function(calEvent, element) {
return element;
},
eventRefresh: function(calEvent, element) {
return element;
},
eventDrag: function(calEvent, element) {
},
eventDrop: function(calEvent, element) {
},
eventResize: function(calEvent, element) {
},
eventNew: function(calEvent, element, dayFreeBusyManager,
calendar, mouseupEvent) {
},
eventMouseover: function(calEvent, $event) {
},
eventMouseout: function(calEvent, $event) {
},
eventDelete: function(calEvent, element, dayFreeBusyManager,
calendar, clickEvent) {
calendar.weekCalendar('removeEvent',calEvent.id);
},
calendarBeforeLoad: function(calendar) {
},
calendarAfterLoad: function(calendar) {
},
noEvents: function() {
},
eventHeader: function(calEvent, calendar) {
var options = calendar.weekCalendar('option');
var one_hour = 3600000;
var displayTitleWithTime = calEvent.end.getTime() - calEvent.start.getTime() <= (one_hour / options.timeslotsPerHour);
if (displayTitleWithTime) {
return calendar.weekCalendar(
'formatTime', calEvent.start) +
': ' + calEvent.title;
} else {
return calendar.weekCalendar(
'formatTime', calEvent.start) +
options.timeSeparator +
calendar.weekCalendar(
'formatTime', calEvent.end);
}
},
eventBody: function(calEvent, calendar) {
return calEvent.title;
},
shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
longMonths: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
shortDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
longDays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
/* multi-users options */
/**
* the available users for calendar.
* if you want to display users separately, enable the
* showAsSeparateUsers option.
* if you provide a list of user and do not enable showAsSeparateUsers
* option, then only the events that belongs to one or several of
* given users will be displayed
* @type {array}
*/
users: [],
/**
* should the calendar be displayed with separate column for each
* users.
* note that this option does nothing if you do not provide at least
* one user.
* @type {boolean}
*/
showAsSeparateUsers: true,
/**
* callback used to read user id from a user object.
* @param {Object} user the user to retrieve the id from.
* @param {number} index the user index from user list.
* @param {jQuery} calendar the calendar object.
* @return {int|String} the user id.
*/
getUserId: function(user, index, calendar) {
return index;
},
/**
* callback used to read user name from a user object.
* @param {Object} user the user to retrieve the name from.
* @param {number} index the user index from user list.
* @param {jQuery} calendar the calendar object.
* @return {String} the user name.
*/
getUserName: function(user, index, calendar) {
return user;
},
/**
* reads the id(s) of user(s) for who the event should be displayed.
* @param {Object} calEvent the calEvent to read informations from.
* @param {jQuery} calendar the calendar object.
* @return {number|String|Array} the user id(s) to appened events for.
*/
getEventUserId: function(calEvent, calendar) {
return calEvent.userId;
},
/**
* sets user id(s) to the calEvent
* @param {Object} calEvent the calEvent to set informations to.
* @param {jQuery} calendar the calendar object.
* @return {Object} the calEvent with modified user id.
*/
setEventUserId: function(userId, calEvent, calendar) {
calEvent.userId = userId;
return calEvent;
},
/* freeBusy options */
/**
* should the calendar display freebusys ?
* @type {boolean}
*/
displayFreeBusys: false,
/**
* read the id(s) for who the freebusy is available
* @param {Object} calEvent the calEvent to read informations from.
* @param {jQuery} calendar the calendar object.
* @return {number|String|Array} the user id(s) to appened events for.
*/
getFreeBusyUserId: function(calFreeBusy, calendar) {
return calFreeBusy.userId;
},
/**
* the default freeBusy object, used to manage default state
* @type {Object}
*/
defaultFreeBusy: {free: false},
/**
* function used to display the freeBusy element
* @type {Function}
* @param {Object} freeBusy the freeBusy timeslot to render.
* @param {jQuery} $freeBusy the freeBusy HTML element.
* @param {jQuery} calendar the calendar element.
*/
freeBusyRender: function(freeBusy, $freeBusy, calendar) {
if (!freeBusy.free) {
$freeBusy.addClass('free-busy-busy');
}
else {
$freeBusy.addClass('free-busy-free');
}
return $freeBusy;
},
/* other options */
/**
* true means start on first day of week, false means starts on
* startDate.
* @param {jQuery} calendar the calendar object.
* @type {Function|bool}
*/
startOnFirstDayOfWeek: function(calendar) {
return $(calendar).weekCalendar('option', 'daysToShow') >= 5;
},
/**
* should the columns be rendered alternatively using odd/even
* class
* @type {boolean}
*/
displayOddEven: false,
textSize: 13,
/**
* the title attribute for the calendar. possible placeholders are:
* <ul>
* <li>%start%</li>
* <li>%end%</li>
* <li>%date%</li>
* </ul>
* @type {Function|string}
* @param {number} option daysToShow.
* @return {String} the title attribute for the calendar.
*/
title: '%start% - %end%',
/**
* default options to pass to callback
* you can pass a function returning an object or a litteral object
* @type {object|function(#calendar)}
*/
jsonOptions: {},
headerSeparator: '<br />',
/**
* returns formatted header for day display
* @type {function(date,calendar)}
*/
getHeaderDate: null,
preventDragOnEventCreation: false,
/**
* the event on which to bind calendar resize
* @type {string}
*/
resizeEvent: 'resize.weekcalendar'
},
/***********************
* Initialise calendar *
***********************/
_create: function() {
var self = this;
self._computeOptions();
self._setupEventDelegation();
self._renderCalendar();
self._loadCalEvents();
self._resizeCalendar();
self._scrollToHour(self.options.date.getHours(), true);
if (this.options.resizeEvent) {
$(window).unbind(this.options.resizeEvent);
$(window).bind(this.options.resizeEvent, function() {
self._resizeCalendar();
});
}
},
/********************
* public functions *
********************/
/*
* Refresh the events for the currently displayed week.
*/
refresh: function() {
//reload with existing week
this._loadCalEvents(this.element.data('startDate'));
},
/*
* Clear all events currently loaded into the calendar
*/
clear: function() {
this._clearCalendar();
},
/*
* Go to this week
*/
today: function() {
this._clearCalendar();
this._loadCalEvents(new Date());
},
/*
* Go to the previous week relative to the currently displayed week
*/
prevWeek: function() {
//minus more than 1 day to be sure we're in previous week - account for daylight savings or other anomolies
var newDate = new Date(this.element.data('startDate').getTime() - (MILLIS_IN_WEEK / 6));
this._clearCalendar();
this._loadCalEvents(newDate);
},
/*
* Go to the next week relative to the currently displayed week
*/
nextWeek: function() {
//add 8 days to be sure of being in prev week - allows for daylight savings or other anomolies
var newDate = new Date(this.element.data('startDate').getTime() + MILLIS_IN_WEEK + MILLIS_IN_DAY);
this._clearCalendar();
this._loadCalEvents(newDate);
},
/*
* Reload the calendar to whatever week the date passed in falls on.
*/
gotoWeek: function(date) {
this._clearCalendar();
this._loadCalEvents(date);
},
/*
* Reload the calendar to whatever week the date passed in falls on.
*/
gotoDate: function(date) {
this._clearCalendar();
this._loadCalEvents(date);
},
/**
* change the number of days to show
*/
setDaysToShow: function(daysToShow) {
var self = this;
var hour = self._getCurrentScrollHour();
self.options.daysToShow = daysToShow;
$(self.element).html('');
self._renderCalendar();
self._loadCalEvents();
self._resizeCalendar();
self._scrollToHour(hour, false);
if (this.options.resizeEvent) {
$(window).unbind(this.options.resizeEvent);
$(window).bind(this.options.resizeEvent, function() {
self._resizeCalendar();
});
}
},
/*
* Remove an event based on it's id
*/
removeEvent: function(eventId) {
var self = this;
self.element.find('.wc-cal-event').each(function() {
if ($(this).data('calEvent').id === eventId) {
$(this).remove();
return false;
}
});
//this could be more efficient rather than running on all days regardless...
self.element.find('.wc-day-column-inner').each(function() {
self._adjustOverlappingEvents($(this));
});
},
/*
* Removes any events that have been added but not yet saved (have no id).
* This is useful to call after adding a freshly saved new event.
*/
removeUnsavedEvents: function() {
var self = this;
self.element.find('.wc-new-cal-event').each(function() {
$(this).remove();
});
//this could be more efficient rather than running on all days regardless...
self.element.find('.wc-day-column-inner').each(function() {
self._adjustOverlappingEvents($(this));
});
},
/*
* update an event in the calendar. If the event exists it refreshes
* it's rendering. If it's a new event that does not exist in the calendar
* it will be added.
*/
updateEvent: function(calEvent) {
this._updateEventInCalendar(calEvent);
},
/*
* Returns an array of timeslot start and end times based on
* the configured grid of the calendar. Returns in both date and
* formatted time based on the 'timeFormat' config option.
*/
getTimeslotTimes: function(date) {
var options = this.options;
var firstHourDisplayed = options.businessHours.limitDisplay ? options.businessHours.start : 0;
var startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate(), firstHourDisplayed);
var times = [],
startMillis = startDate.getTime();
for (var i = 0; i < options.timeslotsPerDay; i++) {
var endMillis = startMillis + options.millisPerTimeslot;
times[i] = {
start: new Date(startMillis),
startFormatted: this.formatTime(new Date(startMillis), options.timeFormat),
end: new Date(endMillis),
endFormatted: this.formatTime(new Date(endMillis), options.timeFormat)
};
startMillis = endMillis;
}
return times;
},
formatDate: function(date, format) {
if (format) {
return this._formatDate(date, format);
} else {
return this._formatDate(date, this.options.dateFormat);
}
},
formatTime: function(date, format) {
if (format) {
return this._formatDate(date, format);
} else if (this.options.timeFormat) {
return this._formatDate(date, this.options.timeFormat);
} else if (this.options.use24Hour) {
return this._formatDate(date, 'H:i');
} else {
return this._formatDate(date, 'h:i a');
}
},
serializeEvents: function() {
var self = this;
var calEvents = [];
self.element.find('.wc-cal-event').each(function() {
calEvents.push($(this).data('calEvent'));
});
return calEvents;
},
next: function() {
if (this._startOnFirstDayOfWeek()) {
return this.nextWeek();
}
var newDate = new Date(this.element.data('startDate').getTime());
newDate.setDate(newDate.getDate() + this.options.daysToShow);
this._clearCalendar();
this._loadCalEvents(newDate);
},
prev: function() {
if (this._startOnFirstDayOfWeek()) {
return this.prevWeek();
}
var newDate = new Date(this.element.data('startDate').getTime());
newDate.setDate(newDate.getDate() - this.options.daysToShow);
this._clearCalendar();
this._loadCalEvents(newDate);
},
getCurrentFirstDay: function() {
return this._dateFirstDayOfWeek(this.options.date || new Date());
},
getCurrentLastDay: function() {
return this._addDays(this.getCurrentFirstDay(), this.options.daysToShow - 1);
},
/*********************
* private functions *
*********************/
_setOption: function(key, value) {
var self = this;
if (self.options[key] != value) {
// event callback change, no need to re-render the events
if (key == 'beforeEventNew') {
self.options[key] = value;
return;
}
// this could be made more efficient at some stage by caching the
// events array locally in a store but this should be done in conjunction
// with a proper binding model.
var currentEvents = self.element.find('.wc-cal-event').map(function() {
return $(this).data('calEvent');
});
var newOptions = {};
newOptions[key] = value;
self._renderEvents({events: currentEvents, options: newOptions}, self.element.find('.wc-day-column-inner'));
}
},
// compute dynamic options based on other config values
_computeOptions: function() {
var options = this.options;
if (options.businessHours.limitDisplay) {
options.timeslotsPerDay = options.timeslotsPerHour * (options.businessHours.end - options.businessHours.start);
options.millisToDisplay = (options.businessHours.end - options.businessHours.start) * 3600000; // 60 * 60 * 1000
options.millisPerTimeslot = options.millisToDisplay / options.timeslotsPerDay;
} else {
options.timeslotsPerDay = options.timeslotsPerHour * 24;
options.millisToDisplay = MILLIS_IN_DAY;
options.millisPerTimeslot = MILLIS_IN_DAY / options.timeslotsPerDay;
}
},
/*
* Resize the calendar scrollable height based on the provided function in options.
*/
_resizeCalendar: function() {
var options = this.options;
if (options && $.isFunction(options.height)) {
var calendarHeight = options.height(this.element);
var headerHeight = this.element.find('.wc-header').outerHeight();
var navHeight = this.element.find('.wc-toolbar').outerHeight();
var scrollContainerHeight = Math.max(calendarHeight - navHeight - headerHeight, options.minBodyHeight);
var timeslotHeight = this.element.find('.wc-time-slots').outerHeight();
this.element.find('.wc-scrollable-grid').height(scrollContainerHeight);
if (timeslotHeight <= scrollContainerHeight) {
this.element.find('.wc-scrollbar-shim').width(0);
}
else {
this.element.find('.wc-scrollbar-shim').width(this._findScrollBarWidth());
}
this._trigger('resize', this.element);
}
},
_findScrollBarWidth: function() {
var parent = $('<div style="width:50px;height:50px;overflow:auto"><div/></div>').appendTo('body');
var child = parent.children();
var width = child.innerWidth() - child.height(99).innerWidth();
parent.remove();
return width || /* default to 16 that is the average */ 16;
},
/*
* configure calendar interaction events that are able to use event
* delegation for greater efficiency
*/
_setupEventDelegation: function() {
var self = this;
var options = this.options;
this.element.click(function(event) {
var $target = $(event.target),
freeBusyManager;
// click is disabled
if ($target.data('preventClick')) {
return;
}
var $calEvent = $target.hasClass('wc-cal-event') ?
$target :
$target.parents('.wc-cal-event');
if (!$calEvent.length || !$calEvent.data('calEvent')) {
return;
}
freeBusyManager = self.getFreeBusyManagerForEvent($calEvent.data('calEvent'));
if (options.allowEventDelete && $target.hasClass('wc-cal-event-delete')) {
options.eventDelete($calEvent.data('calEvent'), $calEvent, freeBusyManager, self.element, event);
} else {
options.eventClick($calEvent.data('calEvent'), $calEvent, freeBusyManager, self.element, event);
}
}).mouseover(function(event) {
var $target = $(event.target);
var $calEvent = $target.hasClass('wc-cal-event') ?
$target :
$target.parents('.wc-cal-event');
if (!$calEvent.length || !$calEvent.data('calEvent')) {
return;
}
if (self._isDraggingOrResizing($calEvent)) {
return;
}
options.eventMouseover($calEvent.data('calEvent'), $calEvent, event);
}).mouseout(function(event) {
var $target = $(event.target);
var $calEvent = $target.hasClass('wc-cal-event') ?
$target :
$target.parents('.wc-cal-event');
if (!$calEvent.length || !$calEvent.data('calEvent')) {
return;
}
if (self._isDraggingOrResizing($calEvent)) {
return;
}
options.eventMouseout($calEvent.data('calEvent'), $calEvent, event);
});
},
/**
* check if a ui draggable or resizable is currently being dragged or
* resized.
*/
_isDraggingOrResizing: function($target) {
return $target.hasClass('ui-draggable-dragging') ||
$target.hasClass('ui-resizable-resizing');
},
/*
* Render the main calendar layout
*/
_renderCalendar: function() {
var $calendarContainer, $weekDayColumns;
var self = this;
var options = this.options;
$calendarContainer = $('<div class=\"ui-widget wc-container\">').appendTo(self.element);
//render the different parts
// nav links
self._renderCalendarButtons($calendarContainer);
// header
self._renderCalendarHeader($calendarContainer);
// body
self._renderCalendarBody($calendarContainer);
$weekDayColumns = $calendarContainer.find('.wc-day-column-inner');
$weekDayColumns.each(function(i, val) {
if (!options.readonly) {
self._addDroppableToWeekDay($(this));
if (options.allowEventCreation) {
self._setupEventCreationForWeekDay($(this));
}
}
});
},
/**
* render the nav buttons on top of the calendar
*/
_renderCalendarButtons: function($calendarContainer) {
var self = this, options = this.options;
if ( !options.showHeader ) return;
if (options.buttons) {
var calendarNavHtml = '';
calendarNavHtml += '<div class=\"ui-widget-header wc-toolbar\">';
calendarNavHtml += '<div class=\"wc-display\"></div>';
calendarNavHtml += '<div class=\"wc-nav\">';
calendarNavHtml += '<button class=\"wc-prev\">' + options.buttonText.lastWeek + '</button>';
calendarNavHtml += '<button class=\"wc-today\">' + options.buttonText.today + '</button>';
calendarNavHtml += '<button class=\"wc-next\">' + options.buttonText.nextWeek + '</button>';
calendarNavHtml += '</div>';
calendarNavHtml += '<h1 class=\"wc-title\"></h1>';
calendarNavHtml += '</div>';
$(calendarNavHtml).appendTo($calendarContainer);
$calendarContainer.find('.wc-nav .wc-today')
.button({
icons: {primary: 'ui-icon-home'}})
.click(function() {
self.today();
return false;
});
$calendarContainer.find('.wc-nav .wc-prev')
.button({
text: false,
icons: {primary: 'ui-icon-seek-prev'}})
.click(function() {
self.element.weekCalendar('prev');
return false;
});
$calendarContainer.find('.wc-nav .wc-next')
.button({
text: false,
icons: {primary: 'ui-icon-seek-next'}})
.click(function() {
self.element.weekCalendar('next');
return false;
});
// now add buttons to switch display
if (this.options.switchDisplay && $.isPlainObject(this.options.switchDisplay)) {
var $container = $calendarContainer.find('.wc-display');
$.each(this.options.switchDisplay, function(label, option) {
var _id = 'wc-switch-display-' + option;
var _input = $('<input type="radio" id="' + _id + '" name="wc-switch-display" class="wc-switch-display"/>');
var _label = $('<label for="' + _id + '"></label>');
_label.html(label);
_input.val(option);
if (parseInt(self.options.daysToShow, 10) === parseInt(option, 10)) {
_input.attr('checked', 'checked');
}
$container
.append(_input)
.append(_label);
});
$container.find('input').change(function() {
self.setDaysToShow(parseInt($(this).val(), 10));
});
}
$calendarContainer.find('.wc-nav, .wc-display').buttonset();
var _height = $calendarContainer.find('.wc-nav').outerHeight();
$calendarContainer.find('.wc-title')
.height(_height)
.css('line-height', _height + 'px');
}else{
var calendarNavHtml = '';
calendarNavHtml += '<div class=\"ui-widget-header wc-toolbar\">';
calendarNavHtml += '<h1 class=\"wc-title\"></h1>';
calendarNavHtml += '</div>';
$(calendarNavHtml).appendTo($calendarContainer);
}
},
/**
* render the calendar header, including date and user header
*/
_renderCalendarHeader: function($calendarContainer) {
var self = this, options = this.options,
showAsSeparatedUser = options.showAsSeparateUsers && options.users && options.users.length,
rowspan = '', colspan = '', calendarHeaderHtml;
if (showAsSeparatedUser) {
rowspan = ' rowspan=\"2\"';
colspan = ' colspan=\"' + options.users.length + '\" ';
}
//first row
calendarHeaderHtml = '<div class=\"ui-widget-content wc-header\">';
calendarHeaderHtml += '<table><tbody><tr><td class=\"wc-time-column-header\"></td>';
for (var i = 1; i <= options.daysToShow; i++) {
calendarHeaderHtml += '<td class=\"wc-day-column-header wc-day-' + i + '\"' + colspan + '></td>';
}
calendarHeaderHtml += '<td class=\"wc-scrollbar-shim\"' + rowspan + '></td></tr>';
//users row
if (showAsSeparatedUser) {
calendarHeaderHtml += '<tr><td class=\"wc-time-column-header\"></td>';
var uLength = options.users.length,
_headerClass = '';
for (var i = 1; i <= options.daysToShow; i++) {
for (var j = 0; j < uLength; j++) {
_headerClass = [];
if (j == 0) {
_headerClass.push('wc-day-column-first');
}
if (j == uLength - 1) {
_headerClass.push('wc-day-column-last');
}
if (!_headerClass.length) {
_headerClass = 'wc-day-column-middle';
}
else {
_headerClass = _headerClass.join(' ');
}
calendarHeaderHtml += '<td class=\"' + _headerClass + ' wc-user-header wc-day-' + i + ' wc-user-' + self._getUserIdFromIndex(j) + '\">';
// calendarHeaderHtml+= "<div class=\"wc-user-header wc-day-" + i + " wc-user-" + self._getUserIdFromIndex(j) +"\" >";
calendarHeaderHtml += self._getUserName(j);
// calendarHeaderHtml+= "</div>";
calendarHeaderHtml += '</td>';
}
}
calendarHeaderHtml += '</tr>';
}
//close the header
calendarHeaderHtml += '</tbody></table></div>';
$(calendarHeaderHtml).appendTo($calendarContainer);
},
/**
* render the calendar body.
* Calendar body is composed of several distinct parts.
* Each part is displayed in a separated row to ease rendering.
* for further explanations, see each part rendering function.
*/
_renderCalendarBody: function($calendarContainer) {
var self = this, options = this.options,
showAsSeparatedUser = options.showAsSeparateUsers && options.users && options.users.length,
$calendarBody, $calendarTableTbody;
// create the structure
$calendarBody = '<div class=\"wc-scrollable-grid\">';
$calendarBody += '<table class=\"wc-time-slots\">';
$calendarBody += '<tbody>';
$calendarBody += '</tbody>';
$calendarBody += '</table>';
$calendarBody += '</div>';
$calendarBody = $($calendarBody);
$calendarTableTbody = $calendarBody.find('tbody');
self._renderCalendarBodyTimeSlots($calendarTableTbody);
self._renderCalendarBodyOddEven($calendarTableTbody);
self._renderCalendarBodyFreeBusy($calendarTableTbody);
self._renderCalendarBodyEvents($calendarTableTbody);
$calendarBody.appendTo($calendarContainer);
//set the column height
$calendarContainer.find('.wc-full-height-column').height(options.timeslotHeight * options.timeslotsPerDay);
//set the timeslot height
$calendarContainer.find('.wc-time-slot').height(options.timeslotHeight - 1); //account for border
//init the time row header height
/**
TODO if total height for an hour is less than 11px, there is a display problem.
Find a way to handle it
*/
$calendarContainer.find('.wc-time-header-cell').css({
height: (options.timeslotHeight * options.timeslotsPerHour) - 11,
padding: 5
});
//add the user data to every impacted column
if (showAsSeparatedUser) {
for (var i = 0, uLength = options.users.length; i < uLength; i++) {
$calendarContainer.find('.wc-user-' + self._getUserIdFromIndex(i))
.data('wcUser', options.users[i])
.data('wcUserIndex', i)
.data('wcUserId', self._getUserIdFromIndex(i));
}
}
},
/**
* render the timeslots separation
*/
_renderCalendarBodyTimeSlots: function($calendarTableTbody) {
var options = this.options,
renderRow, i, j,
showAsSeparatedUser = options.showAsSeparateUsers && options.users && options.users.length,
start = (options.businessHours.limitDisplay ? options.businessHours.start : 0),
end = (options.businessHours.limitDisplay ? options.businessHours.end : 24),
rowspan = 1;
//calculate the rowspan
if (options.displayOddEven) { rowspan += 1; }
if (options.displayFreeBusys) { rowspan += 1; }
if (rowspan > 1) {
rowspan = ' rowspan=\"' + rowspan + '\"';
}
else {
rowspan = '';
}
renderRow = '<tr class=\"wc-grid-row-timeslot\">';
renderRow += '<td class=\"wc-grid-timeslot-header\"' + rowspan + '></td>';
renderRow += '<td colspan=\"' + options.daysToShow * (showAsSeparatedUser ? options.users.length : 1) + '\">';
renderRow += '<div class=\"wc-no-height-wrapper wc-time-slot-wrapper\">';
renderRow += '<div class=\"wc-time-slots\">';
for (i = start; i < end; i++) {
for (j = 0; j < options.timeslotsPerHour - 1; j++) {
renderRow += '<div class=\"wc-time-slot\"></div>';
}
renderRow += '<div class=\"wc-time-slot wc-hour-end\"></div>';
}
renderRow += '</div>';
renderRow += '</div>';
renderRow += '</td>';
renderRow += '</tr>';
$(renderRow).appendTo($calendarTableTbody);
},
/**
* render the odd even columns
*/
_renderCalendarBodyOddEven: function($calendarTableTbody) {
if (this.options.displayOddEven) {
var options = this.options,
renderRow = '<tr class=\"wc-grid-row-oddeven\">',
showAsSeparatedUser = options.showAsSeparateUsers && options.users && options.users.length,
oddEven,
// let's take advantage of the jquery ui framework
oddEvenClasses = {'odd': 'wc-column-odd', 'even': 'ui-state-hover wc-column-even'};
//now let's display oddEven placeholders
for (var i = 1; i <= options.daysToShow; i++) {
if (!showAsSeparatedUser) {
oddEven = (oddEven == 'odd' ? 'even' : 'odd');
renderRow += '<td class=\"wc-day-column day-' + i + '\">';
renderRow += '<div class=\"wc-no-height-wrapper wc-oddeven-wrapper\">';
renderRow += '<div class=\"wc-full-height-column ' + oddEvenClasses[oddEven] + '\"></div>';
renderRow += '</div>';
renderRow += '</td>';
}
else {
var uLength = options.users.length;
for (var j = 0; j < uLength; j++) {
oddEven = (oddEven == 'odd' ? 'even' : 'odd');
renderRow += '<td class=\"wc-day-column day-' + i + '\">';
renderRow += '<div class=\"wc-no-height-wrapper wc-oddeven-wrapper\">';
renderRow += '<div class=\"wc-full-height-column ' + oddEvenClasses[oddEven] + '\" ></div>';
renderRow += '</div>';
renderRow += '</td>';
}
}
}
renderRow += '</tr>';
$(renderRow).appendTo($calendarTableTbody);
}
},
/**
* render the freebusy placeholders
*/
_renderCalendarBodyFreeBusy: function($calendarTableTbody) {
if (this.options.displayFreeBusys) {
var self = this, options = this.options,
renderRow = '<tr class=\"wc-grid-row-freebusy\">',
showAsSeparatedUser = options.showAsSeparateUsers && options.users && options.users.length;
renderRow += '</td>';
//now let's display freebusy placeholders
for (var i = 1; i <= options.daysToShow; i++) {
if (options.displayFreeBusys) {
if (!showAsSeparatedUser) {
renderRow += '<td class=\"wc-day-column day-' + i + '\">';
renderRow += '<div class=\"wc-no-height-wrapper wc-freebusy-wrapper\">';
renderRow += '<div class=\"wc-full-height-column wc-column-freebusy wc-day-' + i + '\"></div>';
renderRow += '</div>';
renderRow += '</td>';
}
else {
var uLength = options.users.length;
for (var j = 0; j < uLength; j++) {
renderRow += '<td class=\"wc-day-column day-' + i + '\">';
renderRow += '<div class=\"wc-no-height-wrapper wc-freebusy-wrapper\">';
renderRow += '<div class=\"wc-full-height-column wc-column-freebusy wc-day-' + i;
renderRow += ' wc-user-' + self._getUserIdFromIndex(j) + '\">';
renderRow += '</div>';
renderRow += '</div>';
renderRow += '</td>';
}
}
}
}
renderRow += '</tr>';
$(renderRow).appendTo($calendarTableTbody);