directives.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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-edit pointer"></i>'+
  12. '</span><h4>{{panel.title}}</h4>';
  13. elem.prepend($compile(angular.element(template))(scope));
  14. }
  15. };
  16. })
  17. .directive('arrayJoin', function() {
  18. return {
  19. restrict: 'A',
  20. require: 'ngModel',
  21. link: function(scope, element, attr, ngModel) {
  22. function split_array(text) {
  23. return (text || '').split(',');
  24. }
  25. function join_array(text) {
  26. if(_.isArray(text))
  27. return (text || '').join(',');
  28. else
  29. return text
  30. }
  31. ngModel.$parsers.push(split_array);
  32. ngModel.$formatters.push(join_array);
  33. }
  34. };
  35. })
  36. .directive('upload', function(timer){
  37. return {
  38. restrict: 'A',
  39. link: function(scope, elem, attrs) {
  40. function file_selected(evt) {
  41. var files = evt.target.files; // FileList object
  42. // files is a FileList of File objects. List some properties.
  43. var output = [];
  44. for (var i = 0, f; f = files[i]; i++) {
  45. var reader = new FileReader();
  46. reader.onload = (function(theFile) {
  47. return function(e) {
  48. // Render thumbnail.
  49. scope.dashboards = JSON.parse(e.target.result)
  50. timer.cancel_all();
  51. scope.$apply();
  52. };
  53. })(f);
  54. reader.readAsText(f);
  55. }
  56. }
  57. // Check for the various File API support.
  58. if (window.File && window.FileReader && window.FileList && window.Blob) {
  59. // Something
  60. document.getElementById('upload').addEventListener('change', file_selected, false);
  61. } else {
  62. alert('Sorry, the HTML5 File APIs are not fully supported in this browser.');
  63. }
  64. }
  65. }
  66. }).directive('ngModelOnblur', function() {
  67. return {
  68. restrict: 'A',
  69. require: 'ngModel',
  70. link: function(scope, elm, attr, ngModelCtrl) {
  71. if (attr.type === 'radio' || attr.type === 'checkbox') return;
  72. elm.unbind('input').unbind('keydown').unbind('change');
  73. elm.bind('blur', function() {
  74. scope.$apply(function() {
  75. ngModelCtrl.$setViewValue(elm.val());
  76. });
  77. });
  78. }
  79. };
  80. });
  81. ;