event_emitter.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. var EventEmitter = {};
  33. var stopPropagation = function() { this.propagationStopped = true; };
  34. var preventDefault = function() { this.defaultPrevented = true; };
  35. EventEmitter._emit =
  36. EventEmitter._dispatchEvent = function(eventName, e) {
  37. this._eventRegistry || (this._eventRegistry = {});
  38. this._defaultHandlers || (this._defaultHandlers = {});
  39. var listeners = this._eventRegistry[eventName] || [];
  40. var defaultHandler = this._defaultHandlers[eventName];
  41. if (!listeners.length && !defaultHandler)
  42. return;
  43. if (typeof e != "object" || !e)
  44. e = {};
  45. if (!e.type)
  46. e.type = eventName;
  47. if (!e.stopPropagation)
  48. e.stopPropagation = stopPropagation;
  49. if (!e.preventDefault)
  50. e.preventDefault = preventDefault;
  51. listeners = listeners.slice();
  52. for (var i=0; i<listeners.length; i++) {
  53. listeners[i](e, this);
  54. if (e.propagationStopped)
  55. break;
  56. }
  57. if (defaultHandler && !e.defaultPrevented)
  58. return defaultHandler(e, this);
  59. };
  60. EventEmitter._signal = function(eventName, e) {
  61. var listeners = (this._eventRegistry || {})[eventName];
  62. if (!listeners)
  63. return;
  64. listeners = listeners.slice();
  65. for (var i=0; i<listeners.length; i++)
  66. listeners[i](e, this);
  67. };
  68. EventEmitter.once = function(eventName, callback) {
  69. var _self = this;
  70. callback && this.addEventListener(eventName, function newCallback() {
  71. _self.removeEventListener(eventName, newCallback);
  72. callback.apply(null, arguments);
  73. });
  74. };
  75. EventEmitter.setDefaultHandler = function(eventName, callback) {
  76. var handlers = this._defaultHandlers
  77. if (!handlers)
  78. handlers = this._defaultHandlers = {_disabled_: {}};
  79. if (handlers[eventName]) {
  80. var old = handlers[eventName];
  81. var disabled = handlers._disabled_[eventName];
  82. if (!disabled)
  83. handlers._disabled_[eventName] = disabled = [];
  84. disabled.push(old);
  85. var i = disabled.indexOf(callback);
  86. if (i != -1)
  87. disabled.splice(i, 1);
  88. }
  89. handlers[eventName] = callback;
  90. };
  91. EventEmitter.removeDefaultHandler = function(eventName, callback) {
  92. var handlers = this._defaultHandlers
  93. if (!handlers)
  94. return;
  95. var disabled = handlers._disabled_[eventName];
  96. if (handlers[eventName] == callback) {
  97. var old = handlers[eventName];
  98. if (disabled)
  99. this.setDefaultHandler(eventName, disabled.pop());
  100. } else if (disabled) {
  101. var i = disabled.indexOf(callback);
  102. if (i != -1)
  103. disabled.splice(i, 1);
  104. }
  105. };
  106. EventEmitter.on =
  107. EventEmitter.addEventListener = function(eventName, callback, capturing) {
  108. this._eventRegistry = this._eventRegistry || {};
  109. var listeners = this._eventRegistry[eventName];
  110. if (!listeners)
  111. listeners = this._eventRegistry[eventName] = [];
  112. if (listeners.indexOf(callback) == -1)
  113. listeners[capturing ? "unshift" : "push"](callback);
  114. return callback;
  115. };
  116. EventEmitter.off =
  117. EventEmitter.removeListener =
  118. EventEmitter.removeEventListener = function(eventName, callback) {
  119. this._eventRegistry = this._eventRegistry || {};
  120. var listeners = this._eventRegistry[eventName];
  121. if (!listeners)
  122. return;
  123. var index = listeners.indexOf(callback);
  124. if (index !== -1)
  125. listeners.splice(index, 1);
  126. };
  127. EventEmitter.removeAllListeners = function(eventName) {
  128. if (this._eventRegistry) this._eventRegistry[eventName] = [];
  129. };
  130. exports.EventEmitter = EventEmitter;
  131. });