Bläddra i källkod

Implemented CompponentRepository and basic UI components.

Piotrek Koszuliński 9 år sedan
förälder
incheckning
5d0677be7a

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

@@ -8,6 +8,7 @@
 import Controller from './ui/controller.js';
 import utils from './utils.js';
 import ObservableMixin from './observablemixin.js';
+import ComponentRepository from './ui/componentrepository.js';
 
 /**
  * Base class for the editor main view controllers.
@@ -26,6 +27,12 @@ export default class EditorUI extends Controller {
 		 * @member {core.Editor} core.EditorUI.editor
 		 */
 		this.editor = editor;
+
+		/**
+		 * @readonly
+		 * @type {core.ui.ComponentRepository}
+		 */
+		this.featureComponents = new ComponentRepository( editor );
 	}
 }
 

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

@@ -0,0 +1,28 @@
+/**
+ * @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.
+ *
+ * @class core.ui.button.Button
+ * @extends core.ui.Controller
+ */
+
+export default class Button extends Controller {
+	/**
+	 * Creates a new button instance.
+	 *
+	 * @method constructor
+	 */
+	constructor( model, view ) {
+		super( model, view );
+
+		view.on( 'clicked', () => model.fire( 'executed' ) );
+	}
+}

+ 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.
+ *
+ * @class core.ui.button.ButtonView
+ * @extends core.ui.View
+ */
+
+export default class ButtonView extends View {
+	/**
+	 * Creates a new button view instance.
+	 *
+	 * @method constructor
+	 * @param {Model} model
+	 */
+	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( 'clicked' );
+					}
+				}
+			}
+		};
+	}
+}

+ 48 - 0
packages/ckeditor5-ui/src/ui/componentrepository.js

@@ -0,0 +1,48 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+import CKEditorError from '../ckeditorerror.js';
+
+/**
+ * @class core.ui.ComponentRepository
+ */
+
+export default class ComponentRepository {
+	constructor( editor ) {
+		/**
+		 * @readonly
+		 * @type {core.Editor}
+		 */
+		this.editor = editor;
+
+		this._components = new Map();
+	}
+
+	add( name, ControllerClass, ViewClass, model ) {
+		if ( this._components.get( name ) ) {
+			throw new CKEditorError(
+				'componentrepository-item-exists: The item already exists in the component registry', { name }
+			);
+		}
+
+		this._components.set( name, {
+			ControllerClass,
+			ViewClass,
+			model
+		} );
+	}
+
+	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;
+	}
+}

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

@@ -0,0 +1,29 @@
+/**
+ * @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.
+ *
+ * @class core.ui.toolbar.Toolbar
+ * @extends core.ui.Controller
+ */
+
+export default class Toolbar extends Controller {
+	/**
+	 * Creates a new toolbar instance.
+	 *
+	 * @method constructor
+	 */
+	constructor( model, view ) {
+		super( model, view );
+
+		this.collections.add( new ControllerCollection( 'buttons' ) );
+	}
+}

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

@@ -0,0 +1,36 @@
+/**
+ * @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.
+ *
+ * @class core.ui.toolbar.ToolbarView
+ * @extends core.ui.View
+ */
+
+export default class ToolbarView extends View {
+	/**
+	 * Creates a new toolbar view instance.
+	 *
+	 * @method constructor
+	 * @param {Model} model
+	 */
+	constructor( model ) {
+		super( model );
+
+		this.template = {
+			tag: 'div',
+			attributes: {
+				class: 'ck-toolbar'
+			}
+		};
+
+		this.register( 'buttons', el => el );
+	}
+}