Source: events/event.js

  1. /**
  2. * @fileOverview Requirejs module containing the antie.events.Event abstract base class.
  3. * @author Chris Warren <chris.warren@bbc.co.uk>
  4. * @version 1.0.0
  5. */
  6. /**
  7. * @preserve Copyright (c) 2013-present British Broadcasting Corporation. All rights reserved.
  8. * @license See https://github.com/fmtvp/tal/blob/master/LICENSE for full licence
  9. */
  10. define(
  11. 'antie/events/event',
  12. [
  13. 'antie/class',
  14. 'antie/runtimecontext'
  15. ],
  16. function(Class, RuntimeContext) {
  17. 'use strict';
  18. var eventCount = 0;
  19. var eventListeners = {};
  20. /**
  21. * Abstract base class for events.
  22. * @abstract
  23. * @extends antie.Class
  24. * @name antie.events.Event
  25. * @class
  26. * @param {String} type The event type (e.g. <code>keydown</code>, <code>databound</code>).
  27. */
  28. var Event = Class.extend(/** @lends antie.events.Event.prototype */ {
  29. /**
  30. * @constructor
  31. * @ignore
  32. */
  33. init: function init (type) {
  34. this.type = type;
  35. this._propagationStopped = false;
  36. this._defaultPrevented = false;
  37. eventCount++;
  38. },
  39. /**
  40. * Stop propagation of the event through the widget tree.
  41. */
  42. stopPropagation: function stopPropagation () {
  43. this._propagationStopped = true;
  44. eventCount--;
  45. if (!eventCount) {
  46. this.fireEvent('emptyStack');
  47. }
  48. },
  49. /**
  50. * Check to see if the propagation of this event has been stopped.
  51. * @returns Boolean true if the event has been stopped, otherwise Boolean false.
  52. */
  53. isPropagationStopped: function isPropagationStopped () {
  54. return this._propagationStopped;
  55. },
  56. /**
  57. * Prevent any default handler being called for this event.
  58. */
  59. preventDefault: function preventDefault () {
  60. this._defaultPrevented = true;
  61. },
  62. /**
  63. * Check to see if the default handler has been prevented.
  64. * @returns Boolean true if the default handler has been prevented.
  65. */
  66. isDefaultPrevented: function isDefaultPrevented () {
  67. return this._defaultPrevented;
  68. },
  69. /**
  70. * Add an event listener function to the event stack. Used for 'meta-events'.
  71. * @memberOf antie.events.Event
  72. * @static
  73. * @param {String} ev The event type to listen for (e.g. <code>emptyStack</code>)
  74. * @param {Function} func The handler to be called when the event is fired.
  75. */
  76. addEventListener: function addEventListener (ev, func) {
  77. var listeners = eventListeners[ev];
  78. if (typeof listeners === 'undefined') {
  79. listeners = [];
  80. eventListeners[ev] = listeners;
  81. }
  82. if (!~listeners.indexOf(func)) {
  83. listeners.push(func);
  84. }
  85. },
  86. /**
  87. * Removes an event listener function to the event stack. Used for 'meta-events'.
  88. * @memberOf antie.events.Event
  89. * @static
  90. * @param {String} ev The event type that the listener is to be removed from (e.g. <code>emptyStack</code>)
  91. * @param {Function} func The handler to be removed.
  92. */
  93. removeEventListener: function removeEventListener (ev, func) {
  94. var listeners = eventListeners[ev],
  95. listener;
  96. if (!listeners) {
  97. RuntimeContext.getDevice().getLogger().error('Attempting to remove non-existent event listener');
  98. return false;
  99. }
  100. listener = listeners.indexOf(func);
  101. if (~listener) {
  102. listeners.splice(listener, 1);
  103. }
  104. },
  105. /**
  106. * Fires an event on the event stack.
  107. * Note: this does not bubble or propagate the event, the concept is meaningless in the context
  108. * of the event stack.
  109. * @memberOf antie.events.Event
  110. * @static
  111. * @param {String} ev The event to fire (e.g. <code>emptyStack</code>).
  112. * @see antie.events.Event
  113. */
  114. fireEvent: function fireEvent (ev) {
  115. var listeners = eventListeners[ev];
  116. if(listeners) {
  117. for(var func in listeners) {
  118. if(listeners.hasOwnProperty(func)) {
  119. listeners[func]();
  120. }
  121. }
  122. }
  123. }
  124. });
  125. return Event;
  126. }
  127. );