mxEventSource.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxEventSource
  7. *
  8. * Base class for objects that dispatch named events. To create a subclass that
  9. * inherits from mxEventSource, the following code is used.
  10. *
  11. * (code)
  12. * function MyClass() { };
  13. *
  14. * MyClass.prototype = new mxEventSource();
  15. * MyClass.prototype.constructor = MyClass;
  16. * (end)
  17. *
  18. * Known Subclasses:
  19. *
  20. * <mxGraphModel>, <mxGraph>, <mxGraphView>, <mxEditor>, <mxCellOverlay>,
  21. * <mxToolbar>, <mxWindow>
  22. *
  23. * Constructor: mxEventSource
  24. *
  25. * Constructs a new event source.
  26. */
  27. function mxEventSource(eventSource)
  28. {
  29. this.setEventSource(eventSource);
  30. };
  31. /**
  32. * Variable: eventListeners
  33. *
  34. * Holds the event names and associated listeners in an array. The array
  35. * contains the event name followed by the respective listener for each
  36. * registered listener.
  37. */
  38. mxEventSource.prototype.eventListeners = null;
  39. /**
  40. * Variable: eventsEnabled
  41. *
  42. * Specifies if events can be fired. Default is true.
  43. */
  44. mxEventSource.prototype.eventsEnabled = true;
  45. /**
  46. * Variable: eventSource
  47. *
  48. * Optional source for events. Default is null.
  49. */
  50. mxEventSource.prototype.eventSource = null;
  51. /**
  52. * Function: isEventsEnabled
  53. *
  54. * Returns <eventsEnabled>.
  55. */
  56. mxEventSource.prototype.isEventsEnabled = function()
  57. {
  58. return this.eventsEnabled;
  59. };
  60. /**
  61. * Function: setEventsEnabled
  62. *
  63. * Sets <eventsEnabled>.
  64. */
  65. mxEventSource.prototype.setEventsEnabled = function(value)
  66. {
  67. this.eventsEnabled = value;
  68. };
  69. /**
  70. * Function: getEventSource
  71. *
  72. * Returns <eventSource>.
  73. */
  74. mxEventSource.prototype.getEventSource = function()
  75. {
  76. return this.eventSource;
  77. };
  78. /**
  79. * Function: setEventSource
  80. *
  81. * Sets <eventSource>.
  82. */
  83. mxEventSource.prototype.setEventSource = function(value)
  84. {
  85. this.eventSource = value;
  86. };
  87. /**
  88. * Function: addListener
  89. *
  90. * Binds the specified function to the given event name. If no event name
  91. * is given, then the listener is registered for all events.
  92. *
  93. * The parameters of the listener are the sender and an <mxEventObject>.
  94. */
  95. mxEventSource.prototype.addListener = function(name, funct)
  96. {
  97. if (this.eventListeners == null)
  98. {
  99. this.eventListeners = [];
  100. }
  101. this.eventListeners.push(name);
  102. this.eventListeners.push(funct);
  103. };
  104. /**
  105. * Function: removeListener
  106. *
  107. * Removes all occurrences of the given listener from <eventListeners>.
  108. */
  109. mxEventSource.prototype.removeListener = function(funct)
  110. {
  111. if (this.eventListeners != null)
  112. {
  113. var i = 0;
  114. while (i < this.eventListeners.length)
  115. {
  116. if (this.eventListeners[i+1] == funct)
  117. {
  118. this.eventListeners.splice(i, 2);
  119. }
  120. else
  121. {
  122. i += 2;
  123. }
  124. }
  125. }
  126. };
  127. /**
  128. * Function: fireEvent
  129. *
  130. * Dispatches the given event to the listeners which are registered for
  131. * the event. The sender argument is optional. The current execution scope
  132. * ("this") is used for the listener invocation (see <mxUtils.bind>).
  133. *
  134. * Example:
  135. *
  136. * (code)
  137. * fireEvent(new mxEventObject("eventName", key1, val1, .., keyN, valN))
  138. * (end)
  139. *
  140. * Parameters:
  141. *
  142. * evt - <mxEventObject> that represents the event.
  143. * sender - Optional sender to be passed to the listener. Default value is
  144. * the return value of <getEventSource>.
  145. */
  146. mxEventSource.prototype.fireEvent = function(evt, sender)
  147. {
  148. if (this.eventListeners != null && this.isEventsEnabled())
  149. {
  150. if (evt == null)
  151. {
  152. evt = new mxEventObject();
  153. }
  154. if (sender == null)
  155. {
  156. sender = this.getEventSource();
  157. }
  158. if (sender == null)
  159. {
  160. sender = this;
  161. }
  162. var args = [sender, evt];
  163. for (var i = 0; i < this.eventListeners.length; i += 2)
  164. {
  165. var listen = this.eventListeners[i];
  166. if (listen == null || listen == evt.getName())
  167. {
  168. this.eventListeners[i+1].apply(this, args);
  169. }
  170. }
  171. }
  172. };