mediawiki-extensions-Visual.../modules/ve-mw/ce/nodes/ve.ce.MWExtensionNode.js
Timo Tijhof 25d00cc777 build: Update jscs and jshint
jshint:
* Update to grunt-contrib-jshint v0.10.0 (jshint v2.5.0).
* Remove coding style options covered by jscs.
* Enable new option "freeze" (prohibits changing native prototypes).
  http://www.jshint.com/blog/new-in-jshint-oct-2013/#option-freeze
* Re-order to match http://www.jshint.com/docs/options/

jscs:
* Update to grunt-jscs-checker v0.4.4 (jscs v1.4.5).
* Format .jscsrc file in a more spacious way and order the
  properties less arbitrarily (using the jscs's readme order).
* Enforce more details of our coding style
* Get rid of the unsable "sticky" operator rules which have been
  deprecated in favour of using other rules instead that are able
  to enforce this more accurately.
  - disallowLeftStickedOperators: Remove deprecated rule.
    * Ternary covered by requireSpacesInConditionalExpression.
    * Rest covered by requireSpace{Before,After}BinaryOperators.

  - requireLeftStickedOperators: Remove deprecated rule.
     * Comma covered by disallowSpaceBeforeBinaryOperators.

  - requireRightStickedOperators: Remove deprecated rule.
    * Logical not (!) covered by disallowSpaceAfterPrefixUnaryOperators.

See also If46b94ce1, Ib731f11b1 and I0b0cadbc5 in oojs/core.

Also:
* Update grunt-contrib-watch to latest upstream version.
  Change log at https://github.com/gruntjs/grunt-contrib-watch/blob/v0.6.1/CHANGELOG#L1-L17

Change-Id: I6c5a34afea8b05a3dca617897c192594df06ca90
2014-05-15 16:52:34 +00:00

109 lines
3.4 KiB
JavaScript

/*!
* VisualEditor ContentEditable MWExtensionNode class.
*
* @copyright 2011-2014 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 );
ve.ce.ClickableNode.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 );
OO.mixinClass( ve.ce.MWExtensionNode, ve.ce.ClickableNode );
/* 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,
xmlDoc = ( new DOMParser() ).parseFromString( '<' + this.getModel().getExtensionName() + '/>', 'text/xml' ),
wikitext = ( new XMLSerializer() ).serializeToString(
$( xmlDoc.documentElement ).attr( attrs ).text( extsrc )[0]
);
xhr = ve.init.mw.Target.static.apiRequest( {
'action': 'visualeditor',
'paction': 'parsefragment',
'page': mw.config.get( 'wgRelevantPageName' ),
'wikitext': wikitext
}, { 'type': 'POST' } )
.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();
};