summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Scribunto/modules/ext.scribunto.edit.js
blob: ff5338996609361bdda85752d7c9afcc1121794a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/* eslint-disable no-use-before-define */

( 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,
		input,
		output,
		$spinner,
		sessionContent = null,
		sessionKey = null,
		pending = false,
		clearNextRequest = false;

	function refocus() {
		// Needed for Mozilla to scroll correctly
		input.blur();
		input.focus();
	}

	function initConsole() {
		input = document.getElementById( 'mw-scribunto-input' );
		output = document.getElementById( 'mw-scribunto-output' );
		$spinner = $.createSpinner( { size: 'small', type: 'block' } );

		recalculateInputHeight();
		println( mw.msg( 'scribunto-console-intro' ), 'mw-scribunto-message' );
	}

	/**
	 * Use onkeydown because IE doesn't support onkeypress for arrow keys
	 *
	 * @param {jQuery.Event} e
	 */
	function inputKeydown( e ) {
		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( input ) ) {
				hist( 'up' );
			}
		} else if ( e.keyCode === 40 ) {
			// down
			// go down in history if at end or ctrl-down
			if ( e.ctrlKey || caretInLastLine( input ) ) {
				hist( 'down' );
			}
		}

		setTimeout( recalculateInputHeight, 0 );
	}

	function inputFocus() {
		if ( sessionContent === null ) {
			// No previous state to clear
			return;
		}

		if ( clearNextRequest ) {
			// User already knows
			return;
		}

		if ( getContent() !== sessionContent ) {
			printClearBar( 'scribunto-console-cleared' );
			clearNextRequest = true;
		}
	}

	function caretInFirstLine( textbox ) {
		var firstLineBreak;

		// IE doesn't support selectionStart/selectionEnd
		if ( textbox.selectionStart === undefined ) {
			return true;
		}

		firstLineBreak = textbox.value.indexOf( '\n' );

		return ( ( firstLineBreak === -1 ) || ( textbox.selectionStart <= firstLineBreak ) );
	}

	function caretInLastLine( textbox ) {
		var lastLineBreak;

		// IE doesn't support selectionStart/selectionEnd
		if ( textbox.selectionEnd === undefined ) {
			return true;
		}

		lastLineBreak = textbox.value.lastIndexOf( '\n' );

		return ( textbox.selectionEnd > lastLineBreak );
	}

	function recalculateInputHeight() {
		var rows = input.value.split( /\n/ ).length +
			// prevent scrollbar flickering in Mozilla
			1 +
			// leave room for scrollbar in Opera
			( window.opera ? 1 : 0 );

		// without this check, it is impossible to select text in Opera 7.60 or Opera 8.0.
		if ( input.rows !== rows ) {
			input.rows = rows;
		}
	}

	function println( s, type ) {
		var newdiv;
		if ( ( s = String( s ) ) ) {
			newdiv = document.createElement( 'div' );
			newdiv.appendChild( document.createTextNode( s ) );
			newdiv.className = type;
			output.appendChild( newdiv );
			return newdiv;
		}
	}

	function printClearBar( msg ) {
		$( '<div>' )
			.attr( 'class', 'mw-scribunto-clear' )
			.text( mw.msg( msg ) )
			.appendTo( output );
	}

	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.

		var L = histList.length;

		if ( L === 1 ) {
			return;
		}

		if ( direction === 'up' ) {
			if ( histPos === L - 1 ) {
				// Save this entry in case the user hits the down key.
				histList[ histPos ] = input.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 () {
						var caretPos;
						input.value = '';
						input.value = histList[ histPos ];
						caretPos = input.value.length;
						if ( input.setSelectionRange ) {
							input.setSelectionRange( caretPos, caretPos );
						}
					},
					0
				);
			}
		} else {
			// direction down
			if ( histPos < L - 1 ) {
				histPos++;
				input.value = histList[ histPos ];
			} else if ( histPos === L - 1 ) {
				// Already on the current entry: clear but save
				if ( input.value ) {
					histList[ histPos ] = input.value;
					++histPos;
					input.value = '';
				}
			}
		}
	}

	function printQuestion( q ) {
		println( q, 'mw-scribunto-input' );
	}

	function printError( er ) {
		var lineNumberString;

		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 {
			// Because security errors in Moz /only/ have toString.
			println( er, 'mw-scribunto-error' );
		}
	}

	function setPending() {
		pending = true;
		input.readOnly = true;
		$spinner.insertBefore( input );
	}

	function clearPending() {
		$spinner.remove();
		pending = false;
		input.readOnly = false;
	}

	function go() {
		var params, api, content, sentContent;

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

		question = input.value;

		if ( question === '' ) {
			return;
		}

		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.
		input.value = '';
		// can't preventDefault on input, so also clear it later
		setTimeout( function () {
			input.value = '';
		}, 0 );

		recalculateInputHeight();
		printQuestion( question );

		params = {
			action: 'scribunto-console',
			title: mw.config.get( 'wgPageName' ),
			question: question
		};

		content = getContent();
		sentContent = false;

		if ( !sessionKey || sessionContent !== content ) {
			params.clear = true;
			params.content = content;
			sentContent = true;
		}
		if ( sessionKey ) {
			params.session = sessionKey;
		}
		if ( clearNextRequest ) {
			params.clear = true;
			clearNextRequest = false;
		}

		api = new mw.Api();
		setPending();

		api.post( params )
			.done( function ( result ) {
				if ( result.sessionIsNew === '' && !sentContent ) {
					// Session was lost. Resend query, with content
					printClearBar( 'scribunto-console-cleared-session-lost' );
					sessionContent = null;
					clearPending();
					input.value = params.question;
					go();
					return;
				}
				sessionKey = result.session;
				sessionContent = content;
				if ( result.type === 'error' ) {
					$( '<div>' ).addClass( 'mw-scribunto-error' ).html( result.html ).appendTo( output );
				} else {
					if ( result.print !== '' ) {
						println( result.print, 'mw-scribunto-print' );
					}
					if ( result.return !== '' ) {
						println( result.return, 'mw-scribunto-normalOutput' );
					}
				}
				clearPending();
				setTimeout( refocus, 0 );
			} )
			.fail( function ( code, result ) {
				if ( result.error && result.error.info ) {
					printError( result.error.info );
				} else if ( result.exception ) {
					printError( 'Error sending API request: ' + result.exception );
				} else {
					mw.log( result );
					printError( 'error' );
				}
				clearPending();
				setTimeout( refocus, 0 );
			} );
	}

	function getContent() {
		var $textarea = $( '#wpTextbox1' ),
			context = $textarea.data( 'wikiEditor-context' );

		if ( context === undefined || context.codeEditor === undefined ) {
			return $textarea.val();
		} else {
			return $textarea.textSelection( 'getContents' );
		}
	}

	function onClearClick() {
		$( '#mw-scribunto-output' ).empty();
		clearNextRequest = true;
		refocus();
	}

	function initEditPage() {
		var $wpTextbox1,
			$console = $( '#mw-scribunto-console' );
		if ( !$console.length ) {
			// There is no console in the DOM; on read-only (protected) pages,
			// we need to add it here, because the hook does not insert
			// it server-side.
			$wpTextbox1 = $( '#wpTextbox1' );
			if ( !$wpTextbox1.length || !$wpTextbox1.prop( 'readonly' ) ) {
				return;
			}

			$console = $( '<div>' ).attr( { id: 'mw-scribunto-console' } );
			$wpTextbox1.after( $console );
		}

		$( '<fieldset>' )
			.attr( 'class', 'mw-scribunto-console-fieldset' )
			.append( $( '<legend>' ).text( mw.msg( 'scribunto-console-title' ) ) )
			.append( $( '<div id="mw-scribunto-output"></div>' ) )
			.append(
				$( '<div>' ).append(
					$( '<textarea>' )
						.attr( {
							id: 'mw-scribunto-input',
							'class': 'mw-scribunto-input',
							wrap: 'off',
							rows: 1,
							dir: 'ltr',
							lang: 'en'
						} )
						.bind( 'keydown', inputKeydown )
						.bind( 'focus', inputFocus )
				)
			)
			.append(
				$( '<div>' ).append(
					$( '<input>' )
						.attr( {
							type: 'button',
							value: mw.msg( 'scribunto-console-clear' )
						} )
						.bind( 'click', onClearClick )
				)
			)
			.wrap( '<form>' )
			.appendTo( $console );

		initConsole();
	}

	$( function () {
		var action = mw.config.get( 'wgAction' );
		if ( action === 'edit' || action === 'submit' || action === 'editredlink' ) {
			initEditPage();
		}
	} );

}( jQuery, mediaWiki ) );