directives.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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('addPanel', function($compile) {
  23. return {
  24. restrict: 'A',
  25. link: function($scope, elem, attrs) {
  26. $scope.$watch('panel.type', function(n,o) {
  27. var _type = $scope.panel.type;
  28. $scope.reset_panel(_type);
  29. if(!_.isUndefined($scope.panel.type)) {
  30. var template = '<div ng-controller="'+$scope.panel.type+'">'+
  31. '<span ng-include src="\'partials/paneladd.html\'"></span>'+
  32. '</div>';
  33. elem.html($compile(angular.element(template))($scope));
  34. }
  35. });
  36. }
  37. };
  38. })
  39. .directive('arrayJoin', function() {
  40. return {
  41. restrict: 'A',
  42. require: 'ngModel',
  43. link: function(scope, element, attr, ngModel) {
  44. function split_array(text) {
  45. return (text || '').split(',');
  46. }
  47. function join_array(text) {
  48. if(_.isArray(text)) {
  49. return (text || '').join(',');
  50. } else {
  51. return text;
  52. }
  53. }
  54. ngModel.$parsers.push(split_array);
  55. ngModel.$formatters.push(join_array);
  56. }
  57. };
  58. })
  59. .directive('ngModelOnblur', function() {
  60. return {
  61. restrict: 'A',
  62. require: 'ngModel',
  63. link: function(scope, elm, attr, ngModelCtrl) {
  64. if (attr.type === 'radio' || attr.type === 'checkbox') {
  65. return;
  66. }
  67. elm.unbind('input').unbind('keydown').unbind('change');
  68. elm.bind('blur', function() {
  69. scope.$apply(function() {
  70. ngModelCtrl.$setViewValue(elm.val());
  71. });
  72. });
  73. }
  74. };
  75. })
  76. .directive('ngBlur', ['$parse', function($parse) {
  77. return function(scope, element, attr) {
  78. var fn = $parse(attr['ngBlur']);
  79. element.bind('blur', function(event) {
  80. scope.$apply(function() {
  81. fn(scope, {$event:event});
  82. });
  83. });
  84. };
  85. }]);