module.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. )))).size(0)
  42. .doSearch();
  43. // Populate scope when we have results
  44. results.then(function(results) {
  45. $scope.hits = results.hits.total;
  46. $scope.data = {};
  47. _.each(results.facets.map.terms, function(v) {
  48. $scope.data[v.term.toUpperCase()] = v.count;
  49. });
  50. });
  51. }
  52. function set_time(time) {
  53. $scope.panel.time = time;
  54. $scope.panel.index = _.isUndefined(time.index) ? $scope.panel.index : time.index
  55. $scope.get_data();
  56. }
  57. $scope.init()
  58. })
  59. .directive('map', function() {
  60. return {
  61. restrict: 'A',
  62. link: function(scope, elem, attrs) {
  63. // If the data or row state changes, re-render
  64. scope.$watch(function () {
  65. return angular.toJson([scope.data, scope.row])
  66. }, function() {
  67. if(!(_.isUndefined(scope.data)))
  68. render_panel(scope,elem,attrs);
  69. });
  70. // Or if the window is resized
  71. angular.element(window).bind('resize', function(){
  72. render_panel(scope,elem,attrs);
  73. });
  74. function render_panel(scope,elem,attrs) {
  75. // Using LABjs, wait until all scripts are loaded before rendering panel
  76. var scripts = $LAB.script("common/lib/panels/jquery.jvectormap.min.js")
  77. .script("common/lib/panels/map."+scope.panel.map+".js")
  78. // Populate element. Note that jvectormap appends, does not replace.
  79. scripts.wait(function(){
  80. elem.text('');
  81. $('.jvectormap-zoomin,.jvectormap-zoomout,.jvectormap-label').remove();
  82. var map = elem.vectorMap({
  83. map: scope.panel.map,
  84. regionStyle: {initial: {fill: '#ddd'}},
  85. zoomOnScroll: false,
  86. backgroundColor: '#fff',
  87. series: {
  88. regions: [{
  89. values: scope.data,
  90. scale: scope.panel.colors,
  91. normalizeFunction: 'polynomial'
  92. }]
  93. },
  94. onRegionLabelShow: function(event, label, code){
  95. $('.jvectormap-label').css({
  96. "position" : "absolute",
  97. "display" : "none",
  98. "border" : "solid 1px #CDCDCD",
  99. "background" : "#292929",
  100. "color" : "white",
  101. "font-family" : "sans-serif, Verdana",
  102. "font-size" : "smaller",
  103. "padding" : "3px"
  104. })
  105. var count = _.isUndefined(scope.data[code]) ? 0 : scope.data[code];
  106. $('.jvectormap-label').text(label.text() + ": " + count);
  107. },
  108. onRegionOut: function(event, code) {
  109. }
  110. });
  111. })
  112. }
  113. }
  114. };
  115. });