directives.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*jshint globalstrict:true */
  2. /*global angular:true */
  3. 'use strict';
  4. angular.module('kibana.directives', [])
  5. .directive('panel', function($compile) {
  6. return {
  7. restrict: 'A',
  8. compile: function(element, attrs) {
  9. return function(scope, element, attrs) {
  10. scope.$watch(function () {
  11. return (attrs.panel && scope.index) ? true : false;
  12. }, function (ready) {
  13. if (ready) {
  14. element.html($compile("<div "+attrs.panel+" params={{panel}} style='height:{{row.height}}'></div>")(scope))
  15. }
  16. });
  17. }
  18. }
  19. }
  20. })
  21. .directive('upload', function(){
  22. return {
  23. restrict: 'A',
  24. link: function(scope, elem, attrs) {
  25. function file_selected(evt) {
  26. var files = evt.target.files; // FileList object
  27. // files is a FileList of File objects. List some properties.
  28. var output = [];
  29. for (var i = 0, f; f = files[i]; i++) {
  30. var reader = new FileReader();
  31. reader.onload = (function(theFile) {
  32. return function(e) {
  33. // Render thumbnail.
  34. scope.dashboards = JSON.parse(e.target.result)
  35. scope.$apply();
  36. };
  37. })(f);
  38. reader.readAsText(f);
  39. }
  40. }
  41. // Check for the various File API support.
  42. if (window.File && window.FileReader && window.FileList && window.Blob) {
  43. // Something
  44. document.getElementById('upload').addEventListener('change', file_selected, false);
  45. } else {
  46. alert('Sorry, the HTML5 File APIs are not fully supported in this browser.');
  47. }
  48. }
  49. }
  50. })
  51. .directive('datepicker', function(){
  52. return {
  53. restrict: 'A',
  54. require: 'ngModel',
  55. link: function(scope, elem, attrs) {
  56. elem.datepicker({
  57. noDefault: false, // set this to true if you don't want the current date inserted if the value-attribute is empty
  58. format: 'mm/dd/yyyy hh:ii:ss'
  59. });
  60. }
  61. };
  62. })
  63. .directive('date', function(dateFilter) {
  64. return {
  65. require: 'ngModel',
  66. link: function(scope, elm, attrs, ctrl) {
  67. var dateFormat = attrs['date'] || 'yyyy-MM-dd HH:mm:ss';
  68. var minDate = Date.parse(attrs['min']) || 0;
  69. var maxDate = Date.parse(attrs['max']) || 9007199254740992;
  70. ctrl.$parsers.unshift(function(viewValue) {
  71. var parsedDateMilissec = Date.parse(viewValue);
  72. if (parsedDateMilissec > 0) {
  73. if (parsedDateMilissec >= minDate && parsedDateMilissec <= maxDate) {
  74. ctrl.$setValidity('date', true);
  75. return new Date(parsedDateMilissec);
  76. }
  77. }
  78. // in all other cases it is invalid, return undefined (no model update)
  79. ctrl.$setValidity('date', false);
  80. return undefined;
  81. });
  82. ctrl.$formatters.unshift(function(modelValue) {
  83. return dateFilter(modelValue, dateFormat);
  84. });
  85. }
  86. };
  87. });