directives.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. ngModel.$parsers.push(split_array);
  45. ngModel.$formatters.push(join_array);
  46. }
  47. };
  48. })
  49. // Is this even used anymore? I don't think is is
  50. .directive('upload', function(timer,dashboard){
  51. return {
  52. restrict: 'A',
  53. link: function(scope, elem, attrs) {
  54. function file_selected(evt) {
  55. var files = evt.target.files; // FileList object
  56. // files is a FileList of File objects. List some properties.
  57. var output = [];
  58. for (var i = 0, f; f = files[i]; i++) {
  59. var reader = new FileReader();
  60. reader.onload = (function(theFile) {
  61. return function(e) {
  62. dashboard.dash_load(JSON.parse(e.target.result))
  63. scope.$apply();
  64. };
  65. })(f);
  66. reader.readAsText(f);
  67. }
  68. }
  69. // Check for the various File API support.
  70. if (window.File && window.FileReader && window.FileList && window.Blob) {
  71. // Something
  72. document.getElementById('upload').addEventListener('change', file_selected, false);
  73. } else {
  74. alert('Sorry, the HTML5 File APIs are not fully supported in this browser.');
  75. }
  76. }
  77. }
  78. }).directive('ngModelOnblur', function() {
  79. return {
  80. restrict: 'A',
  81. require: 'ngModel',
  82. link: function(scope, elm, attr, ngModelCtrl) {
  83. if (attr.type === 'radio' || attr.type === 'checkbox') return;
  84. elm.unbind('input').unbind('keydown').unbind('change');
  85. elm.bind('blur', function() {
  86. scope.$apply(function() {
  87. ngModelCtrl.$setViewValue(elm.val());
  88. });
  89. });
  90. }
  91. };
  92. });
  93. ;