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
|
|
|
|
|
|
|
/**
|
|
|
|
* Compatability map
|
|
|
|
*/
|
2012-07-03 05:52:27 +00:00
|
|
|
browsers: {
|
2010-09-15 03:08:35 +00:00
|
|
|
// Left-to-right languages
|
2012-07-03 05:52:27 +00:00
|
|
|
ltr: {
|
|
|
|
msie: [['>=', 7]],
|
|
|
|
firefox: [['>=', 3]],
|
|
|
|
opera: [['>=', 9.6]],
|
|
|
|
safari: [['>=', 4]]
|
2010-09-15 03:08:35 +00:00
|
|
|
},
|
|
|
|
// Right-to-left languages
|
2012-07-03 05:52:27 +00:00
|
|
|
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
|
|
|
/**
|
|
|
|
* Internally used functions
|
|
|
|
*/
|
|
|
|
fn: {
|
|
|
|
/**
|
|
|
|
* Creates a preview module within a wikiEditor
|
|
|
|
* @param context Context object of editor to create module in
|
|
|
|
* @param config Configuration object to create module from
|
|
|
|
*/
|
2013-11-07 00:38:43 +00:00
|
|
|
create: function ( context ) {
|
2015-01-07 18:25:47 +00:00
|
|
|
var api = new mw.Api();
|
|
|
|
|
2010-09-15 03:08:35 +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',
|
2012-07-03 05:52:27 +00:00
|
|
|
'init': function ( context ) {
|
2010-09-15 03:08:35 +00:00
|
|
|
// Gets the latest copy of the wikitext
|
2010-09-17 23:58:41 +00:00
|
|
|
var wikitext = context.$textarea.textSelection( 'getContents' );
|
2010-09-15 03:08:35 +00:00
|
|
|
// Aborts when nothing has changed since the last preview
|
2012-07-03 05:52:27 +00:00
|
|
|
if ( context.modules.preview.previewText === wikitext ) {
|
2010-09-15 03:08:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
context.modules.preview.$preview.find( '.wikiEditor-preview-contents' ).empty();
|
|
|
|
context.modules.preview.$preview.find( '.wikiEditor-preview-loading' ).show();
|
2015-01-07 18:25:47 +00:00
|
|
|
api.post( {
|
|
|
|
action: 'parse',
|
|
|
|
title: mw.config.get( 'wgPageName' ),
|
|
|
|
text: wikitext,
|
|
|
|
prop: 'text|modules',
|
|
|
|
pst: ''
|
2014-05-04 13:50:52 +00:00
|
|
|
} ).done( function ( data ) {
|
|
|
|
if ( !data.parse || !data.parse.text || data.parse.text['*'] === undefined ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
context.modules.preview.previewText = wikitext;
|
|
|
|
context.modules.preview.$preview.find( '.wikiEditor-preview-loading' ).hide();
|
|
|
|
context.modules.preview.$preview.find( '.wikiEditor-preview-contents' )
|
|
|
|
.html( data.parse.text['*'] )
|
2014-11-10 20:02:43 +00:00
|
|
|
.append( '<div class="visualClear"></div>' )
|
2014-05-04 13:50:52 +00:00
|
|
|
.find( 'a:not([href^=#])' )
|
|
|
|
.click( false );
|
|
|
|
|
|
|
|
var loadmodules = data.parse.modules.concat(
|
|
|
|
data.parse.modulescripts,
|
2015-09-24 17:01:24 +00:00
|
|
|
data.parse.modulestyles
|
2014-05-04 13:50:52 +00:00
|
|
|
);
|
|
|
|
mw.loader.load( loadmodules );
|
|
|
|
} );
|
2010-09-15 03:08:35 +00:00
|
|
|
}
|
|
|
|
} );
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2010-09-15 03:08:35 +00:00
|
|
|
context.$changesTab = context.fn.addView( {
|
|
|
|
'name': 'changes',
|
|
|
|
'titleMsg': 'wikieditor-preview-changes-tab',
|
2012-07-03 05:52:27 +00:00
|
|
|
'init': function ( context ) {
|
2010-09-15 03:08:35 +00:00
|
|
|
// Gets the latest copy of the wikitext
|
2010-09-17 23:58:41 +00:00
|
|
|
var wikitext = context.$textarea.textSelection( 'getContents' );
|
2010-09-15 03:08:35 +00:00
|
|
|
// Aborts when nothing has changed since the last time
|
2013-11-07 00:38:43 +00:00
|
|
|
if ( context.modules.preview.changesText === wikitext ) {
|
2010-09-15 03:08:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
context.$changesTab.find( 'table.diff tbody' ).empty();
|
|
|
|
context.$changesTab.find( '.wikiEditor-preview-loading' ).show();
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2010-09-15 03:08:35 +00:00
|
|
|
// Call the API. First PST the input, then diff it
|
2015-01-07 18:25:47 +00:00
|
|
|
api.post( {
|
|
|
|
action: 'parse',
|
|
|
|
title: mw.config.get( 'wgPageName' ),
|
|
|
|
onlypst: '',
|
|
|
|
text: wikitext
|
2014-05-04 13:50:52 +00:00
|
|
|
} ).done( function ( data ) {
|
2010-09-15 03:08:35 +00:00
|
|
|
try {
|
|
|
|
var postdata2 = {
|
2012-07-03 05:52:27 +00:00
|
|
|
action: 'query',
|
|
|
|
indexpageids: '',
|
|
|
|
prop: 'revisions',
|
|
|
|
titles: mw.config.get( 'wgPageName' ),
|
|
|
|
rvdifftotext: data.parse.text['*'],
|
|
|
|
rvprop: ''
|
2010-09-15 03:08:35 +00:00
|
|
|
};
|
2012-07-03 05:52:27 +00:00
|
|
|
var section = $( '[name="wpSection"]' ).val();
|
2013-11-07 00:38:43 +00:00
|
|
|
if ( section !== '' ) {
|
2012-07-03 05:52:27 +00:00
|
|
|
postdata2.rvsection = section;
|
2013-11-07 00:38:43 +00:00
|
|
|
}
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2015-01-07 18:25:47 +00:00
|
|
|
api.post( postdata2 )
|
|
|
|
.done( function ( data ) {
|
2014-05-04 13:50:52 +00:00
|
|
|
// Add diff CSS
|
|
|
|
mw.loader.load( 'mediawiki.action.history.diff' );
|
|
|
|
try {
|
|
|
|
var diff = data.query.pages[data.query.pageids[0]]
|
|
|
|
.revisions[0].diff['*'];
|
|
|
|
|
2014-11-10 20:02:43 +00:00
|
|
|
context.$changesTab.find( 'table.diff tbody' )
|
|
|
|
.html( diff )
|
|
|
|
.append( '<div class="visualClear"></div>' );
|
2014-05-04 13:50:52 +00:00
|
|
|
context.modules.preview.changesText = wikitext;
|
|
|
|
} catch ( e ) {
|
|
|
|
// "data.blah is undefined" error, ignore
|
|
|
|
}
|
|
|
|
context.$changesTab.find( '.wikiEditor-preview-loading' ).hide();
|
|
|
|
} );
|
|
|
|
} catch ( e ) {
|
|
|
|
// "data.blah is undefined" error, ignore
|
|
|
|
}
|
|
|
|
} );
|
2010-09-15 03:08:35 +00:00
|
|
|
}
|
|
|
|
} );
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2012-07-03 05:52:27 +00:00
|
|
|
var loadingMsg = mw.msg( 'wikieditor-preview-loading' );
|
2010-09-15 03:08:35 +00:00
|
|
|
context.modules.preview.$preview
|
|
|
|
.add( context.$changesTab )
|
2012-07-03 05:52:27 +00:00
|
|
|
.append( $( '<div>' )
|
2010-09-15 03:08:35 +00:00
|
|
|
.addClass( 'wikiEditor-preview-loading' )
|
2012-07-03 05:52:27 +00:00
|
|
|
.append( $( '<img>' )
|
2010-09-15 03:08:35 +00:00
|
|
|
.addClass( 'wikiEditor-preview-spinner' )
|
|
|
|
.attr( {
|
|
|
|
'src': $.wikiEditor.imgPath + 'dialogs/loading.gif',
|
|
|
|
'valign': 'absmiddle',
|
|
|
|
'alt': loadingMsg,
|
|
|
|
'title': loadingMsg
|
|
|
|
} )
|
|
|
|
)
|
|
|
|
.append(
|
2012-07-03 05:52:27 +00:00
|
|
|
$( '<span>' ).text( loadingMsg )
|
2010-09-15 03:08:35 +00:00
|
|
|
)
|
|
|
|
)
|
2012-07-03 05:52:27 +00:00
|
|
|
.append( $( '<div>' )
|
2010-09-15 03:08:35 +00:00
|
|
|
.addClass( 'wikiEditor-preview-contents' )
|
|
|
|
);
|
|
|
|
context.$changesTab.find( '.wikiEditor-preview-contents' )
|
2012-07-03 05:52:27 +00:00
|
|
|
.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 ) );
|