mediawiki-extensions-Visual.../modules/ve/ce/ve.ce.BranchNode.js
Timo Tijhof b11bbed7a6 JSDuck: Generated code documentation!
See CODING.md for how to run it.

Mistakes fixed:
* Warning: Unknown type function
  -> Function
* Warning: Unknown type DOMElement
  -> HTMLElement
* Warning: Unknown type DOM Node
  -> HTMLElement
* Warning: Unknown type Integer
  -> Mixed
* Warning: Unknown type Command
  -> ve.Command
* Warning: Unknown type any
  -> number
* Warning: Unknown type ve.Transaction
  -> ve.dm.Transaction
* Warning: Unknown type ve.dm.AnnotationSet
  -> ve.AnnotationSet
* Warning: Unknown type false
  -> boolean
* Warning: Unknown type ve.dm.AlienNode
  ve.dm doesn't have a generic AlienNode like ve.ce
  -> Unknown type ve.dm.AlienInlineNode|ve.dm.AlienBlockNode
* Warning: Unknown type ve.ve.Surface
  -> ve.ce.Surface
* ve.example.lookupNode:
  -> Last @param should be @return
* ve.dm.Transaction.prototype.pushReplace:
  -> @param {Array] should be @param {Array}
* Warning: ve.BranchNode.js:27: {@link ve.Node#hasChildren} links to non-existing member
  -> (removed)
* Warning: ve.LeafNode.js:21: {@link ve.Node#hasChildren} links to non-existing member
  -> (removed)

Differences fixed:
* Variadic arguments are like @param {Type...} [name]
  instead of @param {Type} [name...]
* Convert all file headers from /** to /*! because JSDuck tries
  to parse all /** blocks and fails to parse with all sorts of
   errors for "Global property", "Unnamed property", and
  "Duplicate property".
  Find: \/\*\*([^@]+)(@copyright)
  Replace: /*!$1$2
* Indented blocks are considered code examples.
  A few methods had documentation with numbered lists that were
  indented, which have now been updated to not be intended.
* The free-form text descriptions are parsed with Markdown,
  which requires lists to be separated from paragraphs by an
  empty line.
  And we should use `backticks` instead of {braces} for inline
  code in text paragraphs.
* Doc blocks for classes and their constructor have to be
  in the correct order (@constructor, @param, @return must be
  before @class, @abstract, @extends etc.)
* `@extends Class` must not have Class {wrapped}
* @throws must start with a {Type}
* @example means something else. It is used for an  inline demo
  iframe, not code block. For that simply indent with spaces.
* @member means something else.
  Non-function properties are marked with @property, not @member.
* To create a link to a class or member, in most cases the name
  is enough to create a link. E.g. Foo, Foo.bar, Foo.bar#quux,
  where a hash stands for "instance member", so Foo.bar#quux,
  links to Foo.bar.prototype.quux (the is not supported, as
  "prototype" is considered an implementation detail, it only
  indexes class name and method name).
  If the magic linker doesn't work for some case, the
  verbose syntax is {@link #target label}.
* @property can't have sub-properties (nested @param and @return
  values are supported, only @static @property can't be nested).
  We only have one case of this, which can be worked around by
  moving those in a new virtual class. The code is unaltered
  (only moved down so that it isn't with the scope of the main
  @class block). ve.dm.TransactionProcessor.processors.

New:
* @mixins: Classes mixed into the current class.
* @event: Events that can be emitted by a class. These are also
  inherited by subclasses. (+ @param, @return and @preventable).
  So ve.Node#event-attach is inherited to ve.dm.BreakNode,
  just like @method is.
* @singleton: Plain objects such as ve, ve.dm, ve.ce were missing
  documentation causing a tree error. Documented those as a
  JSDuck singleton, which they but just weren't documented yet.
  NB: Members of @singleton don't need @static (if present,
  triggers a compiler warning).
* @chainable: Shorthand for "@return this". We were using
  "@return {classname}" which is ambiguous (returns the same
  instance or another instance?), @chainable is specifically
  for "@return this". Creates proper labels in the generated
  HTML pages.

Removed:
* @mixin: (not to be confused with @mixins). Not supported by
  JSDuck. Every class is standalone anyway. Where needed marked
  them @class + @abstract instead.

Change-Id: I6a7c9e8ee8f995731bc205d666167874eb2ebe23
2013-01-05 01:16:32 +01:00

275 lines
7.9 KiB
JavaScript

/*!
* VisualEditor content editable BranchNode class.
*
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* ContentEditable node that can have branch or leaf children.
*
* @class
* @abstract
* @extends ve.ce.Node
* @mixins ve.BranchNode
* @constructor
* @param {string} type Symbolic name of node type
* @param {ve.dm.BranchNode} model Model to observe
* @param {jQuery} [$element] Element to use as a container
*/
ve.ce.BranchNode = function VeCeBranchNode( type, model, $element ) {
// Mixin constructor
ve.BranchNode.call( this );
// Parent constructor
ve.ce.Node.call( this, type, model, $element );
// Properties
this.domWrapperElementType = this.$.get( 0 ).nodeName.toLowerCase();
this.slugs = { };
this.emitChildUpdate = ve.bind( function () {
this.emit( 'childUpdate' );
}, this );
// Events
this.model.addListenerMethod( this, 'splice', 'onSplice' );
// DOM Changes
this.$.addClass( 've-ce-branchNode' );
// Initialization
this.onSplice.apply( this, [0, 0].concat( model.getChildren() ) );
};
/**
* @event rewrap
* @param {jQuery} $old
* @param {jQuery} $new
*/
/* Inheritance */
ve.inheritClass( ve.ce.BranchNode, ve.ce.Node );
ve.mixinClass( ve.ce.BranchNode, ve.BranchNode );
/* Static Members */
// TODO: Consider using different (or additional) CSS classes for inline and block aliens
if ( $.browser.msie ) {
ve.ce.BranchNode.$inlineSlugTemplate = $( '<span class="ve-ce-slug">&nbsp;</span>' );
} else {
ve.ce.BranchNode.$inlineSlugTemplate = $( '<span class="ve-ce-slug">&#xFEFF;</span>' );
}
ve.ce.BranchNode.$blockSlugTemplate =
ve.ce.BranchNode.$inlineSlugTemplate.clone().css( 'display', 'block' );
/* Static Methods */
/**
* Gets the appropriate element type for the DOM wrapper of a node.
*
* This method reads the `key` attribute from a `model` and looks up a type in the node's statically
* defined `domWrapperElementTypes` member, which is a mapping of possible values of that attribute
* and DOM element types.
*
* @method
* @param {ve.dm.BranchNode} model Model node is based on
* @param {string} key Attribute name to read type value from
* @returns {string} DOM element type for wrapper
* @throws {Error} Attribute is not defined in the model
* @throws {Error} Attribute value is not a key in `domWrapperElementTypes`
*/
ve.ce.BranchNode.getDomWrapperType = function ( model, key ) {
var types,
value = model.getAttribute( key );
if ( value === undefined ) {
throw new Error( 'Undefined attribute: ' + key );
}
types = ve.ce.nodeFactory.lookup( model.getType() ).domWrapperElementTypes;
if ( types[value] === undefined ) {
throw new Error( 'Invalid attribute value: ' + value );
}
return types[value];
};
/**
* Gets a jQuery selection of a new DOM wrapper for a node.
*
* This method uses #getDomWrapperType to determine the proper element type to use.
*
* @method
* @param {ve.dm.BranchNode} model Model node is based on
* @param {string} key Attribute name to read type value from
* @returns {jQuery} Selection of DOM wrapper
*/
ve.ce.BranchNode.getDomWrapper = function ( model, key ) {
var type = ve.ce.BranchNode.getDomWrapperType( model, key );
return $( document.createElement( type ) );
};
/* Methods */
/**
* Updates the DOM wrapper of this node if needed.
*
* This method uses {getDomWrapperType} to determine the proper element type to use.
*
* WARNING: The contents, .data( 'node' ) and any classes the wrapper already has will be moved to
* the new wrapper, but other attributes and any other information added using $.data() will be
* lost upon updating the wrapper. To retain information added to the wrapper, subscribe to the
* 'rewrap' event and copy information from the {$old} wrapper the {$new} wrapper.
*
* @method
* @param {string} key Attribute name to read type value from
* @emits rewrap
*/
ve.ce.BranchNode.prototype.updateDomWrapper = function ( key ) {
var $element,
type = ve.ce.BranchNode.getDomWrapperType( this.model, key );
if ( type !== this.domWrapperElementType ) {
$element = $( document.createElement( type ) );
// Copy classes
$element.attr( 'class', this.$.attr( 'class' ) );
// Copy .data( 'node' )
$element.data( 'node', this.$.data( 'node' ) );
// Move contents
$element.append( this.$.contents() );
// Emit an event that can be handled to copy other things over if needed
this.emit( 'rewrap', this.$, $element );
// Swap elements
this.$.replaceWith( $element );
// Use new element from now on
this.$ = $element;
// Remember which type we are using now
this.domWrapperElementType = type;
}
};
/**
* Responds to splice events on a ve.dm.BranchNode.
*
* ve.ce.Node objects are generated from the inserted ve.dm.Node objects, producing a view that's a
* mirror of its model.
*
* @method
* @param {number} index Index to remove and or insert nodes at
* @param {number} howmany Number of nodes to remove
* @param {ve.dm.BranchNode...} [nodes] Variadic list of nodes to insert
*/
ve.ce.BranchNode.prototype.onSplice = function ( index ) {
var i,
length,
args = Array.prototype.slice.call( arguments ),
$anchor,
removals;
// Convert models to views and attach them to this node
if ( args.length >= 3 ) {
for ( i = 2, length = args.length; i < length; i++ ) {
args[i] = ve.ce.nodeFactory.create( args[i].getType(), args[i] );
args[i].model.addListener( 'update', this.emitChildUpdate );
}
}
removals = this.children.splice.apply( this.children, args );
for ( i = 0, length = removals.length; i < length; i++ ) {
removals[i].model.removeListener( 'update', this.emitChildUpdate );
removals[i].detach();
// Update DOM
removals[i].$.detach();
removals[i].setLive( false );
}
if ( args.length >= 3 ) {
if ( index ) {
// Get the element before the insertion point
$anchor = this.children[ index - 1 ].$.last();
}
for ( i = args.length - 1; i >= 2; i-- ) {
args[i].attach( this );
if ( index ) {
$anchor.after( args[i].$ );
} else {
this.$.prepend( args[i].$ );
}
if ( this.live !== args[i].isLive() ) {
args[i].setLive( this.live );
}
}
}
this.setupSlugs();
};
/**
* @method
*/
ve.ce.BranchNode.prototype.setupSlugs = function () {
var key, $slug, i;
// Remove all slugs in this branch
for( key in this.slugs ) {
this.slugs[key].remove();
delete this.slugs[key];
}
if ( this.canHaveGrandchildren() ) {
$slug = ve.ce.BranchNode.$blockSlugTemplate.clone();
} else {
$slug = ve.ce.BranchNode.$inlineSlugTemplate.clone();
}
if ( this.getLength() === 0 ) {
this.slugs[0] = $slug.clone().appendTo( this.$ );
} else {
// Iterate over all children of this branch and add slugs in appropriate places
for ( i = 0; i < this.children.length; i++ ) {
// First sluggable child (left side)
if ( i === 0 && this.children[i].canHaveSlugBefore() ) {
this.slugs[i] = $slug.clone().insertBefore( this.children[i].$.first() );
}
if ( this.children[i].canHaveSlugAfter() ) {
if (
// Last sluggable child (right side)
i === this.children.length - 1 ||
// Sluggable child followed by another sluggable child (in between)
( this.children[i + 1] && this.children[i + 1].canHaveSlugBefore() )
) {
this.slugs[i + 1] = $slug.clone().insertAfter( this.children[i].$.last() );
}
}
}
}
};
/**
* @method
* @param offset
*/
ve.ce.BranchNode.prototype.getSlugAtOffset = function ( offset ) {
var i,
startOffset = this.model.getOffset() + ( this.isWrapped() ? 1 : 0 );
if ( offset === startOffset ) {
return this.slugs[0] || null;
}
for ( i = 0; i < this.children.length; i++ ) {
startOffset += this.children[i].model.getOuterLength();
if ( offset === startOffset ) {
return this.slugs[i + 1] || null;
}
}
};
/**
* @method
* @param live
*/
ve.ce.BranchNode.prototype.setLive = function ( live ) {
this.live = live;
this.emit( 'live' );
for ( var i = 0; i < this.children.length; i++ ) {
this.children[i].setLive( live );
}
};