2012-07-19 00:11:26 +00:00
|
|
|
/**
|
|
|
|
* VisualEditor content editable Node class.
|
2012-07-19 21:25:16 +00:00
|
|
|
*
|
2012-07-19 00:11:26 +00:00
|
|
|
* @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
|
|
|
/**
|
2012-06-20 01:20:28 +00:00
|
|
|
* Generic ContentEditable node.
|
|
|
|
*
|
2012-03-08 12:27:02 +00:00
|
|
|
* @class
|
|
|
|
* @abstract
|
|
|
|
* @constructor
|
|
|
|
* @extends {ve.Node}
|
2012-06-20 01:20:28 +00:00
|
|
|
* @param {String} type Symbolic name of node type
|
2012-03-08 12:27:02 +00:00
|
|
|
* @param {ve.dm.Node} model Model to observe
|
2012-06-20 01:20:28 +00:00
|
|
|
* @param {jQuery} [$element] Element to use as a container
|
2012-03-08 12:27:02 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.Node = function ( type, model, $element ) {
|
2012-03-08 12:27:02 +00:00
|
|
|
// Inheritance
|
2012-06-20 01:20:28 +00:00
|
|
|
ve.Node.call( this, type );
|
|
|
|
|
2012-03-08 12:27:02 +00:00
|
|
|
// Properties
|
|
|
|
this.model = model;
|
2012-04-04 18:53:23 +00:00
|
|
|
this.$ = $element || $( '<div></div>' );
|
2012-06-20 01:20:28 +00:00
|
|
|
this.parent = null;
|
|
|
|
|
|
|
|
this.$.data( 'node', this );
|
2012-03-08 12:27:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2012-06-20 01:20:28 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.Node.prototype.getChildNodeTypes = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return this.model.getChildNodeTypes();
|
2012-04-05 21:31:59 +00:00
|
|
|
};
|
|
|
|
|
2012-03-08 12:27:02 +00:00
|
|
|
/**
|
2012-06-20 01:20:28 +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
|
2012-06-20 01:20:28 +00:00
|
|
|
* @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
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.Node.prototype.getParentNodeTypes = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return this.model.getParentNodeTypes();
|
2012-03-08 12:27:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-06-20 01:20:28 +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
|
2012-06-20 01:20:28 +00:00
|
|
|
* @returns {Boolean} Model node can have children
|
2012-03-08 12:27:02 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.Node.prototype.canHaveChildren = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return this.model.canHaveChildren();
|
2012-03-08 12:27:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-06-20 01:20:28 +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
|
2012-06-20 01:20:28 +00:00
|
|
|
* @returns {Boolean} Model node can have children
|
2012-03-08 12:27:02 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.Node.prototype.canHaveChildren = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return this.model.canHaveChildren();
|
2012-03-08 12:27:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-06-20 01:20:28 +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
|
2012-06-20 01:20:28 +00:00
|
|
|
* @returns {Boolean} Model node can have grandchildren
|
2012-03-08 12:27:02 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.Node.prototype.canHaveGrandchildren = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
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
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.Node.prototype.isWrapped = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return this.model.isWrapped();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if this node can contain content.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {Boolean} Node can contain content
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.Node.prototype.canContainContent = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return this.model.canContainContent();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if this node is content.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {Boolean} Node is content
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.Node.prototype.isContent = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return this.model.isContent();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-07-20 00:45:41 +00:00
|
|
|
* Checks if this node can have a slug before it
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @method
|
2012-07-20 00:45:41 +00:00
|
|
|
* @returns {Boolean} Whether the node can have a slug before it
|
2012-06-20 01:20:28 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.Node.prototype.canHaveSlugBefore = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return !this.canContainContent() && this.getParentNodeTypes() === null && this.type !== 'text';
|
|
|
|
};
|
|
|
|
|
2012-07-20 00:45:41 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2012-06-20 01:20:28 +00:00
|
|
|
/**
|
|
|
|
* Gets model length.
|
|
|
|
*
|
|
|
|
* This method passes through to the model.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {Integer} Model length
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.Node.prototype.getLength = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return this.model.getLength();
|
2012-03-08 12:27:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-06-20 01:20:28 +00:00
|
|
|
* Gets model outer length.
|
|
|
|
*
|
|
|
|
* This method passes through to the model.
|
|
|
|
*
|
2012-03-08 12:27:02 +00:00
|
|
|
* @method
|
2012-06-20 01:20:28 +00:00
|
|
|
* @returns {Integer} Model outer length
|
2012-03-08 12:27:02 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.Node.prototype.getOuterLength = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return this.model.getOuterLength();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if this node can be split.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {Boolean} Node can be split
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.Node.prototype.canBeSplit = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
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-06-20 01:20:28 +00:00
|
|
|
*
|
2012-03-08 12:27:02 +00:00
|
|
|
* @method
|
|
|
|
* @returns {ve.dm.Node} Reference to the model this node observes
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.Node.prototype.getModel = function () {
|
2012-03-08 12:27:02 +00:00
|
|
|
return this.model;
|
|
|
|
};
|
|
|
|
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.Node.getSplitableNode = function ( node ) {
|
2012-03-08 12:27:02 +00:00
|
|
|
var splitableNode = null;
|
2012-06-20 01:20:28 +00:00
|
|
|
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.Node.traverseUpstream( node, function ( node ) {
|
2012-06-20 01:20:28 +00:00
|
|
|
if ( node.canBeSplit() ) {
|
|
|
|
splitableNode = node;
|
2012-06-21 06:19:56 +00:00
|
|
|
return true;
|
2012-06-20 01:20:28 +00:00
|
|
|
} else {
|
2012-06-21 06:19:56 +00:00
|
|
|
return false;
|
2012-03-08 12:27:02 +00:00
|
|
|
}
|
|
|
|
} );
|
2012-06-20 01:20:28 +00:00
|
|
|
|
2012-03-08 12:27:02 +00:00
|
|
|
return splitableNode;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
ve.extendClass( ve.ce.Node, ve.Node );
|