Browse Source

Converted existing classes and tests to ES6 style.

Piotrek Koszuliński 10 years ago
parent
commit
1369d568c2

+ 0 - 57
packages/ckeditor5-utils/src/basicclass.js

@@ -1,57 +0,0 @@
-/**
- * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
-
-'use strict';
-
-/**
- * A class implementing basic features useful for other classes.
- *
- * @class BasicClass
- * @mixins Emitter
- */
-
-CKEDITOR.define( [ 'emittermixin', 'utils' ], function( EmitterMixin, utils ) {
-	function BasicClass() {
-	}
-
-	// Injects the events API.
-	utils.extend( BasicClass.prototype, EmitterMixin );
-
-	/**
-	 * Creates a subclass constructor based on this class.
-	 *
-	 * The function to becuase a subclass constructor can be passed as `proto.constructor`.
-	 *
-	 * @static
-	 * @param {Object} [proto] Extensions to be added to the subclass prototype.
-	 * @param {Object} [statics] Extension to be added as static members of the subclass constructor.
-	 * @returns {Object} The subclass constructor.
-	 */
-	BasicClass.extend = function( proto, statics ) {
-		var that = this;
-		var child = ( proto && proto.hasOwnProperty( 'constructor' ) ) ?
-			proto.constructor :
-			function() {
-				that.apply( this, arguments );
-			};
-
-		// Copy the statics.
-		utils.extend( child, this, statics );
-
-		// Use the same prototype.
-		child.prototype = Object.create( this.prototype );
-
-		// Add the new prototype stuff.
-		if ( proto ) {
-			proto = utils.clone( proto );
-			delete proto.constructor;
-			utils.extend( child.prototype, proto );
-		}
-
-		return child;
-	};
-
-	return BasicClass;
-} );

+ 23 - 25
packages/ckeditor5-utils/src/collection.js

@@ -9,27 +9,34 @@
  * Collections are ordered sets of models.
  *
  * @class Collection
- * @extends BasicClass
+ * @mixins EventEmitter
  */
 
-CKEDITOR.define( [ 'basicclass' ], function( BasicClass ) {
-	var Collection = BasicClass.extend( {
+CKEDITOR.define( [ 'emittermixin', 'utils' ], function( EmitterMixin, utils ) {
+	class Collection {
 		/**
 		 * Creates a new Collection instance.
 		 *
 		 * @constructor
 		 */
-		constructor: function Collection() {
+		constructor() {
 			/**
 			 * The internal list of models in the collection.
 			 *
 			 * @property _models
 			 * @private
 			 */
-			Object.defineProperty( this, '_models', {
-				value: []
-			} );
-		},
+			this._models = [];
+		}
+
+		/**
+		 * The number of items available in the collection.
+		 *
+		 * @property length
+		 */
+		get length() {
+			return this._models.length;
+		}
 
 		/**
 		 * Adds an item into the collection.
@@ -39,11 +46,11 @@ CKEDITOR.define( [ 'basicclass' ], function( BasicClass ) {
 		 *
 		 * @param {Model} model The item to be added.
 		 */
-		add: function( model ) {
+		add( model ) {
 			this._models.push( model );
 
 			this.fire( 'add', model );
-		},
+		}
 
 		/**
 		 * Gets one item from the collection.
@@ -51,7 +58,7 @@ CKEDITOR.define( [ 'basicclass' ], function( BasicClass ) {
 		 * @param {Number} index The index to take the item from.
 		 * @returns {Model} The requested item.
 		 */
-		get: function( index ) {
+		get( index ) {
 			var model = this._models[ index ];
 
 			if ( !model ) {
@@ -59,7 +66,7 @@ CKEDITOR.define( [ 'basicclass' ], function( BasicClass ) {
 			}
 
 			return model;
-		},
+		}
 
 		/**
 		 * Removes an item from the collection.
@@ -67,7 +74,7 @@ CKEDITOR.define( [ 'basicclass' ], function( BasicClass ) {
 		 * @param {Model|Number} modelOrIndex Either the item itself or its index inside the collection.
 		 * @returns {Model} The removed item.
 		 */
-		remove: function( modelOrIndex ) {
+		remove( modelOrIndex ) {
 			// If a model has been passed, convert it to its index.
 			if ( typeof modelOrIndex != 'number' ) {
 				modelOrIndex = this._models.indexOf( modelOrIndex );
@@ -87,18 +94,9 @@ CKEDITOR.define( [ 'basicclass' ], function( BasicClass ) {
 
 			return removedModel;
 		}
-	} );
-
-	/**
-	 * The number of items available in the collection.
-	 *
-	 * @property length
-	 */
-	Object.defineProperty( Collection.prototype, 'length', {
-		get: function() {
-			return this._models.length;
-		}
-	} );
+	}
+
+	utils.extend( Collection.prototype, EmitterMixin );
 
 	return Collection;
 } );

+ 13 - 16
packages/ckeditor5-utils/src/config.js

@@ -13,21 +13,20 @@
  */
 
 CKEDITOR.define( [ 'model', 'utils' ], function( Model, utils ) {
-	var Config = Model.extend( {
+	class Config extends Model {
 		/**
 		 * Creates an instance of the {@link Config} class.
 		 *
 		 * @param {Object} [configurations] The initial configurations to be set.
 		 * @constructor
 		 */
-		constructor: function Config( configurations ) {
-			// Call super-constructor.
-			Model.apply( this );
+		constructor( configurations ) {
+			super();
 
 			if ( configurations ) {
 				this.set( configurations );
 			}
-		},
+		}
 
 		/**
 		 * Set configuration values.
@@ -66,11 +65,11 @@ CKEDITOR.define( [ 'model', 'utils' ], function( Model, utils ) {
 		 * configuration entries. Configuration names are case-insensitive.
 		 * @param {*} [value=null] The configuration value. Used if a name is passed to nameOrConfigurations.
 		 */
-		set: function( name, value ) {
+		set( name, value ) {
 			// Just pass the call to the original set() in case of an object. It'll deal with recursing through the
 			// object and calling set( name, value ) again for each property.
 			if ( utils.isObject( name ) ) {
-				Model.prototype.set.apply( this, arguments );
+				super.set.apply( this, arguments );
 
 				return;
 			}
@@ -115,8 +114,8 @@ CKEDITOR.define( [ 'model', 'utils' ], function( Model, utils ) {
 			}
 
 			// Call the original set() on the target.
-			Model.prototype.set.call( target, name, value );
-		},
+			super.set.call( target, name, value );
+		}
 
 		/**
 		 * Gets the value for a configuration entry.
@@ -130,7 +129,7 @@ CKEDITOR.define( [ 'model', 'utils' ], function( Model, utils ) {
 		 * @param {String} name The configuration name. Configuration names are case-insensitive.
 		 * @returns {*} The configuration value or `undefined` if the configuration entry was not found.
 		 */
-		get: function( name ) {
+		get( name ) {
 			// The target for this configuration is, for now, this object.
 			//jscs:disable safeContextKeyword
 			var source = this;
@@ -162,9 +161,7 @@ CKEDITOR.define( [ 'model', 'utils' ], function( Model, utils ) {
 			if ( this.definition ) {
 				return this.definition[ name ];
 			}
-
-			return undefined;
-		},
+		}
 
 		/**
 		 * Defines the name and default value for configurations. It accepts the same parameters as the
@@ -181,10 +178,10 @@ CKEDITOR.define( [ 'model', 'utils' ], function( Model, utils ) {
 		 * @param {*} [value] The configuration value. Used if a name is passed to nameOrConfigurations. If undefined,
 		 * the configuration is set to `null`.
 		 */
-		define: function( name, value ) {
+		define( name, value ) {
 			if ( !this.definition ) {
 				/**
-				 *
+				 * TODO
 				 *
 				 * @property
 				 * @type {Config}
@@ -194,7 +191,7 @@ CKEDITOR.define( [ 'model', 'utils' ], function( Model, utils ) {
 
 			this.definition.set( name, value );
 		}
-	} );
+	}
 
 	return Config;
 } );

+ 6 - 6
packages/ckeditor5-utils/src/emittermixin.js

@@ -24,7 +24,7 @@ CKEDITOR.define( [ 'eventinfo', 'utils' ], function( EventInfo, utils ) {
 		 * @param {Number} [priority=10] The priority of this callback in relation to other callbacks to that same event.
 		 * Lower values are called first.
 		 */
-		on: function( event, callback, ctx, priority ) {
+		on( event, callback, ctx, priority ) {
 			var callbacks = getCallbacks( this, event );
 
 			// Set the priority defaults.
@@ -61,7 +61,7 @@ CKEDITOR.define( [ 'eventinfo', 'utils' ], function( EventInfo, utils ) {
 		 * @param {Number} [priority=10] The priority of this callback in relation to other callbacks to that same event.
 		 * Lower values are called first.
 		 */
-		once: function( event, callback, ctx, priority ) {
+		once( event, callback, ctx, priority ) {
 			var onceCallback = function( event ) {
 				// Go off() at the first call.
 				event.off();
@@ -82,7 +82,7 @@ CKEDITOR.define( [ 'eventinfo', 'utils' ], function( EventInfo, utils ) {
 		 * @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: function( event, callback, ctx ) {
+		off( event, callback, ctx ) {
 			var callbacks = getCallbacksIfAny( this, event );
 
 			if ( !callbacks ) {
@@ -110,7 +110,7 @@ CKEDITOR.define( [ 'eventinfo', 'utils' ], function( EventInfo, utils ) {
 		 * @param {Number} [priority=10] The priority of this callback in relation to other callbacks to that same event.
 		 * Lower values are called first.
 		 */
-		listenTo: function( emitter, event, callback, ctx, priority ) {
+		listenTo( emitter, event, callback, ctx, priority ) {
 			var emitters, emitterId, emitterInfo, eventCallbacks;
 
 			// _listeningTo contains a list of emitters that this object is listening to.
@@ -166,7 +166,7 @@ CKEDITOR.define( [ 'eventinfo', 'utils' ], function( EventInfo, utils ) {
 		 * @param {Function} [callback] (Requires the `event`) The function to be removed from the call list for the given
 		 * `event`.
 		 */
-		stopListening: function( emitter, event, callback ) {
+		stopListening( emitter, event, callback ) {
 			var emitters = this._listeningTo;
 			var emitterId = emitter && emitter._emitterId;
 			var emitterInfo = emitters && emitterId && emitters[ emitterId ];
@@ -213,7 +213,7 @@ CKEDITOR.define( [ 'eventinfo', 'utils' ], function( EventInfo, utils ) {
 		 * @param {String} event The name of the event.
 		 * @param {...*} [args] Additional arguments to be passed to the callbacks.
 		 */
-		fire: function( event, args ) {
+		fire( event, args ) {
 			var callbacks = getCallbacksIfAny( this, event );
 
 			if ( !callbacks ) {

+ 28 - 26
packages/ckeditor5-utils/src/eventinfo.js

@@ -13,32 +13,34 @@
  */
 
 CKEDITOR.define( [ 'utils' ], function( utils ) {
-	function EventInfo( source, name ) {
-		/**
-		 * The object that fired the event.
-		 */
-		this.source = source;
-
-		/**
-		 * The event name.
-		 */
-		this.name = name;
-
-		// The following methods are defined in the constructor because they must be re-created per instance.
-
-		/**
-		 * Stops the event emitter to call further callbacks for this event interaction.
-		 *
-		 * @method
-		 */
-		this.stop = utils.spy();
-
-		/**
-		 * Removes the current callback from future interactions of this event.
-		 *
-		 * @method
-		 */
-		this.off = utils.spy();
+	class EventInfo {
+		constructor( source, name ) {
+			/**
+			 * The object that fired the event.
+			 */
+			this.source = source;
+
+			/**
+			 * The event name.
+			 */
+			this.name = name;
+
+			// The following methods are defined in the constructor because they must be re-created per instance.
+
+			/**
+			 * Stops the event emitter to call further callbacks for this event interaction.
+			 *
+			 * @method
+			 */
+			this.stop = utils.spy();
+
+			/**
+			 * Removes the current callback from future interactions of this event.
+			 *
+			 * @method
+			 */
+			this.off = utils.spy();
+		}
 	}
 
 	return EventInfo;

+ 10 - 10
packages/ckeditor5-utils/src/model.js

@@ -9,11 +9,11 @@
  * The base MVC model class.
  *
  * @class Model
- * @extends BasicClass
+ * @mixins EventEmitter
  */
 
-CKEDITOR.define( [ 'basicclass', 'utils' ], function( BasicClass, utils ) {
-	var Model = BasicClass.extend( {
+CKEDITOR.define( [ 'emittermixin', 'utils' ], function( EmitterMixin, utils ) {
+	class Model {
 		/**
 		 * Creates a new Model instance.
 		 *
@@ -21,16 +21,14 @@ CKEDITOR.define( [ 'basicclass', 'utils' ], function( BasicClass, utils ) {
 		 * @param {Object} [properties] The properties to be appended to the instance during creation.
 		 * @method constructor
 		 */
-		constructor: function Model( attributes, properties ) {
+		constructor( attributes, properties ) {
 			/**
 			 * The internal hash containing the model's state.
 			 *
 			 * @property _attributes
 			 * @private
 			 */
-			Object.defineProperty( this, '_attributes', {
-				value: {}
-			} );
+			this._attributes = {};
 
 			// Extend this instance with the additional (out of state) properties.
 			if ( properties ) {
@@ -41,7 +39,7 @@ CKEDITOR.define( [ 'basicclass', 'utils' ], function( BasicClass, utils ) {
 			if ( attributes ) {
 				this.set( attributes );
 			}
-		},
+		}
 
 		/**
 		 * Creates and sets the value of a model attribute of this object. This attribute will be part of the model
@@ -52,7 +50,7 @@ CKEDITOR.define( [ 'basicclass', 'utils' ], function( BasicClass, utils ) {
 		 * @param {String} name The attributes name.
 		 * @param {*} value The attributes value.
 		 */
-		set: function( name, value ) {
+		set( name, value ) {
 			// If the first parameter is an Object, we gonna interact through its properties.
 			if ( utils.isObject( name ) ) {
 				Object.keys( name ).forEach( function( attr ) {
@@ -83,7 +81,9 @@ CKEDITOR.define( [ 'basicclass', 'utils' ], function( BasicClass, utils ) {
 
 			this[ name ] = value;
 		}
-	} );
+	}
+
+	utils.extend( Model.prototype, EmitterMixin );
 
 	return Model;
 } );

+ 1 - 1
packages/ckeditor5-utils/src/promise.js

@@ -21,7 +21,7 @@ CKEDITOR.define( function() {
 
 	/* istanbul ignore next: we expect this to never happen for now, so we'll not have coverage for this */
 	if ( !window.Promise ) {
-		throw 'The Promise class is not available natively. CKEditor is not compatible with this browser.';
+		throw new Error( 'The Promise class is not available natively. CKEditor is not compatible with this browser.' );
 	}
 
 	return window.Promise;

+ 1 - 1
packages/ckeditor5-utils/src/utils.js

@@ -23,7 +23,7 @@ CKEDITOR.define( [ 'utils-lodash', 'lib/lodash/lodash-ckeditor' ], function( lod
 		 *
 		 * @returns {Function} The spy function.
 		 */
-		spy: function() {
+		spy() {
 			var spy = function() {
 				spy.called = true;
 			};

+ 0 - 83
packages/ckeditor5-utils/tests/basicclass/basicclass.js

@@ -1,83 +0,0 @@
-/**
- * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
-
-/* globals describe, it, expect, bender */
-
-'use strict';
-
-var modules = bender.amd.require( 'basicclass' );
-
-describe( 'extend', function() {
-	it( 'should extend classes', function() {
-		var BasicClass = modules.basicclass;
-
-		var Truck = BasicClass.extend( {
-			loadContainers: function() {}
-		} );
-
-		var volvoTruck = new Truck();
-
-		expect( volvoTruck ).to.be.an.instanceof( Truck );
-		expect( volvoTruck ).to.be.an.instanceof( BasicClass );
-		expect( volvoTruck ).to.have.property( 'loadContainers' ).to.be.a( 'function' );
-
-		var Spacecraft = Truck.extend( {
-			jumpToHyperspace: function() {}
-		} );
-
-		var falcon = new Spacecraft();
-		expect( falcon ).to.be.an.instanceof( Spacecraft );
-		expect( falcon ).to.be.an.instanceof( Truck );
-		expect( falcon ).to.be.an.instanceof( BasicClass );
-		expect( falcon ).to.have.property( 'loadContainers' ).to.be.a( 'function' );
-		expect( falcon ).to.have.property( 'jumpToHyperspace' ).to.be.a( 'function' );
-	} );
-
-	it( 'should extend the prototype and add statics', function() {
-		var BasicClass = modules.basicclass;
-
-		var Truck = BasicClass.extend( {
-			property1: 1,
-			property2: function() {}
-		}, {
-			static1: 1,
-			static2: function() {}
-		} );
-
-		expect( Truck ).to.have.property( 'static1' ).to.equal( 1 );
-		expect( Truck ).to.have.property( 'static2' ).to.be.a( 'function' );
-
-		var truck = new Truck();
-
-		expect( truck ).to.have.property( 'property1' ).to.equal( 1 );
-		expect( truck ).to.have.property( 'property2' ).to.be.a( 'function' );
-	} );
-
-	it( 'should use a custom constructor', function() {
-		var BasicClass = modules.basicclass;
-
-		function customConstructor() {}
-
-		var Truck = BasicClass.extend( {
-			constructor: customConstructor
-		} );
-
-		expect( Truck ).to.equal( customConstructor );
-		expect( Truck.prototype ).to.not.have.ownProperty( 'constructor' );
-
-		expect( new Truck() ).to.be.an.instanceof( Truck );
-		expect( new Truck() ).to.be.an.instanceof( BasicClass );
-	} );
-} );
-
-describe( 'BasicClass', function() {
-	it( 'should be an event emitter', function() {
-		var BasicClass = modules.basicclass;
-
-		var basic = new BasicClass();
-
-		expect( basic ).to.have.property( 'fire' ).to.be.a( 'function' );
-	} );
-} );

+ 2 - 2
packages/ckeditor5-utils/tests/mvc/model/model.js

@@ -15,7 +15,7 @@ describe( 'Model', function() {
 	beforeEach( 'Create a test model instance', function() {
 		var Model = modules.model;
 
-		Car = Model.extend();
+		Car = class extends Model {};
 
 		car = new Car( {
 			color: 'red',
@@ -142,7 +142,7 @@ describe( 'Model', function() {
 		it( 'should create new Model based classes', function() {
 			var Model = modules.model;
 
-			var Truck = Car.extend();
+			class Truck extends Car {}
 
 			var truck = new Truck();