mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-12-01 17:36:35 +00:00
db9f941fa6
Objectives: * Rename this.$ to this.$element * Rename this.$$ to this.$ * Get rid of the need to use this.frame.$$ * Rename OO.ui.Element.get$$ to OO.ui.Element.getJQuery Changes: (using Sublime Text regex patterns) * Replace "get$$" with "getJQuery" * Replace "\.(\$)([^\$a-zA-Z])" with ".$element$2" * Replace "\.(\$\$)" with ".$" * Replace "'$$'" with "'$'" * Set this.$ to null in constructor of OO.ui.Window * Set this.$ to this.frame.$ in initialize method of OO.ui.Window * Replace "\.(frame.\$)([^\$a-zA-Z])" with ".\$$2" Bonus: * Use this.$() in a bunch of places where $() was erroneously used Change-Id: If3d870124ab8d10f8223532cda95c2b2b075db94
115 lines
3.4 KiB
JavaScript
115 lines
3.4 KiB
JavaScript
/*!
|
|
* 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.
|
|
*
|
|
* Configuration options for .update():
|
|
* - 'extsrc': override the contents of the tag (string)
|
|
* - 'attrs': override the attributes of the tag (object)
|
|
*
|
|
* @class
|
|
* @abstract
|
|
* @extends ve.ce.LeafNode
|
|
* @mixins ve.ce.FocusableNode
|
|
* @mixins ve.ce.ProtectedNode
|
|
* @mixins ve.ce.RelocatableNode
|
|
* @mixins ve.ce.GeneratedContentNode
|
|
*
|
|
* @constructor
|
|
* @param {ve.dm.MWExtensionNode} model Model to observe
|
|
* @param {Object} [config] Configuration options
|
|
*/
|
|
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 );
|
|
ve.ce.RelocatableNode.call( this );
|
|
ve.ce.GeneratedContentNode.call( this );
|
|
|
|
// DOM changes
|
|
this.$element.addClass( 've-ce-mwExtensionNode' );
|
|
};
|
|
|
|
/* Inheritance */
|
|
|
|
OO.inheritClass( ve.ce.MWExtensionNode, ve.ce.LeafNode );
|
|
|
|
OO.mixinClass( ve.ce.MWExtensionNode, ve.ce.FocusableNode );
|
|
OO.mixinClass( ve.ce.MWExtensionNode, ve.ce.ProtectedNode );
|
|
OO.mixinClass( ve.ce.MWExtensionNode, ve.ce.RelocatableNode );
|
|
OO.mixinClass( ve.ce.MWExtensionNode, ve.ce.GeneratedContentNode );
|
|
|
|
/* Methods */
|
|
|
|
/** */
|
|
ve.ce.MWExtensionNode.prototype.generateContents = function ( config ) {
|
|
var xhr,
|
|
deferred = $.Deferred(),
|
|
mwData = this.getModel().getAttribute( 'mw' ),
|
|
extsrc = config && config.extsrc !== undefined ? config.extsrc : mwData.body.extsrc,
|
|
attrs = config && config.attrs || mwData.attrs,
|
|
extensionNode = $( document.createElement( this.getModel().getExtensionName() ) )
|
|
.attr( attrs ).text( extsrc );
|
|
|
|
xhr = $.ajax( {
|
|
'url': mw.util.wikiScript( 'api' ),
|
|
'data': {
|
|
'action': 'visualeditor',
|
|
'paction': 'parsefragment',
|
|
'page': mw.config.get( 'wgRelevantPageName' ),
|
|
'wikitext': extensionNode[0].outerHTML,
|
|
'token': mw.user.tokens.get( 'editToken' ),
|
|
'format': 'json'
|
|
},
|
|
'dataType': 'json',
|
|
'type': 'POST',
|
|
// Wait up to 100 seconds before giving up
|
|
'timeout': 100000,
|
|
'cache': 'false'
|
|
} )
|
|
.done( ve.bind( this.onParseSuccess, this, deferred ) )
|
|
.fail( ve.bind( this.onParseError, this, deferred ) );
|
|
|
|
return deferred.promise( { abort: xhr.abort } );
|
|
};
|
|
|
|
/**
|
|
* 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 = this.$( data.content ).get();
|
|
deferred.resolve( contentNodes );
|
|
};
|
|
|
|
/** */
|
|
ve.ce.MWExtensionNode.prototype.afterRender = function () {
|
|
// Rerender after images load
|
|
// TODO: ignore shields, and count multiple images
|
|
this.$element.find( 'img' ).on( 'load', ve.bind( function () {
|
|
this.emit( 'rerender' );
|
|
}, this ) );
|
|
};
|
|
|
|
/**
|
|
* 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();
|
|
};
|