EventBase.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * UE采用的事件基类
  3. * @file
  4. * @module UE
  5. * @class EventBase
  6. * @since 1.2.6.1
  7. */
  8. /**
  9. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  10. * @unfile
  11. * @module UE
  12. */
  13. /**
  14. * UE采用的事件基类,继承此类的对应类将获取addListener,removeListener,fireEvent方法。
  15. * 在UE中,Editor以及所有ui实例都继承了该类,故可以在对应的ui对象以及editor对象上使用上述方法。
  16. * @unfile
  17. * @module UE
  18. * @class EventBase
  19. */
  20. /**
  21. * 通过此构造器,子类可以继承EventBase获取事件监听的方法
  22. * @constructor
  23. * @example
  24. * ```javascript
  25. * UE.EventBase.call(editor);
  26. * ```
  27. */
  28. var EventBase = (UE.EventBase = function() {});
  29. EventBase.prototype = {
  30. /**
  31. * 注册事件监听器
  32. * @method addListener
  33. * @param { String } types 监听的事件名称,同时监听多个事件使用空格分隔
  34. * @param { Function } fn 监听的事件被触发时,会执行该回调函数
  35. * @waining 事件被触发时,监听的函数假如返回的值恒等于true,回调函数的队列中后面的函数将不执行
  36. * @example
  37. * ```javascript
  38. * editor.addListener('selectionchange',function(){
  39. * console.log("选区已经变化!");
  40. * })
  41. * editor.addListener('beforegetcontent aftergetcontent',function(type){
  42. * if(type == 'beforegetcontent'){
  43. * //do something
  44. * }else{
  45. * //do something
  46. * }
  47. * console.log(this.getContent) // this是注册的事件的编辑器实例
  48. * })
  49. * ```
  50. * @see UE.EventBase:fireEvent(String)
  51. */
  52. addListener: function(types, listener) {
  53. types = utils.trim(types).split(/\s+/);
  54. for (var i = 0, ti; (ti = types[i++]); ) {
  55. getListener(this, ti, true).push(listener);
  56. }
  57. },
  58. on: function(types, listener) {
  59. return this.addListener(types, listener);
  60. },
  61. off: function(types, listener) {
  62. return this.removeListener(types, listener);
  63. },
  64. trigger: function() {
  65. return this.fireEvent.apply(this, arguments);
  66. },
  67. /**
  68. * 移除事件监听器
  69. * @method removeListener
  70. * @param { String } types 移除的事件名称,同时移除多个事件使用空格分隔
  71. * @param { Function } fn 移除监听事件的函数引用
  72. * @example
  73. * ```javascript
  74. * //changeCallback为方法体
  75. * editor.removeListener("selectionchange",changeCallback);
  76. * ```
  77. */
  78. removeListener: function(types, listener) {
  79. types = utils.trim(types).split(/\s+/);
  80. for (var i = 0, ti; (ti = types[i++]); ) {
  81. utils.removeItem(getListener(this, ti) || [], listener);
  82. }
  83. },
  84. /**
  85. * 触发事件
  86. * @method fireEvent
  87. * @param { String } types 触发的事件名称,同时触发多个事件使用空格分隔
  88. * @remind 该方法会触发addListener
  89. * @return { * } 返回触发事件的队列中,最后执行的回调函数的返回值
  90. * @example
  91. * ```javascript
  92. * editor.fireEvent("selectionchange");
  93. * ```
  94. */
  95. /**
  96. * 触发事件
  97. * @method fireEvent
  98. * @param { String } types 触发的事件名称,同时触发多个事件使用空格分隔
  99. * @param { *... } options 可选参数,可以传入一个或多个参数,会传给事件触发的回调函数
  100. * @return { * } 返回触发事件的队列中,最后执行的回调函数的返回值
  101. * @example
  102. * ```javascript
  103. *
  104. * editor.addListener( "selectionchange", function ( type, arg1, arg2 ) {
  105. *
  106. * console.log( arg1 + " " + arg2 );
  107. *
  108. * } );
  109. *
  110. * //触发selectionchange事件, 会执行上面的事件监听器
  111. * //output: Hello World
  112. * editor.fireEvent("selectionchange", "Hello", "World");
  113. * ```
  114. */
  115. fireEvent: function() {
  116. var types = arguments[0];
  117. types = utils.trim(types).split(" ");
  118. for (var i = 0, ti; (ti = types[i++]); ) {
  119. var listeners = getListener(this, ti),
  120. r,
  121. t,
  122. k;
  123. if (listeners) {
  124. k = listeners.length;
  125. while (k--) {
  126. if (!listeners[k]) continue;
  127. t = listeners[k].apply(this, arguments);
  128. if (t === true) {
  129. return t;
  130. }
  131. if (t !== undefined) {
  132. r = t;
  133. }
  134. }
  135. }
  136. if ((t = this["on" + ti.toLowerCase()])) {
  137. r = t.apply(this, arguments);
  138. }
  139. }
  140. return r;
  141. }
  142. };
  143. /**
  144. * 获得对象所拥有监听类型的所有监听器
  145. * @unfile
  146. * @module UE
  147. * @since 1.2.6.1
  148. * @method getListener
  149. * @public
  150. * @param { Object } obj 查询监听器的对象
  151. * @param { String } type 事件类型
  152. * @param { Boolean } force 为true且当前所有type类型的侦听器不存在时,创建一个空监听器数组
  153. * @return { Array } 监听器数组
  154. */
  155. function getListener(obj, type, force) {
  156. var allListeners;
  157. type = type.toLowerCase();
  158. return (
  159. (allListeners =
  160. obj.__allListeners || (force && (obj.__allListeners = {}))) &&
  161. (allListeners[type] || (force && (allListeners[type] = [])))
  162. );
  163. }