directives.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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}} "+
  15. "style='height:{{row.height}}'></div>")(scope))
  16. }
  17. });
  18. }
  19. }
  20. }
  21. })
  22. .directive('arrayJoin', function() {
  23. return {
  24. restrict: 'A',
  25. require: 'ngModel',
  26. link: function(scope, element, attr, ngModel) {
  27. function split_array(text) {
  28. return (text || '').split(',');
  29. }
  30. function join_array(text) {
  31. if(_.isArray(text))
  32. return (text || '').join(',');
  33. else
  34. return text
  35. }
  36. ngModel.$parsers.push(split_array);
  37. ngModel.$formatters.push(join_array);
  38. }
  39. };
  40. })
  41. .directive('upload', function(){
  42. return {
  43. restrict: 'A',
  44. link: function(scope, elem, attrs) {
  45. function file_selected(evt) {
  46. var files = evt.target.files; // FileList object
  47. // files is a FileList of File objects. List some properties.
  48. var output = [];
  49. for (var i = 0, f; f = files[i]; i++) {
  50. var reader = new FileReader();
  51. reader.onload = (function(theFile) {
  52. return function(e) {
  53. // Render thumbnail.
  54. scope.dashboards = JSON.parse(e.target.result)
  55. scope.$apply();
  56. };
  57. })(f);
  58. reader.readAsText(f);
  59. }
  60. }
  61. // Check for the various File API support.
  62. if (window.File && window.FileReader && window.FileList && window.Blob) {
  63. // Something
  64. document.getElementById('upload').addEventListener('change', file_selected, false);
  65. } else {
  66. alert('Sorry, the HTML5 File APIs are not fully supported in this browser.');
  67. }
  68. }
  69. }
  70. })
  71. .directive('datepicker', function(){
  72. return {
  73. restrict: 'A',
  74. require: 'ngModel',
  75. link: function(scope, elem, attrs) {
  76. elem.datepicker({
  77. noDefault: false, // set this to true if you don't want the current date inserted if the value-attribute is empty
  78. format: 'mm/dd/yyyy hh:ii:ss'
  79. });
  80. }
  81. };
  82. })
  83. .directive('date', function(dateFilter) {
  84. return {
  85. require: 'ngModel',
  86. link: function(scope, elm, attrs, ctrl) {
  87. var dateFormat = attrs['date'] || 'yyyy-MM-dd HH:mm:ss';
  88. var minDate = Date.parse(attrs['min']) || 0;
  89. var maxDate = Date.parse(attrs['max']) || 9007199254740992;
  90. ctrl.$parsers.unshift(function(viewValue) {
  91. var parsedDateMilissec = Date.parse(viewValue);
  92. if (parsedDateMilissec > 0) {
  93. if (parsedDateMilissec >= minDate && parsedDateMilissec <= maxDate) {
  94. ctrl.$setValidity('date', true);
  95. return new Date(parsedDateMilissec);
  96. }
  97. }
  98. // in all other cases it is invalid, return undefined (no model update)
  99. ctrl.$setValidity('date', false);
  100. return undefined;
  101. });
  102. ctrl.$formatters.unshift(function(modelValue) {
  103. return dateFilter(modelValue, dateFormat);
  104. });
  105. }
  106. };
  107. });