2013-04-03 18:21:10 +00:00
|
|
|
/*!
|
2013-04-17 17:53:26 +00:00
|
|
|
* VisualEditor ContentEditable GeneratedContentNode class.
|
2013-04-03 18:21:10 +00:00
|
|
|
*
|
|
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ContentEditable generated content node.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @abstract
|
2013-05-29 01:35:42 +00:00
|
|
|
*
|
2013-04-03 18:21:10 +00:00
|
|
|
* @constructor
|
|
|
|
*/
|
2013-05-29 01:35:42 +00:00
|
|
|
ve.ce.GeneratedContentNode = function VeCeGeneratedContentNode() {
|
2013-08-03 23:05:51 +00:00
|
|
|
// Properties
|
|
|
|
this.generatingPromise = null;
|
|
|
|
|
2013-08-28 22:55:35 +00:00
|
|
|
// DOM changes
|
2013-11-01 19:45:59 +00:00
|
|
|
this.$element.addClass( 've-ce-generatedContentNode' );
|
2013-04-03 18:21:10 +00:00
|
|
|
|
|
|
|
// Events
|
2013-08-15 23:32:41 +00:00
|
|
|
this.model.connect( this, { 'update': 'onGeneratedContentNodeUpdate' } );
|
2013-11-06 04:58:53 +00:00
|
|
|
this.connect( this, { 'teardown': 'abortGenerating' } );
|
2013-04-03 18:21:10 +00:00
|
|
|
|
|
|
|
// Initialization
|
2013-08-03 23:05:51 +00:00
|
|
|
this.update();
|
2013-04-03 18:21:10 +00:00
|
|
|
};
|
|
|
|
|
2013-06-19 15:04:02 +00:00
|
|
|
/* Events */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event setup
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event teardown
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event rerender
|
|
|
|
*/
|
|
|
|
|
2013-09-23 12:58:17 +00:00
|
|
|
/* Static members */
|
|
|
|
|
|
|
|
ve.ce.GeneratedContentNode.static = {};
|
|
|
|
|
2013-11-01 19:45:59 +00:00
|
|
|
// this.$element is just a wrapper for the real content, so don't duplicate attributes on it
|
2013-09-23 12:58:17 +00:00
|
|
|
ve.ce.GeneratedContentNode.static.renderHtmlAttributes = false;
|
|
|
|
|
2013-08-03 23:05:51 +00:00
|
|
|
/* Abstract methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start a deferred process to generate the contents of the node.
|
|
|
|
*
|
|
|
|
* If successful, the returned promise must be resolved with the generated DOM elements passed
|
|
|
|
* in as the first parameter, i.e. promise.resolve( domElements ); . Any other parameters to
|
|
|
|
* .resolve() are ignored.
|
|
|
|
*
|
|
|
|
* If the returned promise object is abortable (has an .abort() method), .abort() will be called if
|
|
|
|
* a newer update is started before the current update has finished. When a promise is aborted, it
|
|
|
|
* should cease its work and shouldn't be resolved or rejected. If an outdated update's promise
|
|
|
|
* is resolved or rejected anyway (which may happen if an aborted promise misbehaves, or if the
|
|
|
|
* promise wasn't abortable), this is ignored and doneGenerating()/failGenerating() is not called.
|
|
|
|
*
|
|
|
|
* Additional data may be passed in the config object to instruct this function to render something
|
|
|
|
* different than what's in the model. This data is implementation-specific and is passed through
|
|
|
|
* by forceUpdate().
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
* @param {Object} [config] Optional additional data
|
|
|
|
* @returns {jQuery.Promise} Promise object, may be abortable
|
|
|
|
*/
|
|
|
|
ve.ce.GeneratedContentNode.prototype.generateContents = function () {
|
|
|
|
throw new Error( 've.ce.GeneratedContentNode subclass must implement generateContents' );
|
|
|
|
};
|
|
|
|
|
2013-04-03 18:21:10 +00:00
|
|
|
/* Methods */
|
|
|
|
|
2013-08-15 23:32:41 +00:00
|
|
|
/**
|
|
|
|
* Handler for the update event
|
|
|
|
*/
|
|
|
|
ve.ce.GeneratedContentNode.prototype.onGeneratedContentNodeUpdate = function () {
|
|
|
|
this.update();
|
|
|
|
};
|
|
|
|
|
2013-09-26 02:07:22 +00:00
|
|
|
/**
|
|
|
|
* Make an array of DOM elements suitable for rendering.
|
|
|
|
*
|
|
|
|
* Subclasses can override this to provide their own cleanup steps. This function takes an
|
|
|
|
* array of DOM elements cloned within the source document and returns an array of DOM elements
|
|
|
|
* cloned into the target document. If it's important that the DOM elements still be associated
|
|
|
|
* with the original document, you should modify domElements before calling the parent
|
|
|
|
* implementation, otherwise you should call the parent implementation first and modify its
|
|
|
|
* return value.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement[]} domElements Clones of the DOM elements from the store
|
|
|
|
* @returns {HTMLElement[]} Clones of the DOM elements in the right document, with modifications
|
|
|
|
*/
|
|
|
|
ve.ce.GeneratedContentNode.prototype.getRenderedDomElements = function ( domElements ) {
|
|
|
|
var i, len, attr, $rendering,
|
|
|
|
doc = this.getElementDocument();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback for jQuery.fn.each that resolves the value of attr to the computed
|
|
|
|
* property value. Called in the context of an HTMLElement.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
function resolveAttribute() {
|
|
|
|
this.setAttribute( attr, this[attr] );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy domElements so we can modify the elements
|
|
|
|
// Filter out link, meta and style tags for bug 50043
|
2013-11-01 19:45:59 +00:00
|
|
|
$rendering = this.$( domElements ).not( 'link, meta, style' );
|
2013-09-26 02:07:22 +00:00
|
|
|
// Also remove link, meta and style tags nested inside other tags
|
|
|
|
$rendering.find( 'link, meta, style' ).remove();
|
|
|
|
|
|
|
|
// Render the computed values of some attributes
|
|
|
|
for ( i = 0, len = ve.dm.Converter.computedAttributes.length; i < len; i++ ) {
|
|
|
|
attr = ve.dm.Converter.computedAttributes[i];
|
|
|
|
$rendering.find( '[' + attr + ']' )
|
|
|
|
.add( $rendering.filter( '[' + attr + ']' ) )
|
|
|
|
.each( resolveAttribute );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clone the elements into the target document
|
|
|
|
return ve.copyDomElements( $rendering.toArray(), doc );
|
|
|
|
};
|
|
|
|
|
2013-04-03 18:21:10 +00:00
|
|
|
/**
|
2013-08-03 23:05:51 +00:00
|
|
|
* Rerender the contents of this node.
|
2013-04-03 18:21:10 +00:00
|
|
|
*
|
2013-10-15 17:37:03 +00:00
|
|
|
* @param {Object|string|Array} generatedContents Generated contents, in the default case an HTMLElement array
|
2013-10-22 17:54:59 +00:00
|
|
|
* @fires setup
|
|
|
|
* @fires teardown
|
2013-04-03 18:21:10 +00:00
|
|
|
*/
|
2013-10-15 17:37:03 +00:00
|
|
|
ve.ce.GeneratedContentNode.prototype.render = function ( generatedContents ) {
|
2013-08-03 23:05:51 +00:00
|
|
|
if ( this.live ) {
|
|
|
|
this.emit( 'teardown' );
|
|
|
|
}
|
2013-11-01 19:45:59 +00:00
|
|
|
this.$element.empty().append( this.getRenderedDomElements( ve.copyDomElements( generatedContents ) ) );
|
2013-08-03 23:05:51 +00:00
|
|
|
if ( this.live ) {
|
|
|
|
this.emit( 'setup' );
|
2013-10-15 17:37:03 +00:00
|
|
|
this.afterRender( generatedContents );
|
2013-08-03 23:05:51 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-09-26 19:35:43 +00:00
|
|
|
/**
|
|
|
|
* Trigger rerender events after rendering the contents of the node.
|
|
|
|
*
|
|
|
|
* Nodes may override this method if the rerender event needs to be deferred (e.g. until images have loaded)
|
|
|
|
*
|
2013-10-15 17:37:03 +00:00
|
|
|
* @param {Object|string|Array} generatedContents Generated contents
|
2013-10-22 17:54:59 +00:00
|
|
|
* @fires rerender
|
2013-09-26 19:35:43 +00:00
|
|
|
*/
|
|
|
|
ve.ce.GeneratedContentNode.prototype.afterRender = function () {
|
|
|
|
this.emit( 'rerender' );
|
|
|
|
};
|
|
|
|
|
2013-08-03 23:05:51 +00:00
|
|
|
/**
|
|
|
|
* Update the contents of this node based on the model and config data. If this combination of
|
|
|
|
* model and config data has been rendered before, the cached rendering in the store will be used.
|
|
|
|
*
|
|
|
|
* @param {Object} [config] Optional additional data to pass to generateContents()
|
|
|
|
*/
|
|
|
|
ve.ce.GeneratedContentNode.prototype.update = function ( config ) {
|
|
|
|
var store = this.model.doc.getStore(),
|
2013-10-15 19:59:14 +00:00
|
|
|
index = store.indexOfHash( OO.getHash( [ this.model, config ] ) );
|
2013-04-03 18:21:10 +00:00
|
|
|
if ( index !== null ) {
|
2013-08-03 23:05:51 +00:00
|
|
|
this.render( store.value( index ) );
|
2013-04-03 18:21:10 +00:00
|
|
|
} else {
|
2013-09-24 23:47:56 +00:00
|
|
|
this.forceUpdate( config );
|
2013-04-03 18:21:10 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-08-03 23:05:51 +00:00
|
|
|
* Force the contents to be updated. Like update(), but bypasses the store.
|
|
|
|
*
|
|
|
|
* @param {Object} [config] Optional additional data to pass to generateContents()
|
2013-04-03 18:21:10 +00:00
|
|
|
*/
|
2013-08-03 23:05:51 +00:00
|
|
|
ve.ce.GeneratedContentNode.prototype.forceUpdate = function ( config ) {
|
|
|
|
var promise, node = this;
|
2013-11-06 04:58:53 +00:00
|
|
|
|
2013-08-03 23:05:51 +00:00
|
|
|
if ( this.generatingPromise ) {
|
|
|
|
// Abort the currently pending generation process if possible
|
2013-11-06 04:58:53 +00:00
|
|
|
this.abortGenerating();
|
2013-08-03 23:05:51 +00:00
|
|
|
} else {
|
2013-11-06 04:58:53 +00:00
|
|
|
// Only call startGenerating if we weren't generating before
|
2013-08-03 23:05:51 +00:00
|
|
|
this.startGenerating();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new promise
|
|
|
|
promise = this.generatingPromise = this.generateContents( config );
|
|
|
|
promise
|
|
|
|
// If this promise is no longer the currently pending one, ignore it completely
|
2013-10-15 17:37:03 +00:00
|
|
|
.done( function ( generatedContents ) {
|
2013-08-03 23:05:51 +00:00
|
|
|
if ( node.generatingPromise === promise ) {
|
2013-10-15 17:37:03 +00:00
|
|
|
node.doneGenerating( generatedContents, config );
|
2013-08-03 23:05:51 +00:00
|
|
|
}
|
|
|
|
} )
|
|
|
|
.fail( function () {
|
|
|
|
if ( node.generatingPromise === promise ) {
|
|
|
|
node.failGenerating();
|
|
|
|
}
|
|
|
|
} );
|
2013-04-03 18:21:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the node starts generating new content.
|
2013-08-03 23:05:51 +00:00
|
|
|
*
|
|
|
|
* This function is only called when the node wasn't already generating content. If a second update
|
|
|
|
* comes in, this function will only be called if the first update has already finished (i.e.
|
|
|
|
* doneGenerating or failGenerating has already been called).
|
|
|
|
*
|
2013-04-03 18:21:10 +00:00
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.ce.GeneratedContentNode.prototype.startGenerating = function () {
|
2013-11-01 19:45:59 +00:00
|
|
|
this.$element.addClass( 've-ce-generatedContentNode-generating' );
|
2013-04-03 18:21:10 +00:00
|
|
|
};
|
|
|
|
|
2013-11-06 04:58:53 +00:00
|
|
|
/**
|
|
|
|
* Abort the currently pending generation, if any, and remove the generating CSS class.
|
|
|
|
*
|
|
|
|
* This invokes .abort() on the pending promise if the promise has that method. It also ensures
|
|
|
|
* that if the promise does get resolved or rejected later, this is ignored.
|
|
|
|
*/
|
|
|
|
ve.ce.GeneratedContentNode.prototype.abortGenerating = function () {
|
|
|
|
var promise = this.generatingPromise;
|
|
|
|
if ( promise ) {
|
|
|
|
// Unset this.generatingPromise first so that if the promise is resolved or rejected
|
|
|
|
// from within .abort(), this is ignored as it should be
|
|
|
|
this.generatingPromise = null;
|
|
|
|
if ( $.isFunction( promise.abort ) ) {
|
|
|
|
promise.abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.$element.removeClass( 've-ce-generatedContentNode-generating' );
|
|
|
|
};
|
|
|
|
|
2013-04-03 18:21:10 +00:00
|
|
|
/**
|
|
|
|
* Called when the node successfully finishes generating new content.
|
|
|
|
*
|
|
|
|
* @method
|
2013-10-15 17:37:03 +00:00
|
|
|
* @param {Object|string|Array} generatedContents Generated contents
|
2013-08-03 23:05:51 +00:00
|
|
|
* @param {Object} [config] Config object passed to forceUpdate()
|
2013-04-03 18:21:10 +00:00
|
|
|
*/
|
2013-10-15 17:37:03 +00:00
|
|
|
ve.ce.GeneratedContentNode.prototype.doneGenerating = function ( generatedContents, config ) {
|
2013-11-06 04:58:53 +00:00
|
|
|
var store, hash;
|
|
|
|
|
|
|
|
// Because doneGenerating is invoked asynchronously, the model node may have become detached
|
|
|
|
// in the meantime. Handle this gracefully.
|
|
|
|
if ( this.model.doc ) {
|
|
|
|
store = this.model.doc.getStore();
|
2013-10-15 19:59:14 +00:00
|
|
|
hash = OO.getHash( [ this.model, config ] );
|
2013-11-06 04:58:53 +00:00
|
|
|
store.index( generatedContents, hash );
|
|
|
|
}
|
2013-10-15 17:37:03 +00:00
|
|
|
|
2013-11-01 19:45:59 +00:00
|
|
|
this.$element.removeClass( 've-ce-generatedContentNode-generating' );
|
2013-08-03 23:05:51 +00:00
|
|
|
this.generatingPromise = null;
|
2013-10-15 17:37:03 +00:00
|
|
|
this.render( generatedContents );
|
2013-04-03 18:21:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the has failed to generate new content.
|
2013-08-03 23:05:51 +00:00
|
|
|
*
|
2013-04-03 18:21:10 +00:00
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.ce.GeneratedContentNode.prototype.failGenerating = function () {
|
2013-11-01 19:45:59 +00:00
|
|
|
this.$element.removeClass( 've-ce-generatedContentNode-generating' );
|
2013-08-03 23:05:51 +00:00
|
|
|
this.generatingPromise = null;
|
2013-08-29 01:55:19 +00:00
|
|
|
};
|