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-04-03 18:21:10 +00:00
|
|
|
// DOM Changes
|
|
|
|
this.$.addClass( 've-ce-generatedContentNode' );
|
|
|
|
this.$.attr( 'contenteditable', false );
|
|
|
|
|
|
|
|
// Events
|
2013-05-01 22:21:32 +00:00
|
|
|
this.model.connect( this, { 'update': 'onUpdate' } );
|
2013-04-03 18:21:10 +00:00
|
|
|
|
|
|
|
// Initialization
|
|
|
|
this.onUpdate();
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle update events.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.ce.GeneratedContentNode.prototype.onUpdate = function () {
|
2013-06-06 00:49:22 +00:00
|
|
|
var doc = this.getElementDocument(),
|
|
|
|
store = this.model.doc.getStore(),
|
2013-04-03 18:21:10 +00:00
|
|
|
index = store.indexOfHash( ve.getHash( this.model ) );
|
|
|
|
if ( index !== null ) {
|
2013-05-29 01:35:42 +00:00
|
|
|
if ( this.live ) {
|
|
|
|
this.emit( 'teardown' );
|
|
|
|
}
|
2013-06-06 00:49:22 +00:00
|
|
|
this.$.empty().append(
|
|
|
|
this.$$( store.value( index ) ).map( function ( i, domElement ) {
|
|
|
|
return doc.importNode( domElement, true );
|
|
|
|
} )
|
|
|
|
);
|
2013-05-29 01:35:42 +00:00
|
|
|
if ( this.live ) {
|
|
|
|
this.emit( 'setup' );
|
|
|
|
}
|
2013-04-03 18:21:10 +00:00
|
|
|
} else {
|
|
|
|
this.startGenerating();
|
|
|
|
this.generateContents()
|
|
|
|
.done( ve.bind( this.doneGenerating, this ) )
|
|
|
|
.fail( ve.bind( this.failGenerating, this ) );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start a deferred process to generate the contents of the node.
|
|
|
|
* @returns {jQuery.Promise} Promise object
|
|
|
|
*/
|
|
|
|
ve.ce.GeneratedContentNode.prototype.generateContents = function () {
|
|
|
|
throw new Error( 've.ce.GeneratedContentNode subclass must implement generateContents' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the node starts generating new content.
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.ce.GeneratedContentNode.prototype.startGenerating = function () {
|
|
|
|
// TODO: add 'generating' style
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the node successfully finishes generating new content.
|
|
|
|
*
|
|
|
|
* @method
|
2013-05-02 17:22:56 +00:00
|
|
|
* @param {HTMLElement[]} domElements Generated content
|
2013-04-03 18:21:10 +00:00
|
|
|
*/
|
2013-05-02 17:22:56 +00:00
|
|
|
ve.ce.GeneratedContentNode.prototype.doneGenerating = function ( domElements ) {
|
2013-04-03 18:21:10 +00:00
|
|
|
var store = this.model.doc.getStore(),
|
|
|
|
hash = ve.getHash( this.model );
|
2013-05-02 17:22:56 +00:00
|
|
|
store.index( domElements, hash );
|
2013-04-03 18:21:10 +00:00
|
|
|
// TODO: remove 'generating' style
|
|
|
|
this.onUpdate();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the has failed to generate new content.
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.ce.GeneratedContentNode.prototype.failGenerating = function () {
|
|
|
|
// TODO: remove 'generating' style
|
2013-05-29 01:35:42 +00:00
|
|
|
};
|