ngModelOnBlur.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. define([
  2. 'angular',
  3. 'kbn'
  4. ],
  5. function (angular, kbn) {
  6. 'use strict';
  7. angular
  8. .module('grafana.directives')
  9. .directive('ngModelOnblur', function() {
  10. return {
  11. restrict: 'A',
  12. priority: 1,
  13. require: 'ngModel',
  14. link: function(scope, elm, attr, ngModelCtrl) {
  15. if (attr.type === 'radio' || attr.type === 'checkbox') {
  16. return;
  17. }
  18. elm.off('input keydown change');
  19. elm.bind('blur', function() {
  20. scope.$apply(function() {
  21. ngModelCtrl.$setViewValue(elm.val());
  22. });
  23. });
  24. }
  25. };
  26. })
  27. .directive('emptyToNull', function () {
  28. return {
  29. restrict: 'A',
  30. require: 'ngModel',
  31. link: function (scope, elm, attrs, ctrl) {
  32. ctrl.$parsers.push(function (viewValue) {
  33. if(viewValue === "") { return null; }
  34. return viewValue;
  35. });
  36. }
  37. };
  38. })
  39. .directive('validTimeSpan', function() {
  40. return {
  41. require: 'ngModel',
  42. link: function(scope, elm, attrs, ctrl) {
  43. ctrl.$validators.integer = function(modelValue, viewValue) {
  44. if (ctrl.$isEmpty(modelValue)) {
  45. return true;
  46. }
  47. return kbn.isValidTimeSpan(viewValue);
  48. };
  49. }
  50. };
  51. });
  52. });