Repository navigation
Expand file tree
/
Copy pathjsCalendar.js
More file actions
1480 lines (1326 loc) · 47.1 KB
/
Copy pathjsCalendar.js
File metadata and controls
1480 lines (1326 loc) · 47.1 KB
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
/*
* jsCalendar v1.4.4
*
*
* MIT License
*
* Copyright (c) 2019 Grammatopoulos Athanasios-Vasileios
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
var jsCalendar = (function(){
// Constructor
function JsCalendar(){
// No parameters
if (arguments.length === 0) {
// Do nothing
return;
}
else {
// Construct calendar
this._construct(arguments);
}
}
// Version
JsCalendar.version = 'v1.4.4';
// Sub-Constructor
JsCalendar.prototype._construct = function(args) {
// Parse arguments
args = this._parseArguments(args);
// Set a target
this._setTarget(args.target);
// Init calendar
this._init(args.options);
// Init target
this._initTarget();
// Set date
this._setDate(
(args.date !== null) ? args.date :
(this._target.dataset.hasOwnProperty('date')) ? this._target.dataset.date :
new Date()
);
// If invalid date
if (!this._now) throw new Error('jsCalendar: Date is outside range.');
// Create
this._create();
// Update
this._update();
};
// Languages
JsCalendar.prototype.languages = {
// Default English language
en : {
// Months Names
months : ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
// Days Names
days : ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
// Default handlers
_dateStringParser : function(key, date) {return JsCalendar._defaultDateStringParser(key, date, this);},
_dayStringParser : function(key, date) {return JsCalendar._defaultDayStringParser(key, date, this);}
}
};
// Init calendar
JsCalendar.prototype._init = function(options) {
// Init elements object
this._elements = {};
// Events init
this._events = {};
this._events.date = [];
this._events.month = [];
this._events.day_render = [];
this._events.date_render = [];
this._events.month_render = [];
// Dates variables
this._now = null;
this._date = null;
this._selected = [];
// Language object
this.language = {};
// Parse options
this._parseOptions(options);
};
// Parse options
JsCalendar.prototype._parseArguments = function(args) {
// Arguments object
var obj = {
target : null,
date : null,
options : {}
};
// If no arguments
if (args.length === 0) {
// Throw an error
throw new Error('jsCalendar: No parameters were given.');
}
// Only 1 argument
else if (args.length === 1) {
// If target element
if (
(
// If html element
((typeof HTMLElement === 'object') ? (args[0] instanceof HTMLElement) : args[0]) &&
(typeof args[0] === 'object') && (args[0] !== null) && (args[0].nodeType === 1) &&
(typeof args[0].nodeName === 'string')
) || (
// Or string
typeof args[0] === 'string'
)
) {
obj.target = args[0];
}
// Options argument
else {
// Init arguments
obj.options = args[0];
// Get target
if (typeof args[0].target !== 'undefined') {
obj.target = args[0].target;
}
else {
// Throw an error
throw new Error('jsCalendar: Not target was given.');
}
// Get date
if (typeof args[0].date !== 'undefined') {
obj.date = args[0].date;
}
}
}
// Many arguments
else {
// First is target
obj.target = args[0];
// If date
if (args.length >= 2) {
obj.date = args[1];
}
// If options
if (args.length >= 3) {
obj.options = args[2];
}
}
// Return object
return obj;
};
// Default options
JsCalendar.options = {
language : 'en',
zeroFill : false,
monthFormat : 'month',
dayFormat : 'D',
firstDayOfTheWeek : 1,
navigator : true,
navigatorPosition : 'both',
min : false,
max : false,
onMonthRender : false,
onDayRender : false,
onDateRender : false
};
// Parse options
JsCalendar.prototype._parseOptions = function(doptions) {
// Options Object
this._options = {};
// Input options object (dirty)
var options = {};
// Load default and input options
for (var item in JsCalendar.options) {
// Default options
if (JsCalendar.options.hasOwnProperty(item)) {
this._options[item] = JsCalendar.options[item];
}
// Dynamic options
if (doptions.hasOwnProperty(item)) {
options[item] = doptions[item];
}
// Dataset options
else if (this._target.dataset.hasOwnProperty(item)) {
options[item] = this._target.dataset[item];
}
}
// Check options
if (typeof options.zeroFill !== 'undefined'){
if (options.zeroFill === 'false' || !options.zeroFill) {
this._options.zeroFill = false;
}
else {
this._options.zeroFill = true;
}
}
if (typeof options.monthFormat !== 'undefined'){
this._options.monthFormat = options.monthFormat;
}
if (typeof options.dayFormat !== 'undefined'){
this._options.dayFormat = options.dayFormat;
}
if (typeof options.navigator !== 'undefined'){
if (options.navigator === 'false' || !options.navigator) {
this._options.navigator = false;
}
else {
this._options.navigator = true;
}
}
if (typeof options.navigatorPosition !== 'undefined'){
this._options.navigatorPosition = options.navigatorPosition;
}
// Language
if (typeof options.language === 'string' && typeof this.languages[options.language] !== 'undefined'){
this._options.language = options.language;
}
// Set language
this.setLanguage(this._options.language);
// Set first day of the week
if (typeof options.fdotw !== 'undefined'){
options.firstDayOfTheWeek = options.fdotw;
}
if (typeof options.firstDayOfTheWeek !== 'undefined'){
// If day number
if (typeof options.firstDayOfTheWeek === 'number') {
// Range check (no need to check for bigger than 7 but I don't trust anyone)
if (options.firstDayOfTheWeek >= 1 && options.firstDayOfTheWeek <= 7) {
this._options.firstDayOfTheWeek = options.firstDayOfTheWeek;
}
}
// If string
if (typeof options.firstDayOfTheWeek === 'string') {
// If day number
if (options.firstDayOfTheWeek.match(/^[1-7]$/)) {
this._options.firstDayOfTheWeek = parseInt(options.firstDayOfTheWeek, 10);
}
// else use it as a day name
else {
// So find day
this._options.firstDayOfTheWeek = this.language.days.indexOf(options.firstDayOfTheWeek) + 1;
// Range check (no need to check for bigger then 7 but I don't trust anyone)
if (this._options.firstDayOfTheWeek < 1 || this._options.firstDayOfTheWeek > 7) {
this._options.firstDayOfTheWeek = 1;
}
}
}
}
// Set min calendar date
if (typeof options.min !== 'undefined' && options.min !== 'false' && options.min !== false) {
// Parse date
this._options.min = tools.parseDate(options.min);
}
// Set max calendar date
if (typeof options.max !== 'undefined' && options.max !== 'false' && options.max !== false) {
// Parse date
this._options.max = tools.parseDate(options.max);
}
// Set render handlers
if (typeof options.onMonthRender !== 'undefined') {
// Passed as function name string
if (
typeof options.onMonthRender === 'string' &&
typeof window[options.onMonthRender] === 'function'
) {
this._on('month_render', window[options.onMonthRender]);
}
// Passed as function
else if (typeof options.onMonthRender === 'function') {
this._on('month_render', options.onMonthRender);
}
}
if (typeof options.onDayRender !== 'undefined') {
// Passed as function name string
if (
typeof options.onDayRender === 'string' &&
typeof window[options.onDayRender] === 'function'
) {
this._on('day_render', window[options.onDayRender]);
}
// Passed as function
else if (typeof options.onDayRender === 'function') {
this._on('day_render', options.onDayRender);
}
}
if (typeof options.onDateRender !== 'undefined') {
// Passed as function name string
if (
typeof options.onDateRender === 'string' &&
typeof window[options.onDateRender] === 'function'
) {
this._on('date_render', window[options.onDateRender]);
}
// Passed as function
else if (typeof options.onDateRender === 'function') {
this._on('date_render', options.onDateRender);
}
}
};
// Set target
JsCalendar.prototype._setTarget = function(element) {
// Parse target
var target = tools.getElement(element);
// If target not found
if (!target) {
// Throw an error
throw new Error('jsCalendar: Target was not found.');
}
else {
// Save element
this._target = target;
// Link object to list
var id = this._target.id;
if (id && id.length > 0) {
jsCalendarObjects['#' + id] = this;
}
}
};
// Init target
JsCalendar.prototype._initTarget = function() {
// Add class
if (this._target.className.length > 0){
this._target.className += ' ';
}
this._target.className += 'jsCalendar';
// Create table
this._elements.table = document.createElement('table');
// Create table header
this._elements.head = document.createElement('thead');
this._elements.table.appendChild(this._elements.head);
// Create table body
this._elements.body = document.createElement('tbody');
this._elements.table.appendChild(this._elements.body);
// Insert on page
this._target.appendChild(this._elements.table);
};
// Check if date in range
JsCalendar.prototype._isDateInRange = function(date) {
// If no range
if (this._options.min === false && this._options.max === false) {
return true;
}
// Parse date
date = tools.parseDate(date);
// Check min
if (this._options.min !== false && this._options.min.getTime() > date.getTime()) {
return false;
}
// Check max
if (this._options.max !== false && this._options.max.getTime() < date.getTime()) {
return false;
}
// In range
return true;
};
// Set a Date
JsCalendar.prototype._setDate = function(date) {
// Parse date
date = tools.parseDate(date);
// Check date not in range
if (!this._isDateInRange(date)) {
return;
}
// Set data
this._now = date;
this._date = new Date(this._now.getFullYear(), this._now.getMonth(), 1);
};
// Convert to date string
JsCalendar.prototype._parseToDateString = function(date, format) {
var lang = this.language;
return format.replace(/(MONTH|month|MMM|mmm|mm|m|MM|M|DAY|day|DDD|ddd|dd|d|DD|D|YYYY|yyyy)/g, function(key) {
return lang.dateStringParser(key, date);
});
};
// Get visible month
JsCalendar.prototype._getVisibleMonth = function(date) {
// For date
if (typeof date === 'undefined') {
// Get saved date
date = this._date;
}
else {
date = tools.parseDate(date);
}
// Get month's first day
var first = new Date(date.getTime());
first.setDate(1);
// First day of the month index
var firstDay = first.getDay() - (this._options.firstDayOfTheWeek - 1);
if (firstDay < 0) {
firstDay += 7;
}
// Get month's name
var lang = this.language;
var name = this._options.monthFormat.replace(/(MONTH|month|MMM|mmm|##|#|YYYY|yyyy)/g, function(key) {
return lang.dateStringParser(key, first);
});
// Get visible days
var days = this._getVisibleDates(date);
var daysInMonth = new Date(first.getYear() + 1900, first.getMonth() + 1, 0).getDate();
var current = -1;
// If this is the month
if (first.getYear() === this._now.getYear() && first.getMonth() === this._now.getMonth()) {
// Calculate current
current = firstDay + this._now.getDate() - 1;
}
// Return object
return {
name : name,
days : days,
start : firstDay + 1,
current : current,
end : firstDay + daysInMonth
};
};
// Get visible dates
JsCalendar.prototype._getVisibleDates = function(date) {
// For date
if (typeof date === 'undefined') {
// Get saved date
date = this._date;
}
else {
date = tools.parseDate(date);
}
// Visible days array
var dates = [];
// Get first day of the month
var first = new Date(date.getTime());
first.setDate(1);
first.setHours(0, 0, 0, 0);
// Count days of previous month to show
var previous = first.getDay() - (this._options.firstDayOfTheWeek - 1);
if (previous < 0) {
previous += 7;
}
// Set day to month's first
var day = new Date(first.getTime());
// Previous month's days
while (previous > 0) {
// Calculate previous day
day.setDate(day.getDate() - 1);
// Add page on frond of the list
dates.unshift(new Date(day.getTime()));
// Previous
previous --;
}
// Set day to month's first
day = new Date(first.getTime());
// This month's days
do {
// Add page on back of the list
dates.push(new Date(day.getTime()));
// Calculate next day
day.setDate(day.getDate() + 1);
// Repeat until next month
} while (day.getDate() !== 1);
// Next month's days
var next = 42 - dates.length;
// Add days left
while (next > 0) {
// Add page on back of the list
dates.push(new Date(day.getTime()));
// Calculate next day
day.setDate(day.getDate() + 1);
// Next
next --;
}
// Return days
return dates;
};
// Create calendar
JsCalendar.prototype._create = function() {
var i, j;
// Save instance
var that = this;
// Set created flag
this._elements.created = true;
// Head rows
this._elements.headRows = [];
for (i = 0; i < 2; i++) {
this._elements.headRows.push(document.createElement('tr'));
this._elements.head.appendChild(this._elements.headRows[i]);
}
// Month row
var title_header = document.createElement('th');
title_header.setAttribute('colspan', 7);
this._elements.headRows[0].className = 'jsCalendar-title-row';
this._elements.headRows[0].appendChild(title_header);
this._elements.headLeft = document.createElement('div');
this._elements.headLeft.className = 'jsCalendar-title-left';
title_header.appendChild(this._elements.headLeft);
this._elements.month = document.createElement('div');
this._elements.month.className = 'jsCalendar-title-name';
title_header.appendChild(this._elements.month);
this._elements.headRight = document.createElement('div');
this._elements.headRight.className = 'jsCalendar-title-right';
title_header.appendChild(this._elements.headRight);
// Navigation
if (this._options.navigator) {
this._elements.navLeft = document.createElement('div');
this._elements.navLeft.className = 'jsCalendar-nav-left';
this._elements.navRight = document.createElement('div');
this._elements.navRight.className = 'jsCalendar-nav-right';
if (this._options.navigatorPosition === 'left') {
this._elements.headLeft.appendChild(this._elements.navLeft);
this._elements.headLeft.appendChild(this._elements.navRight);
}
else if (this._options.navigatorPosition === 'right') {
this._elements.headRight.appendChild(this._elements.navLeft);
this._elements.headRight.appendChild(this._elements.navRight);
}
else {
this._elements.headLeft.appendChild(this._elements.navLeft);
this._elements.headRight.appendChild(this._elements.navRight);
}
// Event listeners
this._elements.navLeft.addEventListener('click', function(event){
that.previous();
var date = new Date(that._date.getTime());
date.setDate(1);
that._eventFire('month', date, event);
}, false);
this._elements.navRight.addEventListener('click', function(event){
that.next();
var date = new Date(that._date.getTime());
date.setDate(1);
that._eventFire('month', date, event);
}, false);
}
// Days row
this._elements.headRows[1].className = 'jsCalendar-week-days';
title_header.className = 'jsCalendar-title';
this._elements.days = [];
for (i = 0; i < 7; i++) {
this._elements.days.push(document.createElement('th'));
this._elements.headRows[1].appendChild(this._elements.days[
this._elements.days.length - 1
]);
}
// Body rows
this._elements.bodyRows = [];
this._elements.bodyCols = [];
// 6 rows
for (i = 0; i < 6; i++) {
this._elements.bodyRows.push(document.createElement('tr'));
this._elements.body.appendChild(this._elements.bodyRows[i]);
// 7 days
for (j = 0; j < 7; j++) {
this._elements.bodyCols.push(document.createElement('td'));
this._elements.bodyRows[i].appendChild(this._elements.bodyCols[i * 7 + j]);
this._elements.bodyCols[i * 7 + j].addEventListener('click', (function(index){
return function (event) {
that._eventFire('date', that._active[index], event);
};
})(i * 7 + j), false);
}
}
};
// Select dates on calendar
JsCalendar.prototype._selectDates = function(dates) {
// Copy array instance
dates = dates.slice();
// Parse dates
for (var i = 0; i < dates.length; i++) {
dates[i] = tools.parseDate(dates[i]);
dates[i].setHours(0, 0, 0, 0);
dates[i] = dates[i].getTime();
}
// Insert dates on array
for (i = dates.length - 1; i >= 0; i--) {
// If not already selected
if (this._selected.indexOf(dates[i]) < 0) {
this._selected.push(dates[i]);
}
}
};
// Un-select dates on calendar
JsCalendar.prototype._unselectDates = function(dates) {
// Copy array instance
dates = dates.slice();
// Parse dates
for (var i = 0; i < dates.length; i++) {
dates[i] = tools.parseDate(dates[i]);
dates[i].setHours(0, 0, 0, 0);
dates[i] = dates[i].getTime();
}
// Remove dates of the array
var index;
for (i = dates.length - 1; i >= 0; i--) {
// If selected
index = this._selected.indexOf(dates[i]);
if (index >= 0) {
this._selected.splice(index, 1);
}
}
};
// Unselect all dates on calendar
JsCalendar.prototype._unselectAllDates = function() {
// While not empty
while (this._selected.length) {
this._selected.pop();
}
};
// Update calendar
JsCalendar.prototype._update = function() {
// Get month info
var month = this._getVisibleMonth(this._date);
// Save data
this._active = month.days.slice();
// Update month name
this._elements.month.textContent = month.name;
// Check zeros filling
var prefix = (this._options.zeroFill) ? '0' : '';
// Populate days
var text;
for (var i = month.days.length - 1; i >= 0; i--) {
text = month.days[i].getDate();
this._elements.bodyCols[i].textContent = (text < 10 ? prefix + text : text);
// If date is selected
if (this._selected.indexOf(month.days[i].getTime()) >= 0) {
this._elements.bodyCols[i].className = 'jsCalendar-selected';
}
else {
this._elements.bodyCols[i].removeAttribute('class');
}
}
// Previous month
for (i = 0; i < month.start - 1; i++) {
this._elements.bodyCols[i].className = 'jsCalendar-previous';
}
// Current day
if (month.current >= 0) {
if (this._elements.bodyCols[month.current].className.length > 0) {
this._elements.bodyCols[month.current].className += ' jsCalendar-current';
}
else {
this._elements.bodyCols[month.current].className = 'jsCalendar-current';
}
}
// Next month
for (i = month.end; i < month.days.length; i++) {
this._elements.bodyCols[i].className = 'jsCalendar-next';
}
// Set days of the week locale
for (i = 0; i < 7; i++) {
var that = this;
this._elements.days[i].textContent = this._options.dayFormat.replace(/(DAY|day|DDD|ddd|DD|dd|D)/g, function(key) {
return that.language.dayStringParser(
key,
(i + that._options.firstDayOfTheWeek - 1) % 7
);
});
}
// Call render handlers
var j;
if (this._events.month_render.length > 0) {
var date = month.days[month.start];
// Clear any style
this._elements.month.removeAttribute('style');
// Call the render handlers
for (j = 0; j < this._events.month_render.length; j++) {
this._events.month_render[j].call(this,
// Month index
date.getMonth(),
// Pass the html element
this._elements.month,
// Info about that month
{
start : new Date(date.getTime()),
end : new Date(date.getFullYear(), date.getMonth() + 1, 0, 23, 59, 59, 999),
numberOfDays : month.end - month.start + 1
}
);
}
}
if (this._events.day_render.length > 0) {
for (i = 0; i < 7; i++) {
// Clear any style
this._elements.days[i].removeAttribute('style');
// Call the render handler
for (j = 0; j < this._events.day_render.length; j++) {
this._events.day_render[j].call(this,
// Day index
(i + this._options.firstDayOfTheWeek - 1) % 7,
// Pass the html element
this._elements.days[i],
// Info about that day
{
position : i
}
);
}
}
}
if (this._events.date_render.length > 0) {
for (i = 0; i < month.days.length; i++) {
// Clear any style
this._elements.bodyCols[i].removeAttribute('style');
// Call the render handler
for (j = 0; j < this._events.date_render.length; j++) {
this._events.date_render[j].call(this,
// Date should be clonned
new Date(month.days[i].getTime()),
// Pass the html element
this._elements.bodyCols[i],
// Info about that date
{
isCurrent : (month.current == i),
isSelected : (this._selected.indexOf(month.days[i].getTime()) >= 0),
isPreviousMonth : (i < month.start),
isCurrentMonth : (month.start <= i && i <= month.end),
isNextMonth : (month.end < i),
position : {x: i%7, y: Math.floor(i/7)}
}
);
}
}
}
};
// Fire all event listeners
JsCalendar.prototype._eventFire = function(name, date, event) {
if (!this._events.hasOwnProperty(name)) return;
// Search events
for (var i = 0; i < this._events[name].length; i++) {
(function(callback, instance) {
// Call asynchronous
setTimeout(function(){
// Call callback
callback.call(instance, event, new Date(date.getTime()));
}, 0);
})(this._events[name][i], this);
}
};
// Add a event listener
// This method will be exposed on the future
JsCalendar.prototype._on = function(name, callback) {
// If callback is a function
if(typeof callback === 'function'){
// Add to the list
this._events[name].push(callback);
}
// Not a function
else {
// Throw an error
throw new Error('jsCalendar: Invalid callback function.');
}
// Return
return this;
};
// Add a event listeners
JsCalendar.prototype.onDateClick = function(callback) {
return this._on('date', callback);
};
JsCalendar.prototype.onMonthChange = function(callback) {
return this._on('month', callback);
};
JsCalendar.prototype.onDayRender = function(callback) {
return this._on('day_render', callback);
};
JsCalendar.prototype.onDateRender = function(callback) {
return this._on('date_render', callback);
};
JsCalendar.prototype.onMonthRender = function(callback) {
return this._on('month_render', callback);
};
// Goto a date
JsCalendar.prototype.set = function(date){
// Set new date
this._setDate(date);
// Refresh
this.refresh();
// Return
return this;
};
// Set min date
JsCalendar.prototype.min = function(date){
// If value
if (date) {
// Set min date
this._options.min = tools.parseDate(date);
}
// Disable
else {
this._options.min = false;
}
// Refresh
this.refresh();
// Return
return this;
};
// Set max date
JsCalendar.prototype.max = function(date){
// If value
if (date) {
// Set max date
this._options.max = tools.parseDate(date);
}
// Disable
else {
this._options.max = false;
}
// Refresh
this.refresh();
// Return
return this;
};
// Refresh
// Safe _update
JsCalendar.prototype.refresh = function(date) {
// If date provided
if (typeof date !== 'undefined') {
// If date is in range
if (this._isDateInRange(date)) {
this._date = tools.parseDate(date);
}
}
// If calendar elements ready
if (this._elements.created === true) {
this._update();
}
// Return
return this;
};
// Next month
JsCalendar.prototype.next = function(n){
// Next number
if (typeof n !== 'number') {
n = 1;
}
// Calculate date
var date = new Date(this._date.getFullYear(), this._date.getMonth() + n, 1);
// If date is not in range
if (!this._isDateInRange(date)) {
return this;
}
// Set date
this._date = date;
this.refresh();
// Return
return this;
};
// Next month
JsCalendar.prototype.previous = function(n){
// Next number
if (typeof n !== 'number') {
n = 1;
}
// Calculate date (last day of previous month)
var date = new Date(this._date.getFullYear(), this._date.getMonth() - n + 1, 0);
// If date is not in range
if (!this._isDateInRange(date)) {
return this;
}
// Set date
this._date = date;
this.refresh();
// Return
return this;
};
// Goto a date
JsCalendar.prototype.goto = function(date){
this.refresh(date);
// Return
return this;
};
// Reset to the date
JsCalendar.prototype.reset = function(){
this.refresh(this._now);
// Return
return this;
};
// Select dates
JsCalendar.prototype.select = function(dates){
// If no arguments
if (typeof dates === 'undefined') {
// Return
return this;
}
// If dates not array
if (!(dates instanceof Array)) {
// Lets make it an array
dates = [dates];
}