Source: CompatibilityEventProxy.es.js

  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. import State from 'metal-state';
  15. import isObject from './util/is_object';
  16. /**
  17. * Adds compatibility for YUI events, re-emitting events according to YUI naming
  18. * and adding the capability of adding targets to bubble events to them.
  19. */
  20. class CompatibilityEventProxy extends State {
  21. /**
  22. * @inheritDoc
  23. */
  24. constructor(config, element) {
  25. super(config, element);
  26. this.eventTargets_ = [];
  27. this.host = config.host;
  28. this.namespace = config.namespace;
  29. this.startCompatibility_();
  30. }
  31. /**
  32. * Registers another event target as a bubble target.
  33. *
  34. * @param {!Object} target The YUI component that receives the emitted
  35. * events.
  36. * @private
  37. */
  38. addTarget(target) {
  39. this.eventTargets_.push(target);
  40. }
  41. /**
  42. * Checks if the event is an attribute modification event and adapts the
  43. * event name accordingly.
  44. *
  45. * @param {!String} eventName The event name.
  46. * @private
  47. * @return {String} The adapted event name.
  48. */
  49. checkAttributeEvent_(eventName) {
  50. return eventName.replace(
  51. this.adaptedEvents.match,
  52. this.adaptedEvents.replace
  53. );
  54. }
  55. /**
  56. * Emits the event adapted to YUI.
  57. *
  58. * @param {!String} eventName The event name.
  59. * @param {!Event} event The event.
  60. * @private
  61. */
  62. emitCompatibleEvents_(eventName, event) {
  63. this.eventTargets_.forEach((target) => {
  64. if (target.fire) {
  65. const prefixedEventName = this.namespace
  66. ? this.namespace + ':' + eventName
  67. : eventName;
  68. const yuiEvent = target._yuievt.events[prefixedEventName];
  69. if (isObject(event)) {
  70. try {
  71. event.target = this.host;
  72. }
  73. catch (e) {
  74. // Do nothing
  75. }
  76. }
  77. let emitFacadeReference;
  78. if (!this.emitFacade && yuiEvent) {
  79. emitFacadeReference = yuiEvent.emitFacade;
  80. yuiEvent.emitFacade = false;
  81. }
  82. target.fire(prefixedEventName, event);
  83. if (!this.emitFacade && yuiEvent) {
  84. yuiEvent.emitFacade = emitFacadeReference;
  85. }
  86. }
  87. });
  88. }
  89. /**
  90. * Emits YUI-based events to maintain backwards compatibility.
  91. *
  92. * @private
  93. */
  94. startCompatibility_() {
  95. this.host.on('*', (event, eventFacade) => {
  96. if (!eventFacade) {
  97. eventFacade = event;
  98. }
  99. const compatibleEvent = this.checkAttributeEvent_(eventFacade.type);
  100. if (compatibleEvent !== eventFacade.type) {
  101. eventFacade.type = compatibleEvent;
  102. this.host.emit(compatibleEvent, event, eventFacade);
  103. }
  104. else if (this.eventTargets_.length > 0) {
  105. this.emitCompatibleEvents_(compatibleEvent, event);
  106. }
  107. });
  108. }
  109. }
  110. /**
  111. * State definition.
  112. *
  113. * @ignore
  114. * @static
  115. * @type {!Object}
  116. */
  117. CompatibilityEventProxy.STATE = {
  118. /**
  119. * Replaces event names with adapted YUI names.
  120. *
  121. * @instance
  122. * @memberof CompatibilityEventProxy
  123. * @type {Object}
  124. */
  125. adaptedEvents: {
  126. value: {
  127. match: /(.*)(Changed)$/,
  128. replace: '$1Change',
  129. },
  130. },
  131. /**
  132. * Whether the event facade should be emitted to the target.
  133. *
  134. * @default false
  135. * @instance
  136. * @memberof CompatibilityEventProxy
  137. * @type {String}
  138. */
  139. emitFacade: {
  140. value: false,
  141. },
  142. };
  143. export default CompatibilityEventProxy;