services.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*jshint globalstrict:true */
  2. /*global angular:true */
  3. 'use strict';
  4. angular.module('kibana.services', [])
  5. .service('eventBus', function($rootScope) {
  6. this.broadcast = function(from,to,type,data) {
  7. if(_.isUndefined(data))
  8. var data = from
  9. //console.log('Sent: '+type + ' to ' + to + ' from ' + from + ': ' + angular.toJson(data))
  10. $rootScope.$broadcast(type,{
  11. from: from,
  12. to: to,
  13. data: data
  14. });
  15. }
  16. // This sets up an $on listener that checks to see if the event (packet) is
  17. // addressed to the scope in question and runs the registered function if it
  18. // is.
  19. this.register = function(scope,type,fn) {
  20. scope.$on(type,function(event,packet){
  21. var _id = scope.$id;
  22. var _to = packet.to;
  23. var _from = packet.from;
  24. var _group = (!(_.isUndefined(scope.panel))) ? scope.panel.group : ["NONE"]
  25. //console.log('registered:' + type + " for " + scope.panel.title + " " + scope.$id)
  26. if(!(_.isArray(_to)))
  27. _to = [_to];
  28. if(!(_.isArray(_group)))
  29. _group = [_group];
  30. if((_.intersection(_to,_group).length > 0 ||
  31. _.indexOf(_to,_id) > -1 ||
  32. _.indexOf(_to,'ALL') > -1) &&
  33. _from !== _id
  34. ) {
  35. //console.log('Got: '+type + ' from ' + _from + ' to ' + _to + ': ' + angular.toJson(packet.data))
  36. fn(event,packet.data);
  37. }
  38. });
  39. }
  40. })
  41. .service('timer', function($timeout) {
  42. // This service really just tracks a list of $timeout promises to give us a
  43. // method for cancelling them all when we need to
  44. var timers = [];
  45. this.register = function(promise) {
  46. timers.push(promise);
  47. return promise;
  48. }
  49. this.cancel = function(promise) {
  50. timers = _.without(timers,promise)
  51. $timeout.cancel(promise)
  52. }
  53. this.cancel_all = function() {
  54. _.each(timers, function(t){
  55. $timeout.cancel(t);
  56. });
  57. timers = new Array();
  58. }
  59. });