services.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*jshint globalstrict:true */
  2. /*global angular:true */
  3. 'use strict';
  4. angular.module('kibana.services', [])
  5. .service('eventBus', function($rootScope) {
  6. // An array of registed types
  7. var _types = []
  8. this.broadcast = function(from,to,type,data) {
  9. if(_.isUndefined(data))
  10. var data = from
  11. var packet = {
  12. time: new Date(),
  13. type: type,
  14. from: from,
  15. to: to,
  16. data: data
  17. }
  18. if(_.contains(_types,'$kibana_debug'))
  19. $rootScope.$broadcast('$kibana_debug',packet);
  20. //console.log('Sent: '+type + ' to ' + to + ' from ' + from + ': ' + angular.toJson(data))
  21. $rootScope.$broadcast(type,{
  22. from: from,
  23. to: to,
  24. data: data
  25. });
  26. }
  27. // This sets up an $on listener that checks to see if the event (packet) is
  28. // addressed to the scope in question and runs the registered function if it
  29. // is.
  30. this.register = function(scope,type,fn) {
  31. _types = _.union(_types,[type])
  32. scope.$on(type,function(event,packet){
  33. var _id = scope.$id;
  34. var _to = packet.to;
  35. var _from = packet.from;
  36. var _type = packet.type
  37. var _time = packet.time
  38. var _group = (!(_.isUndefined(scope.panel))) ? scope.panel.group : ["NONE"]
  39. //console.log('registered:' + type + " for " + scope.panel.title + " " + scope.$id)
  40. if(!(_.isArray(_to)))
  41. _to = [_to];
  42. if(!(_.isArray(_group)))
  43. _group = [_group];
  44. // Transmit even only if the send is not the receiver AND one of the following:
  45. // 1) Receiver has group in _to 2) Receiver's $id is in _to
  46. // 3) Event is addressed to ALL 4) Receiver is in ALL group
  47. if((_.intersection(_to,_group).length > 0 ||
  48. _.indexOf(_to,_id) > -1 ||
  49. _.indexOf(_group,'ALL') > -1 ||
  50. _.indexOf(_to,'ALL') > -1) &&
  51. _from !== _id
  52. ) {
  53. //console.log('Got: '+type + ' from ' + _from + ' to ' + _to + ': ' + angular.toJson(packet.data))
  54. fn(event,packet.data,{time:_time,to:_to,from:_from,type:_type});
  55. }
  56. });
  57. }
  58. })
  59. /* Service: fields
  60. Provides a global list of all seen fields for use in editor panels
  61. */
  62. .factory('fields', function($rootScope) {
  63. var fields = {
  64. list : []
  65. }
  66. $rootScope.$on('fields', function(event,f) {
  67. fields.list = _.union(f.data.all,fields.list)
  68. })
  69. return fields;
  70. })
  71. .service('timer', function($timeout) {
  72. // This service really just tracks a list of $timeout promises to give us a
  73. // method for cancelling them all when we need to
  74. var timers = [];
  75. this.register = function(promise) {
  76. timers.push(promise);
  77. return promise;
  78. }
  79. this.cancel = function(promise) {
  80. timers = _.without(timers,promise)
  81. $timeout.cancel(promise)
  82. }
  83. this.cancel_all = function() {
  84. _.each(timers, function(t){
  85. $timeout.cancel(t);
  86. });
  87. timers = new Array();
  88. }
  89. })
  90. .service('keylistener', function($rootScope) {
  91. var keys = [];
  92. $(document).keydown(function (e) {
  93. keys[e.which] = true;
  94. });
  95. $(document).keyup(function (e) {
  96. delete keys[e.which];
  97. });
  98. this.keyActive = function(key) {
  99. return keys[key] == true;
  100. }
  101. });