module.js 4.1 KB

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