grafana/public/app/angular/ng_model_on_blur.ts

61 lines
1.5 KiB
TypeScript
Raw Normal View History

import { rangeUtil } from '@grafana/data';
import coreModule from './core_module';
function ngModelOnBlur() {
return {
2017-12-20 05:33:33 -06:00
restrict: 'A',
priority: 1,
2017-12-20 05:33:33 -06:00
require: 'ngModel',
link: (scope: any, elm: any, attr: any, ngModelCtrl: any) => {
2017-12-20 05:33:33 -06:00
if (attr.type === 'radio' || attr.type === 'checkbox') {
return;
}
2017-12-20 05:33:33 -06:00
elm.off('input keydown change');
elm.bind('blur', () => {
scope.$apply(() => {
ngModelCtrl.$setViewValue(elm.val());
});
});
2017-12-20 05:33:33 -06:00
},
};
}
function emptyToNull() {
return {
2017-12-20 05:33:33 -06:00
restrict: 'A',
require: 'ngModel',
link: (scope: any, elm: any, attrs: any, ctrl: any) => {
ctrl.$parsers.push((viewValue: any) => {
2017-12-20 05:33:33 -06:00
if (viewValue === '') {
return null;
}
return viewValue;
});
2017-12-20 05:33:33 -06:00
},
};
}
function validTimeSpan() {
return {
2017-12-20 05:33:33 -06:00
require: 'ngModel',
link: (scope: any, elm: any, attrs: any, ctrl: any) => {
ctrl.$validators.integer = (modelValue: any, viewValue: any) => {
if (ctrl.$isEmpty(modelValue)) {
return true;
}
2017-12-20 05:33:33 -06:00
if (viewValue.indexOf('$') === 0 || viewValue.indexOf('+$') === 0) {
return true; // allow template variable
}
2018-08-29 07:26:50 -05:00
const info = rangeUtil.describeTextRange(viewValue);
return info.invalid !== true;
};
2017-12-20 05:33:33 -06:00
},
};
}
2017-12-20 05:33:33 -06:00
coreModule.directive('ngModelOnblur', ngModelOnBlur);
coreModule.directive('emptyToNull', emptyToNull);
coreModule.directive('validTimeSpan', validTimeSpan);