Răsfoiți Sursa

Merge branch 't/209'. Closes #209.

Aleksander Nowodzinski 9 ani în urmă
părinte
comite
4fc54691fc

+ 19 - 0
packages/ckeditor5-ui/src/editable/editable.js

@@ -84,3 +84,22 @@ export default class Editable extends Controller {
 }
 
 utils.mix( Editable, ObservableMixin );
+
+/**
+ * The editable model interface.
+ *
+ * @memberOf core.editable
+ * @interface EditableModel
+ */
+
+/**
+ * Whether the editable has focus.
+ *
+ * @member {Boolean} core.editable.EditableModel#isFocused
+ */
+
+/**
+ * Whether the editable is not in read-only mode.
+ *
+ * @member {Boolean} core.editable.EditableModel#isEditable
+ */

+ 2 - 0
packages/ckeditor5-ui/src/editable/editableview.js

@@ -21,6 +21,8 @@ export default class EditableView extends View {
 	 */
 
 	/**
+	 * Sets the {@link #editableElement} property and applies necessary bindings to it.
+	 *
 	 * @param {HTMLElement} editableElement
 	 */
 	setEditableElement( editableElement ) {

+ 0 - 32
packages/ckeditor5-ui/src/editorui.js

@@ -1,32 +0,0 @@
-/**
- * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
-
-'use strict';
-
-import Controller from './ui/controller.js';
-import utils from './utils.js';
-import ObservableMixin from './observablemixin.js';
-
-/**
- * Base class for the editor main view controllers.
- *
- * @memberOf core
- * @extends core.ui.Controller
- * @mixes core.ObservableMixin
- */
-
-export default class EditorUI extends Controller {
-	constructor( editor ) {
-		super();
-
-		/**
-		 * @readonly
-		 * @member {core.Editor} core.EditorUI.editor
-		 */
-		this.editor = editor;
-	}
-}
-
-utils.mix( EditorUI, ObservableMixin );

+ 47 - 0
packages/ckeditor5-ui/src/editorui/editorui.js

@@ -0,0 +1,47 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+import Controller from '../ui/controller.js';
+import ControllerCollection from '../ui/controllercollection.js';
+import ComponentFactory from '../ui/componentfactory.js';
+import ObservableMixin from '../observablemixin.js';
+import utils from '../utils.js';
+
+/**
+ * Base class for the editor main view controllers.
+ *
+ * @memberOf core.editorUI
+ * @extends core.ui.Controller
+ * @mixes core.ObservableMixin
+ */
+
+export default class EditorUI extends Controller {
+	/**
+	 * Creates an EditorUI instance.
+	 *
+	 * @param {core.Editor} editor
+	 */
+	constructor( editor ) {
+		super();
+
+		/**
+		 * @readonly
+		 * @member {core.Editor} core.editorUI.EditorUI#editor
+		 */
+		this.editor = editor;
+
+		/**
+		 * @readonly
+		 * @member {core.ui.ComponentFactory} core.editorUI.EditorUI#featureComponents
+		 */
+		this.featureComponents = new ComponentFactory( editor );
+
+		this.collections.add( new ControllerCollection( 'body' ) );
+	}
+}
+
+utils.mix( EditorUI, ObservableMixin );

+ 55 - 0
packages/ckeditor5-ui/src/editorui/editoruiview.js

@@ -0,0 +1,55 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+import View from '../ui/view.js';
+
+/**
+ * Base class for the editor main views.
+ *
+ * @memberOf core.editorUI
+ * @extends core.ui.View
+ */
+
+export default class EditorUIView extends View {
+	constructor( model ) {
+		super( model );
+
+		this._createBodyRegion();
+
+		/**
+		 * The element holding elements of the 'body' region.
+		 *
+		 * @private
+		 * @member {HTMLElement} core.editorUI.EditorUIView#_bodyRegionContainer
+		 */
+	}
+
+	destroy() {
+		this._bodyRegionContainer.remove();
+		this._bodyRegionContainer = null;
+	}
+
+	/**
+	 * Creates and appends to `<body>` the 'body' region container.
+	 *
+	 * @private
+	 */
+	_createBodyRegion() {
+		const bodyElement = document.createElement( 'div' );
+		document.body.appendChild( bodyElement );
+
+		this.applyTemplateToElement( bodyElement, {
+			attributes: {
+				class: 'ck-body'
+			}
+		} );
+
+		this._bodyRegionContainer = bodyElement;
+
+		this.register( 'body', () => bodyElement );
+	}
+}

+ 54 - 0
packages/ckeditor5-ui/src/ui/button/button.js

@@ -0,0 +1,54 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+import Controller from '../controller.js';
+
+/**
+ * The basic button controller class.
+ *
+ * @memberOf core.ui.button
+ * @extends core.ui.Controller
+ */
+
+export default class Button extends Controller {
+	constructor( model, view ) {
+		super( model, view );
+
+		view.on( 'click', () => model.fire( 'execute' ) );
+	}
+}
+
+/**
+ * The basic button model interface.
+ *
+ * @memberOf core.ui.button
+ * @interface ButtonModel
+ */
+
+/**
+ * The label of the button.
+ *
+ * @member {String} core.ui.button.ButtonModel#label
+ */
+
+/**
+ * Whether the button is "on" (e.g. some feature which this button represents is currently enabled).
+ *
+ * @member {Boolean} core.ui.button.ButtonModel#isOn
+ */
+
+/**
+ * Whether the button is enabled (can be clicked).
+ *
+ * @member {Boolean} core.ui.button.ButtonModel#isEnabled
+ */
+
+/**
+ * Fired when the button action should be executed.
+ *
+ * @event core.ui.button.ButtonModel#execute
+ */

+ 61 - 0
packages/ckeditor5-ui/src/ui/button/buttonview.js

@@ -0,0 +1,61 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+import View from '../view.js';
+
+/**
+ * The basic button view class.
+ *
+ * @memberOf core.ui.button
+ * @extends core.ui.View
+ */
+
+export default class ButtonView extends View {
+	constructor( model ) {
+		super( model );
+
+		const bind = this.attributeBinder;
+
+		this.template = {
+			tag: 'button',
+
+			attributes: {
+				class: [
+					'ck-button',
+					bind.to( 'isEnabled', value => value ? 'ck-enabled' : 'ck-disabled' ),
+					bind.to( 'isOn', value => value ? 'ck-on' : 'ck-off' )
+				]
+			},
+
+			children: [
+				{
+					text: bind.to( 'label' )
+				}
+			],
+
+			on: {
+				mousedown: ( evt ) => {
+					evt.preventDefault();
+				},
+
+				click: () => {
+					// We can't make the button disabled using the disabled attribute, because it won't be focusable.
+					// Though, shouldn't this condition be moved to the button controller?
+					if ( model.isEnabled ) {
+						this.fire( 'click' );
+					}
+				}
+			}
+		};
+	}
+}
+
+/**
+ * Fired when the button is being clicked. It won't be fired when the button is disabled.
+ *
+ * @event core.ui.button.ButtonView#click
+ */

+ 82 - 0
packages/ckeditor5-ui/src/ui/componentfactory.js

@@ -0,0 +1,82 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+import CKEditorError from '../ckeditorerror.js';
+
+/**
+ * Class implementing the UI component factory.
+ *
+ * Factories of specific UI components can be registered under their unique names. Registered
+ * components can be later instantiated by providing the name of the component. The model is shared between all
+ * instances of that component and has to be provided upon registering its factory.
+ *
+ * The main use case for the component factory is the {@link core.editorUI.EditorUI#featureComponents} factory.
+ *
+ * @memberOf core.ui
+ */
+
+export default class ComponentFactory {
+	/**
+	 * Creates ComponentFactory instance.
+	 *
+	 * @constructor
+	 * @param {core.Editor} editor The editor instance.
+	 */
+	constructor( editor ) {
+		/**
+		 * @readonly
+		 * @member {core.Editor} core.ui.ComponentFactory#editor
+		 */
+		this.editor = editor;
+
+		/**
+		 * Registered component factories.
+		 *
+		 * @private
+		 * @member {Map} core.ui.ComponentFactory#_components
+		 */
+		this._components = new Map();
+	}
+
+	/**
+	 * Registers a component factory.
+	 *
+	 * @param {String} name The name of the component.
+	 * @param {Function} ControllerClass The component controller constructor.
+	 * @param {Function} ViewClass The component view constructor.
+	 * @param {core.ui.Model} model The model of the component.
+	 */
+	add( name, ControllerClass, ViewClass, model ) {
+		if ( this._components.get( name ) ) {
+			throw new CKEditorError(
+				'componentfactory-item-exists: The item already exists in the component factory.', { name }
+			);
+		}
+
+		this._components.set( name, {
+			ControllerClass,
+			ViewClass,
+			model
+		} );
+	}
+
+	/**
+	 * Creates a component instance.
+	 *
+	 * @param {String} name The name of the component.
+	 * @returns {core.ui.Controller} The instantiated component.
+	 */
+	create( name ) {
+		const component = this._components.get( name );
+
+		const model = component.model;
+		const view = new component.ViewClass( model );
+		const controller = new component.ControllerClass( this.editor, model, view );
+
+		return controller;
+	}
+}

+ 1 - 1
packages/ckeditor5-ui/src/ui/region.js

@@ -29,7 +29,7 @@ export default class Region {
 		/**
 		 * Views which belong to the region.
 		 *
-		 * @member {Collection} core.ui.Region#views
+		 * @member {core.Collection} core.ui.Region#views
 		 */
 		this.views = new Collection();
 

+ 24 - 0
packages/ckeditor5-ui/src/ui/toolbar/toolbar.js

@@ -0,0 +1,24 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+import Controller from '../controller.js';
+import ControllerCollection from '../controllercollection.js';
+
+/**
+ * The basic toolbar controller class.
+ *
+ * @memberOf core.ui.toolbar
+ * @extends core.ui.Controller
+ */
+
+export default class Toolbar extends Controller {
+	constructor( model, view ) {
+		super( model, view );
+
+		this.collections.add( new ControllerCollection( 'buttons' ) );
+	}
+}

+ 30 - 0
packages/ckeditor5-ui/src/ui/toolbar/toolbarview.js

@@ -0,0 +1,30 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+import View from '../view.js';
+
+/**
+ * The basic toolbar view class.
+ *
+ * @memberOf core.ui.toolbar
+ * @extends core.ui.View
+ */
+
+export default class ToolbarView extends View {
+	constructor( model ) {
+		super( model );
+
+		this.template = {
+			tag: 'div',
+			attributes: {
+				class: [ 'ck-toolbar' ]
+			}
+		};
+
+		this.register( 'buttons', el => el );
+	}
+}

+ 4 - 9
packages/ckeditor5-ui/src/ui/view.js

@@ -13,8 +13,8 @@ import DOMEmitterMixin from './domemittermixin.js';
 import utils from '../utils.js';
 import isPlainObject from '../lib/lodash/isPlainObject.js';
 
-const bindToSymbol = Symbol( 'bind-to' );
-const bindIfSymbol = Symbol( 'bind-if' );
+const bindToSymbol = Symbol( 'bindTo' );
+const bindIfSymbol = Symbol( 'bindIf' );
 
 /**
  * Basic View class.
@@ -86,14 +86,9 @@ export default class View {
 			return this._element;
 		}
 
+		// No template means no element (a virtual view).
 		if ( !this.template ) {
-			/**
-			 * Attempting to access an element of a view, which has no `template`
-			 * property.
-			 *
-			 * @error ui-view-notemplate
-			 */
-			throw new CKEditorError( 'ui-view-notemplate' );
+			return null;
 		}
 
 		// Prepare pre–defined listeners.

+ 7 - 1
packages/ckeditor5-ui/tests/editorui.js → packages/ckeditor5-ui/tests/editorui/editorui.js

@@ -6,7 +6,9 @@
 'use strict';
 
 import Editor from '/ckeditor5/core/editor.js';
-import EditorUI from '/ckeditor5/core/editorui.js';
+import EditorUI from '/ckeditor5/core/editorui/editorui.js';
+import ComponentFactory from '/ckeditor5/core/ui/componentfactory.js';
+import ControllerCollection from '/ckeditor5/core/ui/controllercollection.js';
 
 describe( 'EditorUI', () => {
 	let editor, editorUI;
@@ -19,6 +21,10 @@ describe( 'EditorUI', () => {
 	describe( 'constructor', () => {
 		it( 'sets all the properties', () => {
 			expect( editorUI ).to.have.property( 'editor', editor );
+
+			expect( editorUI.featureComponents ).to.be.instanceof( ComponentFactory );
+
+			expect( editorUI.collections.get( 'body' ) ).to.be.instanceof( ControllerCollection );
 		} );
 	} );
 } );

+ 38 - 0
packages/ckeditor5-ui/tests/editorui/editoruiview.js

@@ -0,0 +1,38 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+import EditorUIView from '/ckeditor5/core/editorui/editoruiview.js';
+import Model from '/ckeditor5/core/ui/model.js';
+
+describe( 'EditorUIView', () => {
+	let editorUIView;
+
+	beforeEach( () => {
+		editorUIView = new EditorUIView( new Model() );
+
+		return editorUIView.init();
+	} );
+
+	describe( 'constructor', () => {
+		it( 'creates the body region', () => {
+			const el = editorUIView.regions.get( 'body' ).element;
+
+			expect( el.parentNode ).to.equal( document.body );
+			expect( el.nextSibling ).to.be.null;
+		} );
+	} );
+
+	describe( 'destroy', () => {
+		it( 'removes the body region container', () => {
+			const el = editorUIView.regions.get( 'body' ).element;
+
+			editorUIView.destroy();
+
+			expect( el.parentNode ).to.be.null;
+		} );
+	} );
+} );

+ 34 - 0
packages/ckeditor5-ui/tests/ui/button/button.js

@@ -0,0 +1,34 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* bender-tags: ui, button */
+
+'use strict';
+
+import Button from '/ckeditor5/core/ui/button/button.js';
+import View from '/ckeditor5/core/ui/view.js';
+import Model from '/ckeditor5/core/ui/model.js';
+
+describe( 'Button', () => {
+	let model, button, view;
+
+	beforeEach( () => {
+		model = new Model();
+		view = new View();
+		button = new Button( model, view );
+	} );
+
+	describe( 'constructor', () => {
+		it( 'creates view#click -> model#execute binding', () => {
+			const spy = sinon.spy();
+
+			model.on( 'execute', spy );
+
+			view.fire( 'click' );
+
+			expect( spy.calledOnce ).to.be.true;
+		} );
+	} );
+} );

+ 84 - 0
packages/ckeditor5-ui/tests/ui/button/buttonview.js

@@ -0,0 +1,84 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* bender-tags: ui, button */
+
+'use strict';
+
+import ButtonView from '/ckeditor5/core/ui/button/buttonview.js';
+import Model from '/ckeditor5/core/ui/model.js';
+
+describe( 'ButtonView', () => {
+	let model, view;
+
+	beforeEach( () => {
+		model = new Model( {
+			label: 'foo',
+			isEnabled: true,
+			isOn: false
+		} );
+		view = new ButtonView( model );
+
+		return view.init();
+	} );
+
+	describe( '<button> bindings', () => {
+		describe( 'class', () => {
+			it( 'is set initially', () => {
+				expect( view.element.classList.contains( 'ck-button' ) ).to.be.true( 'ck-button' );
+				expect( view.element.classList.contains( 'ck-enabled' ) ).to.be.true( 'ck-enabled' );
+				expect( view.element.classList.contains( 'ck-off' ) ).to.be.true( 'ck-off' );
+			} );
+
+			it( 'reacts on model.isEnabled', () => {
+				model.isEnabled = false;
+
+				expect( view.element.classList.contains( 'ck-disabled' ) ).to.be.true( 'ck-disabled' );
+			} );
+
+			it( 'reacts on model.isOn', () => {
+				model.isOn = true;
+
+				expect( view.element.classList.contains( 'ck-on' ) ).to.be.true( 'ck-on' );
+			} );
+		} );
+
+		describe( 'text', () => {
+			it( 'is set initially', () => {
+				expect( view.element.textContent ).to.equal( 'foo' );
+			} );
+
+			it( 'reacts on model.label', () => {
+				model.label = 'bar';
+
+				expect( view.element.textContent ).to.equal( 'bar' );
+			} );
+		} );
+
+		describe( 'mousedown event', () => {
+			it( 'should be prevented', () => {
+				const ret = view.element.dispatchEvent( new Event( 'mousedown', { cancelable: true } ) );
+
+				expect( ret ).to.be.false;
+			} );
+		} );
+
+		describe( 'click event', () => {
+			it( 'triggers click event if button is not disabled', () => {
+				const spy = sinon.spy();
+
+				view.on( 'click', spy );
+
+				view.element.dispatchEvent( new Event( 'click' ) );
+				expect( spy.callCount ).to.equal( 1 );
+
+				model.isEnabled = false;
+
+				view.element.dispatchEvent( new Event( 'click' ) );
+				expect( spy.callCount ).to.equal( 1 );
+			} );
+		} );
+	} );
+} );

+ 59 - 0
packages/ckeditor5-ui/tests/ui/componentfactory.js

@@ -0,0 +1,59 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* bender-tags: ui */
+
+'use strict';
+
+import Editor from '/ckeditor5/core/editor.js';
+import ComponentFactory from '/ckeditor5/core/ui/componentfactory.js';
+import CKEditorError from '/ckeditor5/core/ckeditorerror.js';
+
+describe( 'ComponentFactory', () => {
+	let editor, factory;
+
+	beforeEach( () => {
+		editor = new Editor();
+		factory = new ComponentFactory( editor );
+	} );
+
+	describe( 'constructor', () => {
+		it( 'sets all the properties', () => {
+			expect( factory ).to.have.property( 'editor', editor );
+		} );
+	} );
+
+	describe( 'add', () => {
+		it( 'throws when trying to override already registered component', () => {
+			factory.add( 'foo', class {}, class {}, {} );
+
+			expect( () => {
+				factory.add( 'foo', class {}, class {}, {} );
+			} ).to.throw( CKEditorError, /^componentfactory-item-exists/ );
+		} );
+	} );
+
+	describe( 'create', () => {
+		it( 'creates an instance', () => {
+			class View {}
+
+			class Controller {
+				constructor( ed, model, view ) {
+					expect( ed ).to.equal( editor );
+					expect( model ).to.equal( model );
+					expect( view ).to.be.instanceof( View );
+				}
+			}
+
+			const model = {};
+
+			factory.add( 'foo', Controller, View, model );
+
+			const instance = factory.create( 'foo' );
+
+			expect( instance ).to.be.instanceof( Controller );
+		} );
+	} );
+} );

+ 2 - 0
packages/ckeditor5-ui/tests/ui/model.js

@@ -3,6 +3,8 @@
  * For licensing, see LICENSE.md.
  */
 
+/* bender-tags: ui */
+
 'use strict';
 
 import Model from '/ckeditor5/core/ui/model.js';

+ 25 - 0
packages/ckeditor5-ui/tests/ui/toolbar/toolbar.js

@@ -0,0 +1,25 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* bender-tags: ui, toolbar */
+
+'use strict';
+
+import Toolbar from '/ckeditor5/core/ui/toolbar/toolbar.js';
+import ControllerCollection from '/ckeditor5/core/ui/controllercollection.js';
+
+describe( 'Toolbar', () => {
+	let toolbar;
+
+	beforeEach( () => {
+		toolbar = new Toolbar();
+	} );
+
+	describe( 'constructor', () => {
+		it( 'creates buttons collection', () => {
+			expect( toolbar.collections.get( 'buttons' ) ).to.be.instanceof( ControllerCollection );
+		} );
+	} );
+} );

+ 34 - 0
packages/ckeditor5-ui/tests/ui/toolbar/toolbarview.js

@@ -0,0 +1,34 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* bender-tags: ui, toolbar */
+
+'use strict';
+
+import ToolbarView from '/ckeditor5/core/ui/toolbar/toolbarview.js';
+import Model from '/ckeditor5/core/ui/model.js';
+
+describe( 'ToolbarView', () => {
+	let model, view;
+
+	beforeEach( () => {
+		model = new Model();
+		view = new ToolbarView( model );
+
+		return view.init();
+	} );
+
+	describe( 'the main element bindings', () => {
+		it( 'is fine', () => {
+			expect( view.element.classList.contains( 'ck-toolbar' ) );
+		} );
+	} );
+
+	describe( 'buttons region', () => {
+		it( 'is bound to the main element', () => {
+			expect( view.regions.get( 'buttons' ).element ).to.equal( view.element );
+		} );
+	} );
+} );

+ 1 - 4
packages/ckeditor5-ui/tests/ui/view.js

@@ -42,10 +42,7 @@ describe( 'View', () => {
 			expect( view._regionsSelectors ).to.be.empty;
 			expect( view._element ).to.be.null;
 			expect( view._template ).to.be.null;
-
-			expect( () => {
-				view.element;
-			} ).to.throw( CKEditorError, /ui-view-notemplate/ );
+			expect( view.element ).to.be.null;
 		} );
 	} );