directives.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 ng-show="panel.title">'+
  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('tip', function($compile) {
  23. return {
  24. restrict: 'E',
  25. link: function(scope, elem, attrs) {
  26. var _t = '<i class="icon-'+(attrs.icon||'question-sign')+'" bs-tooltip="\''+
  27. kbn.addslashes(elem.text())+'\'"></i>';
  28. elem.replaceWith($compile(angular.element(_t))(scope));
  29. }
  30. };
  31. })
  32. .directive('addPanel', function($compile) {
  33. return {
  34. restrict: 'A',
  35. link: function($scope, elem, attrs) {
  36. $scope.$watch('panel.type', function(n,o) {
  37. var _type = $scope.panel.type;
  38. $scope.reset_panel(_type);
  39. if(!_.isUndefined($scope.panel.type)) {
  40. var template = '<div ng-controller="'+$scope.panel.type+'">'+
  41. '<span ng-include src="\'partials/paneladd.html\'"></span>'+
  42. '</div>';
  43. elem.html($compile(angular.element(template))($scope));
  44. }
  45. });
  46. }
  47. };
  48. })
  49. .directive('arrayJoin', function() {
  50. return {
  51. restrict: 'A',
  52. require: 'ngModel',
  53. link: function(scope, element, attr, ngModel) {
  54. function split_array(text) {
  55. return (text || '').split(',');
  56. }
  57. function join_array(text) {
  58. if(_.isArray(text)) {
  59. return (text || '').join(',');
  60. } else {
  61. return text;
  62. }
  63. }
  64. ngModel.$parsers.push(split_array);
  65. ngModel.$formatters.push(join_array);
  66. }
  67. };
  68. })
  69. .directive('ngModelOnblur', function() {
  70. return {
  71. restrict: 'A',
  72. require: 'ngModel',
  73. link: function(scope, elm, attr, ngModelCtrl) {
  74. if (attr.type === 'radio' || attr.type === 'checkbox') {
  75. return;
  76. }
  77. elm.unbind('input').unbind('keydown').unbind('change');
  78. elm.bind('blur', function() {
  79. scope.$apply(function() {
  80. ngModelCtrl.$setViewValue(elm.val());
  81. });
  82. });
  83. }
  84. };
  85. })
  86. .directive('ngBlur', ['$parse', function($parse) {
  87. return function(scope, element, attr) {
  88. var fn = $parse(attr['ngBlur']);
  89. element.bind('blur', function(event) {
  90. scope.$apply(function() {
  91. fn(scope, {$event:event});
  92. });
  93. });
  94. };
  95. }])
  96. .directive('dashUpload', function(timer, dashboard, alertSrv){
  97. return {
  98. restrict: 'A',
  99. link: function(scope, elem, attrs) {
  100. function file_selected(evt) {
  101. var files = evt.target.files; // FileList object
  102. // files is a FileList of File objects. List some properties.
  103. var output = [];
  104. var readerOnload = function(theFile) {
  105. return function(e) {
  106. dashboard.dash_load(JSON.parse(e.target.result));
  107. scope.$apply();
  108. };
  109. };
  110. for (var i = 0, f; f = files[i]; i++) {
  111. var reader = new FileReader();
  112. reader.onload = (readerOnload)(f);
  113. reader.readAsText(f);
  114. }
  115. }
  116. // Check for the various File API support.
  117. if (window.File && window.FileReader && window.FileList && window.Blob) {
  118. // Something
  119. document.getElementById('dashupload').addEventListener('change', file_selected, false);
  120. } else {
  121. alertSrv.set('Oops','Sorry, the HTML5 File APIs are not fully supported in this browser.','error');
  122. }
  123. }
  124. };
  125. });