2012-04-30 23:58:41 +00:00
|
|
|
/**
|
|
|
|
* ContentEditable node that can have branch or leaf children.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-30 23:58:41 +00:00
|
|
|
* @class
|
|
|
|
* @abstract
|
|
|
|
* @constructor
|
|
|
|
* @extends {ve.BranchNode}
|
|
|
|
* @extends {ve.ce.Node}
|
2012-05-05 00:50:54 +00:00
|
|
|
* @param {String} type Symbolic name of node type
|
2012-04-30 23:58:41 +00:00
|
|
|
* @param model {ve.dm.BranchNode} Model to observe
|
|
|
|
* @param {jQuery} [$element] Element to use as a container
|
|
|
|
*/
|
2012-05-05 00:50:54 +00:00
|
|
|
ve.ce.BranchNode = function( type, model, $element ) {
|
2012-04-30 23:58:41 +00:00
|
|
|
// Inheritance
|
|
|
|
ve.BranchNode.call( this );
|
2012-05-05 00:50:54 +00:00
|
|
|
ve.ce.Node.call( this, type, model, $element );
|
2012-04-30 23:58:41 +00:00
|
|
|
|
2012-05-04 20:31:14 +00:00
|
|
|
// Properties
|
|
|
|
this.domWrapperElementType = this.$.get(0).nodeName.toLowerCase();
|
|
|
|
|
2012-04-30 23:58:41 +00:00
|
|
|
// Events
|
2012-05-02 20:58:50 +00:00
|
|
|
this.model.addListenerMethod( this, 'splice', 'onSplice' );
|
2012-04-30 23:58:41 +00:00
|
|
|
|
2012-05-10 21:54:33 +00:00
|
|
|
// DOM Changes
|
|
|
|
this.$.addClass( 've-ce-branchNode' );
|
2012-05-10 05:43:46 +00:00
|
|
|
|
2012-05-10 21:54:33 +00:00
|
|
|
// Initialization
|
2012-05-02 20:58:50 +00:00
|
|
|
if ( model.getChildren().length ) {
|
|
|
|
this.onSplice.apply( this, [0, 0].concat( model.getChildren() ) );
|
|
|
|
}
|
2012-04-30 23:58:41 +00:00
|
|
|
};
|
|
|
|
|
2012-05-04 20:31:14 +00:00
|
|
|
/* Static Methods */
|
|
|
|
|
|
|
|
ve.ce.BranchNode.getDomWrapperType = function( model, key ) {
|
|
|
|
var value = model.getAttribute( key );
|
|
|
|
if ( value === undefined ) {
|
|
|
|
throw 'Undefined attribute: ' + key;
|
|
|
|
}
|
|
|
|
var types = ve.ce.factory.lookup( model.getType() ).domWrapperElementTypes;
|
|
|
|
if ( types[value] === undefined ) {
|
|
|
|
throw 'Invalid attribute value: ' + value;
|
|
|
|
}
|
|
|
|
return types[value];
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.ce.BranchNode.getDomWrapper = function( model, key ) {
|
|
|
|
var type = ve.ce.BranchNode.getDomWrapperType( model, key );
|
|
|
|
return $( '<' + type + '></' + type + '>' );
|
|
|
|
};
|
|
|
|
|
2012-04-30 23:58:41 +00:00
|
|
|
/* Methods */
|
|
|
|
|
2012-05-04 20:31:14 +00:00
|
|
|
ve.ce.BranchNode.prototype.updateDomWrapper = function( key ) {
|
|
|
|
var type = ve.ce.BranchNode.getDomWrapperType( this.model, key );
|
|
|
|
if ( type !== this.domWrapperElementType ) {
|
|
|
|
var $element = $( '<' + type + '></' + type + '>' );
|
|
|
|
// Copy classes
|
|
|
|
$element.attr( 'class', this.$.attr( 'class' ) );
|
|
|
|
// Move contents
|
|
|
|
$element.append( this.$.contents() );
|
|
|
|
// Swap elements
|
|
|
|
this.$.replaceWith( $element );
|
|
|
|
// Use new element from now on
|
|
|
|
this.$ = $element;
|
|
|
|
}
|
2012-04-30 23:58:41 +00:00
|
|
|
};
|
|
|
|
|
2012-05-04 19:28:32 +00:00
|
|
|
/**
|
|
|
|
* Responds to splice events on a ve.dm.BranchNode.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-04 19:28:32 +00:00
|
|
|
* ve.ce.Node objects are generated from the inserted ve.dm.Node objects, producing a view that's a
|
|
|
|
* mirror of it's model.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-04 19:28:32 +00:00
|
|
|
* @method
|
|
|
|
* @param {Integer} index Index to remove and or insert nodes at
|
|
|
|
* @param {Integer} howmany Number of nodes to remove
|
|
|
|
* @param {ve.dm.BranchNode} [...] Variadic list of nodes to insert
|
|
|
|
*/
|
2012-04-30 23:58:41 +00:00
|
|
|
ve.ce.BranchNode.prototype.onSplice = function( index, howmany ) {
|
|
|
|
var i,
|
|
|
|
length,
|
|
|
|
args = Array.prototype.slice.call( arguments, 0 );
|
|
|
|
// Convert models to views and attach them to this node
|
|
|
|
if ( args.length >= 3 ) {
|
|
|
|
for ( i = 2, length = args.length; i < length; i++ ) {
|
2012-05-04 20:31:14 +00:00
|
|
|
args[i] = ve.ce.factory.create( args[i].getType(), args[i] );
|
2012-04-30 23:58:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var removals = this.children.splice.apply( this.children, args );
|
|
|
|
for ( i = 0, length = removals.length; i < length; i++ ) {
|
|
|
|
removals[i].detach();
|
|
|
|
// Update DOM
|
|
|
|
removals[i].$.detach();
|
|
|
|
}
|
|
|
|
if ( args.length >= 3 ) {
|
|
|
|
var $target;
|
|
|
|
if ( index ) {
|
|
|
|
// Get the element before the insertion point
|
|
|
|
$anchor = this.$.children().eq( index - 1 );
|
|
|
|
}
|
|
|
|
for ( i = args.length - 1; i >= 2; i-- ) {
|
|
|
|
args[i].attach( this );
|
|
|
|
if ( index ) {
|
|
|
|
$anchor.after( args[i].$ );
|
|
|
|
} else {
|
|
|
|
this.$.prepend( args[i].$ );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
ve.extendClass( ve.ce.BranchNode, ve.BranchNode );
|
|
|
|
ve.extendClass( ve.ce.BranchNode, ve.ce.Node );
|