forked from oliversalzburg/angular-timepicker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular.timepicker.js
More file actions
260 lines (209 loc) · 9.5 KB
/
angular.timepicker.js
File metadata and controls
260 lines (209 loc) · 9.5 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
/*
* Angular Timepicker 1.0.5
* https://github.com/Geta/angular-timepicker
*
* Copyright 2013, Geta AS
* Author: Dzulqarnain Nasir
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/MIT
*/
angular.module('dnTimepicker', ['ui.bootstrap.position', 'dateParser'])
.factory('dnTimepickerHelpers', function() {
'use strict';
return {
stringToMinutes: function(str) {
if(!str) return null;
var t = str.match(/(\d+)(h?)/);
return t[1] ? t[1] * (t[2] ? 60 : 1) : null;
},
buildOptionList: function(minTime, maxTime, step) {
var result = [];
var i = angular.copy(minTime);
while (i <= maxTime) {
result.push(new Date(i));
i.setMinutes(i.getMinutes() + step);
}
return result;
},
getClosestIndex: function(value, from) {
if(!value) return -1;
var closest = null;
var index = -1;
var _value = value.getHours() * 60 + value.getMinutes();
for (var i = 0; i < from.length; i++) {
var current = from[i];
var _current = current.getHours() * 60 + current.getMinutes();
if (closest === null || Math.abs(_current - _value) < Math.abs(closest - _value)) {
closest = _current;
index = i;
}
}
return index;
}
}
})
.directive('dnTimepicker', ['$compile', '$parse', '$position', '$document', 'dateFilter', '$dateParser', 'dnTimepickerHelpers', '$log', function($compile, $parse, $position, $document, dateFilter, $dateParser, dnTimepickerHelpers, $log) {
'use strict';
return {
restrict: 'A',
require: '?ngModel',
link: function(originalScope, element, attrs, ngModel) {
var scope = originalScope.$new();
// Local variables
var current = null,
list = [],
updateList = true;
// Model
scope.timepicker = {
element: null,
timeFormat: 'h:mm a',
minTime: $dateParser('0:00', 'H:mm'),
maxTime: $dateParser('23:59', 'H:mm'),
step: 15,
isOpen: false,
activeIdx: -1,
optionList: function() {
if(updateList) {
list = dnTimepickerHelpers.buildOptionList(scope.timepicker.minTime, scope.timepicker.maxTime, scope.timepicker.step);
updateList = false;
}
return list;
}
};
// Init attribute observers
attrs.$observe('dnTimepicker', function(value) {
if(value) {
scope.timepicker.timeFormat = value;
}
ngModel.$render();
});
// Deprecated - but we should still support it for a while
attrs.$observe('timeFormat', function(value) {
if(value) {
$log.warn('The time-format attribute is deprecated and will be removed in the next version. Specify time format as value for dn-timepicker attribute.')
scope.timepicker.timeFormat = value;
}
ngModel.$render();
});
attrs.$observe('minTime', function(value) {
if(!value) return;
scope.timepicker.minTime = $dateParser(value, scope.timepicker.timeFormat);
updateList = true;
});
attrs.$observe('maxTime', function(value) {
if(!value) return;
scope.timepicker.maxTime = $dateParser(value, scope.timepicker.timeFormat);
updateList = true;
});
attrs.$observe('step', function(value) {
if(!value) return;
var step = dnTimepickerHelpers.stringToMinutes(value);
if(step) scope.timepicker.step = step;
updateList = true;
});
// Set up renderer and parser
ngModel.$render = function() {
var timeString = angular.isDate(ngModel.$viewValue) ? dateFilter(ngModel.$viewValue, scope.timepicker.timeFormat) : '';
element.val(timeString);
if(!isNaN(ngModel.$modelValue)) current = ngModel.$modelValue;
};
// Parses manually entered time
var parseDate = function(viewValue) {
var date = angular.isDate(viewValue) ? viewValue : $dateParser(viewValue, scope.timepicker.timeFormat);
if(isNaN(date)) {
ngModel.$setValidity('time', false);
} else {
ngModel.$setValidity('time', true);
if(current) {
current.setHours(date.getHours());
current.setMinutes(date.getMinutes());
current.setSeconds(date.getSeconds());
return current;
}
}
return date;
};
ngModel.$parsers.unshift(parseDate);
// Set up methods
// Select action handler
scope.select = function(time) {
if(!angular.isDate(time)) {
return;
}
current.setHours(time.getHours());
current.setMinutes(time.getMinutes());
current.setSeconds(time.getSeconds());
ngModel.$setViewValue(current);
ngModel.$render();
};
// Checks for current active item
scope.isActive = function(index) {
return index === scope.timepicker.activeIdx;
};
// Sets the current active item
scope.setActive = function(index) {
scope.timepicker.activeIdx = index;
};
// Opens the timepicker
scope.openPopup = function() {
// Set position
scope.position = $position.position(element);
scope.position.top = scope.position.top + element.prop('offsetHeight');
// Open list
scope.timepicker.isOpen = true;
// Set active item
scope.timepicker.activeIdx = dnTimepickerHelpers.getClosestIndex(ngModel.$modelValue, scope.timepicker.optionList());
// Trigger digest
scope.$digest();
// Scroll to selected
if (scope.timepicker.element && scope.timepicker.activeIdx > -1) {
var target = scope.timepicker.element[0].querySelector('.active');
target.parentNode.scrollTop = target.offsetTop;
}
};
// Append timepicker dropdown
element.after($compile(angular.element('<div dn-timepicker-popup></div>'))(scope));
// Set up the element
element
.bind('focus', function() {
scope.openPopup();
});
$document.bind('click', function(event) {
if (scope.timepicker.isOpen && event.target !== element[0]) {
scope.timepicker.isOpen = false;
scope.$apply();
}
});
//if the model is an ISO string or a millisecond offset, attempt to create a date out of it
if (angular.isString(ngModel.$modelValue)) {
ngModel.$modelValue = new Date(ngModel.$modelValue);
}
// Set initial value
if(!angular.isDate(ngModel.$modelValue)) {
ngModel.$setViewValue(new Date());
}
// Set initial selected item
current = ngModel.$modelValue;
}
};
}])
.directive('dnTimepickerPopup', [function() {
'use strict';
return {
restrict: 'A',
replace: true,
transclude: false,
template: '<ul class="dn-timepicker-popup dropdown-menu" ng-style="{display: timepicker.isOpen && \'block\' || \'none\', top: position.top+\'px\', left: position.left+\'px\'}">\
<li ng-repeat="time in timepicker.optionList()" ng-class="{active: isActive($index) }" ng-mouseenter="setActive($index)">\
<a ng-click="select(time)">{{time | date:timepicker.timeFormat}}</a>\
</li>\
</ul>',
link: function(scope, element, attrs) {
scope.timepicker.element = element;
element.find('a').bind('click', function(event) {
event.preventDefault();
});
}
};
}]);