directives.js 2.6 KB

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