2010-09-15 03:08:35 +00:00
|
|
|
/* Preview module for wikiEditor */
|
2012-07-03 05:52:27 +00:00
|
|
|
( function ( $, mw ) {
|
2013-11-07 00:38:43 +00:00
|
|
|
/*jshint onevar:false */
|
2012-07-03 05:52:27 +00:00
|
|
|
$.wikiEditor.modules.preview = {
|
2010-09-15 03:08:35 +00:00
|
|
|
|
2015-11-15 14:30:18 +00:00
|
|
|
/**
|
2016-03-31 14:26:58 +00:00
|
|
|
* Compatibility map
|
2015-11-15 14:30:18 +00:00
|
|
|
*/
|
|
|
|
browsers: {
|
|
|
|
// Left-to-right languages
|
|
|
|
ltr: {
|
|
|
|
msie: [ [ '>=', 7 ] ],
|
|
|
|
firefox: [ [ '>=', 3 ] ],
|
|
|
|
opera: [ [ '>=', 9.6 ] ],
|
|
|
|
safari: [ [ '>=', 4 ] ]
|
|
|
|
},
|
|
|
|
// Right-to-left languages
|
|
|
|
rtl: {
|
|
|
|
msie: [ [ '>=', 8 ] ],
|
|
|
|
firefox: [ [ '>=', 3 ] ],
|
|
|
|
opera: [ [ '>=', 9.6 ] ],
|
|
|
|
safari: [ [ '>=', 4 ] ]
|
|
|
|
}
|
2010-09-15 03:08:35 +00:00
|
|
|
},
|
2012-07-03 05:52:27 +00:00
|
|
|
|
2010-09-15 03:08:35 +00:00
|
|
|
/**
|
2015-11-15 14:30:18 +00:00
|
|
|
* Internally used functions
|
2010-09-15 03:08:35 +00:00
|
|
|
*/
|
2015-11-15 14:30:18 +00:00
|
|
|
fn: {
|
|
|
|
/**
|
|
|
|
* Creates a preview module within a wikiEditor
|
|
|
|
*
|
|
|
|
* @param {Object} context Context object of editor to create module in
|
|
|
|
*/
|
|
|
|
create: function ( context ) {
|
|
|
|
var api = new mw.Api();
|
2015-01-07 18:25:47 +00:00
|
|
|
|
2015-11-15 14:30:18 +00:00
|
|
|
if ( 'initialized' in context.modules.preview ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
context.modules.preview = {
|
|
|
|
initialized: true,
|
|
|
|
previewText: null,
|
|
|
|
changesText: null
|
|
|
|
};
|
|
|
|
context.modules.preview.$preview = context.fn.addView( {
|
|
|
|
name: 'preview',
|
|
|
|
titleMsg: 'wikieditor-preview-tab',
|
|
|
|
init: function ( context ) {
|
|
|
|
// Gets the latest copy of the wikitext
|
|
|
|
var wikitext = context.$textarea.textSelection( 'getContents' );
|
|
|
|
// Aborts when nothing has changed since the last preview
|
|
|
|
if ( context.modules.preview.previewText === wikitext ) {
|
2014-05-04 13:50:52 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-11-15 14:30:18 +00:00
|
|
|
context.modules.preview.$preview.find( '.wikiEditor-preview-contents' ).empty();
|
|
|
|
context.modules.preview.$preview.find( '.wikiEditor-preview-loading' ).show();
|
|
|
|
api.post( {
|
2016-02-08 11:49:17 +00:00
|
|
|
formatversion: 2,
|
2015-11-15 14:30:18 +00:00
|
|
|
action: 'parse',
|
|
|
|
title: mw.config.get( 'wgPageName' ),
|
|
|
|
text: wikitext,
|
2016-04-15 22:27:35 +00:00
|
|
|
pst: '',
|
|
|
|
prop: 'text|modules|jsconfigvars',
|
|
|
|
preview: true,
|
|
|
|
disableeditsection: true,
|
|
|
|
uselang: mw.config.get( 'wgUserLanguage' )
|
2015-11-15 14:30:18 +00:00
|
|
|
} ).done( function ( data ) {
|
2016-02-08 11:49:17 +00:00
|
|
|
if ( !data.parse || !data.parse.text ) {
|
2015-11-15 14:30:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-05-04 13:50:52 +00:00
|
|
|
|
2016-04-15 22:27:35 +00:00
|
|
|
if ( data.parse.jsconfigvars ) {
|
|
|
|
mw.config.set( data.parse.jsconfigvars );
|
|
|
|
}
|
2015-11-15 14:30:18 +00:00
|
|
|
var loadmodules = data.parse.modules.concat(
|
|
|
|
data.parse.modulescripts,
|
|
|
|
data.parse.modulestyles
|
|
|
|
);
|
|
|
|
mw.loader.load( loadmodules );
|
2016-04-15 22:27:35 +00:00
|
|
|
|
|
|
|
context.modules.preview.previewText = wikitext;
|
|
|
|
context.modules.preview.$preview.find( '.wikiEditor-preview-loading' ).hide();
|
|
|
|
var $content = context.modules.preview.$preview.find( '.wikiEditor-preview-contents' )
|
|
|
|
.detach()
|
|
|
|
.html( data.parse.text );
|
|
|
|
$content.append( '<div class="visualClear"></div>' )
|
|
|
|
.find( 'a:not([href^=#])' )
|
|
|
|
.click( false );
|
|
|
|
|
|
|
|
mw.hook( 'wikipage.content' ).fire( $content );
|
|
|
|
context.modules.preview.$preview.append( $content );
|
2015-11-15 14:30:18 +00:00
|
|
|
} );
|
2010-09-15 03:08:35 +00:00
|
|
|
}
|
2015-11-15 14:30:18 +00:00
|
|
|
} );
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2015-11-15 14:30:18 +00:00
|
|
|
context.$changesTab = context.fn.addView( {
|
|
|
|
name: 'changes',
|
|
|
|
titleMsg: 'wikieditor-preview-changes-tab',
|
|
|
|
init: function ( context ) {
|
|
|
|
// Gets the latest copy of the wikitext
|
|
|
|
var wikitext = context.$textarea.textSelection( 'getContents' );
|
|
|
|
// Aborts when nothing has changed since the last time
|
|
|
|
if ( context.modules.preview.changesText === wikitext ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
context.$changesTab.find( 'table.diff tbody' ).empty();
|
|
|
|
context.$changesTab.find( '.wikiEditor-preview-loading' ).show();
|
2014-05-04 13:50:52 +00:00
|
|
|
|
2016-04-15 22:27:35 +00:00
|
|
|
var section = $( '[name="wpSection"]' ).val();
|
|
|
|
var postdata = {
|
2016-02-08 11:49:17 +00:00
|
|
|
formatversion: 2,
|
2016-04-15 22:27:35 +00:00
|
|
|
action: 'query',
|
|
|
|
prop: 'revisions',
|
|
|
|
titles: mw.config.get( 'wgPageName' ),
|
|
|
|
rvdifftotext: wikitext,
|
|
|
|
rvdifftotextpst: true,
|
|
|
|
rvprop: '',
|
|
|
|
rvsection: section === '' ? undefined : section
|
|
|
|
};
|
2016-09-06 15:51:13 +00:00
|
|
|
var postPromise = api.post( postdata );
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2016-09-06 15:51:13 +00:00
|
|
|
$.when( postPromise, mw.loader.using( 'mediawiki.diff.styles' ) )
|
|
|
|
.done( function ( postResult ) {
|
2016-04-15 22:27:35 +00:00
|
|
|
try {
|
2016-09-06 15:51:13 +00:00
|
|
|
var diff = postResult[ 0 ].query.pages[ 0 ]
|
2016-04-15 22:27:35 +00:00
|
|
|
.revisions[ 0 ].diff.body;
|
2015-11-15 14:30:18 +00:00
|
|
|
|
2016-04-15 22:27:35 +00:00
|
|
|
context.$changesTab.find( 'table.diff tbody' )
|
|
|
|
.html( diff )
|
|
|
|
.append( '<div class="visualClear"></div>' );
|
|
|
|
mw.hook( 'wikipage.diff' )
|
|
|
|
.fire( context.$changesTab.find( 'table.diff' ) );
|
|
|
|
context.modules.preview.changesText = wikitext;
|
2015-11-15 14:30:18 +00:00
|
|
|
} catch ( e ) {
|
|
|
|
// "data.blah is undefined" error, ignore
|
|
|
|
}
|
2016-04-15 22:27:35 +00:00
|
|
|
context.$changesTab.find( '.wikiEditor-preview-loading' ).hide();
|
2015-11-15 14:30:18 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
var loadingMsg = mw.msg( 'wikieditor-preview-loading' );
|
|
|
|
context.modules.preview.$preview
|
|
|
|
.add( context.$changesTab )
|
|
|
|
.append( $( '<div>' )
|
|
|
|
.addClass( 'wikiEditor-preview-loading' )
|
|
|
|
.append( $( '<img>' )
|
|
|
|
.addClass( 'wikiEditor-preview-spinner' )
|
|
|
|
.attr( {
|
|
|
|
src: $.wikiEditor.imgPath + 'dialogs/loading.gif',
|
|
|
|
valign: 'absmiddle',
|
|
|
|
alt: loadingMsg,
|
|
|
|
title: loadingMsg
|
|
|
|
} )
|
|
|
|
)
|
|
|
|
.append(
|
|
|
|
$( '<span>' ).text( loadingMsg )
|
|
|
|
)
|
2010-09-15 03:08:35 +00:00
|
|
|
)
|
2015-11-15 14:30:18 +00:00
|
|
|
.append( $( '<div>' )
|
|
|
|
.addClass( 'wikiEditor-preview-contents' )
|
|
|
|
);
|
|
|
|
context.$changesTab.find( '.wikiEditor-preview-contents' )
|
|
|
|
.html( '<table class="diff"><col class="diff-marker"/><col class="diff-content"/>' +
|
|
|
|
'<col class="diff-marker"/><col class="diff-content"/><tbody/></table>' );
|
|
|
|
}
|
2010-09-15 03:08:35 +00:00
|
|
|
}
|
|
|
|
|
2012-07-03 05:52:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}( jQuery, mediaWiki ) );
|