directives.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 = '<img src="common/img/load.gif" class="panel-loading" ng-show="panel.loading == true">'+
  10. ' <span class="editlink panelextra pointer" style="right:15px;top:0px" ' +
  11. 'bs-modal="\'partials/paneleditor.html\'" ng-show="panel.editable != false">'+
  12. '<span class="small">{{panel.type}}</span> <i class="icon-cog pointer"></i> '+
  13. '</span><h4>{{panel.title}}</h4>';
  14. elem.prepend($compile(angular.element(template))(scope));
  15. }
  16. };
  17. })
  18. .directive('addPanel', function($compile) {
  19. return {
  20. restrict: 'A',
  21. link: function(scope, elem, attrs) {
  22. scope.$watch('panel.type', function(n,o) {
  23. if(!_.isUndefined(scope.panel.type)) {
  24. var template = '<div>'+
  25. '<div ng-controller="'+scope.panel.type+'" ng-include src="\'partials/panelgeneral.html\'"></div>'+
  26. '<div ng-controller="'+scope.panel.type+'" ng-include src="\''+scope.edit_path(scope.panel.type)+'\'">'+
  27. '</div>';
  28. elem.html($compile(angular.element(template))(scope));
  29. }
  30. });
  31. }
  32. };
  33. })
  34. .directive('arrayJoin', function() {
  35. return {
  36. restrict: 'A',
  37. require: 'ngModel',
  38. link: function(scope, element, attr, ngModel) {
  39. function split_array(text) {
  40. return (text || '').split(',');
  41. }
  42. function join_array(text) {
  43. if(_.isArray(text)) {
  44. return (text || '').join(',');
  45. } else {
  46. return text;
  47. }
  48. }
  49. ngModel.$parsers.push(split_array);
  50. ngModel.$formatters.push(join_array);
  51. }
  52. };
  53. })
  54. .directive('ngModelOnblur', function() {
  55. return {
  56. restrict: 'A',
  57. require: 'ngModel',
  58. link: function(scope, elm, attr, ngModelCtrl) {
  59. if (attr.type === 'radio' || attr.type === 'checkbox') {
  60. return;
  61. }
  62. elm.unbind('input').unbind('keydown').unbind('change');
  63. elm.bind('blur', function() {
  64. scope.$apply(function() {
  65. ngModelCtrl.$setViewValue(elm.val());
  66. });
  67. });
  68. }
  69. };
  70. })
  71. .directive('ngBlur', ['$parse', function($parse) {
  72. return function(scope, element, attr) {
  73. var fn = $parse(attr['ngBlur']);
  74. element.bind('blur', function(event) {
  75. scope.$apply(function() {
  76. fn(scope, {$event:event});
  77. });
  78. });
  79. };
  80. }]);