mediawiki-extensions-Visual.../modules/ve/ce/ve.ce.Node.js

251 lines
5.4 KiB
JavaScript
Raw Normal View History

/**
* VisualEditor content editable Node class.
*
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
2012-03-08 12:27:02 +00:00
/**
* Generic ContentEditable node.
*
2012-03-08 12:27:02 +00:00
* @class
* @abstract
* @constructor
* @extends {ve.Node}
* @param {String} type Symbolic name of node type
2012-03-08 12:27:02 +00:00
* @param {ve.dm.Node} model Model to observe
* @param {jQuery} [$element] Element to use as a container
2012-03-08 12:27:02 +00:00
*/
ve.ce.Node = function VeCeNode( type, model, $element ) {
Object management: Object create/inherit/clone utilities * For the most common case: - replace ve.extendClass with ve.inheritClass (chose slightly different names to detect usage of the old/new one, and I like 'inherit' better). - move it up to below the constructor, see doc block for why. * Cases where more than 2 arguments were passed to ve.extendClass are handled differently depending on the case. In case of a longer inheritance tree, the other arguments could be omitted (like in "ve.ce.FooBar, ve.FooBar, ve.Bar". ve.ce.FooBar only needs to inherit from ve.FooBar, because ve.ce.FooBar inherits from ve.Bar). In the case of where it previously had two mixins with ve.extendClass(), either one becomes inheritClass and one a mixin, both to mixinClass(). No visible changes should come from this commit as the instances still all have the same visible properties in the end. No more or less than before. * Misc.: - Be consistent in calling parent constructors in the same order as the inheritance. - Add missing @extends and @param documentation. - Replace invalid {Integer} type hint with {Number}. - Consistent doc comments order: @class, @abstract, @constructor, @extends, @params. - Fix indentation errors A fairly common mistake was a superfluous space before the identifier on the assignment line directly below the documentation comment. $ ack "^ [^*]" --js modules/ve - Typo "Inhertiance" -> "Inheritance". - Replacing the other confusing comment "Inheritance" (inside the constructor) with "Parent constructor". - Add missing @abstract for ve.ui.Tool. - Corrected ve.FormatDropdownTool to ve.ui.FormatDropdownTool.js - Add function names to all @constructor functions. Now that we have inheritance it is important and useful to have these functions not be anonymous. Example of debug shot: http://cl.ly/image/1j3c160w3D45 Makes the difference between < documentNode; > ve_dm_DocumentNode ... : ve_dm_BranchNode ... : ve_dm_Node ... : ve_dm_Node ... : Object ... without names (current situation): < documentNode; > Object ... : Object ... : Object ... : Object ... : Object ... though before this commit, it really looks like this (flattened since ve.extendClass really did a mixin): < documentNode; > Object ... ... ... Pattern in Sublime (case-sensitive) to find nameless constructor functions: "^ve\..*\.([A-Z])([^\.]+) = function \(" Change-Id: Iab763954fb8cf375900d7a9a92dec1c755d5407e
2012-09-05 06:07:47 +00:00
// Parent constructor
ve.Node.call( this, type );
2012-03-08 12:27:02 +00:00
// Properties
this.model = model;
this.$ = $element || $( '<div>' );
this.parent = null;
// Holds the information about whether or not the node is attached to the live DOM
this.live = false;
this.$.data( 'node', this );
// Walk through node model attributes and pick just the HTML ones,
// then apply them to the DOM element
var attributes = this.model.getAttributes(),
attribute;
for ( attribute in attributes ) {
if ( attribute.indexOf( 'html/' ) === 0 ) {
this.$.attr( attribute.substr( 5 ), attributes[attribute] );
}
}
2012-03-08 12:27:02 +00:00
};
Object management: Object create/inherit/clone utilities * For the most common case: - replace ve.extendClass with ve.inheritClass (chose slightly different names to detect usage of the old/new one, and I like 'inherit' better). - move it up to below the constructor, see doc block for why. * Cases where more than 2 arguments were passed to ve.extendClass are handled differently depending on the case. In case of a longer inheritance tree, the other arguments could be omitted (like in "ve.ce.FooBar, ve.FooBar, ve.Bar". ve.ce.FooBar only needs to inherit from ve.FooBar, because ve.ce.FooBar inherits from ve.Bar). In the case of where it previously had two mixins with ve.extendClass(), either one becomes inheritClass and one a mixin, both to mixinClass(). No visible changes should come from this commit as the instances still all have the same visible properties in the end. No more or less than before. * Misc.: - Be consistent in calling parent constructors in the same order as the inheritance. - Add missing @extends and @param documentation. - Replace invalid {Integer} type hint with {Number}. - Consistent doc comments order: @class, @abstract, @constructor, @extends, @params. - Fix indentation errors A fairly common mistake was a superfluous space before the identifier on the assignment line directly below the documentation comment. $ ack "^ [^*]" --js modules/ve - Typo "Inhertiance" -> "Inheritance". - Replacing the other confusing comment "Inheritance" (inside the constructor) with "Parent constructor". - Add missing @abstract for ve.ui.Tool. - Corrected ve.FormatDropdownTool to ve.ui.FormatDropdownTool.js - Add function names to all @constructor functions. Now that we have inheritance it is important and useful to have these functions not be anonymous. Example of debug shot: http://cl.ly/image/1j3c160w3D45 Makes the difference between < documentNode; > ve_dm_DocumentNode ... : ve_dm_BranchNode ... : ve_dm_Node ... : ve_dm_Node ... : Object ... without names (current situation): < documentNode; > Object ... : Object ... : Object ... : Object ... : Object ... though before this commit, it really looks like this (flattened since ve.extendClass really did a mixin): < documentNode; > Object ... ... ... Pattern in Sublime (case-sensitive) to find nameless constructor functions: "^ve\..*\.([A-Z])([^\.]+) = function \(" Change-Id: Iab763954fb8cf375900d7a9a92dec1c755d5407e
2012-09-05 06:07:47 +00:00
/* Inheritance */
ve.inheritClass( ve.ce.Node, ve.Node );
/* Static Memebers */
ve.ce.Node.static = {};
/**
* Template for shield elements.
*
* Uses data URI to inject a 1x1 transparent GIF image into the DOM.
*
* @static
* @member
*/
ve.ce.Node.static.$shieldTemplate = $(
'<img src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" ' +
'class="ve-ce-node-shield">'
);
2012-03-08 12:27:02 +00:00
/* Methods */
/**
* Gets a list of allowed child node types.
*
* This method passes through to the model.
*
* @method
* @returns {String[]|null} List of node types allowed as children or null if any type is allowed
*/
ve.ce.Node.prototype.getChildNodeTypes = function () {
return this.model.getChildNodeTypes();
};
2012-03-08 12:27:02 +00:00
/**
* Gets a list of allowed parent node types.
*
* This method passes through to the model.
*
2012-03-08 12:27:02 +00:00
* @method
* @returns {String[]|null} List of node types allowed as parents or null if any type is allowed
2012-03-08 12:27:02 +00:00
*/
ve.ce.Node.prototype.getParentNodeTypes = function () {
return this.model.getParentNodeTypes();
2012-03-08 12:27:02 +00:00
};
/**
* Checks if model is for a node that can have children.
*
* This method passes through to the model.
*
2012-03-08 12:27:02 +00:00
* @method
* @returns {Boolean} Model node can have children
2012-03-08 12:27:02 +00:00
*/
ve.ce.Node.prototype.canHaveChildren = function () {
return this.model.canHaveChildren();
2012-03-08 12:27:02 +00:00
};
/**
* Checks if model is for a node that can have children.
*
* This method passes through to the model.
*
2012-03-08 12:27:02 +00:00
* @method
* @returns {Boolean} Model node can have children
2012-03-08 12:27:02 +00:00
*/
ve.ce.Node.prototype.canHaveChildren = function () {
return this.model.canHaveChildren();
2012-03-08 12:27:02 +00:00
};
/**
* Checks if model is for a node that can have grandchildren.
*
* This method passes through to the model.
*
2012-03-08 12:27:02 +00:00
* @method
* @returns {Boolean} Model node can have grandchildren
2012-03-08 12:27:02 +00:00
*/
ve.ce.Node.prototype.canHaveGrandchildren = function () {
return this.model.canHaveGrandchildren();
};
/**
* Checks if model is for a wrapped element.
*
* This method passes through to the model.
*
* @method
* @returns {Boolean} Model node is a wrapped element
*/
ve.ce.Node.prototype.isWrapped = function () {
return this.model.isWrapped();
};
/**
* Checks if this node can contain content.
*
* @method
* @returns {Boolean} Node can contain content
*/
ve.ce.Node.prototype.canContainContent = function () {
return this.model.canContainContent();
};
/**
* Checks if this node is content.
*
* @method
* @returns {Boolean} Node is content
*/
ve.ce.Node.prototype.isContent = function () {
return this.model.isContent();
};
/**
* Checks if this node can have a slug before it
*
* @static
* @method
* @returns {Boolean} Whether the node can have a slug before it
*/
ve.ce.Node.prototype.canHaveSlugBefore = function () {
return !this.canContainContent() && this.getParentNodeTypes() === null && this.type !== 'text';
};
/**
* Checks if this node can have a slug after it
*
* @static
* @method
* @returns {Boolean} Whether the node can have a slug after it
*/
ve.ce.Node.prototype.canHaveSlugAfter = ve.ce.Node.prototype.canHaveSlugBefore;
/**
* Gets model length.
*
* This method passes through to the model.
*
* @method
Object management: Object create/inherit/clone utilities * For the most common case: - replace ve.extendClass with ve.inheritClass (chose slightly different names to detect usage of the old/new one, and I like 'inherit' better). - move it up to below the constructor, see doc block for why. * Cases where more than 2 arguments were passed to ve.extendClass are handled differently depending on the case. In case of a longer inheritance tree, the other arguments could be omitted (like in "ve.ce.FooBar, ve.FooBar, ve.Bar". ve.ce.FooBar only needs to inherit from ve.FooBar, because ve.ce.FooBar inherits from ve.Bar). In the case of where it previously had two mixins with ve.extendClass(), either one becomes inheritClass and one a mixin, both to mixinClass(). No visible changes should come from this commit as the instances still all have the same visible properties in the end. No more or less than before. * Misc.: - Be consistent in calling parent constructors in the same order as the inheritance. - Add missing @extends and @param documentation. - Replace invalid {Integer} type hint with {Number}. - Consistent doc comments order: @class, @abstract, @constructor, @extends, @params. - Fix indentation errors A fairly common mistake was a superfluous space before the identifier on the assignment line directly below the documentation comment. $ ack "^ [^*]" --js modules/ve - Typo "Inhertiance" -> "Inheritance". - Replacing the other confusing comment "Inheritance" (inside the constructor) with "Parent constructor". - Add missing @abstract for ve.ui.Tool. - Corrected ve.FormatDropdownTool to ve.ui.FormatDropdownTool.js - Add function names to all @constructor functions. Now that we have inheritance it is important and useful to have these functions not be anonymous. Example of debug shot: http://cl.ly/image/1j3c160w3D45 Makes the difference between < documentNode; > ve_dm_DocumentNode ... : ve_dm_BranchNode ... : ve_dm_Node ... : ve_dm_Node ... : Object ... without names (current situation): < documentNode; > Object ... : Object ... : Object ... : Object ... : Object ... though before this commit, it really looks like this (flattened since ve.extendClass really did a mixin): < documentNode; > Object ... ... ... Pattern in Sublime (case-sensitive) to find nameless constructor functions: "^ve\..*\.([A-Z])([^\.]+) = function \(" Change-Id: Iab763954fb8cf375900d7a9a92dec1c755d5407e
2012-09-05 06:07:47 +00:00
* @returns {Number} Model length
*/
ve.ce.Node.prototype.getLength = function () {
return this.model.getLength();
2012-03-08 12:27:02 +00:00
};
/**
* Gets model outer length.
*
* This method passes through to the model.
*
2012-03-08 12:27:02 +00:00
* @method
Object management: Object create/inherit/clone utilities * For the most common case: - replace ve.extendClass with ve.inheritClass (chose slightly different names to detect usage of the old/new one, and I like 'inherit' better). - move it up to below the constructor, see doc block for why. * Cases where more than 2 arguments were passed to ve.extendClass are handled differently depending on the case. In case of a longer inheritance tree, the other arguments could be omitted (like in "ve.ce.FooBar, ve.FooBar, ve.Bar". ve.ce.FooBar only needs to inherit from ve.FooBar, because ve.ce.FooBar inherits from ve.Bar). In the case of where it previously had two mixins with ve.extendClass(), either one becomes inheritClass and one a mixin, both to mixinClass(). No visible changes should come from this commit as the instances still all have the same visible properties in the end. No more or less than before. * Misc.: - Be consistent in calling parent constructors in the same order as the inheritance. - Add missing @extends and @param documentation. - Replace invalid {Integer} type hint with {Number}. - Consistent doc comments order: @class, @abstract, @constructor, @extends, @params. - Fix indentation errors A fairly common mistake was a superfluous space before the identifier on the assignment line directly below the documentation comment. $ ack "^ [^*]" --js modules/ve - Typo "Inhertiance" -> "Inheritance". - Replacing the other confusing comment "Inheritance" (inside the constructor) with "Parent constructor". - Add missing @abstract for ve.ui.Tool. - Corrected ve.FormatDropdownTool to ve.ui.FormatDropdownTool.js - Add function names to all @constructor functions. Now that we have inheritance it is important and useful to have these functions not be anonymous. Example of debug shot: http://cl.ly/image/1j3c160w3D45 Makes the difference between < documentNode; > ve_dm_DocumentNode ... : ve_dm_BranchNode ... : ve_dm_Node ... : ve_dm_Node ... : Object ... without names (current situation): < documentNode; > Object ... : Object ... : Object ... : Object ... : Object ... though before this commit, it really looks like this (flattened since ve.extendClass really did a mixin): < documentNode; > Object ... ... ... Pattern in Sublime (case-sensitive) to find nameless constructor functions: "^ve\..*\.([A-Z])([^\.]+) = function \(" Change-Id: Iab763954fb8cf375900d7a9a92dec1c755d5407e
2012-09-05 06:07:47 +00:00
* @returns {Number} Model outer length
2012-03-08 12:27:02 +00:00
*/
ve.ce.Node.prototype.getOuterLength = function () {
return this.model.getOuterLength();
};
/**
* Checks if this node can be split.
*
* @method
* @returns {Boolean} Node can be split
*/
ve.ce.Node.prototype.canBeSplit = function () {
return ve.ce.nodeFactory.canNodeBeSplit( this.type );
2012-03-08 12:27:02 +00:00
};
/**
* Gets a reference to the model this node observes.
*
2012-03-08 12:27:02 +00:00
* @method
* @returns {ve.dm.Node} Reference to the model this node observes
*/
ve.ce.Node.prototype.getModel = function () {
2012-03-08 12:27:02 +00:00
return this.model;
};
ve.ce.Node.getSplitableNode = function ( node ) {
2012-03-08 12:27:02 +00:00
var splitableNode = null;
ve.Node.traverseUpstream( node, function ( node ) {
if ( node.canBeSplit() ) {
splitableNode = node;
return true;
} else {
return false;
2012-03-08 12:27:02 +00:00
}
} );
2012-03-08 12:27:02 +00:00
return splitableNode;
};
/**
* @method
* @returns {Boolean} Node is attached to the live DOM
*/
ve.ce.Node.prototype.isLive = function () {
return this.live;
};
/**
* @method
*/
ve.ce.Node.prototype.setLive = function ( live ) {
this.live = live;
this.emit( 'live' );
};