User:Js/ajaxPreview.js: Difference between revisions
< User:Js
Content deleted Content added
m pst= |
m Maintenance: mw:RL/MGU - Updated deprecated module name |
||
(16 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
window.ajaxPreviewMsg = { |
|||
function ajaxPreviewInit(){ |
|||
emptydiff: 'No changes', |
|||
if ((wgNamespaceNumber % 2 == 0) && /\.(js|css)$/.test(wgTitle)) return |
|||
difftip: 'shift-click the button to show changes compared to this old version', |
|||
if (typeof ajaxPreviewKey != 'string') ajaxPreviewKey = 'p' |
|||
diff2old: 'comparison to old version', |
|||
if (typeof ajaxDiffKey != 'string') ajaxDiffKey = 'v' |
|||
viewtip: 'shift-click the button to update interwiki and categories as well\ |
|||
ajaxPreviewPos = window.ajaxPreviewPos || 'right' |
|||
(<a href="//en.wikipedia.org/wiki/User:Js/ajaxPreview#Preview" target=_blank>more</a>)' |
|||
}; |
|||
if (ajaxPreviewPos != 'bottom'){ |
|||
function ajaxPreviewButtons(){ |
|||
var d = document.createElement('div'); d.style.cssText = 'width:100%; clear:both' |
|||
var tNew = document.createElement('div'); tNew.style.cssText = 'float:'+ ajaxPreviewPos |
|||
var ajaxPreviewPos = window.ajaxPreviewPos || 'right' |
|||
if( typeof wgWikiEditorPreferences != 'undefined' && wgWikiEditorPreferences['toolbar'] ) { |
|||
if( !document.getElementById('wpSave') ){ |
|||
return; |
|||
tOld.parentNode.insertBefore(d, tOld.nextSibling); |
|||
} |
|||
tOld.parentNode.insertBefore(tNew, d.nextSibling); |
|||
mw.loader.load( '//en.wikipedia.org/w/index.php?title=User:Js/preview2.js&action=raw&ctype=text/javascript' ); |
|||
if( ajaxPreviewPos != 'bottom' ){ |
|||
var previewToolbar = $('<div style="float:'+ajaxPreviewPos+'" />') |
|||
if ( mw.user.options.get('usebetatoolbar') ) { |
|||
$('#wikiPreview').after('<div style="width:100%; clear:both" />', previewToolbar) |
|||
} else { |
} else { |
||
var |
var el = $('#toolbar') |
||
if( el.length ) el.prepend(previewToolbar) |
|||
tOld.style.clear = 'none' |
|||
else $('#editform').before(previewToolbar) |
|||
tOld.parentNode.insertBefore(d, tOld) |
|||
tOld.parentNode.insertBefore(tNew, tOld) |
|||
} |
} |
||
} |
} |
||
addBtn(window.ajaxPreviewButton, 'wpPreview', ajaxPreviewKey) |
|||
addBtn(window.ajaxDiffButton, 'wpDiff', ajaxDiffKey) |
|||
addBtn(window.ajaxPreviewButton, 'wpPreview', window.ajaxPreviewKey || 'p') |
|||
function addBtn(btnName, id, akey){ |
|||
var btnOld = document.getElementById(id); |
|||
if( mw.config.get( 'wgArticleId' ) ) |
|||
if (!btnOld) return; |
|||
addBtn(window.ajaxDiffButton, 'wpDiff', window.ajaxDiffKey || 'v') |
|||
var btn = document.createElement('input'); |
|||
btn.type = 'button'; |
|||
function addBtn(name, id, akey){ |
|||
btn.onclick = ajaxPreviewClick; |
|||
var $btnOld = $(document.getElementById(id)) |
|||
btn.id = id + 'Live'; |
|||
if( $btnOld.length === 0 ) return |
|||
if (!btnName){ //extract last word from standard buttons |
|||
var $btn = $('<input type="button" />') |
|||
var btnName = btnOld.value.split(' '); btnName = btnName[btnName.length-1]; |
|||
.attr('id', id + 'Live') |
|||
btnName = btnName.substring(0,1).toUpperCase() + btnName.substring(1); |
|||
.attr( 'title', $btnOld.val() + ' (ajax)'); |
|||
} |
|||
if( ajaxPreviewPos == 'bottom' ){ |
|||
btn.value = btnName; |
|||
btn. |
$btn.val( $btnOld.val() ).insertBefore( $btnOld.val('>') ) |
||
if (ajaxPreviewPos == 'bottom'){ |
|||
btnOld.parentNode.insertBefore(btn, btnOld) |
|||
btn.value = btnOld.value |
|||
btnOld.value = '>' |
|||
}else{ |
}else{ |
||
if( !name ){ //extract last word from standard buttons |
|||
btn.style.cssText = 'height:22px; padding:0 1px' |
|||
var name = $btnOld.val(); var i = name.lastIndexOf(' ') + 1 |
|||
tNew.appendChild(btn) |
|||
name = name.substring(i, i+1).toUpperCase() + name.substring(i+1) |
|||
} |
|||
if (akey){ //reassign acces key |
|||
if (btnOld.accessKey == akey){ |
|||
btnOld.accessKey = '' |
|||
btnOld.title = btnOld.title.replace(tooltipAccessKeyRegexp, '') |
|||
} |
} |
||
$btn.val(name).css({height:'22px', padding:'0 1px'}).appendTo(previewToolbar) |
|||
btn.accessKey = akey |
|||
} |
|||
btn.title += ' ['+tooltipAccessKeyPrefix+akey+']' |
|||
if( akey ){ //reassign acces key |
|||
if( $btnOld.attr('accesskey') == akey ) |
|||
$btnOld.removeAttr('accesskey').updateTooltipAccessKeys() |
|||
$btn.attr('accesskey', akey).updateTooltipAccessKeys() |
|||
} |
} |
||
btn.value2 = btn.value |
|||
} |
} |
||
} |
} |
||
Line 57: | Line 58: | ||
if( $.inArray(mw.config.get('wgAction'), ['edit', 'submit']) !== -1 && mw.config.get('wgCanonicalNamespace') !== 'Special' ){ |
|||
function ajaxPreviewClick(){ajaxPreviewRun(this)} |
|||
mw.loader.using( ['mediawiki.util', 'user.options', 'jquery.textSelection'], function () { |
|||
$(ajaxPreviewButtons); |
|||
function ajaxPreviewRun(btn){ |
|||
} ); |
|||
var wkPreview = document.getElementById('wikiPreview') |
|||
var previewform = document.editform; |
|||
var aj = sajax_init_object() |
|||
if (!wkPreview || !previewform || !aj) return |
|||
var oldHeight = wkPreview.offsetHeight |
|||
var el; |
|||
var htm; |
|||
var isDiff = (btn.id=='wpDiffLive'); |
|||
wkPreview.style.opacity = '0.3'; |
|||
wkPreview.style.color = 'gray'; |
|||
document.body.style.cursor = 'wait' |
|||
if (el=document.getElementById('wikiDiff')) el.style.display = 'none' |
|||
if (el=document.getElementById('newarticletext')) el.style.display = 'none' |
|||
btn.style.width = Math.max(btn.scrollWidth, btn.offsetWidth) + 'px'; |
|||
btn.value = '...' |
|||
//prepare |
|||
var txt = previewform.wpTextbox1.value; |
|||
var action = wgScriptPath + "/api.php"; |
|||
var params = ''; |
|||
if (isDiff){ |
|||
params = "action=query&format=json&prop=revisions&rvprop=&indexpageids=&titles="+ encodeURIComponent( wgPageName ); |
|||
params += "&rvdifftotext=" + encodeURIComponent( txt ); |
|||
if (!window.ajaxPreview_CSS) ajaxPreview_CSS = importStylesheetURI('/skins-1.5/common/diff.css') |
|||
}else{ |
|||
params = "action=parse&format=json&pst=&prop=text&title=" + encodeURIComponent( wgPageName ); |
|||
if (previewform.wpSection && previewform.wpSection.value) txt += '\n<br /><references />' |
|||
params += "&text=" + encodeURIComponent( txt ); |
|||
} |
|||
//send |
|||
aj.open('POST', action, true); |
|||
aj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); |
|||
aj.onreadystatechange = function(){ |
|||
if (aj.readyState != 4) return |
|||
wkPreview.style.display = 'block' |
|||
var jsonObject = eval('(' + aj.responseText + ')'); |
|||
if (isDiff){ |
|||
try { |
|||
var htm = '<h2>Changes (Live)</h2>'; |
|||
htm += '<table class="diff"><col class="diff-marker"><col class="diff-content"><col class="diff-marker"><col class="diff-content">'; |
|||
var index = jsonObject['query']['pageids'][0]; |
|||
htm += jsonObject['query']['pages'][index]['revisions'][0]['diff']['*']; |
|||
htm += '</table>'; |
|||
} catch( err ) { |
|||
var htm = 'error: ' + err; |
|||
} |
|||
}else{ |
|||
try { |
|||
var htm = '<h2>Preview (Live)</h2>'; |
|||
htm += jsonObject['parse']['text']['*']; |
|||
} catch( err ) { |
|||
var htm = "error: "+err; |
|||
} |
|||
} |
|||
wkPreview.innerHTML = htm |
|||
btn.value = btn.value2 |
|||
btn.blur() |
|||
if (window.ajaxPreviewScrollTop && wkPreview.scrollIntoView) wkPreview.scrollIntoView() |
|||
else document.documentElement.scrollTop += wkPreview.offsetHeight - oldHeight |
|||
wkPreview.style.opacity = ''; wkPreview.style.color = ''; document.body.style.cursor = '' |
|||
if (!isDiff) ajaxPreviewFinish(wkPreview) |
|||
} |
|||
aj.send(params); |
|||
} |
} |
||
function ajaxPreviewFinish(el){ |
|||
sortables_init() |
|||
if (window.createCollapseButtons){//en.wp |
|||
createCollapseButtons() |
|||
createNavigationBarToggleButton() |
|||
} |
|||
if (window.ajaxPreviewExec) ajaxPreviewExec(el) |
|||
} |
|||
if (wgCanonicalNamespace!='Special' && (wgAction=='edit' || wgAction=='submit')) |
|||
addOnloadHook(ajaxPreviewInit) |
Latest revision as of 17:20, 18 September 2019
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\
(<a href="//en.wikipedia.org/wiki/User:Js/ajaxPreview#Preview" target=_blank>more</a>)'
};
function ajaxPreviewButtons(){
var ajaxPreviewPos = window.ajaxPreviewPos || 'right'
if( !document.getElementById('wpSave') ){
return;
}
mw.loader.load( '//en.wikipedia.org/w/index.php?title=User:Js/preview2.js&action=raw&ctype=text/javascript' );
if( ajaxPreviewPos != 'bottom' ){
var previewToolbar = $('<div style="float:'+ajaxPreviewPos+'" />')
if ( mw.user.options.get('usebetatoolbar') ) {
$('#wikiPreview').after('<div style="width:100%; clear:both" />', previewToolbar)
} else {
var el = $('#toolbar')
if( el.length ) el.prepend(previewToolbar)
else $('#editform').before(previewToolbar)
}
}
addBtn(window.ajaxPreviewButton, 'wpPreview', window.ajaxPreviewKey || 'p')
if( mw.config.get( 'wgArticleId' ) )
addBtn(window.ajaxDiffButton, 'wpDiff', window.ajaxDiffKey || 'v')
function addBtn(name, id, akey){
var $btnOld = $(document.getElementById(id))
if( $btnOld.length === 0 ) return
var $btn = $('<input type="button" />')
.attr('id', id + 'Live')
.attr( 'title', $btnOld.val() + ' (ajax)');
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').updateTooltipAccessKeys()
$btn.attr('accesskey', akey).updateTooltipAccessKeys()
}
}
}
if( $.inArray(mw.config.get('wgAction'), ['edit', 'submit']) !== -1 && mw.config.get('wgCanonicalNamespace') !== 'Special' ){
mw.loader.using( ['mediawiki.util', 'user.options', 'jquery.textSelection'], function () {
$(ajaxPreviewButtons);
} );
}