8
0
Aleksander Nowodzinski 10 лет назад
Родитель
Сommit
5972ba6eb3

+ 1 - 2
packages/ckeditor5-ui/src/collection.js

@@ -7,7 +7,6 @@
 
 import EmitterMixin from './emittermixin.js';
 import CKEditorError from './ckeditorerror.js';
-import utilsObject from './lib/lodash/object.js';
 import utils from './utils.js';
 
 /**
@@ -266,7 +265,7 @@ export default class Collection {
 	}
 }
 
-utilsObject.extend( Collection.prototype, EmitterMixin );
+utils.mix( Collection, EmitterMixin );
 
 /**
  * Fired when an item is added to the collection.

+ 2 - 1
packages/ckeditor5-ui/src/model.js

@@ -9,6 +9,7 @@ import EmitterMixin from './emittermixin.js';
 import CKEditorError from './ckeditorerror.js';
 import utilsObject from './lib/lodash/object.js';
 import utilsLang from './lib/lodash/lang.js';
+import utils from './utils.js';
 
 /**
  * The base MVC model class.
@@ -625,7 +626,7 @@ function attachBindToListeners( model, toBindings ) {
 	} );
 }
 
-utilsObject.extend( Model.prototype, EmitterMixin );
+utils.mix( Model, EmitterMixin );
 
 /**
  * Fired when an attribute changed value.

+ 2 - 2
packages/ckeditor5-ui/src/ui/view.js

@@ -11,7 +11,7 @@ import Region from './region.js';
 import Template from './template.js';
 import CKEditorError from '../ckeditorerror.js';
 import DOMEmitterMixin from './domemittermixin.js';
-import objectUtils from '../lib/lodash/object.js';
+import utils from '../utils.js';
 
 /**
  * Basic View class.
@@ -386,7 +386,7 @@ export default class View extends Model {
 	}
 }
 
-objectUtils.extend( View.prototype, DOMEmitterMixin );
+utils.mix( View, DOMEmitterMixin );
 
 const validSelectorTypes = new Set( [ 'string', 'boolean', 'function' ] );
 

+ 33 - 0
packages/ckeditor5-ui/src/utils.js

@@ -107,6 +107,39 @@ const utils = {
 		}
 
 		return null;
+	},
+
+	/**
+	 * Copies enumerable properties and symbols from the objects given as 2nd+ parameters to the
+	 * prototype of first object (a constructor).
+	 *
+	 *		class Editor {
+	 *			...
+	 *		}
+	 *
+	 *		const SomeMixin = {
+	 *			a() {
+	 *				return 'a';
+	 *			}
+	 *		};
+	 *
+	 *		utils.mix( Editor, SomeMixin, ... );
+	 *
+	 *		new Editor().a(); -> 'a'
+	 *
+	 * @param {Function} [baseClass] Class which prototype will be extended.
+	 * @param {Object} [...mixins] Objects from which to get properties.
+	 */
+	mix( baseClass, ...mixins ) {
+		mixins.forEach( ( mixin ) => {
+			Object.getOwnPropertyNames( mixin ).concat( Object.getOwnPropertySymbols( mixin ) )
+				.forEach( ( key ) => {
+					const sourceDescriptor = Object.getOwnPropertyDescriptor( mixin, key );
+					sourceDescriptor.enumerable = false;
+
+					Object.defineProperty( baseClass.prototype, key, sourceDescriptor );
+				} );
+		} );
 	}
 };