services.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. if(!(_.isArray(_to)))
  25. _to = [_to];
  26. if(!(_.isArray(scope.panel.group)))
  27. scope.panel.group = [scope.panel.group];
  28. if(_.intersection(_to,scope.panel.group).length > 0 || _.indexOf(_to,_id) > -1) {
  29. //console.log('Got: '+type + ' from ' + _from + ' to ' + _to + ': ' + angular.toJson(packet.data))
  30. fn(event,packet.data);
  31. }
  32. });
  33. }
  34. })
  35. .service('timer', function($timeout) {
  36. // This service really just tracks a list of $timeout promises to give us a
  37. // method for cancelling them all when we need to
  38. var timers = [];
  39. this.register = function(promise) {
  40. timers.push(promise);
  41. return promise;
  42. }
  43. this.cancel = function(promise) {
  44. timers = _.without(timers,promise)
  45. $timeout.cancel(promise)
  46. }
  47. this.cancel_all = function() {
  48. _.each(timers, function(t){
  49. $timeout.cancel(t);
  50. });
  51. timers = new Array();
  52. }
  53. });