-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathangular-date-picker.js
More file actions
177 lines (149 loc) · 7.81 KB
/
angular-date-picker.js
File metadata and controls
177 lines (149 loc) · 7.81 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
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([ 'module', 'angular' ], function (module, angular) {
module.exports = factory(angular);
});
} else if (typeof module === 'object') {
module.exports = factory(require('angular'));
} else {
if (!root.mp) {
root.mp = {};
}
root.mp.datePicker = factory(root.angular);
}
}(this, function (angular) {
'use strict';
return angular.module('mp.datePicker', []).directive('datePicker', [ '$window', '$locale', function ($window, $locale) {
// Introduce custom elements for IE8
$window.document.createElement('date-picker');
var tmpl = ''
+ '<div class="angular-date-picker">'
+ ' <div class="_month">'
+ ' <button type="button" class="_previous" ng-click="changeMonthBy(-1)">«</button>'
+ ' <span>{{ months[month] }}</span> {{ year }}'
+ ' <button type="button" class="_next" ng-click="changeMonthBy(1)">»</button>'
+ ' </div>'
+ ' <div class="_days" ng-click="pickDay($event)">'
+ ' <div class="_day-of-week" ng-repeat="dayOfWeek in daysOfWeek" title="{{ dayOfWeek.fullName }}">{{ dayOfWeek.formattedDay || dayOfWeek.firstLetter }}</div>'
+ ' <div class="_day -padding" ng-repeat="day in leadingDays" data-month-offset="-1" ng-class="{ \'-disabled\': day.disabled }">{{ day.day }}</div>'
+ ' <div class="_day" ng-repeat="day in days" ng-class="{ \'-disabled\': day.disabled, \'-selected\': (day.day === selectedDay), \'-today\': (day.day === today) }">{{ day.day }}</div>'
+ ' <div class="_day -padding" ng-repeat="day in trailingDays" data-month-offset="1" ng-class="{ \'-disabled\': day.disabled }">{{ day.day }}</div>'
+ ' </div>'
+ '</div>'
;
return {
restrict: 'AE',
template: tmpl,
replace: true,
require: '?ngModel',
scope: {
onDateSelected: '&',
formatDate: '=', // @todo breaking change: change to & to allow use of date filter directly
parseDate: '=', // @todo change to &
allowDate: '=',
formatDayOfWeek: '='
},
link: function ($scope, $element, $attributes, ngModel) {
var selectedDate = null,
days = [], // Slices of this are used for ngRepeat
months = $locale.DATETIME_FORMATS.STANDALONEMONTH.slice(0),
daysOfWeek = [],
firstDayOfWeek = typeof $locale.DATETIME_FORMATS.FIRSTDAYOFWEEK === 'number'
? ($locale.DATETIME_FORMATS.FIRSTDAYOFWEEK + 1) % 7
: 0;
for (var i = 1; i <= 31; i++) {
days.push(i);
}
for (var i = 0; i < 7; i++) {
var day = $locale.DATETIME_FORMATS.DAY[(i + firstDayOfWeek) % 7];
daysOfWeek.push({
fullName: day,
firstLetter: day.substr(0, 1),
formattedDay: $scope.formatDayOfWeek(day)
});
}
$scope.months = months;
$scope.daysOfWeek = daysOfWeek;
function setYearAndMonth(date) {
$scope.year = date.getFullYear();
$scope.month = date.getMonth();
var now = new Date();
$scope.today = now.getFullYear() === $scope.year && now.getMonth() === $scope.month
? now.getDate()
: null;
$scope.selectedDay = selectedDate
&& selectedDate.getFullYear() === $scope.year
&& selectedDate.getMonth() === $scope.month
? selectedDate.getDate()
: null;
var firstDayOfMonth = new Date($scope.year, $scope.month, 1),
lastDayOfMonth = new Date($scope.year, $scope.month + 1, 0),
lastDayOfPreviousMonth = new Date($scope.year, $scope.month, 0),
daysInMonth = lastDayOfMonth.getDate(),
daysInLastMonth = lastDayOfPreviousMonth.getDate(),
dayOfWeek = firstDayOfMonth.getDay(),
leadingDays = (dayOfWeek - firstDayOfWeek + 7) % 7 || 7, // Ensure there are always leading days to give context
checkIfDateIsAllowed = $scope.allowDate !== undefined && typeof $scope.allowDate === 'function';
$scope.leadingDays = days.slice(- leadingDays - (31 - daysInLastMonth), daysInLastMonth);
$scope.days = days.slice(0, daysInMonth);
// Ensure a total of 6 rows to maintain height consistency
$scope.trailingDays = days.slice(0, 6 * 7 - (leadingDays + daysInMonth));
// Add disabled property to days
$scope.leadingDays = getDaysWithDisabledProperty($scope.leadingDays, -1);
$scope.days = getDaysWithDisabledProperty($scope.days, 0);
$scope.trailingDays = getDaysWithDisabledProperty($scope.trailingDays, 1);
function getDaysWithDisabledProperty(days, monthOffset) {
return days.map(function(day) {
if (!checkIfDateIsAllowed) {
return {day: day, disabled: false};
}
return {day: day, disabled: !$scope.allowDate(new Date($scope.year, $scope.month + monthOffset, day))};
});
}
}
// Default to current year and month
setYearAndMonth(new Date());
if (ngModel) {
ngModel.$render = function () {
selectedDate = ngModel.$viewValue
? $scope.parseDate
? $scope.parseDate(ngModel.$viewValue)
: new Date(ngModel.$viewValue)
: null;
if (selectedDate && !isNaN(selectedDate)) {
setYearAndMonth(selectedDate);
} else {
// Bad input, stay on current year and month, but reset selected date
$scope.selectedDay = null;
}
};
}
$scope.changeMonthBy = function (amount) {
var date = new Date($scope.year, $scope.month + amount, 1);
setYearAndMonth(date);
};
$scope.pickDay = function (evt) {
var target = angular.element(evt.target);
if (target.hasClass('_day')) {
var monthOffset = target.attr('data-month-offset');
if (monthOffset) {
$scope.changeMonthBy(parseInt(monthOffset, 10));
}
var day = parseInt(target.text(), 10);
$scope.selectedDay = day;
selectedDate = new Date($scope.year, $scope.month, day);
if (ngModel) {
ngModel.$setViewValue(
$scope.formatDate
? $scope.formatDate(selectedDate)
: selectedDate.toLocaleDateString()
);
}
$scope.onDateSelected();
}
};
}
};
}])
.name; // pass back as dependency name
}));