module.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. angular.module('kibana.map', [])
  2. .controller('map', function($scope, eventBus) {
  3. var _id = _.uniqueId();
  4. // Set and populate defaults
  5. var _d = {
  6. query : "*",
  7. map : "world",
  8. colors : ['#C8EEFF', '#0071A4'],
  9. size : 100,
  10. exclude : [],
  11. group : "default",
  12. }
  13. _.defaults($scope.panel,_d)
  14. $scope.init = function() {
  15. eventBus.register($scope,'time', function(event,time){set_time(time)});
  16. eventBus.register($scope,'query', function(event, query) {
  17. $scope.panel.query = query;
  18. $scope.get_data();
  19. });
  20. // Now that we're all setup, request the time from our group
  21. eventBus.broadcast($scope.$id,$scope.panel.group,'get_time')
  22. }
  23. $scope.get_data = function() {
  24. // Make sure we have everything for the request to complete
  25. if(_.isUndefined($scope.panel.index) || _.isUndefined($scope.panel.time))
  26. return
  27. var request = $scope.ejs.Request().indices($scope.panel.index);
  28. // Then the insert into facet and make the request
  29. var results = request
  30. .facet(ejs.TermsFacet('map')
  31. .field($scope.panel.field)
  32. .size($scope.panel['size'])
  33. .exclude($scope.panel.exclude)
  34. .facetFilter(ejs.QueryFilter(
  35. ejs.FilteredQuery(
  36. ejs.QueryStringQuery($scope.panel.query || '*'),
  37. ejs.RangeFilter(config.timefield)
  38. .from($scope.panel.time.from)
  39. .to($scope.panel.time.to)
  40. )))).size(0)
  41. .doSearch();
  42. // Populate scope when we have results
  43. results.then(function(results) {
  44. $scope.hits = results.hits.total;
  45. $scope.data = {};
  46. _.each(results.facets.map.terms, function(v) {
  47. $scope.data[v.term.toUpperCase()] = v.count;
  48. });
  49. });
  50. }
  51. function set_time(time) {
  52. $scope.panel.time = time;
  53. $scope.panel.index = _.isUndefined(time.index) ? $scope.panel.index : time.index
  54. $scope.get_data();
  55. }
  56. $scope.init()
  57. })
  58. .directive('map', function() {
  59. return {
  60. restrict: 'A',
  61. link: function(scope, elem, attrs) {
  62. // If the data or row state changes, re-render
  63. scope.$watch(function () {
  64. return angular.toJson([scope.data, scope.row])
  65. }, function() {
  66. if(!(_.isUndefined(scope.data)))
  67. render_panel(scope,elem,attrs);
  68. });
  69. // Or if the window is resized
  70. angular.element(window).bind('resize', function(){
  71. render_panel(scope,elem,attrs);
  72. });
  73. function render_panel(scope,elem,attrs) {
  74. // Using LABjs, wait until all scripts are loaded before rendering panel
  75. var scripts = $LAB.script("panels/map/lib/jquery.jvectormap.min.js")
  76. .script("panels/map/lib/map."+scope.panel.map+".js")
  77. // Populate element. Note that jvectormap appends, does not replace.
  78. scripts.wait(function(){
  79. elem.text('');
  80. $('.jvectormap-zoomin,.jvectormap-zoomout,.jvectormap-label').remove();
  81. var map = elem.vectorMap({
  82. map: scope.panel.map,
  83. regionStyle: {initial: {fill: '#ddd'}},
  84. zoomOnScroll: false,
  85. backgroundColor: '#fff',
  86. series: {
  87. regions: [{
  88. values: scope.data,
  89. scale: scope.panel.colors,
  90. normalizeFunction: 'polynomial'
  91. }]
  92. },
  93. onRegionLabelShow: function(event, label, code){
  94. $('.jvectormap-label').css({
  95. "position" : "absolute",
  96. "display" : "none",
  97. "border" : "solid 1px #CDCDCD",
  98. "background" : "#292929",
  99. "color" : "white",
  100. "font-family" : "sans-serif, Verdana",
  101. "font-size" : "smaller",
  102. "padding" : "3px"
  103. })
  104. var count = _.isUndefined(scope.data[code]) ? 0 : scope.data[code];
  105. $('.jvectormap-label').text(label.text() + ": " + count);
  106. },
  107. onRegionOut: function(event, code) {
  108. }
  109. });
  110. })
  111. }
  112. }
  113. };
  114. });