MediaWiki:Gadget-ajaxPreview.js

/* */

/** * ajaxPreview * Source: http://en.wikipedia.org/wiki/User:Js/ajaxPreview * Slightly altered: *   1) Original script consists of two pages; combined these into one. *    2) Added support for collapsible elements & sortable tables in preview. *   3) Made compatible with ResourceLoader. *    4) Removed loading syntaxhighlight CSS from en.wikipedia (moved to MediaWiki:Gadget-ajaxPreview.css). *   5) Removed diff CSS from bits.wikimedia.org (loading local diff CSS instead). */

window.ajaxPreviewPos = 'bottom'

/* ajaxPreview.js * Source: http://en.wikipedia.org/wiki/User:Js/ajaxPreview.js */

jQuery(function($) {

window.ajaxPreviewMsg = {emptydiff: 'No changes' ,difftip: 'shift-click the button to show changes compared to this old version' ,diff2old: 'comparison to old version' ,viewtip: 'shift-click the button to update interwiki and categories as well\ (more)' } function ajaxPreviewButtons{ var ajaxPreviewPos = window.ajaxPreviewPos || 'right' if( ajaxPreviewPos != 'bottom' ){ var previewToolbar = $('') if( $.wikiEditor ){ $('#wikiPreview').after(' ', previewToolbar) }else{ var el = $('#toolbar') if( el.length ) el.prepend(previewToolbar) else $('#editform').before(previewToolbar) } } addBtn(window.ajaxPreviewButton, 'wpPreview', window.ajaxPreviewKey || 'p') if( wgArticleId ) addBtn(window.ajaxDiffButton, 'wpDiff', window.ajaxDiffKey || 'v') function addBtn(name, id, akey){ var btnOld = $('#'+id) if( !btnOld ) return var btn = $('') if( ajaxPreviewPos == 'bottom' ){ btn.val( btnOld.val ).insertBefore( btnOld.val('>') ) }else{ if( !name ){ //extract last word from standard buttons var name = btnOld.val; var i = name.lastIndexOf(' ') + 1 name = name.substring(i, i+1).toUpperCase + name.substring(i+1) }   btn.val(name).css({height:'22px', padding:'0 1px'}).appendTo(previewToolbar) } if( akey ){ //reassign acces key if( btnOld.attr('accesskey') == akey ) btnOld.removeAttr('accesskey').attr('title', btnOld.attr('title').replace(tooltipAccessKeyRegexp, '') ) btn.attr('accesskey', akey).attr('title', btn.attr('title') + ' ['+tooltipAccessKeyPrefix+akey+']' ) } } }

if( /^(edit|submit)$/.test(wgAction) && wgCanonicalNamespace != 'Special' ){ ajaxPreviewButtons

/* preview2.js * Source: http://en.wikipedia.org/wiki/User:Js/preview2.js */

ajaxPreview = new function{

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

var cssWait, cssDiff, 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 = wgScriptPath + '/api.php?format=json', data, ext var txt = frm.wpTextbox1.value

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

if (isDiff){

if( !cssDiff ) cssDiff = importStylesheetURI(wgScriptPath + '/load.php?modules=mediawiki.action.history.diff&only=styles')

url += '&action=query&prop=revisions' data = { titles: 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['*']+' ') }   }  }catch (err) {  var htm = 'error: ' + err }

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

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 ($('#wikiPreview .mw-collapsible').length) { $('#wikiPreview .mw-collapsible').makeCollapsible; } if (window.ajaxPreviewExec) { ajaxPreviewExec(wkPreview[0]) } if ($('#wikiPreview table.sortable').length) { mw.loader.using('jquery.tablesorter', function {     $('#wikiPreview table.sortable').tablesorter;    }); }

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['*'])

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) }

}

}

});

/* */