2019-07-06 01:43:12 -05:00
|
|
|
import { rangeUtil } from '@grafana/data';
|
2015-09-15 01:17:40 -05:00
|
|
|
|
2022-04-22 08:33:13 -05:00
|
|
|
import coreModule from './core_module';
|
|
|
|
|
2017-11-23 07:53:23 -06:00
|
|
|
function ngModelOnBlur() {
|
|
|
|
return {
|
2017-12-20 05:33:33 -06:00
|
|
|
restrict: 'A',
|
2017-11-23 07:53:23 -06:00
|
|
|
priority: 1,
|
2017-12-20 05:33:33 -06:00
|
|
|
require: 'ngModel',
|
2019-04-28 02:58:12 -05:00
|
|
|
link: (scope: any, elm: any, attr: any, ngModelCtrl: any) => {
|
2017-12-20 05:33:33 -06:00
|
|
|
if (attr.type === 'radio' || attr.type === 'checkbox') {
|
2017-11-23 07:53:23 -06:00
|
|
|
return;
|
|
|
|
}
|
2015-09-15 01:17:40 -05:00
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
elm.off('input keydown change');
|
2018-09-04 10:02:32 -05:00
|
|
|
elm.bind('blur', () => {
|
|
|
|
scope.$apply(() => {
|
2017-11-23 07:53:23 -06:00
|
|
|
ngModelCtrl.$setViewValue(elm.val());
|
2015-09-15 01:17:40 -05:00
|
|
|
});
|
2017-11-23 07:53:23 -06:00
|
|
|
});
|
2017-12-20 05:33:33 -06:00
|
|
|
},
|
2017-11-23 07:53:23 -06:00
|
|
|
};
|
2017-11-01 01:51:40 -05:00
|
|
|
}
|
2015-09-15 01:17:40 -05:00
|
|
|
|
2017-11-23 07:53:23 -06:00
|
|
|
function emptyToNull() {
|
|
|
|
return {
|
2017-12-20 05:33:33 -06:00
|
|
|
restrict: 'A',
|
|
|
|
require: 'ngModel',
|
2019-04-28 02:58:12 -05:00
|
|
|
link: (scope: any, elm: any, attrs: any, ctrl: any) => {
|
|
|
|
ctrl.$parsers.push((viewValue: any) => {
|
2017-12-20 05:33:33 -06:00
|
|
|
if (viewValue === '') {
|
2017-12-19 09:06:54 -06:00
|
|
|
return null;
|
|
|
|
}
|
2017-11-23 07:53:23 -06:00
|
|
|
return viewValue;
|
|
|
|
});
|
2017-12-20 05:33:33 -06:00
|
|
|
},
|
2017-11-23 07:53:23 -06:00
|
|
|
};
|
2017-11-01 01:51:40 -05:00
|
|
|
}
|
2015-09-15 01:17:40 -05:00
|
|
|
|
2017-11-23 07:53:23 -06:00
|
|
|
function validTimeSpan() {
|
|
|
|
return {
|
2017-12-20 05:33:33 -06:00
|
|
|
require: 'ngModel',
|
2019-04-28 02:58:12 -05:00
|
|
|
link: (scope: any, elm: any, attrs: any, ctrl: any) => {
|
|
|
|
ctrl.$validators.integer = (modelValue: any, viewValue: any) => {
|
2017-11-23 07:53:23 -06:00
|
|
|
if (ctrl.$isEmpty(modelValue)) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
if (viewValue.indexOf('$') === 0 || viewValue.indexOf('+$') === 0) {
|
2017-11-23 07:53:23 -06:00
|
|
|
return true; // allow template variable
|
|
|
|
}
|
2018-08-29 07:26:50 -05:00
|
|
|
const info = rangeUtil.describeTextRange(viewValue);
|
2017-11-23 07:53:23 -06:00
|
|
|
return info.invalid !== true;
|
|
|
|
};
|
2017-12-20 05:33:33 -06:00
|
|
|
},
|
2017-11-23 07:53:23 -06:00
|
|
|
};
|
2017-11-01 01:51:40 -05:00
|
|
|
}
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
coreModule.directive('ngModelOnblur', ngModelOnBlur);
|
|
|
|
coreModule.directive('emptyToNull', emptyToNull);
|
|
|
|
coreModule.directive('validTimeSpan', validTimeSpan);
|