directives.js 2.7 KB

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