directives.js 3.1 KB

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