services.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. ) {
  34. //console.log('Got: '+type + ' from ' + _from + ' to ' + _to + ': ' + angular.toJson(packet.data))
  35. fn(event,packet.data);
  36. }
  37. });
  38. }
  39. })
  40. .service('timer', function($timeout) {
  41. // This service really just tracks a list of $timeout promises to give us a
  42. // method for cancelling them all when we need to
  43. var timers = [];
  44. this.register = function(promise) {
  45. timers.push(promise);
  46. return promise;
  47. }
  48. this.cancel = function(promise) {
  49. timers = _.without(timers,promise)
  50. $timeout.cancel(promise)
  51. }
  52. this.cancel_all = function() {
  53. _.each(timers, function(t){
  54. $timeout.cancel(t);
  55. });
  56. timers = new Array();
  57. }
  58. });