-
Notifications
You must be signed in to change notification settings - Fork 11.7k
/
validation.ts
49 lines (42 loc) · 1.24 KB
/
validation.ts
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
import * as dateMath from '@grafana/ui/src/utils/datemath';
import { toUtc, dateTime, isDateTime } from '@grafana/ui/src/utils/moment_wrapper';
export function inputDateDirective() {
return {
restrict: 'A',
require: 'ngModel',
link: ($scope, $elem, attrs, ngModel) => {
const format = 'YYYY-MM-DD HH:mm:ss';
const fromUser = text => {
if (text.indexOf('now') !== -1) {
if (!dateMath.isValid(text)) {
ngModel.$setValidity('error', false);
return undefined;
}
ngModel.$setValidity('error', true);
return text;
}
let parsed;
if ($scope.ctrl.isUtc) {
parsed = toUtc(text, format);
} else {
parsed = dateTime(text, format);
}
if (!parsed.isValid()) {
ngModel.$setValidity('error', false);
return undefined;
}
ngModel.$setValidity('error', true);
return parsed;
};
const toUser = currentValue => {
if (isDateTime(currentValue)) {
return currentValue.format(format);
} else {
return currentValue;
}
};
ngModel.$parsers.push(fromUser);
ngModel.$formatters.push(toUser);
},
};
}