2013-08-03 14:17:16 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor ContentEditable MWExtensionNode class.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*global mw */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ContentEditable MediaWiki extension node.
|
|
|
|
*
|
2013-09-24 22:03:11 +00:00
|
|
|
* Configuration options for .update():
|
|
|
|
* - 'extsrc': override the contents of the tag (string)
|
|
|
|
* - 'attrs': override the attributes of the tag (object)
|
|
|
|
*
|
2013-08-03 14:17:16 +00:00
|
|
|
* @class
|
|
|
|
* @abstract
|
|
|
|
* @extends ve.ce.LeafNode
|
|
|
|
* @mixins ve.ce.FocusableNode
|
|
|
|
* @mixins ve.ce.ProtectedNode
|
2013-09-23 22:04:23 +00:00
|
|
|
* @mixins ve.ce.RelocatableNode
|
2013-08-03 14:17:16 +00:00
|
|
|
* @mixins ve.ce.GeneratedContentNode
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {ve.dm.MWExtensionNode} model Model to observe
|
2013-09-25 10:21:09 +00:00
|
|
|
* @param {Object} [config] Configuration options
|
2013-08-03 14:17:16 +00:00
|
|
|
*/
|
|
|
|
ve.ce.MWExtensionNode = function VeCeMWExtensionNode( model, config ) {
|
|
|
|
// Parent constructor
|
|
|
|
ve.ce.LeafNode.call( this, model, config );
|
|
|
|
|
|
|
|
// Mixin constructors
|
|
|
|
ve.ce.FocusableNode.call( this );
|
|
|
|
ve.ce.ProtectedNode.call( this );
|
2013-09-23 22:04:23 +00:00
|
|
|
ve.ce.RelocatableNode.call( this );
|
2013-08-03 14:17:16 +00:00
|
|
|
ve.ce.GeneratedContentNode.call( this );
|
|
|
|
|
2013-08-28 22:55:35 +00:00
|
|
|
// DOM changes
|
2013-08-03 14:17:16 +00:00
|
|
|
this.$.addClass( 've-ce-mwExtensionNode' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
ve.inheritClass( ve.ce.MWExtensionNode, ve.ce.LeafNode );
|
|
|
|
|
|
|
|
ve.mixinClass( ve.ce.MWExtensionNode, ve.ce.FocusableNode );
|
|
|
|
ve.mixinClass( ve.ce.MWExtensionNode, ve.ce.ProtectedNode );
|
2013-09-23 22:04:23 +00:00
|
|
|
ve.mixinClass( ve.ce.MWExtensionNode, ve.ce.RelocatableNode );
|
2013-08-03 14:17:16 +00:00
|
|
|
ve.mixinClass( ve.ce.MWExtensionNode, ve.ce.GeneratedContentNode );
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/** */
|
2013-09-24 22:03:11 +00:00
|
|
|
ve.ce.MWExtensionNode.prototype.generateContents = function ( config ) {
|
2013-08-15 09:40:58 +00:00
|
|
|
var deferred = $.Deferred(),
|
2013-09-24 22:03:11 +00:00
|
|
|
mwData = this.getModel().getAttribute( 'mw' ),
|
|
|
|
extsrc = config && config.extsrc !== undefined ? config.extsrc : mwData.body.extsrc,
|
|
|
|
attrs = config && config.attrs || mwData.attrs,
|
2013-08-30 23:23:18 +00:00
|
|
|
extensionNode = $( document.createElement( this.getModel().getExtensionName() ) )
|
2013-09-24 22:03:11 +00:00
|
|
|
.attr( attrs ).text( extsrc );
|
2013-08-15 09:40:58 +00:00
|
|
|
|
2013-08-03 14:17:16 +00:00
|
|
|
$.ajax( {
|
|
|
|
'url': mw.util.wikiScript( 'api' ),
|
|
|
|
'data': {
|
|
|
|
'action': 'visualeditor',
|
|
|
|
'paction': 'parsefragment',
|
|
|
|
'page': mw.config.get( 'wgRelevantPageName' ),
|
2013-08-30 23:23:18 +00:00
|
|
|
'wikitext': extensionNode[0].outerHTML,
|
2013-08-03 14:17:16 +00:00
|
|
|
'token': mw.user.tokens.get( 'editToken' ),
|
|
|
|
'format': 'json'
|
|
|
|
},
|
|
|
|
'dataType': 'json',
|
|
|
|
'type': 'POST',
|
|
|
|
// Wait up to 100 seconds before giving up
|
|
|
|
'timeout': 100000,
|
|
|
|
'cache': 'false',
|
|
|
|
'success': ve.bind( this.onParseSuccess, this, deferred ),
|
|
|
|
'error': ve.bind( this.onParseError, this, deferred )
|
|
|
|
} );
|
|
|
|
return deferred.promise();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a successful response from the parser for the wikitext fragment.
|
|
|
|
*
|
|
|
|
* @param {jQuery.Deferred} deferred The Deferred object created by generateContents
|
|
|
|
* @param {Object} response Response data
|
|
|
|
*/
|
|
|
|
ve.ce.MWExtensionNode.prototype.onParseSuccess = function ( deferred, response ) {
|
|
|
|
var data = response.visualeditor, contentNodes = $( data.content ).get();
|
|
|
|
deferred.resolve( contentNodes );
|
2013-09-26 19:35:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** */
|
|
|
|
ve.ce.MWExtensionNode.prototype.afterRender = function () {
|
2013-08-15 09:43:31 +00:00
|
|
|
// Rerender after images load
|
2013-09-26 19:35:43 +00:00
|
|
|
// TODO: ignore shields, and count multiple images
|
2013-08-15 09:43:31 +00:00
|
|
|
this.$.find( 'img' ).on( 'load', ve.bind( function () {
|
|
|
|
this.emit( 'rerender' );
|
|
|
|
}, this ) );
|
2013-08-03 14:17:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle an unsuccessful response from the parser for the wikitext fragment.
|
|
|
|
*
|
|
|
|
* @param {jQuery.Deferred} deferred The promise object created by generateContents
|
|
|
|
* @param {Object} response Response data
|
|
|
|
*/
|
|
|
|
ve.ce.MWExtensionNode.prototype.onParseError = function ( deferred ) {
|
|
|
|
deferred.reject();
|
|
|
|
};
|