2012-10-07 00:41:25 +00:00
|
|
|
( function ( $, mw ) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Debug console
|
|
|
|
* Based on JavaScript Shell 1.4 by Jesse Ruderman (GPL/LGPL/MPL tri-license)
|
|
|
|
*
|
|
|
|
* TODO:
|
|
|
|
* * Refactor, more jQuery, etc.
|
|
|
|
* * Spinner?
|
|
|
|
* * A prompt in front of input lines and the textarea
|
|
|
|
* * Collapsible backtrace display
|
|
|
|
*/
|
|
|
|
|
|
|
|
var
|
|
|
|
histList = [""],
|
|
|
|
histPos = 0,
|
|
|
|
question,
|
|
|
|
_in,
|
|
|
|
_out,
|
|
|
|
lastError = null,
|
|
|
|
sessionContent = null,
|
|
|
|
sessionKey = null,
|
|
|
|
pending = false,
|
|
|
|
clearNextRequest = false;
|
|
|
|
|
|
|
|
function refocus() {
|
|
|
|
_in.blur(); // Needed for Mozilla to scroll correctly.
|
|
|
|
_in.focus();
|
2012-10-07 00:18:42 +00:00
|
|
|
}
|
2012-07-14 04:23:42 +00:00
|
|
|
|
2012-10-07 00:41:25 +00:00
|
|
|
function initConsole() {
|
|
|
|
_in = document.getElementById( "mw-scribunto-input" );
|
|
|
|
_out = document.getElementById( "mw-scribunto-output" );
|
2012-07-14 04:23:42 +00:00
|
|
|
|
2012-10-07 00:41:25 +00:00
|
|
|
recalculateInputHeight();
|
|
|
|
println( mw.msg( 'scribunto-console-intro' ), 'mw-scribunto-message' );
|
|
|
|
}
|
2012-07-14 04:23:42 +00:00
|
|
|
|
2012-10-07 00:41:25 +00:00
|
|
|
function inputKeydown( e ) {
|
|
|
|
// Use onkeydown because IE doesn't support onkeypress for arrow keys
|
|
|
|
|
|
|
|
if ( e.shiftKey && e.keyCode === 13 ) { // shift-enter
|
|
|
|
// don't do anything; allow the shift-enter to insert a line break as normal
|
|
|
|
} else if ( e.keyCode === 13 ) { // enter
|
|
|
|
// execute the input on enter
|
|
|
|
go();
|
|
|
|
} else if ( e.keyCode === 38 ) { // up
|
|
|
|
// go up in history if at top or ctrl-up
|
|
|
|
if ( e.ctrlKey || caretInFirstLine( _in ) ) {
|
|
|
|
hist( 'up' );
|
|
|
|
}
|
|
|
|
} else if ( e.keyCode === 40 ) { // down
|
|
|
|
// go down in history if at end or ctrl-down
|
|
|
|
if ( e.ctrlKey || caretInLastLine( _in ) ) {
|
|
|
|
hist( 'down' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout( recalculateInputHeight, 0 );
|
|
|
|
|
|
|
|
//return true;
|
2012-07-14 04:23:42 +00:00
|
|
|
}
|
|
|
|
|
2012-10-07 00:41:25 +00:00
|
|
|
function inputFocus( e ) {
|
|
|
|
if ( sessionContent === null ) {
|
|
|
|
// No previous state to clear
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( clearNextRequest ) {
|
|
|
|
// User already knows
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( getContent() !== sessionContent ) {
|
|
|
|
printClearBar();
|
|
|
|
clearNextRequest = true;
|
|
|
|
}
|
2012-07-14 04:23:42 +00:00
|
|
|
}
|
|
|
|
|
2012-10-07 00:41:25 +00:00
|
|
|
function caretInFirstLine( textbox ) {
|
|
|
|
// IE doesn't support selectionStart/selectionEnd
|
|
|
|
if ( textbox.selectionStart === undefined ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
var firstLineBreak = textbox.value.indexOf( "\n" );
|
|
|
|
|
|
|
|
return ((firstLineBreak === -1) || (textbox.selectionStart <= firstLineBreak));
|
2012-07-14 04:23:42 +00:00
|
|
|
}
|
|
|
|
|
2012-10-07 00:41:25 +00:00
|
|
|
function caretInLastLine( textbox ) {
|
|
|
|
// IE doesn't support selectionStart/selectionEnd
|
|
|
|
if ( textbox.selectionEnd === undefined ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
var lastLineBreak = textbox.value.lastIndexOf( "\n" );
|
|
|
|
|
|
|
|
return ( textbox.selectionEnd > lastLineBreak );
|
2012-10-07 00:18:42 +00:00
|
|
|
}
|
2012-07-14 04:23:42 +00:00
|
|
|
|
2012-10-07 00:41:25 +00:00
|
|
|
function recalculateInputHeight() {
|
|
|
|
var rows = _in.value.split( /\n/ ).length
|
|
|
|
+ 1 // prevent scrollbar flickering in Mozilla
|
|
|
|
+ ( window.opera ? 1 : 0 ); // leave room for scrollbar in Opera
|
2012-07-14 04:23:42 +00:00
|
|
|
|
2012-10-07 00:41:25 +00:00
|
|
|
// without this check, it is impossible to select text in Opera 7.60 or Opera 8.0.
|
|
|
|
if ( _in.rows !== rows ) {
|
|
|
|
_in.rows = rows;
|
|
|
|
}
|
2012-10-07 00:18:42 +00:00
|
|
|
}
|
2012-07-14 04:23:42 +00:00
|
|
|
|
2012-10-07 00:41:25 +00:00
|
|
|
function println( s, type ) {
|
|
|
|
if ( ( s = String( s ) ) ) {
|
|
|
|
var newdiv = document.createElement( "div" );
|
|
|
|
newdiv.appendChild( document.createTextNode( s ) );
|
|
|
|
newdiv.className = type;
|
|
|
|
_out.appendChild( newdiv );
|
|
|
|
return newdiv;
|
|
|
|
}
|
2012-10-07 00:18:42 +00:00
|
|
|
}
|
2012-10-07 00:41:25 +00:00
|
|
|
|
|
|
|
function printWithRunin( h, s, type ) {
|
|
|
|
var div = println( s, type );
|
|
|
|
var head = document.createElement( "strong" );
|
|
|
|
head.appendChild( document.createTextNode( h + ": " ) );
|
|
|
|
div.insertBefore( head, div.firstChild );
|
2012-07-14 04:23:42 +00:00
|
|
|
}
|
2012-10-07 00:41:25 +00:00
|
|
|
|
|
|
|
function printClearBar() {
|
|
|
|
$( '<div/>' )
|
|
|
|
.attr( 'class', 'mw-scribunto-clear' )
|
|
|
|
.text( mw.msg( 'scribunto-console-cleared' ) )
|
|
|
|
.appendTo( _out );
|
2012-10-07 00:18:42 +00:00
|
|
|
}
|
2012-07-14 04:23:42 +00:00
|
|
|
|
2012-10-07 00:41:25 +00:00
|
|
|
function hist( direction ) {
|
|
|
|
// histList[0] = first command entered, [1] = second, etc.
|
|
|
|
// type something, press up --> thing typed is now in "limbo"
|
|
|
|
// (last item in histList) and should be reachable by pressing
|
|
|
|
// down again.
|
2012-07-14 04:23:42 +00:00
|
|
|
|
2012-10-07 00:41:25 +00:00
|
|
|
var L = histList.length;
|
|
|
|
|
|
|
|
if ( L === 1 ) {
|
|
|
|
return;
|
2012-07-14 04:23:42 +00:00
|
|
|
}
|
2012-10-07 00:41:25 +00:00
|
|
|
|
|
|
|
if ( direction === 'up' ) {
|
|
|
|
if ( histPos === L - 1 ) {
|
|
|
|
// Save this entry in case the user hits the down key.
|
|
|
|
histList[histPos] = _in.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( histPos > 0 ) {
|
|
|
|
histPos--;
|
|
|
|
// Use a timeout to prevent up from moving cursor within new text
|
|
|
|
// Set to nothing first for the same reason
|
|
|
|
setTimeout(
|
|
|
|
function () {
|
|
|
|
_in.value = '';
|
|
|
|
_in.value = histList[histPos];
|
|
|
|
var caretPos = _in.value.length;
|
|
|
|
if ( _in.setSelectionRange ) {
|
|
|
|
_in.setSelectionRange( caretPos, caretPos );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
2012-07-14 04:23:42 +00:00
|
|
|
}
|
2012-10-07 00:41:25 +00:00
|
|
|
else // down
|
2012-07-14 04:23:42 +00:00
|
|
|
{
|
2012-10-07 00:41:25 +00:00
|
|
|
if ( histPos < L - 1 ) {
|
|
|
|
histPos++;
|
|
|
|
_in.value = histList[histPos];
|
|
|
|
}
|
|
|
|
else if ( histPos === L - 1 ) {
|
|
|
|
// Already on the current entry: clear but save
|
|
|
|
if ( _in.value ) {
|
|
|
|
histList[histPos] = _in.value;
|
|
|
|
++histPos;
|
|
|
|
_in.value = "";
|
|
|
|
}
|
2012-07-14 04:23:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-07 00:41:25 +00:00
|
|
|
|
|
|
|
function printQuestion( q ) {
|
|
|
|
println( q, "mw-scribunto-input" );
|
2012-10-07 00:18:42 +00:00
|
|
|
}
|
2012-10-07 00:41:25 +00:00
|
|
|
|
|
|
|
function printError( er ) {
|
|
|
|
var lineNumberString;
|
|
|
|
|
|
|
|
lastError = er; // for debugging the shell
|
|
|
|
if ( er.name ) {
|
|
|
|
// lineNumberString should not be "", to avoid a very wacky bug in IE 6.
|
|
|
|
lineNumberString = (er.lineNumber !== undefined) ? (" on line " + er.lineNumber + ": ") : ": ";
|
|
|
|
// Because IE doesn't have error.toString.
|
|
|
|
println( er.name + lineNumberString + er.message, "mw-scribunto-error" );
|
|
|
|
} else {
|
|
|
|
println( er, "mw-scribunto-error" ); // Because security errors in Moz /only/ have toString.
|
|
|
|
}
|
2012-07-14 04:23:42 +00:00
|
|
|
}
|
|
|
|
|
2012-10-07 00:41:25 +00:00
|
|
|
function setPending() {
|
|
|
|
pending = true;
|
|
|
|
_in.readOnly = true;
|
|
|
|
}
|
2012-07-14 04:23:42 +00:00
|
|
|
|
2012-10-07 00:41:25 +00:00
|
|
|
function clearPending() {
|
|
|
|
pending = false;
|
|
|
|
_in.readOnly = false;
|
2012-10-07 00:18:42 +00:00
|
|
|
}
|
2012-07-14 04:23:42 +00:00
|
|
|
|
2012-10-07 00:41:25 +00:00
|
|
|
function go() {
|
|
|
|
if ( pending ) {
|
|
|
|
// If there is an XHR request pending, don't send another one
|
|
|
|
// We set readOnly on the textarea to give a UI indication, this is
|
|
|
|
// just for paranoia.
|
|
|
|
return;
|
|
|
|
}
|
2012-07-14 04:23:42 +00:00
|
|
|
|
2012-10-07 00:41:25 +00:00
|
|
|
question = _in.value;
|
|
|
|
|
|
|
|
if ( question === "" ) {
|
|
|
|
return;
|
|
|
|
}
|
2012-07-14 04:23:42 +00:00
|
|
|
|
2012-10-07 00:41:25 +00:00
|
|
|
histList[histList.length - 1] = question;
|
|
|
|
histList[histList.length] = "";
|
|
|
|
histPos = histList.length - 1;
|
|
|
|
|
|
|
|
// Unfortunately, this has to happen *before* the script is run, so that
|
|
|
|
// print() output will go in the right place.
|
|
|
|
_in.value = '';
|
|
|
|
// can't preventDefault on input, so also clear it later
|
|
|
|
setTimeout( function () {
|
|
|
|
_in.value = "";
|
|
|
|
}, 0 );
|
|
|
|
|
|
|
|
recalculateInputHeight();
|
|
|
|
printQuestion( question );
|
|
|
|
|
|
|
|
var params = {
|
|
|
|
action: 'scribunto-console',
|
|
|
|
title: mw.config.get( 'wgTitle' ),
|
|
|
|
question: question
|
|
|
|
};
|
|
|
|
|
|
|
|
var content = getContent();
|
|
|
|
if ( !sessionKey || sessionContent !== content ) {
|
|
|
|
params.clear = true;
|
|
|
|
params.content = content;
|
|
|
|
}
|
|
|
|
if ( sessionKey ) {
|
|
|
|
params.session = sessionKey;
|
|
|
|
}
|
|
|
|
if ( clearNextRequest ) {
|
|
|
|
params.clear = true;
|
|
|
|
clearNextRequest = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var api = new mw.Api();
|
|
|
|
setPending();
|
|
|
|
|
|
|
|
api.post( params, {
|
|
|
|
ok: function ( result ) {
|
|
|
|
sessionKey = result.session;
|
|
|
|
sessionContent = content;
|
|
|
|
if ( result.type === 'error' ) {
|
|
|
|
printError( result.message );
|
|
|
|
} else {
|
|
|
|
if ( result.print !== '' ) {
|
|
|
|
println( result.print, 'mw-scribunto-print' );
|
|
|
|
}
|
|
|
|
if ( result['return'] !== '' ) {
|
|
|
|
println( result['return'], "mw-scribunto-normalOutput" );
|
|
|
|
}
|
2012-07-14 04:23:42 +00:00
|
|
|
}
|
2012-10-07 00:41:25 +00:00
|
|
|
clearPending();
|
|
|
|
setTimeout( refocus, 0 );
|
|
|
|
},
|
|
|
|
|
|
|
|
err: function ( code, result ) {
|
|
|
|
if ( 'error' in result && 'info' in result.error ) {
|
|
|
|
printError( result.error.info );
|
|
|
|
} else if ( 'exception' in result ) {
|
2012-12-03 06:14:56 +00:00
|
|
|
printError( 'Error sending API request: ' + result.exception );
|
2012-10-07 00:41:25 +00:00
|
|
|
} else {
|
|
|
|
console.log( result );
|
|
|
|
printError( 'error' );
|
2012-07-14 04:23:42 +00:00
|
|
|
}
|
2012-10-07 00:41:25 +00:00
|
|
|
clearPending();
|
|
|
|
setTimeout( refocus, 0 );
|
2012-07-14 04:23:42 +00:00
|
|
|
}
|
2012-10-07 00:41:25 +00:00
|
|
|
} );
|
|
|
|
}
|
2012-07-14 04:23:42 +00:00
|
|
|
|
2012-10-07 00:41:25 +00:00
|
|
|
function getContent() {
|
|
|
|
var $textarea = $( '#wpTextbox1' ),
|
|
|
|
context = $textarea.data( 'wikiEditor-context' );
|
2012-07-14 04:23:42 +00:00
|
|
|
|
2012-10-07 00:41:25 +00:00
|
|
|
if ( context === undefined || context.codeEditor === undefined ) {
|
|
|
|
return $textarea.val();
|
|
|
|
} else {
|
|
|
|
return $textarea.textSelection( 'getContents' );
|
|
|
|
}
|
|
|
|
}
|
2012-10-07 00:18:42 +00:00
|
|
|
|
2012-10-07 00:41:25 +00:00
|
|
|
function onClearClick( e ) {
|
|
|
|
$( '#mw-scribunto-output' ).empty();
|
|
|
|
clearNextRequest = true;
|
|
|
|
refocus();
|
2012-07-14 04:23:42 +00:00
|
|
|
}
|
|
|
|
|
2012-10-07 00:41:25 +00:00
|
|
|
mw.scribunto.edit = {
|
|
|
|
'init': function () {
|
|
|
|
var action = mw.config.get( 'wgAction' );
|
|
|
|
if ( action === 'edit' || action === 'submit' || action === 'editredlink' ) {
|
|
|
|
this.initEditPage();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'initEditPage': function () {
|
|
|
|
var console = document.getElementById( 'mw-scribunto-console' );
|
|
|
|
if ( !console ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$( '<fieldset/>' )
|
|
|
|
.attr( 'class', 'mw-scribunto-console-fieldset' )
|
|
|
|
.append( $( '<legend/>' ).text( mw.msg( 'scribunto-console-title' ) ) )
|
|
|
|
.append( $( '<div id="mw-scribunto-output"></div>' ) )
|
|
|
|
.append(
|
2012-07-14 04:23:42 +00:00
|
|
|
$( '<div/>' ).append(
|
|
|
|
$( '<textarea/>' )
|
2012-10-07 00:41:25 +00:00
|
|
|
.attr( {
|
2012-07-14 04:23:42 +00:00
|
|
|
id: 'mw-scribunto-input',
|
|
|
|
'class': 'mw-scribunto-input',
|
|
|
|
wrap: 'off',
|
2012-08-15 09:16:43 +00:00
|
|
|
rows: 1,
|
|
|
|
dir: 'ltr',
|
|
|
|
lang: 'en'
|
2012-10-07 00:41:25 +00:00
|
|
|
} )
|
2012-07-14 04:23:42 +00:00
|
|
|
.bind( 'keydown', inputKeydown )
|
|
|
|
.bind( 'focus', inputFocus )
|
|
|
|
)
|
|
|
|
)
|
2012-10-07 00:41:25 +00:00
|
|
|
.append(
|
2012-07-14 04:23:42 +00:00
|
|
|
$( '<div/>' ).append(
|
|
|
|
$( '<input/>' )
|
2012-10-07 00:41:25 +00:00
|
|
|
.attr( {
|
2012-07-14 04:23:42 +00:00
|
|
|
type: 'button',
|
|
|
|
value: mw.msg( 'scribunto-console-clear' )
|
2012-10-07 00:41:25 +00:00
|
|
|
} )
|
2012-07-14 04:23:42 +00:00
|
|
|
.bind( 'click', onClearClick )
|
|
|
|
)
|
|
|
|
)
|
2012-10-07 00:41:25 +00:00
|
|
|
.wrap( '<form/>' )
|
|
|
|
.appendTo( console );
|
|
|
|
initConsole();
|
|
|
|
}
|
|
|
|
};
|
2012-07-14 04:23:42 +00:00
|
|
|
|
2012-10-07 00:41:25 +00:00
|
|
|
$( document ).ready( function () {
|
|
|
|
mw.scribunto.edit.init();
|
|
|
|
} );
|
2012-07-14 04:23:42 +00:00
|
|
|
|
2012-10-07 00:41:25 +00:00
|
|
|
} ) ( jQuery, mediaWiki );
|
2012-07-14 04:23:42 +00:00
|
|
|
|