Moved "is slug gable" functionality to a static method of ve.ce.BranchNode

Also optimized some query stuff

Change-Id: I5675258c6e276a1ba410dcf1ee111392140a04eb
This commit is contained in:
Trevor Parscal 2012-05-24 11:58:33 -07:00
parent c9dddf60da
commit e150ba2e54
2 changed files with 39 additions and 36 deletions

View file

@ -31,6 +31,10 @@ ve.ce.BranchNode = function( type, model, $element ) {
}
};
/* Static Members */
ve.ce.BranchNode.$slugTemplate = $( '<span class="ve-ce-slug">&nbsp;</span>' );
/* Static Methods */
ve.ce.BranchNode.getDomWrapperType = function( model, key ) {
@ -50,6 +54,26 @@ ve.ce.BranchNode.getDomWrapper = function( model, key ) {
return $( '<' + type + '></' + type + '>' );
};
/**
* Checks if a node can have a slug before or after it.
*
* TODO: Implement it as a one of node rules instead of a static array
*
* @static
* @method
* @param {ve.ce.Node} node Node to check
* @returns {Boolean} Node can have a slug
*/
ve.ce.BranchNode.canNodeHaveSlug = function( node ) {
return [
'image',
'list',
'table',
'alienInline',
'alienBlock'
].indexOf( node.getType() ) !== -1;
};
/* Methods */
ve.ce.BranchNode.prototype.updateDomWrapper = function( key ) {
@ -114,27 +138,23 @@ ve.ce.BranchNode.prototype.onSplice = function( index, howmany ) {
this.$slugs.remove();
// Iterate over all children of this branch and add slugs in appropriate places
var slug = '<span class="ve-ce-slug">&nbsp;</span>',
$slug;
for( i = 0; i < this.children.length; i++ ) {
if ( ve.ce.sluggable.indexOf( this.children[i].type ) !== -1 ) {
for ( i = 0; i < this.children.length; i++ ) {
if ( ve.ce.BranchNode.canNodeHaveSlug( this.children[i] ) ) {
if ( i === 0 ) {
// first
$slug = $( slug );
this.children[i].$.before( $slug );
this.$slugs = this.$slugs.add($slug);
// First sluggable child (left side)
this.$slugs = this.$slugs.add(
ve.ce.BranchNode.$slugTemplate.clone().insertBefore( this.children[i].$ )
);
}
if ( i === this.children.length - 1 ) {
// last
$slug = $( slug );
this.children[i].$.after( $slug );
this.$slugs = this.$slugs.add($slug);
}
if ( this.children[i + 1] && ve.ce.sluggable.indexOf( this.children[i + 1].type ) !== -1 ) {
// special next to special
$slug = $( slug );
this.children[i].$.after( $slug );
this.$slugs = this.$slugs.add($slug);
if (
// Last sluggable child (right side)
i === this.children.length - 1 ||
// Sluggable child followed by another sluggable child (in between)
( this.children[i + 1] && ve.ce.BranchNode.canNodeHaveSlug( this.children[i + 1] ) )
) {
this.$slugs = this.$slugs.add(
ve.ce.BranchNode.$slugTemplate.clone().insertAfter( this.children[i].$ )
);
}
}
}

View file

@ -7,23 +7,6 @@ ve.ce = {
//'factory': Initialized in ve.ce.NodeFactory.js
};
/**
* List of all nodes that may need to have "slug" on their left or right side
* For more information look into ve.ce.BrancNode.js
*
* TODO: Implement it as a one of node rules instead of a "global" array
*
* @static
* @member
*/
ve.ce.sluggable = [
'image',
'list',
'table',
'alienInline',
'alienBlock'
];
/**
* RegExp pattern for matching all whitespaces in HTML text.
*