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
|
Object management: Object create/inherit/clone utilities
* For the most common case:
- replace ve.extendClass with ve.inheritClass (chose slightly
different names to detect usage of the old/new one, and I
like 'inherit' better).
- move it up to below the constructor, see doc block for why.
* Cases where more than 2 arguments were passed to
ve.extendClass are handled differently depending on the case.
In case of a longer inheritance tree, the other arguments
could be omitted (like in "ve.ce.FooBar, ve.FooBar,
ve.Bar". ve.ce.FooBar only needs to inherit from ve.FooBar,
because ve.ce.FooBar inherits from ve.Bar).
In the case of where it previously had two mixins with
ve.extendClass(), either one becomes inheritClass and one
a mixin, both to mixinClass().
No visible changes should come from this commit as the
instances still all have the same visible properties in the
end. No more or less than before.
* Misc.:
- Be consistent in calling parent constructors in the
same order as the inheritance.
- Add missing @extends and @param documentation.
- Replace invalid {Integer} type hint with {Number}.
- Consistent doc comments order:
@class, @abstract, @constructor, @extends, @params.
- Fix indentation errors
A fairly common mistake was a superfluous space before the
identifier on the assignment line directly below the
documentation comment.
$ ack "^ [^*]" --js modules/ve
- Typo "Inhertiance" -> "Inheritance".
- Replacing the other confusing comment "Inheritance" (inside
the constructor) with "Parent constructor".
- Add missing @abstract for ve.ui.Tool.
- Corrected ve.FormatDropdownTool to ve.ui.FormatDropdownTool.js
- Add function names to all @constructor functions. Now that we
have inheritance it is important and useful to have these
functions not be anonymous.
Example of debug shot: http://cl.ly/image/1j3c160w3D45
Makes the difference between
< documentNode;
> ve_dm_DocumentNode
...
: ve_dm_BranchNode
...
: ve_dm_Node
...
: ve_dm_Node
...
: Object
...
without names (current situation):
< documentNode;
> Object
...
: Object
...
: Object
...
: Object
...
: Object
...
though before this commit, it really looks like this
(flattened since ve.extendClass really did a mixin):
< documentNode;
> Object
...
...
...
Pattern in Sublime (case-sensitive) to find nameless
constructor functions:
"^ve\..*\.([A-Z])([^\.]+) = function \("
Change-Id: Iab763954fb8cf375900d7a9a92dec1c755d5407e
2012-09-05 06:07:47 +00:00
|
|
|
* @extends {ve.EventEmitter}
|
2012-06-19 04:46:49 +00:00
|
|
|
* @param {String} pageName Name of target page
|
2012-12-04 21:04:19 +00:00
|
|
|
* @param {Number} [revision] Revision ID
|
2012-06-11 06:54:41 +00:00
|
|
|
*/
|
2012-12-04 21:04:19 +00:00
|
|
|
ve.init.mw.Target = function VeInitMwTarget( pageName, revision ) {
|
Object management: Object create/inherit/clone utilities
* For the most common case:
- replace ve.extendClass with ve.inheritClass (chose slightly
different names to detect usage of the old/new one, and I
like 'inherit' better).
- move it up to below the constructor, see doc block for why.
* Cases where more than 2 arguments were passed to
ve.extendClass are handled differently depending on the case.
In case of a longer inheritance tree, the other arguments
could be omitted (like in "ve.ce.FooBar, ve.FooBar,
ve.Bar". ve.ce.FooBar only needs to inherit from ve.FooBar,
because ve.ce.FooBar inherits from ve.Bar).
In the case of where it previously had two mixins with
ve.extendClass(), either one becomes inheritClass and one
a mixin, both to mixinClass().
No visible changes should come from this commit as the
instances still all have the same visible properties in the
end. No more or less than before.
* Misc.:
- Be consistent in calling parent constructors in the
same order as the inheritance.
- Add missing @extends and @param documentation.
- Replace invalid {Integer} type hint with {Number}.
- Consistent doc comments order:
@class, @abstract, @constructor, @extends, @params.
- Fix indentation errors
A fairly common mistake was a superfluous space before the
identifier on the assignment line directly below the
documentation comment.
$ ack "^ [^*]" --js modules/ve
- Typo "Inhertiance" -> "Inheritance".
- Replacing the other confusing comment "Inheritance" (inside
the constructor) with "Parent constructor".
- Add missing @abstract for ve.ui.Tool.
- Corrected ve.FormatDropdownTool to ve.ui.FormatDropdownTool.js
- Add function names to all @constructor functions. Now that we
have inheritance it is important and useful to have these
functions not be anonymous.
Example of debug shot: http://cl.ly/image/1j3c160w3D45
Makes the difference between
< documentNode;
> ve_dm_DocumentNode
...
: ve_dm_BranchNode
...
: ve_dm_Node
...
: ve_dm_Node
...
: Object
...
without names (current situation):
< documentNode;
> Object
...
: Object
...
: Object
...
: Object
...
: Object
...
though before this commit, it really looks like this
(flattened since ve.extendClass really did a mixin):
< documentNode;
> Object
...
...
...
Pattern in Sublime (case-sensitive) to find nameless
constructor functions:
"^ve\..*\.([A-Z])([^\.]+) = function \("
Change-Id: Iab763954fb8cf375900d7a9a92dec1c755d5407e
2012-09-05 06:07:47 +00:00
|
|
|
// Parent constructor
|
2012-06-18 20:12:32 +00:00
|
|
|
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-12-02 05:09:03 +00:00
|
|
|
this.pageExists = mw.config.get( 'wgArticleId', 0 ) !== 0;
|
2012-12-04 21:04:19 +00:00
|
|
|
this.oldid = revision || '';
|
2012-06-11 06:54:41 +00:00
|
|
|
this.editToken = mw.user.tokens.get( 'editToken' );
|
|
|
|
this.apiUrl = mw.util.wikiScript( 'api' );
|
2012-11-28 23:57:00 +00:00
|
|
|
this.submitUrl = ( new mw.Uri( mw.util.wikiGetlink( this.pageName ) ) )
|
|
|
|
.extend( { 'action': 'submit' } );
|
2012-08-30 20:04:22 +00:00
|
|
|
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']
|
|
|
|
);
|
2012-06-18 20:12:32 +00:00
|
|
|
this.loading = false;
|
|
|
|
this.saving = false;
|
2012-11-28 23:57:00 +00:00
|
|
|
this.serializing = false;
|
|
|
|
this.submitting = false;
|
|
|
|
this.baseTimeStamp = null;
|
|
|
|
this.startTimeStamp = null;
|
2012-06-18 20:12:32 +00:00
|
|
|
this.dom = null;
|
2012-11-30 23:09:34 +00:00
|
|
|
this.editNotices = null;
|
2012-06-11 06:54:41 +00:00
|
|
|
this.isMobileDevice = (
|
|
|
|
'ontouchstart' in window ||
|
2012-11-28 18:11:11 +00:00
|
|
|
( window.DocumentTouch && document instanceof window.DocumentTouch )
|
2012-06-11 06:54:41 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
Object management: Object create/inherit/clone utilities
* For the most common case:
- replace ve.extendClass with ve.inheritClass (chose slightly
different names to detect usage of the old/new one, and I
like 'inherit' better).
- move it up to below the constructor, see doc block for why.
* Cases where more than 2 arguments were passed to
ve.extendClass are handled differently depending on the case.
In case of a longer inheritance tree, the other arguments
could be omitted (like in "ve.ce.FooBar, ve.FooBar,
ve.Bar". ve.ce.FooBar only needs to inherit from ve.FooBar,
because ve.ce.FooBar inherits from ve.Bar).
In the case of where it previously had two mixins with
ve.extendClass(), either one becomes inheritClass and one
a mixin, both to mixinClass().
No visible changes should come from this commit as the
instances still all have the same visible properties in the
end. No more or less than before.
* Misc.:
- Be consistent in calling parent constructors in the
same order as the inheritance.
- Add missing @extends and @param documentation.
- Replace invalid {Integer} type hint with {Number}.
- Consistent doc comments order:
@class, @abstract, @constructor, @extends, @params.
- Fix indentation errors
A fairly common mistake was a superfluous space before the
identifier on the assignment line directly below the
documentation comment.
$ ack "^ [^*]" --js modules/ve
- Typo "Inhertiance" -> "Inheritance".
- Replacing the other confusing comment "Inheritance" (inside
the constructor) with "Parent constructor".
- Add missing @abstract for ve.ui.Tool.
- Corrected ve.FormatDropdownTool to ve.ui.FormatDropdownTool.js
- Add function names to all @constructor functions. Now that we
have inheritance it is important and useful to have these
functions not be anonymous.
Example of debug shot: http://cl.ly/image/1j3c160w3D45
Makes the difference between
< documentNode;
> ve_dm_DocumentNode
...
: ve_dm_BranchNode
...
: ve_dm_Node
...
: ve_dm_Node
...
: Object
...
without names (current situation):
< documentNode;
> Object
...
: Object
...
: Object
...
: Object
...
: Object
...
though before this commit, it really looks like this
(flattened since ve.extendClass really did a mixin):
< documentNode;
> Object
...
...
...
Pattern in Sublime (case-sensitive) to find nameless
constructor functions:
"^ve\..*\.([A-Z])([^\.]+) = function \("
Change-Id: Iab763954fb8cf375900d7a9a92dec1c755d5407e
2012-09-05 06:07:47 +00:00
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
ve.inheritClass( ve.init.mw.Target, ve.EventEmitter );
|
|
|
|
|
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-09-17 13:30:50 +00:00
|
|
|
ve.init.mw.Target.onLoad = function ( response ) {
|
2012-11-30 23:00:04 +00:00
|
|
|
var data = response.visualeditor;
|
2012-11-14 18:33:57 +00:00
|
|
|
if ( !data && !response.error ) {
|
2012-11-28 23:57:00 +00:00
|
|
|
ve.init.mw.Target.onLoadError.call(
|
|
|
|
this, null, 'Invalid response in response from server', null
|
|
|
|
);
|
2012-11-14 18:33:57 +00:00
|
|
|
} else if ( response.error || data.result === 'error' ) {
|
2012-11-28 23:57:00 +00:00
|
|
|
ve.init.mw.Target.onLoadError.call( this, null, 'Server error', null );
|
|
|
|
} else if ( typeof data.content !== 'string' ) {
|
|
|
|
ve.init.mw.Target.onLoadError.call(
|
|
|
|
this, null, 'No HTML content in response from server', null
|
|
|
|
);
|
2012-06-18 20:12:32 +00:00
|
|
|
} else {
|
2012-11-28 23:57:00 +00:00
|
|
|
this.dom = $( '<div>' ).html( data.content )[0];
|
2012-11-30 23:09:34 +00:00
|
|
|
this.editNotices = data.notices;
|
2012-11-28 23:57:00 +00:00
|
|
|
this.baseTimeStamp = data.basetimestamp;
|
|
|
|
this.startTimeStamp = data.starttimestamp;
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-11-28 23:57:00 +00:00
|
|
|
* Respond to both DOM and modules being loaded and ready.
|
2012-06-18 20:12:32 +00:00
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-11-28 23:57:00 +00:00
|
|
|
* Respond to an unsuccessful load request.
|
2012-06-18 20:12:32 +00:00
|
|
|
*
|
|
|
|
* This method is called within the context of a target instance.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @method
|
2012-12-07 16:23:23 +00:00
|
|
|
* @param {Object} jqXHR
|
2012-06-18 20:12:32 +00:00
|
|
|
* @param {String} status Text status message
|
2012-12-07 16:23:23 +00:00
|
|
|
* @param {mixed} error HTTP status text
|
|
|
|
* @emits loadError (jqXHR, status, error)
|
2012-06-18 20:12:32 +00:00
|
|
|
*/
|
2012-12-07 16:23:23 +00:00
|
|
|
ve.init.mw.Target.onLoadError = function ( jqXHR, status, error ) {
|
2012-06-18 20:12:32 +00:00
|
|
|
this.loading = false;
|
2012-12-07 16:23:23 +00:00
|
|
|
this.emit( 'loadError', jqXHR, status, error );
|
2012-06-18 20:12:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-11-28 23:57:00 +00:00
|
|
|
* Respond to a successful save request.
|
2012-06-18 20:12:32 +00:00
|
|
|
*
|
|
|
|
* This method is called within the context of a target instance.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @method
|
2012-12-07 16:23:23 +00:00
|
|
|
* @param {Object} response Response data
|
2012-06-18 20:12:32 +00:00
|
|
|
* @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-09-17 13:30:50 +00:00
|
|
|
ve.init.mw.Target.onSave = function ( response ) {
|
2012-06-18 20:12:32 +00:00
|
|
|
this.saving = false;
|
2012-11-30 23:00:04 +00:00
|
|
|
var data = response.visualeditor;
|
2012-11-14 18:33:57 +00:00
|
|
|
if ( !data && !response.error ) {
|
2012-11-28 23:57:00 +00:00
|
|
|
ve.init.mw.Target.onSaveError.call( this, null, 'Invalid response from server', null );
|
2012-11-24 01:41:07 +00:00
|
|
|
} else if ( response.error ) {
|
2012-11-28 23:57:00 +00:00
|
|
|
if (response.error.code === 'editconflict' ) {
|
|
|
|
this.emit( 'editConflict' );
|
|
|
|
} else {
|
|
|
|
ve.init.mw.Target.onSaveError.call(
|
|
|
|
this, null, 'Unsuccessful request: ' + response.error.info, null
|
|
|
|
);
|
|
|
|
}
|
2012-11-24 01:41:07 +00:00
|
|
|
} else if ( data.result !== 'success' ) {
|
2012-11-28 23:57:00 +00:00
|
|
|
ve.init.mw.Target.onSaveError.call( this, null, 'Failed request: ' + data.result, null );
|
2012-06-18 20:12:32 +00:00
|
|
|
} else if ( typeof data.content !== 'string' ) {
|
2012-11-28 23:57:00 +00:00
|
|
|
ve.init.mw.Target.onSaveError.call(
|
|
|
|
this, null, 'Invalid HTML content in response from server', null
|
|
|
|
);
|
2012-06-18 20:12:32 +00:00
|
|
|
} else {
|
|
|
|
this.emit( 'save', data.content );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-11-28 23:57:00 +00:00
|
|
|
* Respond to an unsuccessful save request.
|
2012-06-18 20:12:32 +00:00
|
|
|
*
|
2012-12-07 16:23:23 +00:00
|
|
|
* @static
|
|
|
|
* @method
|
|
|
|
* @context {ve.init.mw.Target}
|
|
|
|
* @param {Object} jqXHR
|
|
|
|
* @param {String} status Text status message
|
|
|
|
* @param {mixed} error HTTP status text
|
|
|
|
* @emits saveError (jqXHR, status, error)
|
|
|
|
*/
|
|
|
|
ve.init.mw.Target.onSaveError = function ( jqXHR, status, error ) {
|
|
|
|
this.saving = false;
|
|
|
|
this.emit( 'saveError', jqXHR, status, error );
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Respond to a successful show changes request.
|
2012-06-18 20:12:32 +00:00
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @method
|
2012-12-07 16:23:23 +00:00
|
|
|
* @param {Object} response Response data
|
2012-06-18 20:12:32 +00:00
|
|
|
* @param {String} status Text status message
|
2012-12-07 16:23:23 +00:00
|
|
|
* @emits save (diffHtml)
|
|
|
|
* @emits saveError (null, message, null)
|
2012-06-18 20:12:32 +00:00
|
|
|
*/
|
2012-12-07 16:23:23 +00:00
|
|
|
ve.init.mw.Target.onShowChanges = function ( response ) {
|
|
|
|
var data = response.visualeditor;
|
|
|
|
if ( !data && !response.error ) {
|
|
|
|
ve.init.mw.Target.onShowChangesError.call( this, null, 'Invalid response from server', null );
|
|
|
|
} else if ( response.error ) {
|
|
|
|
ve.init.mw.Target.onShowChangesError.call(
|
|
|
|
this, null, 'Unsuccessful request: ' + response.error.info, null
|
|
|
|
);
|
|
|
|
} else if ( data.result !== 'success' ) {
|
|
|
|
ve.init.mw.Target.onShowChangesError.call( this, null, 'Failed request: ' + data.result, null );
|
|
|
|
} else if ( typeof data.diff !== 'string' ) {
|
|
|
|
ve.init.mw.Target.onShowChangesError.call(
|
|
|
|
this, null, 'Invalid HTML content in response from server', null
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this.emit( 'showChanges', data.diff );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Respond to error during saveChanges action.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @method
|
|
|
|
* @context {ve.init.mw.Target}
|
|
|
|
* @param {Object} jqXHR
|
|
|
|
* @param {String} status Text status message
|
|
|
|
* @param {mixed} error HTTP status text
|
|
|
|
* @emits showChangesError (jqXHR, status, error)
|
|
|
|
*/
|
|
|
|
ve.init.mw.Target.onShowChangesError = function ( jqXHR, status, error ) {
|
2012-06-18 20:12:32 +00:00
|
|
|
this.saving = false;
|
2012-12-07 16:23:23 +00:00
|
|
|
this.emit( 'showChangesError', jqXHR, status, error );
|
2012-06-18 20:12:32 +00:00
|
|
|
};
|
|
|
|
|
2012-11-28 23:57:00 +00:00
|
|
|
/**
|
|
|
|
* Respond to a successful serialize 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.onSerialize = function ( response ) {
|
|
|
|
this.serializing = false;
|
2012-11-30 23:00:04 +00:00
|
|
|
var data = response.visualeditor;
|
2012-11-28 23:57:00 +00:00
|
|
|
if ( !data && !response.error ) {
|
|
|
|
ve.init.mw.Target.onSerializeError.call( this, null, 'Invalid response from server', null );
|
|
|
|
} else if ( response.error || data.result === 'error' ) {
|
|
|
|
ve.init.mw.Target.onSerializeError.call( this, null, 'Server error', null );
|
|
|
|
} else if ( typeof data.content !== 'string' ) {
|
|
|
|
ve.init.mw.Target.onSerializeError.call(
|
|
|
|
this, null, 'No Wikitext content in response from server', null
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
if ( typeof this.serializeCallback === 'function' ) {
|
|
|
|
this.serializeCallback( data.content );
|
|
|
|
delete this.serializeCallback;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Respond to an unsuccessful serialize 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.onSerializeError = function ( response, status, error ) {
|
|
|
|
this.serializing = false;
|
|
|
|
this.emit( 'serializeError', 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.
|
|
|
|
*
|
2012-11-28 23:57:00 +00:00
|
|
|
* A side-effect of calling this method is that it requests {this.modules} be loaded.
|
2012-06-11 06:54:41 +00:00
|
|
|
*
|
|
|
|
* @method
|
2012-11-28 23:57:00 +00:00
|
|
|
* @returns {Boolean} Loading has been started
|
2012-06-11 06:54:41 +00:00
|
|
|
*/
|
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': {
|
2012-11-30 23:00:04 +00:00
|
|
|
'action': 'visualeditor',
|
2012-06-11 06:54:41 +00:00
|
|
|
'paction': 'parse',
|
2012-06-19 04:46:49 +00:00
|
|
|
'page': this.pageName,
|
2012-12-04 21:04:19 +00:00
|
|
|
'oldid': this.oldid,
|
2012-11-14 18:33:57 +00:00
|
|
|
'token': this.editToken,
|
2012-06-11 06:54:41 +00:00
|
|
|
'format': 'json'
|
|
|
|
},
|
|
|
|
'dataType': 'json',
|
2012-11-14 18:33:57 +00:00
|
|
|
'type': 'POST',
|
2012-11-15 00:02:36 +00:00
|
|
|
// Wait up to 100 seconds before giving up
|
|
|
|
'timeout': 100000,
|
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
|
2012-11-28 23:57:00 +00:00
|
|
|
* target.save( dom, { 'summary': 'test', 'minor': true, 'watch': false } );
|
2012-06-11 06:54:41 +00:00
|
|
|
*
|
|
|
|
* @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-11-28 23:57:00 +00:00
|
|
|
* @returns {Boolean} Saving has been started
|
2012-06-11 06:54:41 +00:00
|
|
|
*/
|
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',
|
2012-11-30 23:00:04 +00:00
|
|
|
'action': 'visualeditor',
|
2012-06-11 06:54:41 +00:00
|
|
|
'paction': 'save',
|
2012-06-19 04:46:49 +00:00
|
|
|
'page': this.pageName,
|
2012-12-04 21:04:19 +00:00
|
|
|
'oldid': this.oldid,
|
2012-11-28 23:57:00 +00:00
|
|
|
'basetimestamp': this.baseTimeStamp,
|
|
|
|
'starttimestamp': this.startTimeStamp,
|
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-11-28 23:57:00 +00:00
|
|
|
|
2012-12-07 16:23:23 +00:00
|
|
|
/**
|
|
|
|
* Posts DOM to Parsoid API to retreive wikitext diff.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {HTMLElement} dom DOM to compare against (via wikitext).
|
|
|
|
*/
|
|
|
|
ve.init.mw.Target.prototype.showChanges = function ( dom ) {
|
|
|
|
$.ajax( {
|
|
|
|
'url': this.apiUrl,
|
|
|
|
'data': {
|
|
|
|
'format': 'json',
|
|
|
|
'action': 'visualeditor',
|
|
|
|
'paction': 'diff',
|
|
|
|
'page': this.pageName,
|
|
|
|
'html': $( dom ).html(),
|
|
|
|
// TODO: API required editToken, though not relevant for diff
|
|
|
|
'token': this.editToken
|
|
|
|
},
|
|
|
|
'dataType': 'json',
|
|
|
|
'type': 'POST',
|
|
|
|
// Wait up to 10 seconds before giving up
|
|
|
|
'timeout': 10000,
|
|
|
|
'success': ve.bind( ve.init.mw.Target.onShowChanges, this ),
|
|
|
|
'error': ve.bind( ve.init.mw.Target.onShowChangesError, this )
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2012-11-28 23:57:00 +00:00
|
|
|
/**
|
|
|
|
* Posts DOM to Parsoid API.
|
|
|
|
*
|
|
|
|
* This method performs a synchronous action and will take the user to a new page when complete.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* target.submit( wikitext, { 'summary': 'test', 'minor': true, 'watch': false } );
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {String} wikitext Wikitext to submit
|
|
|
|
* @param {Object} options Saving options
|
|
|
|
* - {String} summary Edit summary
|
|
|
|
* - {Boolean} minor Edit is a minor edit
|
|
|
|
* - {Boolean} watch Watch this page
|
|
|
|
* @returns {Boolean} Submitting has been started
|
|
|
|
*/
|
|
|
|
ve.init.mw.Target.prototype.submit = function ( wikitext, options ) {
|
|
|
|
// Prevent duplicate requests
|
|
|
|
if ( this.submitting ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Save DOM
|
|
|
|
this.submitting = true;
|
|
|
|
var key,
|
|
|
|
$form = $( '<form method="post" enctype="multipart/form-data"></form>' ),
|
|
|
|
params = {
|
|
|
|
'format': 'text/x-wiki',
|
2012-12-04 21:04:19 +00:00
|
|
|
'oldid': this.oldid,
|
2012-11-28 23:57:00 +00:00
|
|
|
'wpStarttime': this.baseTimeStamp,
|
|
|
|
'wpEdittime': this.startTimeStamp,
|
|
|
|
'wpTextbox1': wikitext,
|
|
|
|
'wpSummary': options.summary,
|
|
|
|
'wpWatchthis': options.watch,
|
|
|
|
'wpMinoredit': options.minor,
|
|
|
|
'wpEditToken': this.editToken,
|
|
|
|
'wpSave': 1
|
|
|
|
};
|
|
|
|
// Add params as hidden fields
|
|
|
|
for ( key in params ) {
|
|
|
|
$form.append( $( '<input type="hidden">' ).attr( { 'name': key, 'value': params[key] } ) );
|
|
|
|
}
|
|
|
|
// Submit the form, mimicking a traditional edit
|
|
|
|
$form.attr( 'action', this.submitUrl ).appendTo( 'body' ).submit();
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets Wikitext from Parsoid API.
|
|
|
|
*
|
|
|
|
* This method performs an asynchronous action and uses a callback function to handle the result.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* target.serialize(
|
|
|
|
* dom,
|
|
|
|
* function ( wikitext ) {
|
|
|
|
* // Do something with the loaded DOM
|
|
|
|
* }
|
|
|
|
* );
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {HTMLElement} dom DOM to serialize
|
|
|
|
* @param {Function} callback Function to call when complete, accepts error and wikitext arguments
|
|
|
|
* @returns {Boolean} Serializing has beeen started
|
|
|
|
*/
|
|
|
|
ve.init.mw.Target.prototype.serialize = function ( dom, callback ) {
|
|
|
|
// Prevent duplicate requests
|
|
|
|
if ( this.serializing ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Load DOM
|
|
|
|
this.serializing = true;
|
|
|
|
this.serializeCallback = callback;
|
|
|
|
$.ajax( {
|
|
|
|
'url': this.apiUrl,
|
|
|
|
'data': {
|
2012-11-30 23:00:04 +00:00
|
|
|
'action': 'visualeditor',
|
2012-11-28 23:57:00 +00:00
|
|
|
'paction': 'serialize',
|
|
|
|
'html': $( dom ).html(),
|
|
|
|
'page': this.pageName,
|
|
|
|
'token': this.editToken,
|
|
|
|
'format': 'json'
|
|
|
|
},
|
|
|
|
'dataType': 'json',
|
|
|
|
'type': 'POST',
|
|
|
|
// Wait up to 100 seconds before giving up
|
|
|
|
'timeout': 100000,
|
|
|
|
'cache': 'false',
|
|
|
|
'success': ve.bind( ve.init.mw.Target.onSerialize, this ),
|
|
|
|
'error': ve.bind( ve.init.mw.Target.onSerializeError, this )
|
|
|
|
} );
|
|
|
|
return true;
|
|
|
|
};
|