services.js 988 B

123456789101112131415161718192021222324252627282930313233343536373839
  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. $rootScope.$broadcast(type,{
  10. from: from,
  11. to: to,
  12. data: data
  13. });
  14. }
  15. // This sets up an $on listener that checks to see if the event (packet) is
  16. // addressed to the scope in question and runs the registered function if it
  17. // is.
  18. this.register = function(scope,type,fn) {
  19. scope.$on(type,function(event,packet){
  20. var _id = scope.$id;
  21. var _to = packet.to;
  22. var _from = packet.from;
  23. if(!(_.isArray(_to)))
  24. _to = [_to];
  25. if(!(_.isArray(scope.panel.group)))
  26. scope.panel.group = [scope.panel.group];
  27. if(_.intersection(_to,scope.panel.group).length > 0 || _.indexOf(_to,_id) > -1) {
  28. fn(event,packet.data);
  29. }
  30. });
  31. }
  32. });