Object.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (C) 2008 Apple Inc. All Rights Reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. /**
  26. * @constructor
  27. * @implements {WebInspector.EventTarget}
  28. */
  29. WebInspector.Object = function() {
  30. }
  31. WebInspector.Object.prototype = {
  32. /**
  33. * @param {string} eventType
  34. * @param {function(WebInspector.Event)} listener
  35. * @param {Object=} thisObject
  36. */
  37. addEventListener: function(eventType, listener, thisObject)
  38. {
  39. if (!listener)
  40. console.assert(false);
  41. if (!this._listeners)
  42. this._listeners = {};
  43. if (!this._listeners[eventType])
  44. this._listeners[eventType] = [];
  45. this._listeners[eventType].push({ thisObject: thisObject, listener: listener });
  46. },
  47. /**
  48. * @param {string} eventType
  49. * @param {function(WebInspector.Event)} listener
  50. * @param {Object=} thisObject
  51. */
  52. removeEventListener: function(eventType, listener, thisObject)
  53. {
  54. console.assert(listener);
  55. if (!this._listeners || !this._listeners[eventType])
  56. return;
  57. var listeners = this._listeners[eventType];
  58. for (var i = 0; i < listeners.length; ++i) {
  59. if (listener && listeners[i].listener === listener && listeners[i].thisObject === thisObject)
  60. listeners.splice(i, 1);
  61. else if (!listener && thisObject && listeners[i].thisObject === thisObject)
  62. listeners.splice(i, 1);
  63. }
  64. if (!listeners.length)
  65. delete this._listeners[eventType];
  66. },
  67. removeAllListeners: function()
  68. {
  69. delete this._listeners;
  70. },
  71. /**
  72. * @param {string} eventType
  73. * @return {boolean}
  74. */
  75. hasEventListeners: function(eventType)
  76. {
  77. if (!this._listeners || !this._listeners[eventType])
  78. return false;
  79. return true;
  80. },
  81. /**
  82. * @param {string} eventType
  83. * @param {*=} eventData
  84. * @return {boolean}
  85. */
  86. dispatchEventToListeners: function(eventType, eventData)
  87. {
  88. if (!this._listeners || !this._listeners[eventType])
  89. return false;
  90. var event = new WebInspector.Event(this, eventType, eventData);
  91. var listeners = this._listeners[eventType].slice(0);
  92. for (var i = 0; i < listeners.length; ++i) {
  93. listeners[i].listener.call(listeners[i].thisObject, event);
  94. if (event._stoppedPropagation)
  95. break;
  96. }
  97. return event.defaultPrevented;
  98. }
  99. }
  100. /**
  101. * @constructor
  102. * @param {WebInspector.EventTarget} target
  103. * @param {string} type
  104. * @param {*=} data
  105. */
  106. WebInspector.Event = function(target, type, data)
  107. {
  108. this.target = target;
  109. this.type = type;
  110. this.data = data;
  111. this.defaultPrevented = false;
  112. this._stoppedPropagation = false;
  113. }
  114. WebInspector.Event.prototype = {
  115. stopPropagation: function()
  116. {
  117. this._stoppedPropagation = true;
  118. },
  119. preventDefault: function()
  120. {
  121. this.defaultPrevented = true;
  122. },
  123. /**
  124. * @param {boolean=} preventDefault
  125. */
  126. consume: function(preventDefault)
  127. {
  128. this.stopPropagation();
  129. if (preventDefault)
  130. this.preventDefault();
  131. }
  132. }
  133. /**
  134. * @interface
  135. */
  136. WebInspector.EventTarget = function()
  137. {
  138. }
  139. WebInspector.EventTarget.prototype = {
  140. /**
  141. * @param {string} eventType
  142. * @param {function(WebInspector.Event)} listener
  143. * @param {Object=} thisObject
  144. */
  145. addEventListener: function(eventType, listener, thisObject) { },
  146. /**
  147. * @param {string} eventType
  148. * @param {function(WebInspector.Event)} listener
  149. * @param {Object=} thisObject
  150. */
  151. removeEventListener: function(eventType, listener, thisObject) { },
  152. removeAllListeners: function() { },
  153. /**
  154. * @param {string} eventType
  155. * @return {boolean}
  156. */
  157. hasEventListeners: function(eventType) { },
  158. /**
  159. * @param {string} eventType
  160. * @param {*=} eventData
  161. * @return {boolean}
  162. */
  163. dispatchEventToListeners: function(eventType, eventData) { },
  164. }
  165. WebInspector.notifications = new WebInspector.Object();