EventChannelRouter.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2015, 2020
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. /**
  8. * This class creates a private event router for particular events while a global event router is used for rest of events.
  9. *
  10. * SamePage scope of private event router is supported. It can be extended in the future for between two widgets, and etc..
  11. */
  12. define(['underscore', './EventRouter'], function (_, EventRouter) {
  13. var EventChannelRouter = EventRouter.extend({
  14. //global event router
  15. globalEventRouter: null,
  16. //array of private event names to the channel
  17. eventNames: null,
  18. //private channel id
  19. channelId: null,
  20. //pretty name
  21. _alias: null,
  22. init: function init(options) {
  23. EventChannelRouter.inherited('init', this, arguments);
  24. this.globalEventRouter = options.globalEventRouter;
  25. this.channelId = options.channelId;
  26. this.eventNames = options.eventNames;
  27. this._alias = options.alias;
  28. },
  29. _isPrivateEvent: function _isPrivateEvent(eventName) {
  30. return _.find(this.eventNames, function (name) {
  31. return name === eventName;
  32. });
  33. },
  34. _getChanneledEventName: function _getChanneledEventName(name) {
  35. return this.channelId + '_' + name;
  36. },
  37. on: function on(eventName, handler, context) {
  38. if (this._isPrivateEvent(eventName)) {
  39. EventChannelRouter.inherited('on', this, [this._getChanneledEventName(eventName), handler, context]);
  40. } else {
  41. this.globalEventRouter.on(eventName, handler, context);
  42. }
  43. },
  44. off: function off(eventName, handler, context) {
  45. if (this._isPrivateEvent(eventName)) {
  46. EventChannelRouter.inherited('off', this, [this._getChanneledEventName(eventName), handler, context]);
  47. } else {
  48. this.globalEventRouter.off(eventName, handler, context);
  49. }
  50. },
  51. trigger: function trigger(eventName, event) {
  52. if (this._isPrivateEvent(eventName)) {
  53. EventChannelRouter.inherited('trigger', this, [this._getChanneledEventName(eventName), event]);
  54. } else {
  55. this.globalEventRouter.trigger(eventName, event);
  56. }
  57. }
  58. });
  59. return EventChannelRouter;
  60. });
  61. //# sourceMappingURL=EventChannelRouter.js.map