-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtimespan-picker.js
More file actions
241 lines (210 loc) · 6.79 KB
/
Copy pathtimespan-picker.js
File metadata and controls
241 lines (210 loc) · 6.79 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
(function () {
'use strict';
/**
** DEPENDENCIES
** - moment
**/
angular.module('lui').directive('luidTimespan', ['moment', function (moment) {
function link(scope, element, attrs, ctrls) {
var ngModelCtrl = ctrls[1];
var luidTimespanCtrl = ctrls[0];
scope.pattern = /^\-?([0-9]+)((h([0-9]{2})?)?(m(in)?)?)?$/i;
if (!!attrs.unit) {
var unit = scope.$eval(attrs.unit);
if (unit == 'h' || unit == 'hour' || unit == 'hours') {
scope.useHours = true;
}
}
scope.ngModelCtrl = ngModelCtrl;
ngModelCtrl.$render = function () {
scope.strDuration = '';
if (!this.$viewValue) {
return;
}
var currentDuration = moment.duration(this.$viewValue);
if (currentDuration < 0) {
scope.strDuration += "-";
currentDuration = moment.duration(-currentDuration);
}
var hours = Math.floor(currentDuration.asHours());
var minutes = currentDuration.minutes();
if (hours === 0) {
scope.strDuration += minutes + 'm';
} else {
scope.strDuration += (hours < 10 ? '0' : '') + hours + 'h' + (minutes < 10 ? '0' : '') + minutes;
}
};
// bind to various events - here only keypress=enter
luidTimespanCtrl.setupEvents(element.find('input'));
// set to given mode or to default mode
luidTimespanCtrl.mode = attrs.mode ? attrs.mode : "timespan";
}
return {
require: ['luidTimespan', '^ngModel'],
controller: 'luidTimespanController',
scope: {
step: '=', // default = 5
unit: '=', // 'hours', 'hour', 'h' or 'm', default='m'
ngDisabled: '=',
placeholder: '@',
mode: "=", // 'timespan', 'moment.duration', default='timespan'
min: '=', //min value
max: '=', //max value
},
restrict: 'EA',
link: link,
template:
"<div class='lui timespan input'>" +
"<input type='text' ng-disabled='ngDisabled' placeholder='{{placeholder}}' ng-pattern='pattern' ng-model='strDuration' ng-change='updateValue()' ng-blur='formatInputValue()' autocorrect='off' spellcheck='false'>" +
"</div>"
};
}])
.controller('luidTimespanController', ['$scope', 'moment', function ($scope, moment) {
var ctrl = this;
function parse(strInput) {
// parsing str to moment.duration
function parseHoursAndMinutes(strInput) {
var d = moment.duration();
var splitted = strInput.split(/h/i);
var isPositive = parseInt(splitted[0]) >= 0;
d.add(parseInt(splitted[0]), 'hours');
var strMin = splitted[1];
if (!!strMin && strMin.length >= 2) {
if (isPositive){
d.add(parseInt(strMin.substring(0, 2)), 'minutes');
} else {
d.subtract(parseInt(strMin.substring(0, 2)), 'minutes');
}
}
return d;
}
function parseMinutes(strInput) {
var d = moment.duration();
var splitted = strInput.split(/m/i);
d.add(parseInt(splitted[0]), 'minutes');
return d;
}
function parseHours(strInput) {
var d = moment.duration();
var splitted = strInput.split(/h/i);
d.add(parseInt(splitted[0]), 'hours');
return d;
}
switch(true){
case (/h/i.test(strInput)) : return parseHoursAndMinutes(strInput);
case (/m/i.test(strInput)) : return parseMinutes(strInput);
case ($scope.useHours) : return parseHours(strInput);
default : return parseMinutes(strInput);
}
}
// updates of some kinds
// incr value by `step` minutes
function incr(step) {
var newDur = moment.duration(currentValue()).add(step, 'minutes');
if (newDur.asMilliseconds() < 0) {
newDur = moment.duration();
}
update(newDur);
}
// sets viewValue and renders
function update(newDuration) {
updateWithoutRender(newDuration);
$scope.ngModelCtrl.$render();
}
function updateWithoutRender(newDuration) {
// Handle min/max values
function correctValue(newValue){
function correctedMinValue(newValue) {
var min = !$scope.min ? undefined : moment.duration($scope.min);
return (!min || min <= newValue) ? newValue : min;
}
function correctedMaxValue(newValue) {
var max = !$scope.max ? undefined : moment.duration($scope.max);
return (!max || max >= newValue) ? newValue : max;
}
return correctedMaxValue(correctedMinValue(newValue));
}
function format(dur) {
if (ctrl.mode === 'timespan') {
var timespan = "";
if (dur.asMilliseconds() < 0){
timespan += "-";
dur = moment.duration(-dur);
}
timespan += (dur.asDays() > 0 ? Math.floor(dur.asDays()) + '.' : '') + (dur.hours() < 10 ? '0' : '') + dur.hours() + ':' + (dur.minutes() < 10 ? '0' : '') + dur.minutes() + ':00';
return timespan;
}
return dur;
}
if (newDuration === undefined) {
return $scope.ngModelCtrl.$setViewValue(undefined);
}
// Check min/max values
newDuration = correctValue(newDuration);
var formattedValue = format(newDuration);
$scope.ngModelCtrl.$setViewValue(formattedValue);
}
function currentValue() {
return $scope.ngModelCtrl.$viewValue;
}
// events - key 'enter'
this.setupEvents = function (elt) {
function getStep(){ return isNaN(parseInt($scope.step)) ? 5 : parseInt($scope.step);}
function setupKeyEvents(elt) {
var step = getStep();
elt.bind('keydown', function (e) {
switch(e.which){
case 38:// up
e.preventDefault();
incr(step);
$scope.$apply();
break;
case 40:// down
e.preventDefault();
incr(-step);
$scope.$apply();
break;
case 13:// enter
e.preventDefault();
$scope.formatInputValue();
$scope.$apply();
break;
}
});
}
function setupMousewheelEvents(elt) {
function isScrollingUp(e) {
e = e.originalEvent ? e.originalEvent : e;
//pick correct delta variable depending on event
var delta = (e.wheelDelta) ? e.wheelDelta : -e.deltaY;
return (e.detail || delta > 0);
}
var step = getStep();
elt.bind('mousewheel wheel', function (e) {
if (this === document.activeElement) {
$scope.$apply(incr((isScrollingUp(e)) ? step : -step));
e.preventDefault();
}
});
}
setupKeyEvents(elt);
setupMousewheelEvents(elt);
};
// public methods for update
$scope.updateValue = function () {
// is only fired when pattern is valid or when it goes from valid to invalid
// improvement possible - check the pattern and set the validity of the all directive via ngModelCtrl.$setValidity
// currently when pattern invalid, the viewValue is set to '00:00:00'
if (!$scope.strDuration) { return updateWithoutRender(undefined); } // empty input => 00:00:00
// parse the strDuration to build newDuration
// the duration of the parsed strDuration
var newDuration = parse($scope.strDuration);
// update viewvalue
updateWithoutRender(newDuration);
};
// display stuff
$scope.formatInputValue = function () {
$scope.ngModelCtrl.$render();
};
}]);
})();