directives.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*jshint globalstrict:true */
  2. /*global angular:true */
  3. 'use strict';
  4. angular.module('kibana.directives', [])
  5. .directive('kibanaPanel', function($compile) {
  6. return {
  7. restrict: 'E',
  8. link: function(scope, elem, attrs) {
  9. var template = '<i class="icon-spinner small icon-spin icon-large panel-loading" '+
  10. 'ng-show="panelMeta.loading == true && !panel.title"></i>'+
  11. ' <span class="editlink panelextra pointer" style="right:15px;top:0px" ' +
  12. 'bs-modal="\'partials/paneleditor.html\'" ng-show="panel.editable != false">'+
  13. '<span class="small">{{panel.type}}</span> <i class="icon-cog pointer"></i> '+
  14. '</span><h4>'+
  15. '{{panel.title}} '+
  16. '<i class="icon-spinner smaller icon-spin icon-large" ng-show="panelMeta.loading == true && panel.title"></i>'+
  17. '</h4>';
  18. elem.prepend($compile(angular.element(template))(scope));
  19. }
  20. };
  21. })
  22. .directive('tip', function($compile) {
  23. return {
  24. restrict: 'E',
  25. link: function(scope, elem, attrs) {
  26. var _t = '<i class="icon-question-sign" bs-tooltip="\''+elem.text()+'\'"></i>';
  27. elem.replaceWith($compile(angular.element(_t))(scope));
  28. }
  29. };
  30. })
  31. .directive('addPanel', function($compile) {
  32. return {
  33. restrict: 'A',
  34. link: function($scope, elem, attrs) {
  35. $scope.$watch('panel.type', function(n,o) {
  36. var _type = $scope.panel.type;
  37. $scope.reset_panel(_type);
  38. if(!_.isUndefined($scope.panel.type)) {
  39. var template = '<div ng-controller="'+$scope.panel.type+'">'+
  40. '<span ng-include src="\'partials/paneladd.html\'"></span>'+
  41. '</div>';
  42. elem.html($compile(angular.element(template))($scope));
  43. }
  44. });
  45. }
  46. };
  47. })
  48. .directive('arrayJoin', function() {
  49. return {
  50. restrict: 'A',
  51. require: 'ngModel',
  52. link: function(scope, element, attr, ngModel) {
  53. function split_array(text) {
  54. return (text || '').split(',');
  55. }
  56. function join_array(text) {
  57. if(_.isArray(text)) {
  58. return (text || '').join(',');
  59. } else {
  60. return text;
  61. }
  62. }
  63. ngModel.$parsers.push(split_array);
  64. ngModel.$formatters.push(join_array);
  65. }
  66. };
  67. })
  68. .directive('ngModelOnblur', function() {
  69. return {
  70. restrict: 'A',
  71. require: 'ngModel',
  72. link: function(scope, elm, attr, ngModelCtrl) {
  73. if (attr.type === 'radio' || attr.type === 'checkbox') {
  74. return;
  75. }
  76. elm.unbind('input').unbind('keydown').unbind('change');
  77. elm.bind('blur', function() {
  78. scope.$apply(function() {
  79. ngModelCtrl.$setViewValue(elm.val());
  80. });
  81. });
  82. }
  83. };
  84. })
  85. .directive('ngBlur', ['$parse', function($parse) {
  86. return function(scope, element, attr) {
  87. var fn = $parse(attr['ngBlur']);
  88. element.bind('blur', function(event) {
  89. scope.$apply(function() {
  90. fn(scope, {$event:event});
  91. });
  92. });
  93. };
  94. }]);