2012-07-19 03:40:49 +00:00
|
|
|
/*global mw */
|
|
|
|
|
2012-07-19 00:11:26 +00:00
|
|
|
/**
|
2012-07-20 23:59:59 +00:00
|
|
|
* VisualEditor MediaWiki initialization Target class.
|
2012-07-19 21:25:16 +00:00
|
|
|
*
|
2012-07-19 00:11:26 +00:00
|
|
|
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
2012-06-11 06:54:41 +00:00
|
|
|
/**
|
2012-07-20 23:59:59 +00:00
|
|
|
* MediaWiki target.
|
2012-06-11 06:54:41 +00:00
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
2012-06-19 04:46:49 +00:00
|
|
|
* @param {String} pageName Name of target page
|
2012-06-11 06:54:41 +00:00
|
|
|
*/
|
2012-08-01 00:49:04 +00:00
|
|
|
ve.init.mw.Target = function ( pageName ) {
|
2012-06-18 20:12:32 +00:00
|
|
|
// Inheritance
|
|
|
|
ve.EventEmitter.call( this );
|
|
|
|
|
2012-06-11 06:54:41 +00:00
|
|
|
// Properties
|
2012-06-19 04:46:49 +00:00
|
|
|
this.pageName = pageName;
|
2012-06-11 06:54:41 +00:00
|
|
|
this.editToken = mw.user.tokens.get( 'editToken' );
|
|
|
|
this.apiUrl = mw.util.wikiScript( 'api' );
|
2012-06-21 20:39:27 +00:00
|
|
|
this.modules = ['ext.visualEditor.core', 'ext.visualEditor.specialMessages'];
|
2012-06-18 20:12:32 +00:00
|
|
|
this.loading = false;
|
|
|
|
this.saving = false;
|
|
|
|
this.dom = null;
|
2012-06-11 06:54:41 +00:00
|
|
|
this.isMobileDevice = (
|
|
|
|
'ontouchstart' in window ||
|
2012-07-19 03:40:49 +00:00
|
|
|
( window.DocumentTouch && document instanceof window.DocumentTouch )
|
2012-06-11 06:54:41 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2012-06-18 20:12:32 +00:00
|
|
|
/* 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
|
2012-07-20 23:59:59 +00:00
|
|
|
* server will be parsed, stored in {this.dom} and then {ve.init.mw.Target.onReady} will be called once
|
2012-06-18 20:12:32 +00:00
|
|
|
* the modules are ready.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @method
|
|
|
|
* @param {Object} response XHR Response object
|
|
|
|
* @param {String} status Text status message
|
2012-06-19 06:07:54 +00:00
|
|
|
* @emits loadError (null, message, null)
|
2012-06-18 20:12:32 +00:00
|
|
|
*/
|
2012-08-01 00:49:04 +00:00
|
|
|
ve.init.mw.Target.onLoad = function ( response, status ) {
|
2012-06-18 20:12:32 +00:00
|
|
|
var data = response['ve-parsoid'];
|
|
|
|
if ( !data ) {
|
|
|
|
this.loading = false;
|
2012-06-19 06:07:54 +00:00
|
|
|
this.emit( 'loadError', null, 'Invalid response from server', null );
|
2012-06-19 19:15:41 +00:00
|
|
|
} else if ( typeof data.result === 'error' ) {
|
|
|
|
this.loading = false;
|
|
|
|
this.emit( 'loadError', null, 'Server error', null );
|
2012-06-18 20:12:32 +00:00
|
|
|
} else if ( typeof data.parsed !== 'string' ) {
|
|
|
|
this.loading = false;
|
2012-06-19 19:15:41 +00:00
|
|
|
this.emit( 'loadError', null, 'No HTML content in response from server', null );
|
2012-06-18 20:12:32 +00:00
|
|
|
} else {
|
2012-08-01 00:49:04 +00:00
|
|
|
this.dom = $( '<div>' ).html( data.parsed )[0];
|
2012-06-18 20:12:32 +00:00
|
|
|
// Everything worked, the page was loaded, continue as soon as the module is ready
|
2012-08-11 08:14:56 +00:00
|
|
|
mw.loader.using( this.modules, ve.bind( ve.init.mw.Target.onReady, this ) );
|
2012-06-18 20:12:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
*/
|
2012-08-01 00:49:04 +00:00
|
|
|
ve.init.mw.Target.onReady = function () {
|
2012-06-18 20:12:32 +00:00
|
|
|
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
|
2012-06-19 06:07:54 +00:00
|
|
|
* @emits loadError (response, text, exception)
|
2012-06-18 20:12:32 +00:00
|
|
|
*/
|
2012-08-01 00:49:04 +00:00
|
|
|
ve.init.mw.Target.onLoadError = function ( response, text, exception ) {
|
2012-06-18 20:12:32 +00:00
|
|
|
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)
|
2012-06-19 06:07:54 +00:00
|
|
|
* @emits saveError (null, message, null)
|
2012-06-18 20:12:32 +00:00
|
|
|
*/
|
2012-08-01 00:49:04 +00:00
|
|
|
ve.init.mw.Target.onSave = function ( response, status ) {
|
2012-06-18 20:12:32 +00:00
|
|
|
this.saving = false;
|
|
|
|
var data = response['ve-parsoid'];
|
2012-06-19 00:37:12 +00:00
|
|
|
if ( !data ) {
|
2012-06-19 06:07:54 +00:00
|
|
|
this.emit( 'saveError', null, 'Invalid response from server', null );
|
2012-06-18 20:12:32 +00:00
|
|
|
} else if ( data.result !== 'success' ) {
|
2012-06-19 06:07:54 +00:00
|
|
|
this.emit( 'saveError', null, 'Unsuccessful request: ' + data.result, null );
|
2012-06-18 20:12:32 +00:00
|
|
|
} else if ( typeof data.content !== 'string' ) {
|
2012-06-19 06:07:54 +00:00
|
|
|
this.emit( 'saveError', null, 'Invalid HTML content in response from server', null );
|
2012-06-18 20:12:32 +00:00
|
|
|
} 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
|
2012-06-19 06:07:54 +00:00
|
|
|
* @emits saveError (response, status, error)
|
2012-06-18 20:12:32 +00:00
|
|
|
*/
|
2012-08-01 00:49:04 +00:00
|
|
|
ve.init.mw.Target.onSaveError = function ( response, status, error ) {
|
2012-06-18 20:12:32 +00:00
|
|
|
this.saving = false;
|
|
|
|
this.emit( 'saveError', response, status, error );
|
|
|
|
};
|
|
|
|
|
2012-06-11 06:54:41 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets DOM from Parsoid API.
|
|
|
|
*
|
|
|
|
* This method performs an asynchronous action and uses a callback function to handle the result.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* target.loadDom(
|
2012-08-07 01:50:44 +00:00
|
|
|
* function ( error, dom ) {
|
2012-06-11 06:54:41 +00:00
|
|
|
* // 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
|
|
|
|
*/
|
Make use of new jshint options
* Restricting "camelcase":
No changes, we were passing all of these already
* Explicitly unrestricting "forin" and "plusplus"
These are off by default in node-jshint, but some distro of jshint
and editors that use their own wrapper around jshint instead of
node-jshint (Eclipse?) may have different defaults. Therefor
setting them to false explicitly. This also serves as a reminder
for the future so we'll always know we don't pass that, in case
we would want to change that.
* Fix order ("quotemark" before "regexp")
* Restricting "unused"
We're not passing all of this, which is why I've set it to false
for now. But I did put it in .jshintrc as placeholder.
I've fixed most of them, there's some left where there is no clean
solution.
* While at it fix a few issues:
- Unused variables ($target, $window)
- Bad practices (using jQuery context for find instead of creation)
- Redundant /*global */ comments
- Parameters that are not used and don't have documentation either
- Lines longer than 100 chars @ 4 spaces/tab
* Note:
- ve.ce.Surface.prototype.onChange takes two arguments but never
uses the former. And even the second one can be null/undefined.
Aside from that, the .change() function emits
another event for the transaction already. Looks like this
should be refactored a bit, two more separated events probably
or one that is actually used better.
- Also cleaned up a lot of comments, some of which were missing,
others were incorrect
- Reworked the contentChange event so we are no longer using the
word new as an object key; expanded a complex object into multiple
arguments being passed through the event to make it easier to work
with and document
Change-Id: I8490815a508c6c379d5f9a743bb4aefd14576aa6
2012-08-07 06:02:18 +00:00
|
|
|
ve.init.mw.Target.prototype.load = function () {
|
2012-06-11 06:54:41 +00:00
|
|
|
// Prevent duplicate requests
|
2012-06-18 20:12:32 +00:00
|
|
|
if ( this.loading ) {
|
2012-06-11 06:54:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Start loading the module immediately
|
2012-06-12 01:21:31 +00:00
|
|
|
mw.loader.load( this.modules );
|
2012-06-11 06:54:41 +00:00
|
|
|
// Load DOM
|
2012-06-18 20:12:32 +00:00
|
|
|
this.loading = true;
|
2012-06-11 06:54:41 +00:00
|
|
|
$.ajax( {
|
|
|
|
'url': this.apiUrl,
|
|
|
|
'data': {
|
|
|
|
'action': 've-parsoid',
|
|
|
|
'paction': 'parse',
|
2012-06-19 04:46:49 +00:00
|
|
|
'page': this.pageName,
|
2012-06-11 06:54:41 +00:00
|
|
|
'format': 'json'
|
|
|
|
},
|
|
|
|
'dataType': 'json',
|
|
|
|
'type': 'GET',
|
2012-06-19 19:15:41 +00:00
|
|
|
// Wait up to 10 seconds before giving up
|
|
|
|
'timeout': 10000,
|
2012-06-11 06:54:41 +00:00
|
|
|
'cache': 'false',
|
2012-08-11 08:14:56 +00:00
|
|
|
'success': ve.bind( ve.init.mw.Target.onLoad, this ),
|
|
|
|
'error': ve.bind( ve.init.mw.Target.onLoadError, this )
|
2012-06-11 06:54:41 +00:00
|
|
|
} );
|
|
|
|
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 },
|
2012-08-07 01:50:44 +00:00
|
|
|
* function ( error, html ) {
|
2012-06-11 06:54:41 +00:00
|
|
|
* // Handle errors and do something with the rendered HTML
|
|
|
|
* }
|
|
|
|
* );
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {HTMLElement} dom DOM to save
|
|
|
|
* @param {Object} options Saving options
|
Make use of new jshint options
* Restricting "camelcase":
No changes, we were passing all of these already
* Explicitly unrestricting "forin" and "plusplus"
These are off by default in node-jshint, but some distro of jshint
and editors that use their own wrapper around jshint instead of
node-jshint (Eclipse?) may have different defaults. Therefor
setting them to false explicitly. This also serves as a reminder
for the future so we'll always know we don't pass that, in case
we would want to change that.
* Fix order ("quotemark" before "regexp")
* Restricting "unused"
We're not passing all of this, which is why I've set it to false
for now. But I did put it in .jshintrc as placeholder.
I've fixed most of them, there's some left where there is no clean
solution.
* While at it fix a few issues:
- Unused variables ($target, $window)
- Bad practices (using jQuery context for find instead of creation)
- Redundant /*global */ comments
- Parameters that are not used and don't have documentation either
- Lines longer than 100 chars @ 4 spaces/tab
* Note:
- ve.ce.Surface.prototype.onChange takes two arguments but never
uses the former. And even the second one can be null/undefined.
Aside from that, the .change() function emits
another event for the transaction already. Looks like this
should be refactored a bit, two more separated events probably
or one that is actually used better.
- Also cleaned up a lot of comments, some of which were missing,
others were incorrect
- Reworked the contentChange event so we are no longer using the
word new as an object key; expanded a complex object into multiple
arguments being passed through the event to make it easier to work
with and document
Change-Id: I8490815a508c6c379d5f9a743bb4aefd14576aa6
2012-08-07 06:02:18 +00:00
|
|
|
* - {String} summary Edit summary
|
|
|
|
* - {Boolean} minor Edit is a minor edit
|
|
|
|
* - {Boolean} watch Watch this page
|
2012-06-11 06:54:41 +00:00
|
|
|
* @param {Function} callback Function to call when complete, accepts error and html arguments
|
|
|
|
* @returns {Boolean} Saving is now in progress
|
|
|
|
*/
|
Make use of new jshint options
* Restricting "camelcase":
No changes, we were passing all of these already
* Explicitly unrestricting "forin" and "plusplus"
These are off by default in node-jshint, but some distro of jshint
and editors that use their own wrapper around jshint instead of
node-jshint (Eclipse?) may have different defaults. Therefor
setting them to false explicitly. This also serves as a reminder
for the future so we'll always know we don't pass that, in case
we would want to change that.
* Fix order ("quotemark" before "regexp")
* Restricting "unused"
We're not passing all of this, which is why I've set it to false
for now. But I did put it in .jshintrc as placeholder.
I've fixed most of them, there's some left where there is no clean
solution.
* While at it fix a few issues:
- Unused variables ($target, $window)
- Bad practices (using jQuery context for find instead of creation)
- Redundant /*global */ comments
- Parameters that are not used and don't have documentation either
- Lines longer than 100 chars @ 4 spaces/tab
* Note:
- ve.ce.Surface.prototype.onChange takes two arguments but never
uses the former. And even the second one can be null/undefined.
Aside from that, the .change() function emits
another event for the transaction already. Looks like this
should be refactored a bit, two more separated events probably
or one that is actually used better.
- Also cleaned up a lot of comments, some of which were missing,
others were incorrect
- Reworked the contentChange event so we are no longer using the
word new as an object key; expanded a complex object into multiple
arguments being passed through the event to make it easier to work
with and document
Change-Id: I8490815a508c6c379d5f9a743bb4aefd14576aa6
2012-08-07 06:02:18 +00:00
|
|
|
ve.init.mw.Target.prototype.save = function ( dom, options ) {
|
2012-06-11 06:54:41 +00:00
|
|
|
// Prevent duplicate requests
|
2012-06-18 20:12:32 +00:00
|
|
|
if ( this.saving ) {
|
2012-06-11 06:54:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Save DOM
|
2012-06-18 20:12:32 +00:00
|
|
|
this.saving = true;
|
2012-06-11 06:54:41 +00:00
|
|
|
$.ajax( {
|
|
|
|
'url': this.apiUrl,
|
|
|
|
'data': {
|
|
|
|
'format': 'json',
|
|
|
|
'action': 've-parsoid',
|
|
|
|
'paction': 'save',
|
2012-06-19 04:46:49 +00:00
|
|
|
'page': this.pageName,
|
2012-06-11 06:54:41 +00:00
|
|
|
'html': $( dom ).html(),
|
|
|
|
'token': this.editToken,
|
|
|
|
'summary': options.summary,
|
|
|
|
'minor': options.minor,
|
|
|
|
'watch': options.watch
|
|
|
|
},
|
|
|
|
'dataType': 'json',
|
|
|
|
'type': 'POST',
|
2012-06-19 19:15:41 +00:00
|
|
|
// Wait up to 10 seconds before giving up
|
|
|
|
'timeout': 10000,
|
2012-08-11 08:14:56 +00:00
|
|
|
'success': ve.bind( ve.init.mw.Target.onSave, this ),
|
|
|
|
'error': ve.bind( ve.init.mw.Target.onSaveError, this )
|
2012-06-11 06:54:41 +00:00
|
|
|
} );
|
|
|
|
return true;
|
|
|
|
};
|
2012-06-18 20:12:32 +00:00
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2012-07-20 23:59:59 +00:00
|
|
|
ve.extendClass( ve.init.mw.Target, ve.EventEmitter );
|