services.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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('timer', function($timeout) {
  60. // This service really just tracks a list of $timeout promises to give us a
  61. // method for cancelling them all when we need to
  62. var timers = [];
  63. this.register = function(promise) {
  64. timers.push(promise);
  65. return promise;
  66. }
  67. this.cancel = function(promise) {
  68. timers = _.without(timers,promise)
  69. $timeout.cancel(promise)
  70. }
  71. this.cancel_all = function() {
  72. _.each(timers, function(t){
  73. $timeout.cancel(t);
  74. });
  75. timers = new Array();
  76. }
  77. });