User:Js/preview2.js

//this script is called from en:user:js/ajaxPreview.js and ru:MediaWiki:Gadget-preview.js //after the buttons are created

ajaxPreview = new function{

var wkPreview = $('#wikiPreview'), frm = document.editform if (!wkPreview.length || !frm) return $('#wpPreviewLive, #wpDiffLive').click(run)

var cssWait, cssLang, cssPreview, cssOutdated var isDiff, isFullPreview, btn, oldHeight, htm, scriptTip var mm = window.ajaxPreviewMsg || {}

function run (e){

btn = $(this) btn.width( btn.outerWidth ).attr('orig', btn.val) btn.val('...') $('#wikiDiff, #newarticletext').hide oldHeight = wkPreview.height scriptTip = '' if (!cssWait) cssWait = mw.util.addCSS('#wikiPreview {opacity: 0.3; color:gray} body {cursor:wait }') else cssWait.disabled = false

var url = mw.config.get('wgScriptPath') + '/api.php?format=json', data, ext var txt = $( '#wpTextbox1' ).textSelection( 'getContents' );

isDiff = btn.attr('id') == 'wpDiffLive'

if (isDiff){

mw.loader.load( 'mediawiki.diff.styles' )

url += '&action=query&prop=revisions' data = { titles: mw.config.get('wgPageName').replace(/_/g,' '), rvdifftotext: txt}

if (frm.wpSection.value) url += '&rvsection=' + frm.wpSection.value

if (frm.oldid.value && frm.oldid.value != '0'){ //can compare to currently edited older version, not to the latest if (e.shiftKey) { url += '&rvstartid=' + frm.oldid.value + '&rvendid=' + frm.oldid.value scriptTip = mm.diff2old }      else scriptTip = mm.difftip } }else{ //preview

if (frm.wpSection.value) txt += ' '     else htm = mm.emptydiff }else{ resp = resp.parse htm = resp.text['*'] if (frm.wpSection.value == 'new'){ //add summary as H2       htm = ' ' + resp.parsedsummary['*'] + ' ' + htm }else{ var $sum = $(frm).find('div.mw-summary-preview') //create summary preview if needed if (!$sum.length) $sum = $(' ').insertAfter('#wpSummary') $sum.html(' '+resp.parsedsummary['*']+' ') }     if (resp.modules) { mw.loader.load( resp.modules.concat( resp.modulescripts, resp.modulestyles, resp.modulemessages ) ); }   }  }catch (err) {  var htm = 'error: ' + err }

htm = (scriptTip ?    ' ! '     +scriptTip+' ' : '') + ''+btn.val+' (ajax) ' // font-style:italic; text-align:right + htm wkPreview.html(htm).show

// New content is available, fire the hook mw.hook( 'wikipage.content' ).fire( wkPreview );

if (window.ajaxPreviewScrollTop) wkPreview[0].scrollIntoView else $(window).scrollTop( $(window).scrollTop + wkPreview.height - oldHeight )

if (!isDiff) finalizePreview(resp)

}

function finalizePreview(resp){

if (!cssPreview) cssPreview = mw.util.addCSS('span.editsection {display:none} .hiddencats {opacity:0.5}')  // demonstrate that hiddencats will not be updated if (window.collapsibleDivs){ //ruwiki collapsibleDivs } if (window.collapsibleTables){ //ruwiki collapsibleTables } if ( $( '.tex' ).length && mw.user.options.get('math') === '6' ) { // 6 == mathjax mode mw.loader.using( 'ext.math.mathjax.enabler', function {      $('#wikiPreview').renderTex;    } ); } if (window.ajaxPreviewExec) ajaxPreviewExec(wkPreview[0])

// !!! 36476 if( $('body').hasClass('ltr') ) $('#wikiPreview').addClass('mw-content-ltr')

if (!isFullPreview){ if (!cssOutdated) cssOutdated = mw.util.addCSS('.templatesUsed, #p-lang, #catlinks {opacity:0.5}') else cssOutdated.disabled = false //$('#p-lang, #catlinks').attr('title', 'Not updated by latest preview') return }

//otherwise update other areas if (cssOutdated) cssOutdated.disabled = true

$('#catlinks').replaceWith(resp.categorieshtml['*'])

if( resp.languageshtml ) { htm = resp.languageshtml['*'] var plang = $('#p-lang') if (!plang.length) plang = $('#p-tb').clone(true).attr('id','p-lang').insertAfter('#p-tb') .find('h5').text( htm.replace(/[:<].+$/,'') ).end plang.find('ul').html(     '' + htm.replace(/^[^<]*/,'').replace(/<\/a>[^<]+/g,'') + ''    ) }

htm = '' var tt = resp.templates for (var i=0; i' + tt[i]['*'] + '' $('#editform').find('div.templatesUsed').find('ul').html(htm) }

}