forked from vickramravichandran/angular-datepicker-light
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-datepicker-light.js
1404 lines (1148 loc) · 50.9 KB
/
angular-datepicker-light.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
(function (global, factory) {
if (typeof exports === 'object' && typeof module !== 'undefined') {
// commonJS
module.exports = factory(require('angular'), require('moment'));
}
else if (typeof define === 'function' && define.amd) {
// AMD
define(['module', 'angular', 'moment'], function (module, angular, moment) {
module.exports = factory(angular, moment);
});
}
else {
factory(global.angular, global.moment);
}
}(this, function (angular, moment) {
angular
.module('datepickerLightModule', [])
.service('datepickerLightService', datepickerLightService)
.directive('angularDatepickerLight', datepickerLightDirective);
// polyfill for Array.prototype.find
if (!angular.isFunction(Array.prototype.find)) {
Array.prototype.find = function (callback) {
var that = this;
var found;
for (var i = 0; i < that.length; i++) {
var item = that[i];
if (callback(item) === true) {
found = item;
break;
}
}
return found;
}
}
datepickerLightDirective.$inject = ["$compile", "$document", "$window", "$timeout", "$templateRequest", "datepickerLightService"];
function datepickerLightDirective($compile, $document, $window, $timeout, $templateRequest, datepickerLightService) {
return {
restrict: "A",
scope: {
options: '&angularDatepickerLight'
},
transclude: false,
controllerAs: "ctrl",
bindToController: true,
require: ["angularDatepickerLight", "?ngModel"],
link: postLinkFn,
controller: MainCtrl
}
function postLinkFn(scope, element, attrs, ctrls) {
var ctrl = ctrls[0]; //directive controller
ctrl.textModelCtrl = ctrls[1]; // textbox model controller
datepickerLightService.addDirectiveCtrl(ctrl);
// execute the options expression in the parent scope
var options = ctrl.options() || {
};
//wait for page load before initialization to avoid any missing selectors and other related issues.
$timeout(function () {
//load elements if string is provided as selector, to include updated references after page load
if(typeof(options.toggleTarget) == "string"){
options.toggleTarget = angular.element(options.toggleTarget);
}
if(typeof(options.altTarget) == "string"){
options.altTarget = angular.element(options.altTarget);
}
ctrl.init(angular.extend({
}, defaultOptions, options));
// store the jquery element on the controller
ctrl.target = element;
if(ctrl.isUiSelectEnabled())
initContainer(html_uiselect);
else
initContainer(html);
function initContainer(template) {
var templateFn = $compile(template);
ctrl.container = templateFn(scope);
if (angular.isDefined(ctrl.options.containerCssClass && ctrl.options.containerCssClass !== null)) {
ctrl.container.addClass(ctrl.options.containerCssClass);
}
// if a jquery parent is specified in options append the container
if (angular.isElement(ctrl.options.inline)) {
ctrl.options.inline.append(ctrl.container);
// else append container after the textbox
} else {
element.after(ctrl.container);
}
// if container is not inline, than make its position absolute
if (!ctrl.isInline()) {
ctrl.container.addClass("datepicker-absolute-container");
}
if(ctrl.hideTodayDateEnabled()) {
angular.element(".bottom-panel").addClass("hide-container")
}
// if altTarget is specified in options append the container
// altTarget supported only for non-inline
if (!ctrl.isInline() && angular.isElement(ctrl.options.altTarget)) {
// focus the textbox when the alt target(ex: image icon) is clicked
ctrl.options.altTarget.on("click focus", function (e) {
scope.$evalAsync(function () {
//If clicked element is inside specified altTarget, but it also happens to be toggleTarget, than ignore altTarget operation on its click while toggleTarget operation is performed on it.
//TODO: Remove JQuery dependency, i.e convert JQUery's is methods to pure JS/jqlite or angular versions
if($(e.target).is($(ctrl.options.toggleTarget)) ) {
return;
}
ctrl.activate();
});
});
}
// if altTarget is specified in options append the container
// altTarget supported only for non-inline
if (!ctrl.isInline() && angular.isElement(ctrl.options.toggleTarget)) {
// focus the textbox when the alt target(ex: image icon) is clicked
ctrl.options.toggleTarget.on("click focus", function (e) {
scope.$evalAsync(function () {
ctrl.toggleShow();
});
});
}
// prevents text select on mouse drag, dblclick
ctrl.container.css("MozUserSelect", "none").bind("selectstart", function () {
return false;
});
}
$document.ready(function(){
// activate all inline date pickers and call ready callback
scope.$evalAsync(function () {
if (ctrl.isInline()) {
ctrl.activate();
}
ctrl.ready();
});
})
// when the target(textbox) gets focus activate the corresponding container
element.on("click focus", function (e) {
scope.$evalAsync(function () {
ctrl.activate();
});
});
// when the target(textbox) changes
element.on("keydown", function (e) {
scope.$evalAsync(function () {
var term = element.val();
if (term.length === 0 || term === ctrl.targetText) {
return;
}
// wait few millisecs before trying to parse
// this allows checking if user has stopped typing
var delay = $timeout(function () {
// is term unchanged?
if (term == element.val()) {
ctrl.tryApplyDateFromTarget();
}
//cancel the timeout
$timeout.cancel(delay);
}, 300);
});
});
angular.element($window).on("resize", function (e) {
scope.$evalAsync(function () {
ctrl.hide();
});
})
// hide container upon CLICK outside of the dropdown rectangle region
$document.on("click", function (e) {
scope.$evalAsync(function () {
_documentClick(e);
});
});
// cleanup on destroy
var destroyFn = scope.$on('$destroy', function () {
if (ctrl.container) {
ctrl.container.remove();
ctrl.container = null;
}
destroyFn()
});
function _documentKeyDown(e) {
// hide inactive instances
datepickerLightService.hideIfInactive();
}
function _documentClick(e) {
//ignore if target which was clicked is inside container
var target = $(e.target);
//TODO: better detection of clicks on ui-select drop-downs, instead of mare checking of option class
//TODO: Remove JQuery dependency, i.e convert JQUery's has and is methods to pure JS/jqlite or angular versions
if(ctrl.container && $(ctrl.container[0]).has(target).length > 0 || target.hasClass('option') || target.parent().hasClass('option') ) {
return;
}
//ignore if toggleTarget or binded input field is clicked
if(ctrl.options.toggleTarget && $(ctrl.options.toggleTarget).has(target).length > 0 || target.is($(ctrl.target)) ){
return;
}
// hide inactive dropdowns
datepickerLightService.hideIfInactive();
// we care about the active non-inline one only
if (ctrl.instanceId !== ctrl.activeInstanceId() || ctrl.isInline()) {
return;
}
// no container. probably destroyed in scope $destroy
if (!ctrl.container) {
return;
}
//if no above condition was true, than it means click was outside the container and calender should get hide
ctrl.hide();
}
});
}
}
MainCtrl.$inject = ["$window", "$document", "datepickerLightService"];
function MainCtrl($window, $document, datepickerLightService) {
var that = this;
var activeInstanceId = 0,
minDate = null,
maxDate = null,
calendarItems = [];
var monthNamesLong = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
//var monthNamesShort = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
var dayNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
this.target = null;
this.targetText = null;
this.containerVisible = false;
this.validYears = [];
this.todayDate = null;
this.todayDateDisplay = null;
this.selectedData = null;
this.selectedMonth = null;
this.selectedYear = null;
this.dayNames = [];
this.weeks = [];
this.activeInstanceId = function () {
return activeInstanceId;
}
// hide any open containers other than the active container
this.hideIfInactive = function () {
if (that.isInline()) {
return;
}
// hide if this is not the active instance
if (that.instanceId !== activeInstanceId) {
that.hide();
}
}
this.isInline = function () {
// options.inline can be either true or a jquery object
return that.options.inline === true
|| angular.isElement(that.options.inline);
}
this.isUiSelectEnabled = function () {
// options.useAngularUiSelect can be either true or false
return that.options.useAngularUiSelect === true;
}
this.hideTodayDateEnabled = function () {
// options.useAngularUiSelect can be either true or false
return that.options.hideTodayDate === true;
}
this.init = function (options) {
that.options = options;
that.instanceId = ++instanceCount;
var now = new Date();
minDate = parseDate(that.options.minDate);
maxDate = parseDate(that.options.maxDate);
prevYers = that.options.prevYers;
nextYears = that.options.nextYears;
// set min and max date values
// if not provided in options default minDate to 1/1/(-5 years) and maxDate to 12/31/(+5 years) of next year
if (isUndefinedOrNull(minDate)) {
minDate = new Date(now.getFullYear() - prevYers, 0, 1);
}
// maxdate
if (isUndefinedOrNull(maxDate)) {
maxDate = new Date(now.getFullYear() + nextYears, 11, 31);
}
// create day names array starting at options.firstDayOfWeek
var startIndex = that.options.firstDayOfWeek;
// keep within valid range 0..6
if (startIndex < 0 || startIndex > 6) {
startIndex = that.options.firstDayOfWeek = 0;
}
while (that.dayNames.length < 7) {
that.dayNames.push(dayNames[startIndex]);
startIndex < 6 ? startIndex++ : startIndex = 0;
}
// create an object array of month names
// a simple simple string array will not work with ng-options
that.monthNames = [];
for (i = 0; i <= 11; i++) {
that.monthNames.push({
index: i,
name: monthNamesLong[i]
});
}
// if 'now' is outside valid date range default to minDate
if (isDateInRange(now)) {
that.todayDate = now;
}
else {
that.todayDate = minDate;
}
that.todayDateDisplay = formatDate(now, "ddd MMM DD YYYY");
var defaultDate = getDefaultDate();
that.selectedMonth = defaultDate.getMonth();
that.selectedYear = defaultDate.getFullYear();
that.selectedData = getCellData(defaultDate);
buildCalendar();
// build years array
for (i = minDate.getFullYear() ; i <= maxDate.getFullYear() ; i++) {
that.validYears.push(i);
}
}
this.tryApplyDateFromTarget = function () {
// textModelCtrl will be null if ng-model directive is not applied to the input element
// or the target is a non-input div, span etc. in which case we get the element using jquery
if (that.textModelCtrl === null) {
that.targetText = jQueryTargetValue();
}
else {
that.targetText = that.textModelCtrl.$viewValue;
}
var date = parseDate(that.targetText);
// if date is valid and in range build calendar if needed
if (date !== null && isDateInRange(date)) {
// sets the that.selectedMonth and that.selectedYear if different
var updated = setMonthYear(date);
// build calendar only if month or year changed
if (updated === true) {
buildCalendar();
}
applySelection(date, true);
}
}
this.activate = function () {
activeInstanceId = that.instanceId;
// update the the target with the current selected date if the target text is not a valid date
var targetValue = jQueryTargetValue();
if (targetValue === null || targetValue.length === 0 || parseDate(targetValue) === null) {
//if date in model is not valid and setCurrentDateWhenEmpty is true, set current date to model
if (!isUndefinedOrNull(that.selectedData) && that.options.setCurrentDateWhenEmpty) {
updateTargetModel(formatDate(that.selectedData.date));
}
}
else {
//updateTargetModel(formatDate(targetValue));
that.tryApplyDateFromTarget();
}
that.show();
}
this.ready = function() {
safeCallback(that.options.ready, methods);
}
this.monthChange = function () {
buildCalendar();
safeCallback(that.options.monthYearChanged, {
month: that.selectedMonth,
year: that.selectedYear
});
}
this.yearChange = function () {
buildCalendar();
safeCallback(that.options.monthYearChanged, {
month: that.selectedMonth,
year: that.selectedYear
});
}
this.gotoPreviousMonth = function (month, year) {
var my = getPreviousMonthYear(month, year);
that.selectedMonth = my.month;
that.selectedYear = my.year;
buildCalendar();
safeCallback(that.options.monthYearChange, {
month: that.selectedMonth,
year: that.selectedYear
});
}
this.gotoNextMonth = function (month, year) {
var my = getNextMonthYear(month, year);
that.selectedMonth = my.month;
that.selectedYear = my.year;
buildCalendar();
safeCallback(that.options.monthYearChange, {
month: that.selectedMonth,
year: that.selectedYear
});
}
this.dateCellCssClass = function (cellData) {
var css = {
};
var dateVisible = that.isDateVisible(cellData.date);
css["date-visible"] = dateVisible;
css["date-other-month"] = dateVisible && that.isOtherMonth(cellData.date);
css["date-selected"] = dateVisible && that.isDateSelected(cellData.date);
css["date-disabled"] = dateVisible && (cellData.enabled === false);
// custom css class added in callback
if (!isUndefinedOrNull(cellData.cssClass)) {
css[cellData.cssClass] = true;
}
return css;
}
this.dateDisplay = function (cellData) {
return formatDate(cellData.date, 'DD');
}
this.dateSelect = function (cellData) {
// sets the that.selectedMonth and that.selectedYear if different
// this is required if the selected date belongs to other month
var updated = setMonthYear(cellData.date);
// build calendar only if month or year changed
if (updated === true) {
buildCalendar();
}
applySelection(cellData.date);
that.hide();
}
this.todayDateSelect = function () {
// if date is not in range, do not select and do not hide
if (!isDateInRange(that.todayDate)) {
return;
}
// sets the that.selectedMonth and that.selectedYear if different
var updated = setMonthYear(that.todayDate);
// build calendar only if month or year changed
if (updated === true) {
buildCalendar();
}
applySelection(that.todayDate);
that.hide();
}
this.toggleShow = function() {
if(that.containerVisible){
this.hide();
}
else{
this.activate();
}
}
this.show = function () {
that.containerVisible = true;
// callback
safeCallback(that.options.datepickerShown);
}
this.hide = function () {
// do not hide if displayed inline
if (that.isInline()) {
return;
}
// exit if already hidden
if (that.containerVisible === false) {
return;
}
that.containerVisible = false;
// callback
safeCallback(that.options.datepickerHidden);
}
this.isDateVisible = function (date) {
// if date is from previous/next month show/hide as set in options
if (date.getMonth() !== that.selectedMonth) {
if (that.options.showOtherMonthDates === true) {
return true;
}
return false;
}
return true;
}
this.isOtherMonth = function (date) {
return date.getMonth() !== that.selectedMonth;
}
this.isDateSelected = function (date) {
if (!isUndefinedOrNull(that.selectedData) && angular.isObject(that.selectedData)) {
return areDatesEqual(date, that.selectedData.date);
}
return false;
}
this.isDateEnabled = function (date) {
var cellData = calendarItems.find(function (cellData) {
return areDatesEqual(cellData.date, date);
});
// object exists?
if (!isUndefinedOrNull(cellData)) {
return cellData.enabled === true;
}
// cell data with given date not found
return false;
}
this.getDate = function() {
if (!isUndefinedOrNull(that.selectedData) && angular.isObject(that.selectedData)) {
return that.selectedData.date;
}
}
this.setDate = function(dateToSelect) {
if (isUndefinedOrNull(dateToSelect)) {
return;
}
// is it a Date object?
if (angular.isDate(dateToSelect) && isDateInRange(dateToSelect)) {
that.dateSelect(getCellData(dateToSelect));
return;
}
// parse string
if (angular.isString(dateToSelect)) {
var date = parseDate(dateToSelect);
if (!isUndefinedOrNull(date) && isDateInRange(date)) {
that.dateSelect(getCellData(date));
}
}
}
function getDefaultDate() {
var defaultDate;
// if defaultDate is not provided default to today
if (isUndefinedOrNull(that.options.defaultDate)) {
return that.todayDate;
}
// is it a date instance?
if (angular.isDate(that.options.defaultDate)) {
defaultDate = that.options.defaultDate;
}
// try parsing the 'defaultDate' from options
else {
var date = parseDate(that.options.defaultDate);
// if parsing failed set default to todayDate
if (date === null) {
defaultDate = that.todayDate;
}
else {
defaultDate = date;
}
}
// at this point defaultDate is either 'today' or 'options.defaultDate'
// if defaultDate is outside valid date range default to todayDate
if (!isDateInRange(defaultDate)) {
defaultDate = that.todayDate;
}
return defaultDate;
}
function getPreviousMonthYear(month, year) {
var monthCopy = month,
yearCopy = year;
if (month > 0) {
month--;
} else if (month === 0) {
month = 11;
year--;
}
if (isMonthYearInRange(month, year)) {
return {
month: month,
year: year
};
}
return {
month: monthCopy,
year: yearCopy
};
}
function getNextMonthYear(month, year) {
var monthCopy = month,
yearCopy = year;
if (month < 11) {
month++;
} else if (month === 11) {
month = 0;
year++;
}
if (isMonthYearInRange(month, year)) {
return {
month: month,
year: year
};
}
return {
month: monthCopy,
year: yearCopy
};
}
// returns true if the month and year are within min date and max date
function isMonthYearInRange (month, year) {
// adjust month to 1-based because format date returns month as 1-based
var monthPlusOne = month + 1;
var leadingZero = monthPlusOne < 10 ? "0" : "";
if (parseInt(year + leadingZero + monthPlusOne) < parseInt(formatDate(minDate, 'YYYYMM'))
|| parseInt(year + leadingZero + monthPlusOne) > parseInt(formatDate(maxDate, 'YYYYMM'))) {
return false;
}
return true;
}
// sets that.selectedMonth and that.selectedYear if different.
// returns true if the properties were set
function setMonthYear(date) {
var month = date.getMonth();
var year = date.getFullYear();
// update properties if different
if (month !== that.selectedMonth || year !== that.selectedYear) {
that.selectedMonth = month;
that.selectedYear = year;
return true;
}
return false;
}
// maybe there is a better way to do this
// 1. start counting the days from the left side (firstDayOfWeek)
// 2. increment firstDayOfWeek to mimic moving towards the right side (firstDayOfMonth)
// 3. if firstDayOfWeek becomes > 6 reset to 0
// continue until firstDayOfWeek === firstDayOfMonth
function getDaysBeforeFirstDayOfMonth(firstDayOfWeek, firstDayOfMonth) {
if (firstDayOfMonth === firstDayOfWeek
|| firstDayOfMonth < 0 || firstDayOfMonth > 6
|| firstDayOfWeek < 0 || firstDayOfWeek > 6) {
return;
}
var daysBefore = 0;
while (firstDayOfWeek !== firstDayOfMonth) {
daysBefore++;
firstDayOfWeek++;
if (firstDayOfWeek > 6) {
firstDayOfWeek = 0;
}
}
return daysBefore;
}
// build the calendar array
function buildCalendar() {
var year = that.selectedYear;
var month = that.selectedMonth;
var firstDateOfMonth = getDate(year, month, 1);
var firstDayOfMonth = firstDateOfMonth.getDay();
var firstDayOfWeek = that.options.firstDayOfWeek;
var rowIndex = 0,
datesInWeek = 0,
date = 1;
calendarItems = [];
that.weeks = [];
// if first day of month != firstDayOfWeek then start dates from prior month
if (firstDayOfWeek != firstDayOfMonth) {
var daysBefore = getDaysBeforeFirstDayOfMonth(firstDayOfWeek, firstDayOfMonth);
if (!isUndefinedOrNull(daysBefore)) {
// 0 is one day prior; 1 is two days prior and so forth
date = date - daysBefore;
}
}
while (date <= getDaysInMonth(year, month)) {
calendarItems.push(getCellData(getDate(year, month, date++)));
}
// fill remaining cells with dates from next month
while ((calendarItems.length % 7) !== 0) {
calendarItems.push(getCellData(getDate(year, month, date++)));
}
// populate the that.weeks array. create a 2D array of 7 days per row
angular.forEach(calendarItems, function (cellData) {
if ((datesInWeek % 7) === 0) {
that.weeks.push([]);
rowIndex = that.weeks.length - 1;
}
that.weeks[rowIndex].push(cellData);
datesInWeek++;
});
//raise the callback for each cell data
raiseRenderDateCallback(calendarItems);
}
function raiseRenderDateCallback(cellDataCollection) {
// raise the callback for each date
angular.forEach(cellDataCollection, function (cellData) {
// if date is outside min/max date range set to disable.
// do not callback for dates outside range
if (!isDateInRange(cellData.date)) {
cellData.enabled = false;
return;
}
// callback for each date
var cbRetVal = safeCallback(that.options.renderDate, {
date: cellData.date
});
// pass an empty literal in case nothing was returned from callback
copySupportedProperties(cbRetVal || {
}, cellData);
});
}
function copySupportedProperties(cbRetVal, cellData) {
// if callbackArgs.enabled is undefined default to true
if (isUndefinedOrNull(cbRetVal.enabled)) {
cellData.enabled = true;
}
else {
cellData.enabled = cbRetVal.enabled;
}
// in order to set selected = true, enabled must also be true
if (cbRetVal.selected === true && cellData.enabled === true) {
cellData.selected = true;
}
else {
cellData.selected = cbRetVal.selected;
}
// css class to apply to the date <td>
cellData.cssClass = cbRetVal.cssClass
// arbitrary custom data to store with date
cellData.data = cbRetVal.data;
// tooltip for the date <td>
cellData.tooltip = cbRetVal.tooltip;
}
function postRenderDateCallback(dateToSelect) {
// after the renderDate callbacks find the object with cellData.selected == true.
// this might have been set on the cellData during callback
var selectedCellData = calendarItems.find(function (cellData) {
// must be enabled to be able to select
return cellData.selected === true && cellData.enabled === true;
});
// if no object exists with 'selected' = true perform the following:
// 1. if 'dateToSelect' is provided find the cell data with that date
// 2. select the cellData if it exists and the selected property is not set
if (isUndefinedOrNull(selectedCellData)) {
if (angular.isDate(dateToSelect)) {
var cellData = calendarItems.find(function (cellData) {
// must be enabled to be able to select
return areDatesEqual(cellData.date, dateToSelect)
&& cellData.enabled === true;
});
// set 'selected' to true only if its undefined/null
if (angular.isObject(cellData) && isUndefinedOrNull(cellData.selected)) {
selectedCellData = cellData;
}
}
}
return selectedCellData;
}
function getDate(year, month, day) {
return new Date(year, month, day);
}
function getDaysInMonth(year, month) {
return moment().year(year).month(month).daysInMonth();
}
function getCellData(date) {
return {
date: date,
enabled: true,
selected: angular.undefined,
tooltip: angular.undefined,
cssClass: angular.undefined,
data: angular.undefined,
}
}
function applySelection(dateToSelect, updatingFromTarget) {
var cellData = postRenderDateCallback(dateToSelect);
if (isUndefinedOrNull(cellData)) {
return;
}
// do not select if disabled
if (cellData.enabled === false) {
return;
}
var cbRetVal = safeCallback(that.options.beforeDateSelect, {
date: cellData.date,
data: cellData.data
});
// cancel selection if set to true from callback
if (!isUndefinedOrNull(cbRetVal) && cbRetVal.cancel === true) {
return;
}
that.selectedMonth = cellData.date.getMonth();
that.selectedYear = cellData.date.getFullYear();
that.selectedData = cellData;
if (updatingFromTarget !== true) {
updateTargetModel(formatDate(that.selectedData.date));
}
safeCallback(that.options.dateSelected, {
date: cellData.date,
data: cellData.data
});
}
function parseDate(value) {
var mom = moment(value, that.options.dateFormat);
if (!mom.isValid()) {
return null;
}
return mom.toDate();
}
function formatDate(date, format) {
var mom = moment(date);
if (!mom.isValid()) {
return;
}
// use the format argument if provided
if (angular.isString(format)) {
return mom.format(format);
}
return mom.format(that.options.dateFormat);
}
function isDateInRange(date) {
var momDate = moment(date);
//if min date is defined
if( that.options.minDate ) {
var momMinDate = moment(minDate);
// and max date is also defined