mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-15 18:39:52 +00:00
ab7d6bf082
* Commands for Sublime: Find*: "(\* @[a-z]+) ([^{].*) \{(.*)\}" Replace: "$1 {$3} $2" Save all && Close all Find: " function(" Replace: " function (" Save all && Close all Find: "Intialization" Replace: "Initialization" Save all && Close all * Consistent use of types (documented in CODING.rm): - Merged {Integer} into {Number}. - Merged {DOM Node} into {DOMElement}. * Remove work-around /*jshint newcap: false */ from ve.js Calling Object() as a function to to use the internal toObject no longer throws a newcap warning in JSHint. It only does that normal functions now . (e.g. var a = Cap(); or var a = new uncap();) * Add missing annotations (@static, @method, ..). * Remove unused variables * Remove null-assignments to variables that should just be undefined. There's a few variables explicitly set to null whereas they are set a few lines under and not used otherwise (e.g. 'tx' in ve.ce.Surface.prototype.onPaste) Change-Id: I0721a08f8ecd93c25595aedaa1aadb0e08b83799
256 lines
7.1 KiB
JavaScript
256 lines
7.1 KiB
JavaScript
/*global mw */
|
|
|
|
/**
|
|
* VisualEditor MediaWiki initialization Target class.
|
|
*
|
|
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
*/
|
|
|
|
/**
|
|
* MediaWiki target.
|
|
*
|
|
* @class
|
|
* @constructor
|
|
* @extends {ve.EventEmitter}
|
|
* @param {String} pageName Name of target page
|
|
*/
|
|
ve.init.mw.Target = function VeInitMwTarget( pageName, oldId ) {
|
|
// Parent constructor
|
|
ve.EventEmitter.call( this );
|
|
|
|
// Properties
|
|
this.pageName = pageName;
|
|
this.oldId = oldId;
|
|
this.editToken = mw.user.tokens.get( 'editToken' );
|
|
this.apiUrl = mw.util.wikiScript( 'api' );
|
|
this.modules = ['ext.visualEditor.core', 'ext.visualEditor.specialMessages']
|
|
.concat(
|
|
window.devicePixelRatio > 1 ?
|
|
['ext.visualEditor.viewPageTarget.icons-vector', 'ext.visualEditor.icons-vector'] :
|
|
['ext.visualEditor.viewPageTarget.icons-raster', 'ext.visualEditor.icons-raster']
|
|
);
|
|
this.loading = false;
|
|
this.saving = false;
|
|
this.dom = null;
|
|
this.isMobileDevice = (
|
|
'ontouchstart' in window ||
|
|
( window.DocumentTouch && document instanceof window.DocumentTouch )
|
|
);
|
|
};
|
|
|
|
/* Inheritance */
|
|
|
|
ve.inheritClass( ve.init.mw.Target, ve.EventEmitter );
|
|
|
|
/* Static Methods */
|
|
|
|
/**
|
|
* Handle response to a successful load request.
|
|
*
|
|
* This method is called within the context of a target instance. If successful the DOM from the
|
|
* server will be parsed, stored in {this.dom} and then {ve.init.mw.Target.onReady} will be called once
|
|
* the modules are ready.
|
|
*
|
|
* @static
|
|
* @method
|
|
* @param {Object} response XHR Response object
|
|
* @param {String} status Text status message
|
|
* @emits loadError (null, message, null)
|
|
*/
|
|
ve.init.mw.Target.onLoad = function ( response ) {
|
|
var data = response['ve-parsoid'];
|
|
if ( !data ) {
|
|
this.loading = false;
|
|
this.emit( 'loadError', null, 'Invalid response from server', null );
|
|
} else if ( typeof data.result === 'error' ) {
|
|
this.loading = false;
|
|
this.emit( 'loadError', null, 'Server error', null );
|
|
} else if ( typeof data.parsed !== 'string' ) {
|
|
this.loading = false;
|
|
this.emit( 'loadError', null, 'No HTML content in response from server', null );
|
|
} else {
|
|
this.dom = $( '<div>' ).html( data.parsed )[0];
|
|
// Everything worked, the page was loaded, continue as soon as the module is ready
|
|
mw.loader.using( this.modules, ve.bind( ve.init.mw.Target.onReady, this ) );
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Handle both DOM and modules being loaded and ready.
|
|
*
|
|
* This method is called within the context of a target instance. After the load event is emitted
|
|
* this.dom is cleared, allowing it to be garbage collected.
|
|
*
|
|
* @static
|
|
* @method
|
|
* @emits load (dom)
|
|
*/
|
|
ve.init.mw.Target.onReady = function () {
|
|
this.loading = false;
|
|
this.emit( 'load', this.dom );
|
|
// Release DOM data
|
|
this.dom = null;
|
|
};
|
|
|
|
/**
|
|
* Handle response to a successful load request.
|
|
*
|
|
* This method is called within the context of a target instance.
|
|
*
|
|
* @static
|
|
* @method
|
|
* @param {Object} response XHR Response object
|
|
* @param {String} status Text status message
|
|
* @param {Mixed} error Thrown exception or HTTP error string
|
|
* @emits loadError (response, text, exception)
|
|
*/
|
|
ve.init.mw.Target.onLoadError = function ( response, text, exception ) {
|
|
this.loading = false;
|
|
this.emit( 'loadError', response, text, exception );
|
|
};
|
|
|
|
/**
|
|
* Handle response to a successful save request.
|
|
*
|
|
* This method is called within the context of a target instance.
|
|
*
|
|
* @static
|
|
* @method
|
|
* @param {Object} response XHR Response object
|
|
* @param {String} status Text status message
|
|
* @emits save (html)
|
|
* @emits saveError (null, message, null)
|
|
*/
|
|
ve.init.mw.Target.onSave = function ( response ) {
|
|
this.saving = false;
|
|
var data = response['ve-parsoid'];
|
|
if ( !data ) {
|
|
this.emit( 'saveError', null, 'Invalid response from server', null );
|
|
} else if ( data.result !== 'success' ) {
|
|
this.emit( 'saveError', null, 'Unsuccessful request: ' + data.result, null );
|
|
} else if ( typeof data.content !== 'string' ) {
|
|
this.emit( 'saveError', null, 'Invalid HTML content in response from server', null );
|
|
} else {
|
|
this.emit( 'save', data.content );
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Handle response to a successful save request.
|
|
*
|
|
* This method is called within the context of a target instance.
|
|
*
|
|
* @static
|
|
* @method
|
|
* @param {Object} data HTTP Response object
|
|
* @param {String} status Text status message
|
|
* @param {Mixed} error Thrown exception or HTTP error string
|
|
* @emits saveError (response, status, error)
|
|
*/
|
|
ve.init.mw.Target.onSaveError = function ( response, status, error ) {
|
|
this.saving = false;
|
|
this.emit( 'saveError', response, status, error );
|
|
};
|
|
|
|
/* Methods */
|
|
|
|
/**
|
|
* Gets DOM from Parsoid API.
|
|
*
|
|
* This method performs an asynchronous action and uses a callback function to handle the result.
|
|
*
|
|
* @example
|
|
* target.loadDom(
|
|
* function ( error, dom ) {
|
|
* // Handle errors and do something with the loaded DOM
|
|
* }
|
|
* );
|
|
*
|
|
* @method
|
|
* @param {Function} callback Function to call when complete, accepts error and dom arguments
|
|
* @returns {Boolean} Loading is now in progress
|
|
*/
|
|
ve.init.mw.Target.prototype.load = function () {
|
|
// Prevent duplicate requests
|
|
if ( this.loading ) {
|
|
return false;
|
|
}
|
|
// Start loading the module immediately
|
|
mw.loader.load( this.modules );
|
|
// Load DOM
|
|
this.loading = true;
|
|
$.ajax( {
|
|
'url': this.apiUrl,
|
|
'data': {
|
|
'action': 've-parsoid',
|
|
'paction': 'parse',
|
|
'page': this.pageName,
|
|
'oldid': this.oldId,
|
|
'format': 'json'
|
|
},
|
|
'dataType': 'json',
|
|
'type': 'GET',
|
|
// Wait up to 10 seconds before giving up
|
|
'timeout': 10000,
|
|
'cache': 'false',
|
|
'success': ve.bind( ve.init.mw.Target.onLoad, this ),
|
|
'error': ve.bind( ve.init.mw.Target.onLoadError, this )
|
|
} );
|
|
return true;
|
|
};
|
|
|
|
/**
|
|
* Posts DOM to Parsoid API.
|
|
*
|
|
* This method performs an asynchronous action and uses a callback function to handle the result.
|
|
*
|
|
* @example
|
|
* target.saveDom(
|
|
* dom,
|
|
* { 'summary': 'test', 'minor': true, 'watch': false },
|
|
* function ( error, html ) {
|
|
* // Handle errors and do something with the rendered HTML
|
|
* }
|
|
* );
|
|
*
|
|
* @method
|
|
* @param {HTMLElement} dom DOM to save
|
|
* @param {Object} options Saving options
|
|
* - {String} summary Edit summary
|
|
* - {Boolean} minor Edit is a minor edit
|
|
* - {Boolean} watch Watch this page
|
|
* @param {Function} callback Function to call when complete, accepts error and html arguments
|
|
* @returns {Boolean} Saving is now in progress
|
|
*/
|
|
ve.init.mw.Target.prototype.save = function ( dom, options ) {
|
|
// Prevent duplicate requests
|
|
if ( this.saving ) {
|
|
return false;
|
|
}
|
|
// Save DOM
|
|
this.saving = true;
|
|
$.ajax( {
|
|
'url': this.apiUrl,
|
|
'data': {
|
|
'format': 'json',
|
|
'action': 've-parsoid',
|
|
'paction': 'save',
|
|
'page': this.pageName,
|
|
'oldid': this.oldId,
|
|
'html': $( dom ).html(),
|
|
'token': this.editToken,
|
|
'summary': options.summary,
|
|
'minor': options.minor,
|
|
'watch': options.watch
|
|
},
|
|
'dataType': 'json',
|
|
'type': 'POST',
|
|
// Wait up to 10 seconds before giving up
|
|
'timeout': 10000,
|
|
'success': ve.bind( ve.init.mw.Target.onSave, this ),
|
|
'error': ve.bind( ve.init.mw.Target.onSaveError, this )
|
|
} );
|
|
return true;
|
|
};
|