forked from angular-ui/bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimepicker.js
232 lines (200 loc) · 7.25 KB
/
timepicker.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
angular.module('ui.bootstrap.timepicker', [])
.constant('timepickerConfig', {
hourStep: 1,
minuteStep: 1,
showMeridian: true,
meridians: ['AM', 'PM'],
readonlyInput: false,
mousewheel: true
})
.directive('timepicker', ['$parse', '$log', 'timepickerConfig', function ($parse, $log, timepickerConfig) {
return {
restrict: 'EA',
require:'?^ngModel',
replace: true,
scope: {},
templateUrl: 'template/timepicker/timepicker.html',
link: function(scope, element, attrs, ngModel) {
if ( !ngModel ) {
return; // do nothing if no ng-model
}
var selected = new Date(), meridians = timepickerConfig.meridians;
var hourStep = timepickerConfig.hourStep;
if (attrs.hourStep) {
scope.$parent.$watch($parse(attrs.hourStep), function(value) {
hourStep = parseInt(value, 10);
});
}
var minuteStep = timepickerConfig.minuteStep;
if (attrs.minuteStep) {
scope.$parent.$watch($parse(attrs.minuteStep), function(value) {
minuteStep = parseInt(value, 10);
});
}
// 12H / 24H mode
scope.showMeridian = timepickerConfig.showMeridian;
if (attrs.showMeridian) {
scope.$parent.$watch($parse(attrs.showMeridian), function(value) {
scope.showMeridian = !!value;
if ( ngModel.$error.time ) {
// Evaluate from template
var hours = getHoursFromTemplate(), minutes = getMinutesFromTemplate();
if (angular.isDefined( hours ) && angular.isDefined( minutes )) {
selected.setHours( hours );
refresh();
}
} else {
updateTemplate();
}
});
}
// Get scope.hours in 24H mode if valid
function getHoursFromTemplate ( ) {
var hours = parseInt( scope.hours, 10 );
var valid = ( scope.showMeridian ) ? (hours > 0 && hours < 13) : (hours >= 0 && hours < 24);
if ( !valid ) {
return undefined;
}
if ( scope.showMeridian ) {
if ( hours === 12 ) {
hours = 0;
}
if ( scope.meridian === meridians[1] ) {
hours = hours + 12;
}
}
return hours;
}
function getMinutesFromTemplate() {
var minutes = parseInt(scope.minutes, 10);
return ( minutes >= 0 && minutes < 60 ) ? minutes : undefined;
}
function pad( value ) {
return ( angular.isDefined(value) && value.toString().length < 2 ) ? '0' + value : value;
}
// Input elements
var inputs = element.find('input'), hoursInputEl = inputs.eq(0), minutesInputEl = inputs.eq(1);
// Respond on mousewheel spin
var mousewheel = (angular.isDefined(attrs.mousewheel)) ? scope.$eval(attrs.mousewheel) : timepickerConfig.mousewheel;
if ( mousewheel ) {
var isScrollingUp = function(e) {
if (e.originalEvent) {
e = e.originalEvent;
}
//pick correct delta variable depending on event
var delta = (e.wheelDelta) ? e.wheelDelta : -e.deltaY;
return (e.detail || delta > 0);
};
hoursInputEl.bind('mousewheel wheel', function(e) {
scope.$apply( (isScrollingUp(e)) ? scope.incrementHours() : scope.decrementHours() );
e.preventDefault();
});
minutesInputEl.bind('mousewheel wheel', function(e) {
scope.$apply( (isScrollingUp(e)) ? scope.incrementMinutes() : scope.decrementMinutes() );
e.preventDefault();
});
}
scope.readonlyInput = (angular.isDefined(attrs.readonlyInput)) ? scope.$eval(attrs.readonlyInput) : timepickerConfig.readonlyInput;
if ( ! scope.readonlyInput ) {
var invalidate = function(invalidHours, invalidMinutes) {
ngModel.$setViewValue( null );
ngModel.$setValidity('time', false);
if (angular.isDefined(invalidHours)) {
scope.invalidHours = invalidHours;
}
if (angular.isDefined(invalidMinutes)) {
scope.invalidMinutes = invalidMinutes;
}
};
scope.updateHours = function() {
var hours = getHoursFromTemplate();
if ( angular.isDefined(hours) ) {
selected.setHours( hours );
refresh( 'h' );
} else {
invalidate(true);
}
};
hoursInputEl.bind('blur', function(e) {
if ( !scope.validHours && scope.hours < 10) {
scope.$apply( function() {
scope.hours = pad( scope.hours );
});
}
});
scope.updateMinutes = function() {
var minutes = getMinutesFromTemplate();
if ( angular.isDefined(minutes) ) {
selected.setMinutes( minutes );
refresh( 'm' );
} else {
invalidate(undefined, true);
}
};
minutesInputEl.bind('blur', function(e) {
if ( !scope.invalidMinutes && scope.minutes < 10 ) {
scope.$apply( function() {
scope.minutes = pad( scope.minutes );
});
}
});
} else {
scope.updateHours = angular.noop;
scope.updateMinutes = angular.noop;
}
ngModel.$render = function() {
var date = ngModel.$modelValue ? new Date( ngModel.$modelValue ) : null;
if ( isNaN(date) ) {
ngModel.$setValidity('time', false);
$log.error('Timepicker directive: "ng-model" value must be a Date object, a number of milliseconds since 01.01.1970 or a string representing an RFC2822 or ISO 8601 date.');
} else {
if ( date ) {
selected = date;
}
makeValid();
updateTemplate();
}
};
// Call internally when we know that model is valid.
function refresh( keyboardChange ) {
makeValid();
ngModel.$setViewValue( new Date(selected) );
updateTemplate( keyboardChange );
}
function makeValid() {
ngModel.$setValidity('time', true);
scope.invalidHours = false;
scope.invalidMinutes = false;
}
function updateTemplate( keyboardChange ) {
var hours = selected.getHours(), minutes = selected.getMinutes();
if ( scope.showMeridian ) {
hours = ( hours === 0 || hours === 12 ) ? 12 : hours % 12; // Convert 24 to 12 hour system
}
scope.hours = keyboardChange === 'h' ? hours : pad(hours);
scope.minutes = keyboardChange === 'm' ? minutes : pad(minutes);
scope.meridian = selected.getHours() < 12 ? meridians[0] : meridians[1];
}
function addMinutes( minutes ) {
var dt = new Date( selected.getTime() + minutes * 60000 );
selected.setHours( dt.getHours(), dt.getMinutes() );
refresh();
}
scope.incrementHours = function() {
addMinutes( hourStep * 60 );
};
scope.decrementHours = function() {
addMinutes( - hourStep * 60 );
};
scope.incrementMinutes = function() {
addMinutes( minuteStep );
};
scope.decrementMinutes = function() {
addMinutes( - minuteStep );
};
scope.toggleMeridian = function() {
addMinutes( 12 * 60 * (( selected.getHours() < 12 ) ? 1 : -1) );
};
}
};
}]);