mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/WikiEditor
synced 2024-11-28 10:11:03 +00:00
7721909f9f
* Code clean up in preparation for enabling linting in the future * Update code to use latest code conventions and best practices: - Make use of jQuery.Event (e.g. no need to check both e.keyCode and e.which) - jQuery: .size() -> .length - jQuery: (where appropiate) .attr() -> .prop() Setting properties like 'checked' via attr() has been deprecated in jQuery. - Whitespace - Single quotes instead of double quotes - Use literal keys in object literals instead of strings - Pass mediaWiki to closure, use mw. locally instead of "mediaWiki" global directly. - Fix indentation - Brackets around if, else and for bodies - Strict comparison to 0, null, false, true etc. - Fix missing radix parameter in parseInt - Use local $ instead of global $ - Use `foo || bar` instead of `foo ? foo : bar` - Variable scope hoisting - Double/redundant variable declarations - ['foo'] is better written in dot notation - New line at EOF - Consistency in jQuery construction: Tag name for element creation $( '<div>' ) Valid html for html parsing $( '<div foo="bar"></div>' ) - Fix regex escape warnings per JSLint/JSHint. Do escape ][, don't escape >< - .. * Add .jshintrc / .jshintignore * Updated most files, but not all. Too much at once. Change-Id: I445639b25a9688b3cdf9e5449e3d31cbcfa9c7ae
170 lines
4.7 KiB
JavaScript
170 lines
4.7 KiB
JavaScript
/* Preview module for wikiEditor */
|
|
( function ( $, mw ) {
|
|
|
|
$.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
|
|
var wikitext = context.$textarea.textSelection( 'getContents' );
|
|
// 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(
|
|
mw.util.wikiScript( 'api' ),
|
|
{
|
|
format: 'json',
|
|
action: 'parse',
|
|
title: mw.config.get( 'wgPageName' ),
|
|
text: wikitext,
|
|
prop: 'text',
|
|
pst: ''
|
|
},
|
|
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( false );
|
|
},
|
|
'json'
|
|
);
|
|
}
|
|
} );
|
|
|
|
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();
|
|
|
|
// Call the API. First PST the input, then diff it
|
|
var postdata = {
|
|
format: 'json',
|
|
action: 'parse',
|
|
onlypst: '',
|
|
text: wikitext
|
|
};
|
|
|
|
$.post( mw.util.wikiScript( 'api' ), postdata, function ( data ) {
|
|
try {
|
|
var postdata2 = {
|
|
format: 'json',
|
|
action: 'query',
|
|
indexpageids: '',
|
|
prop: 'revisions',
|
|
titles: mw.config.get( 'wgPageName' ),
|
|
rvdifftotext: data.parse.text['*'],
|
|
rvprop: ''
|
|
};
|
|
var section = $( '[name="wpSection"]' ).val();
|
|
if ( section !== '' )
|
|
postdata2.rvsection = section;
|
|
|
|
$.post( mw.util.wikiScript( 'api' ), postdata2, function ( data ) {
|
|
// Add diff CSS
|
|
mw.loader.load( 'mediawiki.action.history.diff' );
|
|
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' );
|
|
}
|
|
} );
|
|
|
|
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 )
|
|
)
|
|
)
|
|
.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, mediaWiki ) );
|