Explorar el Código

Introduced node elements.

Piotr Jasiun hace 10 años
padre
commit
5e506cb13c

+ 84 - 0
packages/ckeditor5-engine/src/treeview/element.js

@@ -0,0 +1,84 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+CKEDITOR.define( [ 'utils', 'treeview/Node' ], ( utils, Node ) => {
+	class Element extends Node {
+		constructor( name, attrs, children ) {
+			/**
+			 * @readolny
+			 */
+			this.name = name;
+
+			if ( utils.isPlainObject( attrs ) ) {
+				this._attrs = utils.objectToMap( attrs );
+			} else {
+				this._attrs = new Map( attrs );
+			}
+
+			this._children = utils.clone( children );
+
+			this.updateChildren = false;
+			this.updateAttrs = false;
+
+			this.DOMElement = null;
+		}
+
+		getChild( index ) {
+			return this._children[ index ];
+		}
+
+		getChildCount() {
+			return this._children.length;
+		}
+
+		getChildIndex( node ) {
+			return this._children.indexOf( node );
+		}
+
+		insertChildren( index, nodes ) {
+			if ( !utils.isIterable( nodes ) ) {
+				nodes = [ nodes ];
+			}
+
+			for ( node of nodes ) {
+				node.parent = this;
+
+				this._children.splice( index, 0, node );
+			}
+
+			this.updateChildList = true;
+		}
+
+		removeChildren( index, number ) {
+			for ( let i = index; i < index + number; i++ ) {
+				this._children[ i ].parent = null;
+			}
+
+			this.updateChildList = true;
+
+			return new this._children.splice( index, number );
+		}
+
+		getAttr( key ) {
+			return this._attrs.get( key );
+		}
+
+		setAttr( key, value ) {
+			this.updateAttrs = true;
+
+			this._attrs.set( key, value );
+		}
+
+		hasAttr( key ) {
+			return this._attrs.has( key );
+		}
+
+		removeAttr( key ) {
+			return this._attrs.delete( key );
+		}
+	}
+} );

+ 56 - 0
packages/ckeditor5-engine/src/treeview/node.js

@@ -0,0 +1,56 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+CKEDITOR.define( [], () => {
+	class Node {
+		constructor() {
+			this.parent = null;
+
+			this.DOMElement = null;
+		}
+
+		getTreeView() {
+			if ( !this.parent ) {
+				return null;
+			} else {
+				return this.parent.getTreeView();
+			}
+		}
+
+		getNextSibling() {
+			const index = this.getIndex();
+
+			return ( index !== null && this.parent.getChild( index + 1 ) ) || null;
+		}
+
+		getPreviousSibling() {
+			const index = this.getIndex();
+
+			return ( index !== null && this.parent.getChild( index - 1 ) ) || null;
+		}
+
+		getIndex() {
+			let pos;
+
+			if ( !this.parent ) {
+				return null;
+			}
+
+			// No parent or child doesn't exist in parent's children.
+			if ( ( pos = this.parent.getChildIndex( this ) ) == -1 ) {
+				/**
+				 * The node's parent does not contain this node. It means that the document tree is corrupted.
+				 *
+				 * @error treeview-node-not-found-in-parent
+				 */
+				throw new CKEditorError( 'treeview-node-not-found-in-parent: The node\'s parent does not contain this node.' );
+			}
+
+			return pos;
+		}
+	}
+} );

+ 27 - 0
packages/ckeditor5-engine/src/treeview/rootelement.js

@@ -0,0 +1,27 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+CKEDITOR.define( [ 'treeview/element' ], ( Element ) => {
+	/**
+	 * Class for nodes that are roots of trees in tree data view.
+	 *
+	 * @class treeView.RootElement
+	 */
+	class RootElement extends Element {
+		constructor( TreeView, name ) {
+			super( name );
+
+			this._treeView = TreeView;
+		}
+
+		getTreeView() {
+			return this._treeView;
+		}
+	}
+
+	return RootElement;
+} );

+ 14 - 0
packages/ckeditor5-engine/src/treeview/text.js

@@ -0,0 +1,14 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+CKEDITOR.define( [], () => {
+	class Text extends Node {
+		constructor( text ) {
+			this.text = text;
+		}
+	}
+} );

+ 1 - 1
packages/ckeditor5-engine/tests/ui/controller.js

@@ -50,7 +50,7 @@ describe( 'Controller', () => {
 
 					throw new Error( 'This should not be executed.' );
 				} )
-				.catch( ( err ) => {
+				.catch ( ( err ) => {
 					expect( err ).to.be.instanceof( CKEditorError );
 					expect( err.message ).to.match( /ui-controller-init-re/ );
 				} );