services.js 1.8 KB

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