forked from zhaber/ng-bootstrap-datetimepicker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatetimepicker.js
More file actions
283 lines (266 loc) · 11 KB
/
datetimepicker.js
File metadata and controls
283 lines (266 loc) · 11 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
angular.module('ui.bootstrap.datetimepicker', ["ui.bootstrap.dateparser", "ui.bootstrap.datepicker", "ui.bootstrap.timepicker"])
.directive('datepickerPopup', function () {
return {
restrict: 'EAC',
require: 'ngModel',
link: function (scope, element, attr, controller) {
//remove the default formatter from the input directive to prevent conflict
controller.$formatters.shift();
}
}
})
.directive('datetimepicker', [
function () {
if (angular.version.full < '1.4.4') {
return {
restrict: 'EA',
template: "<div class=\"alert alert-danger\">Angular 1.4.0 or above is required for datetimepicker to work correctly</div>"
};
}
return {
restrict: 'EA',
require: 'ngModel',
scope: {
ngModel: '=',
ngChange: '&',
dayFormat: "=",
monthFormat: "=",
yearFormat: "=",
dayHeaderFormat: "=",
dayTitleFormat: "=",
monthTitleFormat: "=",
yearRange: "=",
dateOptions: "=?",
dateDisabled: "&",
dateNgClick: "&",
hourStep: "=",
dateOpened: "=",
minuteStep: "=",
showMeridian: "=",
meredians: "=",
mousewheel: "=",
readonlyTime: "=",
readonlyDate: "=",
disabledDate: "=",
hiddenTime: "=",
hiddenDate: "="
},
template: function (elem, attrs) {
function dashCase(name) {
return name.replace(/[A-Z]/g, function (letter, pos) {
return (pos ? '-' : '') + letter.toLowerCase();
});
}
function createAttr(innerAttr, dateTimeAttrOpt) {
var dateTimeAttr = angular.isDefined(dateTimeAttrOpt) ? dateTimeAttrOpt : innerAttr;
if (attrs[dateTimeAttr]) {
return dashCase(innerAttr) + "=\"" + dateTimeAttr + "\" ";
} else {
return '';
}
}
function createOptionsAttr(innerAttr, dateTimeAttrOpt) {
var dateTimeAttr = angular.isDefined(dateTimeAttrOpt) ? dateTimeAttrOpt : innerAttr;
return dashCase(innerAttr) + "=\"dateOptions." + dateTimeAttr + "\" ";
}
function createFuncAttr(innerAttr, funcArgs, dateTimeAttrOpt, defaultImpl) {
var dateTimeAttr = angular.isDefined(dateTimeAttrOpt) ? dateTimeAttrOpt : innerAttr;
if (attrs[dateTimeAttr]) {
return dashCase(innerAttr) + "=\"" + dateTimeAttr + "({" + funcArgs + "})\" ";
} else {
return angular.isDefined(defaultImpl) ? dashCase(innerAttr) + "=\"" + defaultImpl + "\"" : "";
}
}
function createEvalAttr(innerAttr, dateTimeAttrOpt) {
var dateTimeAttr = angular.isDefined(dateTimeAttrOpt) ? dateTimeAttrOpt : innerAttr;
if (attrs[dateTimeAttr]) {
return dashCase(innerAttr) + "=\"" + attrs[dateTimeAttr] + "\" ";
} else {
return dashCase(innerAttr) + " ";
}
}
function createAttrConcat(previousAttrs, attr) {
return previousAttrs + createAttr.apply(null, attr)
}
function createOptionsAttrConcat(previousAttrs, attr) {
return previousAttrs + createOptionsAttr.apply(null, attr)
}
var dateTmpl = "<div class=\"datetimepicker-wrapper\">" +
"<input class=\"form-control\" type=\"text\" " +
"name=\"datepicker\"" +
"ng-change=\"date_change($event)\" " +
"is-open=\"innerDateOpened\" " +
"datepicker-options=\"dateOptions\" " +
"uib-datepicker-popup=\"{{dateFormat}}\"" +
"ng-model=\"ngModel\" " + [
["dayFormat"],
["monthFormat"],
["yearFormat"],
["dayHeaderFormat"],
["dayTitleFormat"],
["monthTitleFormat"],
["yearRange"],
["showButtonBar"],
["ngHide", "hiddenDate"],
["ngReadonly", "readonlyDate"],
["ngDisabled", "disabledDate"]
].reduce(createAttrConcat, '') +
createFuncAttr("ngClick",
"$event: $event, opened: opened",
"dateNgClick",
"open($event)") +
createEvalAttr("currentText", "currentText") +
createEvalAttr("clearText", "clearText") +
createEvalAttr("datepickerAppendToBody", "datepickerAppendToBody") +
createEvalAttr("closeText", "closeText") +
createEvalAttr("placeholder", "placeholder") +
"/>\n" +
"</div>\n";
var timeTmpl = "<div class=\"datetimepicker-wrapper\" name=\"timepicker\" ng-model=\"time\" ng-change=\"time_change()\" style=\"display:inline-block\">\n" +
"<uib-timepicker " + [
["hourStep"],
["minuteStep"],
["showMeridian"],
["meredians"],
["mousewheel"],
["ngHide", "hiddenTime"],
["ngDisabled", "readonlyTime"]
].reduce(createAttrConcat, '') + [
["min", "minDate"],
["max", "maxDate"]
].reduce(createOptionsAttrConcat, '') +
createEvalAttr("showSpinners", "showSpinners") +
"></timepicker>\n" +
"</div>";
// form is isolated so the directive is registered as one component in the parent form (not date and time)
var tmpl = "<ng-form name=\"datetimepickerForm\" isolate-form>" + dateTmpl + timeTmpl + "</ng-form>";
return tmpl;
},
controller: ['$scope', '$attrs',
function ($scope, $attrs) {
$scope.date_change = function () {
// If we changed the date only, set the time (h,m) on it.
// This is important in case the previous date was null.
// This solves the issue when the user set a date and time, cleared the date, and chose another date,
// and then, the time was cleared too - which is unexpected
var time = $scope.time;
if ($scope.ngModel && $scope.time) { // if ngModel is null, that's because the user cleared the date field
$scope.ngModel.setHours(time.getHours(), time.getMinutes(), 0, 0);
}
};
$scope.time_change = function () {
if ($scope.ngModel && $scope.time) {
// convert from ISO format to Date
if (!($scope.ngModel instanceof Date)) $scope.ngModel = new Date($scope.ngModel);
$scope.ngModel.setHours($scope.time.getHours(), $scope.time.getMinutes(), 0, 0);
} // else the time is invalid, keep the current Date value in datepicker
};
$scope.open = function ($event) {
$event.preventDefault();
$event.stopPropagation();
$scope.innerDateOpened = true;
};
$attrs.$observe('dateFormat', function(newDateFormat, oldValue) {
$scope.dateFormat = newDateFormat;
});
$scope.dateOptions = angular.isDefined($scope.dateOptions) ? $scope.dateOptions : {};
$scope.dateOptions.dateDisabled = $scope.dateDisabled;
}
],
link: function (scope, element, attrs, ctrl) {
var firstTimeAssign = true;
scope.$watch(function () {
return scope.ngModel;
}, function (newTime) {
var timeElement = element[0].querySelector('[name=timepicker]');
// if a time element is focused, updating its model will cause hours/minutes to be formatted by padding with leading zeros
if (timeElement && !timeElement.contains(document.activeElement)) {
if (newTime === null || newTime === '') { // if the newTime is not defined
if (firstTimeAssign) { // if it's the first time we assign the time value
// create a new default time where the hours, minutes, seconds and milliseconds are set to 0.
newTime = new Date();
newTime.setHours(0, 0, 0, 0);
} else { // clear the time
scope.time = null;
if (scope.ngChange) scope.$eval(scope.ngChange);
return;
}
}
// Update timepicker (watch on ng-model in timepicker does not use object equality),
// also if the ngModel was not a Date, convert it to date
newTime = new Date(newTime);
if (isNaN(newTime.getTime()) === false) {
scope.time = newTime; // change the time in timepicker
if (firstTimeAssign) {
firstTimeAssign = false;
}
else if (scope.ngChange) {
scope.$eval(scope.ngChange);
}
}
}
}, true);
scope.$watch(function () {
return scope.datetimepickerForm.$error;
}, function (errors) {
Object.keys(ctrl.$error).forEach(function (error) {
ctrl.$setValidity(error, true);
});
Object.keys(errors).forEach(function (error) {
ctrl.$setValidity(error, false);
});
}, true);
scope.$watch(function () {
return scope.datetimepickerForm.timepicker.$touched || scope.datetimepickerForm.datepicker.$touched;
}, function (touched) {
if (touched) {
ctrl.$setTouched();
}
});
scope.$watch(function () {
return scope.datetimepickerForm.$dirty;
}, function (dirty) {
if (dirty) {
ctrl.$setDirty();
}
});
scope.$watch('dateOpened', function (value) {
scope.innerDateOpened = value;
});
scope.$watch('innerDateOpened', function (value) {
if (angular.isDefined(scope.dateOpened)) {
scope.dateOpened = value;
}
});
}
}
}
]).directive('isolateForm', [function () {
return {
restrict: 'A',
require: '?form',
link: function (scope, elm, attrs, ctrl) {
if (!ctrl) {
return;
}
// Do a copy of the controller
var ctrlCopy = {};
angular.copy(ctrl, ctrlCopy);
// Get the parent of the form
var parent = elm.parent().controller('form');
if (!parent) {
return;
}
// Remove parent link to the controller
parent.$removeControl(ctrl);
// Replace form controller with an "isolated form"
var isolatedFormCtrl = {
$setValidity: function (validationToken, isValid, control) {
ctrlCopy.$setValidity(validationToken, isValid, control);
parent.$setValidity(validationToken, true, ctrl);
}
};
angular.extend(ctrl, isolatedFormCtrl);
}
};
}]);