2013-08-03 14:17:16 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor ContentEditable MWExtensionNode class.
|
|
|
|
*
|
2014-01-05 12:05:05 +00:00
|
|
|
* @copyright 2011-2014 VisualEditor Team and others; see AUTHORS.txt
|
2013-08-03 14:17:16 +00:00
|
|
|
* @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
|
|
|
|
* @mixins ve.ce.FocusableNode
|
|
|
|
* @mixins ve.ce.GeneratedContentNode
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
*/
|
2014-05-20 11:56:37 +00:00
|
|
|
ve.ce.MWExtensionNode = function VeCeMWExtensionNode() {
|
2013-08-03 14:17:16 +00:00
|
|
|
// Mixin constructors
|
|
|
|
ve.ce.FocusableNode.call( this );
|
|
|
|
ve.ce.GeneratedContentNode.call( this );
|
|
|
|
|
2013-08-28 22:55:35 +00:00
|
|
|
// DOM changes
|
2013-11-01 19:45:59 +00:00
|
|
|
this.$element.addClass( 've-ce-mwExtensionNode' );
|
2013-08-03 14:17:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-10-11 21:44:09 +00:00
|
|
|
OO.inheritClass( ve.ce.MWExtensionNode, ve.ce.LeafNode );
|
2013-08-03 14:17:16 +00:00
|
|
|
|
2013-10-11 21:44:09 +00:00
|
|
|
OO.mixinClass( ve.ce.MWExtensionNode, ve.ce.FocusableNode );
|
|
|
|
OO.mixinClass( ve.ce.MWExtensionNode, ve.ce.GeneratedContentNode );
|
2013-08-03 14:17:16 +00:00
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/** */
|
2013-09-24 22:03:11 +00:00
|
|
|
ve.ce.MWExtensionNode.prototype.generateContents = function ( config ) {
|
2013-10-17 11:39:27 +00:00
|
|
|
var xhr,
|
|
|
|
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-12-01 17:09:07 +00:00
|
|
|
xmlDoc = ( new DOMParser() ).parseFromString( '<' + this.getModel().getExtensionName() + '/>', 'text/xml' ),
|
|
|
|
wikitext = ( new XMLSerializer() ).serializeToString(
|
|
|
|
$( xmlDoc.documentElement ).attr( attrs ).text( extsrc )[0]
|
|
|
|
);
|
2013-08-15 09:40:58 +00:00
|
|
|
|
2014-05-23 09:57:58 +00:00
|
|
|
xhr = ve.init.target.constructor.static.apiRequest( {
|
2014-01-05 04:44:13 +00:00
|
|
|
'action': 'visualeditor',
|
|
|
|
'paction': 'parsefragment',
|
|
|
|
'page': mw.config.get( 'wgRelevantPageName' ),
|
2014-05-15 16:12:43 +00:00
|
|
|
'wikitext': wikitext
|
2014-01-05 04:44:13 +00:00
|
|
|
}, { 'type': 'POST' } )
|
2013-10-17 11:39:27 +00:00
|
|
|
.done( ve.bind( this.onParseSuccess, this, deferred ) )
|
|
|
|
.fail( ve.bind( this.onParseError, this, deferred ) );
|
|
|
|
|
|
|
|
return deferred.promise( { abort: xhr.abort } );
|
2013-08-03 14:17:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 ) {
|
2013-11-01 19:45:59 +00:00
|
|
|
var data = response.visualeditor, contentNodes = this.$( data.content ).get();
|
2013-08-03 14:17:16 +00:00
|
|
|
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-11-01 19:45:59 +00:00
|
|
|
this.$element.find( 'img' ).on( 'load', ve.bind( function () {
|
2013-08-15 09:43:31 +00:00
|
|
|
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();
|
|
|
|
};
|
2014-05-20 11:56:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ContentEditable MediaWiki inline extension node.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @abstract
|
|
|
|
* @extends ve.ce.LeafNode
|
|
|
|
* @mixins ve.ce.MWExtensionNode
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {ve.dm.MWInlineExtensionNode} model Model to observe
|
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
*/
|
|
|
|
ve.ce.MWInlineExtensionNode = function VeCeMWInlineExtensionNode( model, config ) {
|
|
|
|
// Parent constructor
|
|
|
|
ve.ce.LeafNode.call( this, model, config );
|
|
|
|
|
|
|
|
// Mixin constructors
|
|
|
|
ve.ce.MWExtensionNode.call( this );
|
|
|
|
|
|
|
|
// DOM changes
|
|
|
|
this.$element.addClass( 've-ce-mwInlineExtensionNode' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.ce.MWInlineExtensionNode, ve.ce.LeafNode );
|
|
|
|
|
|
|
|
OO.mixinClass( ve.ce.MWInlineExtensionNode, ve.ce.MWExtensionNode );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ContentEditable MediaWiki block extension node.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @abstract
|
|
|
|
* @extends ve.ce.BranchNode
|
|
|
|
* @mixins ve.ce.MWExtensionNode
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {ve.dm.MWBlockExtensionNode} model Model to observe
|
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
*/
|
|
|
|
ve.ce.MWBlockExtensionNode = function VeCeMWBlockExtensionNode( model, config ) {
|
|
|
|
// Parent constructor
|
|
|
|
ve.ce.BranchNode.call( this, model, config );
|
|
|
|
|
|
|
|
// Mixin constructors
|
|
|
|
ve.ce.MWExtensionNode.call( this );
|
|
|
|
|
|
|
|
// DOM changes
|
|
|
|
this.$element.addClass( 've-ce-mwBlockExtensionNode' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.ce.MWBlockExtensionNode, ve.ce.BranchNode );
|
|
|
|
|
|
|
|
OO.mixinClass( ve.ce.MWBlockExtensionNode, ve.ce.MWExtensionNode );
|