2010-09-15 03:08:35 +00:00
|
|
|
/* Preview module for wikiEditor */
|
|
|
|
( function( $ ) { $.wikiEditor.modules.preview = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compatability map
|
|
|
|
*/
|
|
|
|
'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]]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
create: function( context, config ) {
|
|
|
|
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
|
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
|
|
|
|
if ( context.modules.preview.previewText == wikitext ) {
|
|
|
|
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
|
|
|
{
|
|
|
|
'action': 'parse',
|
2011-06-26 19:25:27 +00:00
|
|
|
'title': mw.config.get( 'wgPageName' ),
|
2010-09-15 03:08:35 +00:00
|
|
|
'text': wikitext,
|
|
|
|
'prop': 'text',
|
|
|
|
'pst': '',
|
|
|
|
'format': 'json'
|
|
|
|
},
|
|
|
|
function( data ) {
|
|
|
|
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['*'] )
|
|
|
|
.find( 'a:not([href^=#])' ).click( function() { return false; } );
|
|
|
|
},
|
|
|
|
'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',
|
|
|
|
'init': function( context ) {
|
|
|
|
// 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 = {
|
|
|
|
'action': 'parse',
|
|
|
|
'onlypst': '',
|
|
|
|
'text': wikitext,
|
|
|
|
'format': 'json'
|
|
|
|
};
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2011-06-26 19:25:27 +00:00
|
|
|
$.post( mw.util.wikiScript( 'api' ), postdata, function( data ) {
|
2010-09-15 03:08:35 +00:00
|
|
|
try {
|
|
|
|
var postdata2 = {
|
|
|
|
'action': 'query',
|
|
|
|
'indexpageids': '',
|
|
|
|
'prop': 'revisions',
|
2011-06-26 19:25:27 +00:00
|
|
|
'titles': mw.config.get( 'wgPageName' ),
|
2010-09-15 03:08:35 +00:00
|
|
|
'rvdifftotext': data.parse.text['*'],
|
|
|
|
'rvprop': '',
|
|
|
|
'format': 'json'
|
|
|
|
};
|
|
|
|
var section = $( '[name=wpSection]' ).val();
|
|
|
|
if ( section != '' )
|
2011-07-02 08:46:50 +00:00
|
|
|
postdata2['rvsection'] = section;
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2011-07-02 08:46:50 +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'
|
|
|
|
);
|
|
|
|
} catch( e ) { } // "blah is undefined" error, ignore
|
|
|
|
}, 'json' );
|
|
|
|
}
|
|
|
|
} );
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2010-10-27 00:16:32 +00:00
|
|
|
var loadingMsg = mediaWiki.msg( 'wikieditor-preview-loading' );
|
2010-09-15 03:08:35 +00:00
|
|
|
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></span>' ).text( loadingMsg )
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.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>' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}; } )( jQuery );
|