directives.js 2.3 KB

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