filterSrv.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. define([
  2. 'angular',
  3. 'underscore',
  4. 'config',
  5. 'kbn'
  6. ], function (angular, _, config, kbn) {
  7. 'use strict';
  8. var module = angular.module('kibana.services');
  9. module.service('filterSrv', function(dashboard, $rootScope, $timeout) {
  10. // Create an object to hold our service state on the dashboard
  11. dashboard.current.services.filter = dashboard.current.services.filter || {};
  12. // defaults
  13. var _d = {
  14. list: [],
  15. time: {}
  16. };
  17. // Save a reference to this
  18. var self = this;
  19. // Call this whenever we need to reload the important stuff
  20. this.init = function() {
  21. _.defaults(dashboard.current.services.filter, _d);
  22. self.list = dashboard.current.services.filter.list;
  23. self.time = dashboard.current.services.filter.time;
  24. self.templateSettings = {
  25. interpolate : /\[\[([\s\S]+?)\]\]/g,
  26. };
  27. };
  28. this.filterOptionSelected = function(filter, option) {
  29. filter.current = option;
  30. self.filterTemplateData = undefined;
  31. dashboard.refresh();
  32. };
  33. this.add = function(filter) {
  34. self.list.push(filter);
  35. };
  36. this.applyFilterToTarget = function(target) {
  37. if (target.indexOf('[[') === -1) {
  38. return target;
  39. }
  40. if (!self.filterTemplateData) {
  41. self.filterTemplateData = {};
  42. _.each(self.list, function(filter) {
  43. self.filterTemplateData[filter.name] = filter.current.value;
  44. });
  45. }
  46. return _.template(target, self.filterTemplateData, self.templateSettings);
  47. };
  48. this.remove = function(filter) {
  49. self.list = dashboard.current.services.filter.list = _.without(self.list, filter);
  50. if(!$rootScope.$$phase) {
  51. $rootScope.$apply();
  52. }
  53. $timeout(function(){
  54. dashboard.refresh();
  55. },0);
  56. };
  57. this.setTime = function(time) {
  58. _.extend(self.time, time);
  59. $timeout(function(){
  60. dashboard.refresh();
  61. },0);
  62. };
  63. this.timeRange = function(parse) {
  64. var _t = self.time;
  65. if(_.isUndefined(_t)) {
  66. return false;
  67. }
  68. if(parse === false) {
  69. return {
  70. from: _t.from,
  71. to: _t.to
  72. };
  73. } else {
  74. var
  75. _from = _t.from,
  76. _to = _t.to || new Date();
  77. return {
  78. from : kbn.parseDate(_from),
  79. to : kbn.parseDate(_to)
  80. };
  81. }
  82. };
  83. // Now init
  84. self.init();
  85. });
  86. });