2010-09-15 03:08:35 +00:00
|
|
|
/* Preview module for wikiEditor */
|
2012-07-03 05:52:27 +00:00
|
|
|
( function ( $, mw ) {
|
|
|
|
|
|
|
|
$.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
|
|
|
|
*/
|
2012-07-03 05:52:27 +00:00
|
|
|
create: function ( context, config ) {
|
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();
|
|
|
|
$.post(
|
2011-06-26 19:25:27 +00:00
|
|
|
mw.util.wikiScript( 'api' ),
|
2010-09-15 03:08:35 +00:00
|
|
|
{
|
2012-07-03 05:52:27 +00:00
|
|
|
format: 'json',
|
|
|
|
action: 'parse',
|
|
|
|
title: mw.config.get( 'wgPageName' ),
|
|
|
|
text: wikitext,
|
|
|
|
prop: 'text',
|
|
|
|
pst: ''
|
2010-09-15 03:08:35 +00:00
|
|
|
},
|
2012-07-03 05:52:27 +00:00
|
|
|
function ( data ) {
|
2010-09-15 03:08:35 +00:00
|
|
|
if (
|
|
|
|
typeof data.parse == 'undefined' ||
|
|
|
|
typeof data.parse.text == 'undefined' ||
|
|
|
|
typeof 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['*'] )
|
2012-07-03 05:52:27 +00:00
|
|
|
.find( 'a:not([href^=#])' ).click( false );
|
2010-09-15 03:08:35 +00:00
|
|
|
},
|
|
|
|
'json'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} );
|
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
|
|
|
|
if ( context.modules.preview.changesText == wikitext ) {
|
|
|
|
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
|
|
|
|
var postdata = {
|
2012-07-03 05:52:27 +00:00
|
|
|
format: 'json',
|
|
|
|
action: 'parse',
|
|
|
|
onlypst: '',
|
|
|
|
text: wikitext
|
2010-09-15 03:08:35 +00:00
|
|
|
};
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2012-07-03 05:52:27 +00:00
|
|
|
$.post( mw.util.wikiScript( 'api' ), postdata, function ( data ) {
|
2010-09-15 03:08:35 +00:00
|
|
|
try {
|
|
|
|
var postdata2 = {
|
2012-07-03 05:52:27 +00:00
|
|
|
format: 'json',
|
|
|
|
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();
|
|
|
|
if ( section !== '' )
|
|
|
|
postdata2.rvsection = section;
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2012-07-03 05:52:27 +00:00
|
|
|
$.post( mw.util.wikiScript( 'api' ), postdata2, function ( data ) {
|
2010-09-15 03:08:35 +00:00
|
|
|
// Add diff CSS
|
2011-07-08 18:00:38 +00:00
|
|
|
mw.loader.load( 'mediawiki.action.history.diff' );
|
2010-09-15 03:08:35 +00:00
|
|
|
try {
|
|
|
|
var diff = data.query.pages[data.query.pageids[0]]
|
|
|
|
.revisions[0].diff['*'];
|
|
|
|
context.$changesTab.find( 'table.diff tbody' )
|
|
|
|
.html( diff );
|
|
|
|
context.$changesTab
|
|
|
|
.find( '.wikiEditor-preview-loading' ).hide();
|
|
|
|
context.modules.preview.changesText = wikitext;
|
|
|
|
} catch ( e ) { } // "blah is undefined" error, ignore
|
|
|
|
}, 'json'
|
|
|
|
);
|
2012-07-03 05:52:27 +00:00
|
|
|
} catch ( e ) { } // "blah is undefined" error, ignore
|
2010-09-15 03:08:35 +00:00
|
|
|
}, 'json' );
|
|
|
|
}
|
|
|
|
} );
|
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 ) );
|