瀏覽代碼

Code refactoring in DOMEmitterMixin module.

Aleksander Nowodzinski 10 年之前
父節點
當前提交
cb5fcdee5c
共有 1 個文件被更改,包括 100 次插入102 次删除
  1. 100 102
      packages/ckeditor5-engine/src/ui/domemittermixin.js

+ 100 - 102
packages/ckeditor5-engine/src/ui/domemittermixin.js

@@ -58,7 +58,7 @@ CKEDITOR.define( [ 'emittermixin', 'utils', 'log' ], function( EmitterMixin, uti
 			// Check if emitter is an instance of DOM Node. If so, replace the argument with
 			// Check if emitter is an instance of DOM Node. If so, replace the argument with
 			// corresponding ProxyEmitter (or create one if not existing).
 			// corresponding ProxyEmitter (or create one if not existing).
 			if ( emitter instanceof Node ) {
 			if ( emitter instanceof Node ) {
-				args[ 0 ] = this._getProxyEmitter( emitter ) || this._createProxyEmitter( emitter );
+				args[ 0 ] = this._getProxyEmitter( emitter ) || new ProxyEmitter( emitter );
 			}
 			}
 
 
 			// Execute parent class method with Emitter (or ProxyEmitter) instance.
 			// Execute parent class method with Emitter (or ProxyEmitter) instance.
@@ -103,105 +103,16 @@ CKEDITOR.define( [ 'emittermixin', 'utils', 'log' ], function( EmitterMixin, uti
 		},
 		},
 
 
 		/**
 		/**
-		 * Creates a ProxyEmitter instance. Such an instance is a bridge between a DOM Node firing events
-		 * and any Host listening to them. It is backwards compatible with {@link EmitterMixin#on}.
-		 *
-		 * @param {Node} node DOM Node that fires events.
-		 * @returns {Object} ProxyEmitter instance bound to the DOM Node.
-		 */
-		_createProxyEmitter( node ) {
-			var proxyEmitter = utils.extend( {}, EmitterMixin, {
-				/**
-				 * Registers a callback function to be executed when an event is fired.
-				 *
-				 * It attaches a native DOM listener to the DOM Node. When fired,
-				 * a corresponding Emitter event will also fire with DOM Event object as an argument.
-				 *
-				 * @param {String} event The name of the event.
-				 * @param {Function} callback The function to be called on event.
-				 * @param {Object} [ctx] The object that represents `this` in the callback. Defaults to the object firing the
-				 * event.
-				 * @param {Number} [priority=10] The priority of this callback in relation to other callbacks to that same event.
-				 * Lower values are called first.
-				 */
-				on( event ) {
-					// Execute parent class method first.
-					EmitterMixin.on.apply( this, arguments );
-
-					// If the DOM Listener for given event already exist it is pointless
-					// to attach another one.
-					if ( this._domListeners && this._domListeners[ event ] ) {
-						return;
-					}
-
-					// Create a native DOM listener callback. When the native DOM event
-					// is fired it will fire corresponding event on this ProxyEmitter.
-					// Note: A native DOM Event is passed as an argument.
-					var domListener = domEvt => this.fire( event, domEvt );
-
-					// Supply the DOM listener callback with a function that will help
-					// detach it from the DOM Node, when it is no longer necessary.
-					// See: {@link off}.
-					domListener.removeListener = () => {
-						this._domNode.removeEventListener( event, domListener );
-						delete this._domListeners[ event ];
-					};
-
-					// Attach the native DOM listener to DOM Node.
-					this._domNode.addEventListener( event, domListener );
-
-					if ( !this._domListeners ) {
-						this._domListeners = {};
-					}
-
-					// Store the native DOM listener in this ProxyEmitter. It will be helpful
-					// when stopping listening to the event.
-					this._domListeners[ event ] = domListener;
-				},
-
-				/**
-				 * Stops executing the callback on the given event.
-				 *
-				 * @param {String} event The name of the event.
-				 * @param {Function} callback The function to stop being called.
-				 * @param {Object} [ctx] The context object to be removed, pared with the given callback. To handle cases where
-				 * the same callback is used several times with different contexts.
-				 */
-				off( event ) {
-					// Execute parent class method first.
-					EmitterMixin.off.apply( this, arguments );
-
-					var callbacks;
-
-					// Remove native DOM listeners which are orphans. If no callbacks
-					// are awaiting given event, detach native DOM listener from DOM Node.
-					// See: {@link on}.
-					if ( !( callbacks = this._events[ event ] ) || !callbacks.length ) {
-						this._domListeners[ event ].removeListener();
-					}
-				}
-			} );
-
-			// Set emitter ID to match DOM Node "expando" property.
-			proxyEmitter._emitterId = this._getNodeUID( node );
-
-			// Remember the DOM Node this ProxyEmitter is bound to.
-			proxyEmitter._domNode = node;
-
-			return proxyEmitter;
-		},
-
-		/**
-		 * Retrieves ProxyEmitter for given DOM Node residing in this Host.
+		 * Retrieves ProxyEmitter instance for given DOM Node residing in this Host.
 		 *
 		 *
 		 * @param {Node} node DOM Node of the ProxyEmitter.
 		 * @param {Node} node DOM Node of the ProxyEmitter.
-		 * @return {Emitter} Emitter instance if found.
+		 * @return {ProxyEmitter} ProxyEmitter instance or null.
 		 */
 		 */
 		_getProxyEmitter( node ) {
 		_getProxyEmitter( node ) {
 			var proxy, emitters, emitterInfo;
 			var proxy, emitters, emitterInfo;
 
 
 			// Get node UID. It allows finding Proxy Emitter for this DOM Node.
 			// Get node UID. It allows finding Proxy Emitter for this DOM Node.
-			var uid = this._getNodeUID( node );
+			var uid = getNodeUID( node );
 
 
 			// Find existing Proxy Emitter for this DOM Node among emitters.
 			// Find existing Proxy Emitter for this DOM Node among emitters.
 			if ( ( emitters = this._listeningTo ) ) {
 			if ( ( emitters = this._listeningTo ) ) {
@@ -210,21 +121,108 @@ CKEDITOR.define( [ 'emittermixin', 'utils', 'log' ], function( EmitterMixin, uti
 				}
 				}
 			}
 			}
 
 
-			return proxy;
+			return proxy || null;
+		}
+	};
+
+	/**
+	 * Creates a ProxyEmitter instance. Such an instance is a bridge between a DOM Node firing events
+	 * and any Host listening to them. It is backwards compatible with {@link EmitterMixin#on}.
+	 *
+	 * @class DOMEmitterMixin
+	 * @mixins EmitterMixin
+	 * @param {Node} node DOM Node that fires events.
+	 * @returns {Object} ProxyEmitter instance bound to the DOM Node.
+	 */
+	class ProxyEmitter {
+		constructor( node ) {
+			// Set emitter ID to match DOM Node "expando" property.
+			this._emitterId = getNodeUID( node );
+
+			// Remember the DOM Node this ProxyEmitter is bound to.
+			this._domNode = node;
+		}
+	}
+
+	utils.extend( ProxyEmitter.prototype, EmitterMixin, {
+		/**
+		 * Registers a callback function to be executed when an event is fired.
+		 *
+		 * It attaches a native DOM listener to the DOM Node. When fired,
+		 * a corresponding Emitter event will also fire with DOM Event object as an argument.
+		 *
+		 * @param {String} event The name of the event.
+		 * @param {Function} callback The function to be called on event.
+		 * @param {Object} [ctx] The object that represents `this` in the callback. Defaults to the object firing the
+		 * event.
+		 * @param {Number} [priority=10] The priority of this callback in relation to other callbacks to that same event.
+		 * Lower values are called first.
+		 */
+		on( event ) {
+			// Execute parent class method first.
+			EmitterMixin.on.apply( this, arguments );
+
+			// If the DOM Listener for given event already exist it is pointless
+			// to attach another one.
+			if ( this._domListeners && this._domListeners[ event ] ) {
+				return;
+			}
+
+			// Create a native DOM listener callback. When the native DOM event
+			// is fired it will fire corresponding event on this ProxyEmitter.
+			// Note: A native DOM Event is passed as an argument.
+			var domListener = domEvt => this.fire( event, domEvt );
+
+			// Supply the DOM listener callback with a function that will help
+			// detach it from the DOM Node, when it is no longer necessary.
+			// See: {@link off}.
+			domListener.removeListener = () => {
+				this._domNode.removeEventListener( event, domListener );
+				delete this._domListeners[ event ];
+			};
+
+			// Attach the native DOM listener to DOM Node.
+			this._domNode.addEventListener( event, domListener );
+
+			if ( !this._domListeners ) {
+				this._domListeners = {};
+			}
+
+			// Store the native DOM listener in this ProxyEmitter. It will be helpful
+			// when stopping listening to the event.
+			this._domListeners[ event ] = domListener;
 		},
 		},
 
 
 		/**
 		/**
-		 * Gets an unique DOM Node identifier. The identifier will be set if not defined.
+		 * Stops executing the callback on the given event.
 		 *
 		 *
-		 * @param {Node} node
-		 * @return {Number} UID for given DOM Node.
+		 * @param {String} event The name of the event.
+		 * @param {Function} callback The function to stop being called.
+		 * @param {Object} [ctx] The context object to be removed, pared with the given callback. To handle cases where
+		 * the same callback is used several times with different contexts.
 		 */
 		 */
-		_getNodeUID( node ) {
-			return node[ EXP_KEY_NAME ] || ( node[ EXP_KEY_NAME ] = utils.uid() );
-		}
-	};
+		off( event ) {
+			// Execute parent class method first.
+			EmitterMixin.off.apply( this, arguments );
+
+			var callbacks;
 
 
-	var EXP_KEY_NAME = 'data-ck-expando';
+			// Remove native DOM listeners which are orphans. If no callbacks
+			// are awaiting given event, detach native DOM listener from DOM Node.
+			// See: {@link on}.
+			if ( !( callbacks = this._events[ event ] ) || !callbacks.length ) {
+				this._domListeners[ event ].removeListener();
+			}
+		}
+	} );
+
+	/**
+	 * Gets an unique DOM Node identifier. The identifier will be set if not defined.
+	 *
+	 * @param {Node} node
+	 * @return {Number} UID for given DOM Node.
+	 */
+	var getNodeUID = node => node[ 'data-ck-expando' ] || ( node[ 'data-ck-expando' ] = utils.uid() );
 
 
 	return DOMEmitterMixin;
 	return DOMEmitterMixin;
 } );
 } );