directives.js 2.5 KB

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