This repository has been archived by the owner on Dec 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
datepicker.js
712 lines (622 loc) · 27.9 KB
/
datepicker.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
'use strict';
angular.module('mgcrea.ngStrap.datepicker', [
'mgcrea.ngStrap.helpers.dateParser',
'mgcrea.ngStrap.helpers.dateFormatter',
'mgcrea.ngStrap.tooltip'])
.provider('$datepicker', function () {
var defaults = this.defaults = {
animation: 'am-fade',
// Uncommenting the following line will break backwards compatability
// prefixEvent: 'datepicker',
prefixClass: 'datepicker',
placement: 'bottom-left',
templateUrl: 'datepicker/datepicker.tpl.html',
trigger: 'focus',
container: false,
keyboard: true,
html: false,
delay: 0,
// lang: $locale.id,
useNative: false,
dateType: 'date',
dateFormat: 'shortDate',
timezone: null,
modelDateFormat: null,
dayFormat: 'dd',
monthFormat: 'MMM',
yearFormat: 'yyyy',
monthTitleFormat: 'MMMM yyyy',
yearTitleFormat: 'yyyy',
strictFormat: false,
autoclose: false,
minDate: -Infinity,
maxDate: +Infinity,
startView: 0,
minView: 0,
startWeek: 0,
daysOfWeekDisabled: '',
hasToday: false,
hasClear: false,
iconLeft: 'glyphicon glyphicon-chevron-left',
iconRight: 'glyphicon glyphicon-chevron-right'
};
this.$get = function ($window, $document, $rootScope, $sce, $dateFormatter, datepickerViews, $tooltip, $timeout) {
var isNative = /(ip[ao]d|iphone|android)/ig.test($window.navigator.userAgent);
var isTouch = ('createTouch' in $window.document) && isNative;
if (!defaults.lang) defaults.lang = $dateFormatter.getDefaultLocale();
function DatepickerFactory (element, controller, config) {
var $datepicker = $tooltip(element, angular.extend({}, defaults, config));
var parentScope = config.scope;
var options = $datepicker.$options;
var scope = $datepicker.$scope;
if (options.startView) options.startView -= options.minView;
// View vars
var pickerViews = datepickerViews($datepicker);
$datepicker.$views = pickerViews.views;
var viewDate = pickerViews.viewDate;
scope.$mode = options.startView;
scope.$iconLeft = options.iconLeft;
scope.$iconRight = options.iconRight;
scope.$hasToday = options.hasToday;
scope.$hasClear = options.hasClear;
var $picker = $datepicker.$views[scope.$mode];
// Scope methods
scope.$select = function (date, disabled) {
if (disabled) return;
$datepicker.select(date);
};
scope.$selectPane = function (value) {
$datepicker.$selectPane(value);
};
scope.$toggleMode = function () {
$datepicker.setMode((scope.$mode + 1) % $datepicker.$views.length);
};
scope.$setToday = function () {
if (options.autoclose) {
$datepicker.setMode(0);
$datepicker.select(new Date());
} else {
$datepicker.select(new Date(), true);
}
};
scope.$clear = function () {
if (options.autoclose) {
$datepicker.setMode(0);
$datepicker.select(null);
} else {
$datepicker.select(null, true);
}
};
// Public methods
$datepicker.update = function (date) {
// console.warn('$datepicker.update() newValue=%o', date);
if (angular.isDate(date) && !isNaN(date.getTime())) {
$datepicker.$date = date;
$picker.update.call($picker, date);
}
// Build only if pristine
$datepicker.$build(true);
};
$datepicker.updateDisabledDates = function (dateRanges) {
options.disabledDateRanges = dateRanges;
for (var i = 0, l = scope.rows.length; i < l; i++) {
angular.forEach(scope.rows[i], $datepicker.$setDisabledEl);
}
};
$datepicker.select = function (date, keep) {
// console.warn('$datepicker.select', date, scope.$mode);
if (angular.isDate(date)) {
if (!angular.isDate(controller.$dateValue) || isNaN(controller.$dateValue.getTime())) {
controller.$dateValue = new Date(date);
}
} else {
controller.$dateValue = null;
}
if (!scope.$mode || keep) {
controller.$setViewValue(angular.copy(date));
controller.$render();
if (options.autoclose && !keep) {
$timeout(function () { $datepicker.hide(true); });
}
} else {
angular.extend(viewDate, {year: date.getFullYear(), month: date.getMonth(), date: date.getDate()});
$datepicker.setMode(scope.$mode - 1);
$datepicker.$build();
}
};
$datepicker.setMode = function (mode) {
// console.warn('$datepicker.setMode', mode);
scope.$mode = mode;
$picker = $datepicker.$views[scope.$mode];
$datepicker.$build();
};
// Protected methods
$datepicker.$build = function (pristine) {
// console.warn('$datepicker.$build() viewDate=%o', viewDate);
if (pristine === true && $picker.built) return;
if (pristine === false && !$picker.built) return;
$picker.build.call($picker);
};
$datepicker.$updateSelected = function () {
for (var i = 0, l = scope.rows.length; i < l; i++) {
angular.forEach(scope.rows[i], updateSelected);
}
};
$datepicker.$isSelected = function (date) {
return $picker.isSelected(date);
};
$datepicker.$setDisabledEl = function (el) {
el.disabled = $picker.isDisabled(el.date);
};
$datepicker.$selectPane = function (value) {
var steps = $picker.steps;
// set targetDate to first day of month to avoid problems with
// date values rollover. This assumes the viewDate does not
// depend on the day of the month
var targetDate = new Date(Date.UTC(viewDate.year + ((steps.year || 0) * value), viewDate.month + ((steps.month || 0) * value), 1));
angular.extend(viewDate, {year: targetDate.getUTCFullYear(), month: targetDate.getUTCMonth(), date: targetDate.getUTCDate()});
$datepicker.$build();
};
$datepicker.$onMouseDown = function (evt) {
// Prevent blur on mousedown on .dropdown-menu
evt.preventDefault();
evt.stopPropagation();
// Emulate click for mobile devices
if (isTouch) {
var targetEl = angular.element(evt.target);
if (targetEl[0].nodeName.toLowerCase() !== 'button') {
targetEl = targetEl.parent();
}
targetEl.triggerHandler('click');
}
};
$datepicker.$onKeyDown = function (evt) {
if (!/(38|37|39|40|13)/.test(evt.keyCode) || evt.shiftKey || evt.altKey) return;
evt.preventDefault();
evt.stopPropagation();
if (evt.keyCode === 13) {
if (!scope.$mode) {
$datepicker.hide(true);
} else {
scope.$apply(function () { $datepicker.setMode(scope.$mode - 1); });
}
return;
}
// Navigate with keyboard
$picker.onKeyDown(evt);
parentScope.$digest();
};
// Private
function updateSelected (el) {
el.selected = $datepicker.$isSelected(el.date);
}
function focusElement () {
element[0].focus();
}
// Overrides
var _init = $datepicker.init;
$datepicker.init = function () {
if (isNative && options.useNative) {
element.prop('type', 'date');
element.css('-webkit-appearance', 'textfield');
return;
} else if (isTouch) {
element.prop('type', 'text');
element.attr('readonly', 'true');
element.on('click', focusElement);
}
_init();
};
var _destroy = $datepicker.destroy;
$datepicker.destroy = function () {
if (isNative && options.useNative) {
element.off('click', focusElement);
}
_destroy();
};
var _show = $datepicker.show;
$datepicker.show = function () {
if ((!isTouch && element.attr('readonly')) || element.attr('disabled')) return;
_show();
// use timeout to hookup the events to prevent
// event bubbling from being processed imediately.
$timeout(function () {
// if $datepicker is no longer showing, don't setup events
if (!$datepicker.$isShown) return;
$datepicker.$element.on(isTouch ? 'touchstart' : 'mousedown', $datepicker.$onMouseDown);
if (options.keyboard) {
element.on('keydown', $datepicker.$onKeyDown);
}
}, 0, false);
};
var _hide = $datepicker.hide;
$datepicker.hide = function (blur) {
if (!$datepicker.$isShown) return;
$datepicker.$element.off(isTouch ? 'touchstart' : 'mousedown', $datepicker.$onMouseDown);
if (options.keyboard) {
element.off('keydown', $datepicker.$onKeyDown);
}
_hide(blur);
};
return $datepicker;
}
DatepickerFactory.defaults = defaults;
return DatepickerFactory;
};
})
.directive('bsDatepicker', function ($window, $parse, $q, $dateFormatter, $dateParser, $datepicker) {
// var defaults = $datepicker.defaults;
var isNative = /(ip[ao]d|iphone|android)/ig.test($window.navigator.userAgent);
return {
restrict: 'EAC',
require: 'ngModel',
link: function postLink (scope, element, attr, controller) {
// Directive options
var options = {scope: scope};
angular.forEach(['template', 'templateUrl', 'controller', 'controllerAs', 'placement', 'container', 'delay', 'trigger', 'html', 'animation', 'autoclose', 'dateType', 'dateFormat', 'timezone', 'modelDateFormat', 'dayFormat', 'strictFormat', 'startWeek', 'startDate', 'useNative', 'lang', 'startView', 'minView', 'iconLeft', 'iconRight', 'daysOfWeekDisabled', 'id', 'prefixClass', 'prefixEvent', 'hasToday', 'hasClear'], function (key) {
if (angular.isDefined(attr[key])) options[key] = attr[key];
});
// use string regex match boolean attr falsy values, leave truthy values be
var falseValueRegExp = /^(false|0|)$/i;
angular.forEach(['html', 'container', 'autoclose', 'useNative', 'hasToday', 'hasClear'], function (key) {
if (angular.isDefined(attr[key]) && falseValueRegExp.test(attr[key])) {
options[key] = false;
}
});
// bind functions from the attrs to the show and hide events
angular.forEach(['onBeforeShow', 'onShow', 'onBeforeHide', 'onHide'], function (key) {
var bsKey = 'bs' + key.charAt(0).toUpperCase() + key.slice(1);
if (angular.isDefined(attr[bsKey])) {
options[key] = scope.$eval(attr[bsKey]);
}
});
// Initialize datepicker
var datepicker = $datepicker(element, controller, options);
options = datepicker.$options;
// Set expected iOS format
if (isNative && options.useNative) options.dateFormat = 'yyyy-MM-dd';
var lang = options.lang;
var formatDate = function (date, format) {
return $dateFormatter.formatDate(date, format, lang);
};
var dateParser = $dateParser({format: options.dateFormat, lang: lang, strict: options.strictFormat});
// Visibility binding support
if (attr.bsShow) {
scope.$watch(attr.bsShow, function (newValue, oldValue) {
if (!datepicker || !angular.isDefined(newValue)) return;
if (angular.isString(newValue)) newValue = !!newValue.match(/true|,?(datepicker),?/i);
if (newValue === true) {
datepicker.show();
} else {
datepicker.hide();
}
});
}
// Observe attributes for changes
angular.forEach(['minDate', 'maxDate'], function (key) {
// console.warn('attr.$observe(%s)', key, attr[key]);
if (angular.isDefined(attr[key])) {
attr.$observe(key, function (newValue) {
// console.warn('attr.$observe(%s)=%o', key, newValue);
datepicker.$options[key] = dateParser.getDateForAttribute(key, newValue);
// Build only if dirty
if (!isNaN(datepicker.$options[key])) datepicker.$build(false);
validateAgainstMinMaxDate(controller.$dateValue);
});
}
});
// Observe date format
if (angular.isDefined(attr.dateFormat)) {
attr.$observe('dateFormat', function (newValue) {
datepicker.$options.dateFormat = newValue;
});
}
// Watch model for changes
scope.$watch(attr.ngModel, function (newValue, oldValue) {
datepicker.update(controller.$dateValue);
}, true);
// Normalize undefined/null/empty array,
// so that we don't treat changing from undefined->null as a change.
function normalizeDateRanges (ranges) {
if (!ranges || !ranges.length) return null;
return ranges;
}
if (angular.isDefined(attr.disabledDates)) {
scope.$watch(attr.disabledDates, function (disabledRanges, previousValue) {
disabledRanges = normalizeDateRanges(disabledRanges);
previousValue = normalizeDateRanges(previousValue);
if (disabledRanges) {
datepicker.updateDisabledDates(disabledRanges);
}
});
}
function validateAgainstMinMaxDate (parsedDate) {
if (!angular.isDate(parsedDate)) return;
var isMinValid = isNaN(datepicker.$options.minDate) || parsedDate.getTime() >= datepicker.$options.minDate;
var isMaxValid = isNaN(datepicker.$options.maxDate) || parsedDate.getTime() <= datepicker.$options.maxDate;
var isValid = isMinValid && isMaxValid;
controller.$setValidity('date', isValid);
controller.$setValidity('min', isMinValid);
controller.$setValidity('max', isMaxValid);
// Only update the model when we have a valid date
if (isValid) controller.$dateValue = parsedDate;
}
// viewValue -> $parsers -> modelValue
controller.$parsers.unshift(function (viewValue) {
// console.warn('$parser("%s"): viewValue=%o', element.attr('ng-model'), viewValue);
var date;
// Null values should correctly reset the model value & validity
if (!viewValue) {
controller.$setValidity('date', true);
// BREAKING CHANGE:
// return null (not undefined) when input value is empty, so angularjs 1.3
// ngModelController can go ahead and run validators, like ngRequired
return null;
}
var parsedDate = dateParser.parse(viewValue, controller.$dateValue);
if (!parsedDate || isNaN(parsedDate.getTime())) {
controller.$setValidity('date', false);
// return undefined, causes ngModelController to
// invalidate model value
return;
}
validateAgainstMinMaxDate(parsedDate);
if (options.dateType === 'string') {
date = dateParser.timezoneOffsetAdjust(parsedDate, options.timezone, true);
return formatDate(date, options.modelDateFormat || options.dateFormat);
}
date = dateParser.timezoneOffsetAdjust(controller.$dateValue, options.timezone, true);
if (options.dateType === 'number') {
return date.getTime();
} else if (options.dateType === 'unix') {
return date.getTime() / 1000;
} else if (options.dateType === 'iso') {
return date.toISOString();
}
return new Date(date);
});
// modelValue -> $formatters -> viewValue
controller.$formatters.push(function (modelValue) {
// console.warn('$formatter("%s"): modelValue=%o (%o)', element.attr('ng-model'), modelValue, typeof modelValue);
var date;
if (angular.isUndefined(modelValue) || modelValue === null) {
date = NaN;
} else if (angular.isDate(modelValue)) {
date = modelValue;
} else if (options.dateType === 'string') {
date = dateParser.parse(modelValue, null, options.modelDateFormat);
} else if (options.dateType === 'unix') {
date = new Date(modelValue * 1000);
} else {
date = new Date(modelValue);
}
// Setup default value?
// if (isNaN(date.getTime())) {
// var today = new Date();
// date = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 0, 0, 0, 0);
// }
// do not adjust date if timezone is UTC
if (options.timezone === 'UTC') {
controller.$dateValue = date;
} else {
controller.$dateValue = dateParser.timezoneOffsetAdjust(date, options.timezone);
}
return getDateFormattedString();
});
// viewValue -> element
controller.$render = function () {
// console.warn('$render("%s"): viewValue=%o', element.attr('ng-model'), controller.$viewValue);
element.val(getDateFormattedString());
};
function getDateFormattedString () {
return !controller.$dateValue || isNaN(controller.$dateValue.getTime()) ? '' : formatDate(controller.$dateValue, options.dateFormat);
}
// Garbage collection
scope.$on('$destroy', function () {
if (datepicker) datepicker.destroy();
options = null;
datepicker = null;
});
}
};
})
.provider('datepickerViews', function () {
// var defaults = this.defaults = {
// dayFormat: 'dd',
// daySplit: 7
// };
// Split array into smaller arrays
function split (arr, size) {
var arrays = [];
while (arr.length > 0) {
arrays.push(arr.splice(0, size));
}
return arrays;
}
// Modulus operator
function mod (n, m) {
return ((n % m) + m) % m;
}
this.$get = function ($dateFormatter, $dateParser, $sce) {
return function (picker) {
var scope = picker.$scope;
var options = picker.$options;
var lang = options.lang;
var formatDate = function (date, format) {
return $dateFormatter.formatDate(date, format, lang);
};
var dateParser = $dateParser({format: options.dateFormat, lang: lang, strict: options.strictFormat});
var weekDaysMin = $dateFormatter.weekdaysShort(lang);
var weekDaysLabels = weekDaysMin.slice(options.startWeek).concat(weekDaysMin.slice(0, options.startWeek));
var weekDaysLabelsHtml = $sce.trustAsHtml('<th class="dow text-center">' + weekDaysLabels.join('</th><th class="dow text-center">') + '</th>');
var startDate = picker.$date || (options.startDate ? dateParser.getDateForAttribute('startDate', options.startDate) : new Date());
var viewDate = {year: startDate.getFullYear(), month: startDate.getMonth(), date: startDate.getDate()};
var views = [{
format: options.dayFormat,
split: 7,
steps: {month: 1},
update: function (date, force) {
if (!this.built || force || date.getFullYear() !== viewDate.year || date.getMonth() !== viewDate.month) {
angular.extend(viewDate, {year: picker.$date.getFullYear(), month: picker.$date.getMonth(), date: picker.$date.getDate()});
picker.$build();
} else if (date.getDate() !== viewDate.date || date.getDate() === 1) {
// chaging picker current month will cause viewDate.date to be set to first day of the month,
// in $datepicker.$selectPane, so picker would not update selected day display if
// user picks first day of the new month.
// As a workaround, we are always forcing update when picked date is first day of month.
viewDate.date = picker.$date.getDate();
picker.$updateSelected();
}
},
build: function () {
var firstDayOfMonth = new Date(viewDate.year, viewDate.month, 1);
var firstDayOfMonthOffset = firstDayOfMonth.getTimezoneOffset();
var firstDate = new Date(+firstDayOfMonth - mod(firstDayOfMonth.getDay() - options.startWeek, 7) * 864e5);
var firstDateOffset = firstDate.getTimezoneOffset();
var today = dateParser.timezoneOffsetAdjust(new Date(), options.timezone).toDateString();
// Handle daylight time switch
if (firstDateOffset !== firstDayOfMonthOffset) firstDate = new Date(+firstDate + (firstDateOffset - firstDayOfMonthOffset) * 60e3);
var days = [];
var day;
for (var i = 0; i < 42; i++) { // < 7 * 6
day = dateParser.daylightSavingAdjust(new Date(firstDate.getFullYear(), firstDate.getMonth(), firstDate.getDate() + i));
days.push({date: day, isToday: day.toDateString() === today, label: formatDate(day, this.format), selected: picker.$date && this.isSelected(day), muted: day.getMonth() !== viewDate.month, disabled: this.isDisabled(day)});
}
scope.title = formatDate(firstDayOfMonth, options.monthTitleFormat);
scope.showLabels = true;
scope.labels = weekDaysLabelsHtml;
scope.rows = split(days, this.split);
scope.isTodayDisabled = this.isDisabled(new Date());
this.built = true;
},
isSelected: function (date) {
return picker.$date && date.getFullYear() === picker.$date.getFullYear() && date.getMonth() === picker.$date.getMonth() && date.getDate() === picker.$date.getDate();
},
isDisabled: function (date) {
var time = date.getTime();
// Disabled because of min/max date.
if (time < options.minDate || time > options.maxDate) return true;
// Disabled due to being a disabled day of the week
if (options.daysOfWeekDisabled.indexOf(date.getDay()) !== -1) return true;
// Disabled because of disabled date range.
if (options.disabledDateRanges) {
for (var i = 0; i < options.disabledDateRanges.length; i++) {
if (time >= options.disabledDateRanges[i].start && time <= options.disabledDateRanges[i].end) {
return true;
}
}
}
return false;
},
onKeyDown: function (evt) {
if (!picker.$date) {
return;
}
var actualTime = picker.$date.getTime();
var newDate;
if (evt.keyCode === 37) newDate = new Date(actualTime - 1 * 864e5);
else if (evt.keyCode === 38) newDate = new Date(actualTime - 7 * 864e5);
else if (evt.keyCode === 39) newDate = new Date(actualTime + 1 * 864e5);
else if (evt.keyCode === 40) newDate = new Date(actualTime + 7 * 864e5);
if (!this.isDisabled(newDate)) picker.select(newDate, true);
}
}, {
name: 'month',
format: options.monthFormat,
split: 4,
steps: {year: 1},
update: function (date, force) {
if (!this.built || date.getFullYear() !== viewDate.year) {
angular.extend(viewDate, {year: picker.$date.getFullYear(), month: picker.$date.getMonth(), date: picker.$date.getDate()});
picker.$build();
} else if (date.getMonth() !== viewDate.month) {
angular.extend(viewDate, {month: picker.$date.getMonth(), date: picker.$date.getDate()});
picker.$updateSelected();
}
},
build: function () {
// var firstMonth = new Date(viewDate.year, 0, 1);
var months = [];
var month;
for (var i = 0; i < 12; i++) {
month = new Date(viewDate.year, i, 1);
months.push({date: month, label: formatDate(month, this.format), selected: picker.$isSelected(month), disabled: this.isDisabled(month)});
}
scope.title = formatDate(month, options.yearTitleFormat);
scope.showLabels = false;
scope.rows = split(months, this.split);
this.built = true;
},
isSelected: function (date) {
return picker.$date && date.getFullYear() === picker.$date.getFullYear() && date.getMonth() === picker.$date.getMonth();
},
isDisabled: function (date) {
var lastDate = +new Date(date.getFullYear(), date.getMonth() + 1, 0);
return lastDate < options.minDate || date.getTime() > options.maxDate;
},
onKeyDown: function (evt) {
if (!picker.$date) {
return;
}
var actualMonth = picker.$date.getMonth();
var newDate = new Date(picker.$date);
if (evt.keyCode === 37) newDate.setMonth(actualMonth - 1);
else if (evt.keyCode === 38) newDate.setMonth(actualMonth - 4);
else if (evt.keyCode === 39) newDate.setMonth(actualMonth + 1);
else if (evt.keyCode === 40) newDate.setMonth(actualMonth + 4);
if (!this.isDisabled(newDate)) picker.select(newDate, true);
}
}, {
name: 'year',
format: options.yearFormat,
split: 4,
steps: {year: 12},
update: function (date, force) {
if (!this.built || force || parseInt(date.getFullYear() / 20, 10) !== parseInt(viewDate.year / 20, 10)) {
angular.extend(viewDate, {year: picker.$date.getFullYear(), month: picker.$date.getMonth(), date: picker.$date.getDate()});
picker.$build();
} else if (date.getFullYear() !== viewDate.year) {
angular.extend(viewDate, {year: picker.$date.getFullYear(), month: picker.$date.getMonth(), date: picker.$date.getDate()});
picker.$updateSelected();
}
},
build: function () {
var firstYear = viewDate.year - viewDate.year % (this.split * 3);
var years = [];
var year;
for (var i = 0; i < 12; i++) {
year = new Date(firstYear + i, 0, 1);
years.push({date: year, label: formatDate(year, this.format), selected: picker.$isSelected(year), disabled: this.isDisabled(year)});
}
scope.title = years[0].label + '-' + years[years.length - 1].label;
scope.showLabels = false;
scope.rows = split(years, this.split);
this.built = true;
},
isSelected: function (date) {
return picker.$date && date.getFullYear() === picker.$date.getFullYear();
},
isDisabled: function (date) {
var lastDate = +new Date(date.getFullYear() + 1, 0, 0);
return lastDate < options.minDate || date.getTime() > options.maxDate;
},
onKeyDown: function (evt) {
if (!picker.$date) {
return;
}
var actualYear = picker.$date.getFullYear();
var newDate = new Date(picker.$date);
if (evt.keyCode === 37) newDate.setYear(actualYear - 1);
else if (evt.keyCode === 38) newDate.setYear(actualYear - 4);
else if (evt.keyCode === 39) newDate.setYear(actualYear + 1);
else if (evt.keyCode === 40) newDate.setYear(actualYear + 4);
if (!this.isDisabled(newDate)) picker.select(newDate, true);
}
}];
return {
views: options.minView ? Array.prototype.slice.call(views, options.minView) : views,
viewDate: viewDate
};
};
};
});