forked from rorymadden/angular-date-dropdowns
-
Notifications
You must be signed in to change notification settings - Fork 1
/
directive.js
177 lines (156 loc) · 5.48 KB
/
directive.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
(function () {
'use strict';
var dd = angular.module('rorymadden.date-dropdowns', []);
dd.factory('rsmdateutils', function () {
var that = this,
dayRange = [1, 31],
months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
function changeDate (date) {
if(date.day > 28) {
date.day--;
return date;
} else if (date.month > 11) {
date.day = 31;
date.month--;
return date;
}
};
return {
checkDate: function (date) {
var d;
if (!date.day || date.month === null || date.month === undefined || !date.year) return false;
d = new Date(Date.UTC(date.year, date.month, date.day));
if (d && (d.getMonth() === date.month && d.getDate() === Number(date.day))) {
return d;
}
return this.checkDate(changeDate(date));
},
days: (function () {
var days = [];
while (dayRange[0] <= dayRange[1]) {
days.push(dayRange[0]++);
}
return days;
}()),
months: (function () {
var lst = [],
mLen = months.length;
for (var i = 0; i < mLen; i++) {
lst.push({
value: i,
name: months[i]
});
}
return lst;
}())
};
})
dd.directive('rsmdatedropdowns', ['rsmdateutils', function (rsmdateutils) {
return {
restrict: 'A',
replace: true,
require: 'ngModel',
scope: {
model: '=ngModel'
},
controller: ['$scope', 'rsmdateutils', function ($scope, rsmDateUtils) {
$scope.days = rsmDateUtils.days;
$scope.months = rsmDateUtils.months;
$scope.dateFields = {};
$scope.dateFields.day = new Date($scope.model).getUTCDate();
$scope.dateFields.month = new Date($scope.model).getUTCMonth();
$scope.dateFields.year = new Date($scope.model).getUTCFullYear();
// Initialize with current date (if set)
$scope.$watch('model', function (newDate) {
if(newDate) {
$scope.dateFields.day = new Date(newDate).getUTCDate();
$scope.dateFields.month = new Date(newDate).getUTCMonth();
$scope.dateFields.year = new Date(newDate).getUTCFullYear();
}
});
$scope.checkDate = function () {
var date = rsmDateUtils.checkDate($scope.dateFields);
if (date) {
$scope.model = date;
}
};
}],
template:
'<div class="form-inline">' +
' <div class="form-group col-xs-3">' +
' <select name="dateFields.day" data-ng-model="dateFields.day" placeholder="Day" class="form-control" ng-options="day for day in days" ng-change="checkDate()" ng-disabled="disableFields"></select>' +
' </div>' +
' <div class="form-group col-xs-5">' +
' <select name="dateFields.month" data-ng-model="dateFields.month" placeholder="Month" class="form-control" ng-options="month.value as month.name for month in months" value="{{ dateField.month }}" ng-change="checkDate()" ng-disabled="disableFields"></select>' +
' </div>' +
' <div class="form-group col-xs-4">' +
' <select ng-show="!yearText" name="dateFields.year" data-ng-model="dateFields.year" placeholder="Year" class="form-control" ng-options="year for year in years" ng-change="checkDate()" ng-disabled="disableFields"></select>' +
' <input ng-show="yearText" type="text" name="dateFields.year" data-ng-model="dateFields.year" placeholder="Year" class="form-control" ng-disabled="disableFields">' +
' </div>' +
'</div>',
link: function (scope, element, attrs, ctrl) {
var currentYear = parseInt(attrs.startingYear, 10) || new Date().getFullYear(),
numYears = parseInt(attrs.numYears,10) || 100,
oldestYear = currentYear - numYears,
overridable = [
'dayDivClass',
'dayClass',
'monthDivClass',
'monthClass',
'yearDivClass',
'yearClass'
],
required;
scope.years = [];
scope.yearText = attrs.yearText ? true : false;
if (attrs.ngDisabled) {
scope.$parent.$watch(attrs.ngDisabled, function (newVal) {
scope.disableFields = newVal;
});
}
if (attrs.required) {
required = attrs.required.split(' ');
ctrl.$parsers.push(function (value) {
angular.forEach(required, function (elem) {
if (!angular.isNumber(elem)) {
ctrl.$setValidity('required', false);
}
});
ctrl.$setValidity('required', true);
});
}
for (var i = currentYear; i >= oldestYear; i--) {
scope.years.push(i);
}
(function () {
var oLen = overridable.length,
oCurrent,
childEle;
while (oLen--) {
oCurrent = overridable[oLen];
childEle = element[0].children[Math.floor(oLen / 2)];
if (oLen % 2 && oLen != 2) {
childEle = childEle.children[0];
}
if (attrs[oCurrent]) {
angular.element(childEle).attr('class', attrs[oCurrent]);
}
}
}());
}
};
}]);
}());