dbg toolbar: prevents screen jumping
[lhc/web/wiklou.git] / resources / mediawiki / mediawiki.debug.js
1 /**
2 * JavaScript for the new debug toolbar, enabled with $wgDebugToolbar
3 *
4 * @author John Du Hart
5 * @since 1.19
6 */
7
8 ( function ( $, mw, undefined ) {
9 "use strict";
10
11 var hovzer = $.getFootHovzer();
12
13 var debug = mw.Debug = {
14 /**
15 * Toolbar container element
16 *
17 * @var {jQuery}
18 */
19 $container: null,
20
21 /**
22 * Object containing data for the debug toolbar
23 *
24 * @var {Object}
25 */
26 data: {},
27
28 /**
29 * Initializes the debugging pane
30 *
31 * @param {Object} data
32 */
33 init: function ( data ) {
34
35 this.data = data;
36 this.buildHtml();
37
38 // Insert the container into the DOM
39 hovzer.$.append( this.$container );
40 hovzer.update();
41
42 $( '.mw-debug-panelink' ).click( this.switchPane );
43 },
44
45 /**
46 * Switches between panes
47 *
48 * @todo Store cookie for last pane open
49 * @context {Element}
50 * @param {jQuery.Event} e
51 */
52 switchPane: function ( e ) {
53 var currentPaneId = debug.$container.data( 'currentPane' ),
54 requestedPaneId = $(this).prop( 'id' ).substr( 9 ),
55 $currentPane = $( '#mw-debug-pane-' + currentPaneId ),
56 $requestedPane = $( '#mw-debug-pane-' + requestedPaneId ),
57 hovDone = false;
58
59 function updateHov() {
60 if ( !hovDone ) {
61 hovzer.update();
62 hovDone = true;
63 }
64 }
65
66 // Skip hash fragment handling. Prevents screen from jumping.
67 e.preventDefault();
68
69 $( this ).addClass( 'current ');
70 $( '.mw-debug-panelink' ).not( this ).removeClass( 'current ');
71
72 // Hide the current pane
73 if ( requestedPaneId === currentPaneId ) {
74 $currentPane.slideUp( updateHov );
75 debug.$container.data( 'currentPane', null );
76 return;
77 }
78
79 debug.$container.data( 'currentPane', requestedPaneId );
80
81 if ( currentPaneId === undefined || currentPaneId === null ) {
82 $requestedPane.slideDown( updateHov );
83 } else {
84 $currentPane.hide();
85 $requestedPane.show();
86 updateHov();
87 }
88 },
89
90 /**
91 * Constructs the HTML for the debugging toolbar
92 */
93 buildHtml: function () {
94 var $container, $bits, panes, id;
95
96 $container = $( '<div id="mw-debug-toolbar" class="mw-debug"></div>' );
97
98 $bits = $( '<div class="mw-debug-bits"></div>' );
99
100 /**
101 * Returns a jQuery element for a debug-bit div
102 *
103 * @param id
104 * @return {jQuery}
105 */
106 function bitDiv( id ) {
107 return $( '<div>' ).attr({
108 id: 'mw-debug-' + id,
109 'class': 'mw-debug-bit'
110 })
111 .appendTo( $bits );
112 }
113
114 /**
115 * Returns a jQuery element for a pane link
116 *
117 * @param id
118 * @param text
119 * @return {jQuery}
120 */
121 function paneLabel( id, text ) {
122 return $( '<a>' )
123 .attr({
124 'class': 'mw-debug-panelabel',
125 href: '#mw-debug-pane-' + id
126 })
127 .text( text );
128 }
129
130 /**
131 * Returns a jQuery element for a debug-bit div with a for a pane link
132 *
133 * @param id CSS id snippet. Will be prefixed with 'mw-debug-'
134 * @param text Text to show
135 * @param count Optional count to show
136 * @return {jQuery}
137 */
138 function paneTriggerBitDiv( id, text, count ) {
139 if( count ) {
140 text = text + ' (' + count + ')';
141 }
142 return $( '<div>' ).attr({
143 id: 'mw-debug-' + id,
144 'class': 'mw-debug-bit mw-debug-panelink'
145 })
146 .append( paneLabel( id, text ) )
147 .appendTo( $bits );
148 }
149
150 paneTriggerBitDiv( 'console', 'Console', this.data.log.length );
151
152 paneTriggerBitDiv( 'querylist', 'Queries', this.data.queries.length );
153
154 paneTriggerBitDiv( 'debuglog', 'Debug Log', this.data.debugLog.length );
155
156 paneTriggerBitDiv( 'request', 'Request' );
157
158 paneTriggerBitDiv( 'includes', 'PHP includes', this.data.includes.length );
159
160 bitDiv( 'mwversion' )
161 .append( $( '<a href="//www.mediawiki.org/"></a>' ).text( 'MediaWiki' ) )
162 .append( ': ' + this.data.mwVersion );
163
164 bitDiv( 'phpversion' )
165 .append( $( '<a href="//www.php.net/"></a>' ).text( 'PHP' ) )
166 .append( ': ' + this.data.phpVersion );
167
168 bitDiv( 'time' )
169 .text( 'Time: ' + this.data.time.toFixed( 5 ) );
170
171 bitDiv( 'memory' )
172 .text( 'Memory: ' + this.data.memory )
173 .append( $( '<span title="Peak usage"></span>' ).text( ' (' + this.data.memoryPeak + ')' ) );
174
175
176 $bits.appendTo( $container );
177
178 panes = {
179 console: this.buildConsoleTable(),
180 querylist: this.buildQueryTable(),
181 debuglog: this.buildDebugLogTable(),
182 request: this.buildRequestPane(),
183 includes: this.buildIncludesPane()
184 };
185
186 for ( id in panes ) {
187 if ( !panes.hasOwnProperty( id ) ) {
188 continue;
189 }
190
191 $( '<div>' )
192 .attr({
193 'class': 'mw-debug-pane',
194 id: 'mw-debug-pane-' + id
195 })
196 .append( panes[id] )
197 .appendTo( $container );
198 }
199
200 this.$container = $container;
201 },
202
203 /**
204 * Builds the console panel
205 */
206 buildConsoleTable: function () {
207 var $table, entryTypeText, i, length, entry;
208
209 $table = $( '<table id="mw-debug-console">' );
210
211 $('<colgroup>').css( 'width', /*padding=*/20 + ( 10*/*fontSize*/11 ) ).appendTo( $table );
212 $('<colgroup>').appendTo( $table );
213 $('<colgroup>').css( 'width', 350 ).appendTo( $table );
214
215
216 entryTypeText = function( entryType ) {
217 switch ( entryType ) {
218 case 'log':
219 return 'Log';
220 case 'warn':
221 return 'Warning';
222 case 'deprecated':
223 return 'Deprecated';
224 default:
225 return 'Unknown';
226 }
227 };
228
229 for ( i = 0, length = this.data.log.length; i < length; i += 1 ) {
230 entry = this.data.log[i];
231 entry.typeText = entryTypeText( entry.type );
232
233 $( '<tr>' )
234 .append( $( '<td>' )
235 .text( entry.typeText )
236 .attr( 'class', 'mw-debug-console-' + entry.type )
237 )
238 .append( $( '<td>' ).html( entry.msg ) )
239 .append( $( '<td>' ).text( entry.caller ) )
240 .appendTo( $table );
241 }
242
243 return $table;
244 },
245
246 /**
247 * Query list pane
248 */
249 buildQueryTable: function () {
250 var $table, i, length, query;
251
252 $table = $( '<table id="mw-debug-querylist"></table>' );
253
254 $( '<tr>' )
255 .append( $('<th>#</th>').css( 'width', '4em' ) )
256 .append( $('<th>SQL</th>') )
257 .append( $('<th>Time</th>').css( 'width', '8em' ) )
258 .append( $('<th>Call</th>').css( 'width', '12em' ) )
259 .appendTo( $table );
260
261 for ( i = 0, length = this.data.queries.length; i < length; i += 1 ) {
262 query = this.data.queries[i];
263
264 $( '<tr>' )
265 .append( $( '<td>' ).text( i + 1 ) )
266 .append( $( '<td>' ).text( query.sql ) )
267 .append( $( '<td class="stats">' ).text( ( query.time * 1000 ).toFixed( 4 ) + 'ms' ) )
268 .append( $( '<td>' ).text( query['function'] ) )
269 .appendTo( $table );
270 }
271
272
273 return $table;
274 },
275
276 /**
277 * Legacy debug log pane
278 */
279 buildDebugLogTable: function () {
280 var $list, i, length, line;
281 $list = $( '<ul>' );
282
283 for ( i = 0, length = this.data.debugLog.length; i < length; i += 1 ) {
284 line = this.data.debugLog[i];
285 $( '<li>' )
286 .html( mw.html.escape( line ).replace( /\n/g, "<br />\n" ) )
287 .appendTo( $list );
288 }
289
290 return $list;
291 },
292
293 /**
294 * Request information pane
295 */
296 buildRequestPane: function () {
297
298 function buildTable( title, data ) {
299 var $unit, $table, key;
300
301 $unit = $( '<div>' ).append( $( '<h2>' ).text( title ) );
302
303 $table = $( '<table>' ).appendTo( $unit );
304
305 $( '<tr>' )
306 .html( '<th>Key</th><th>Value</th>' )
307 .appendTo( $table );
308
309 for ( key in data ) {
310 if ( !data.hasOwnProperty( key ) ) {
311 continue;
312 }
313
314 $( '<tr>' )
315 .append( $( '<th>' ).text( key ) )
316 .append( $( '<td>' ).text( data[key] ) )
317 .appendTo( $table );
318 }
319
320 return $unit;
321 }
322
323 return $( '<div>' )
324 .text( this.data.request.method + ' ' + this.data.request.url )
325 .append( buildTable( 'Headers', this.data.request.headers ) )
326 .append( buildTable( 'Parameters', this.data.request.params ) );
327 },
328
329 /**
330 * Included files pane
331 */
332 buildIncludesPane: function () {
333 var $table, i, length, file;
334
335 $table = $( '<table>' );
336
337 for ( i = 0, length = this.data.includes.length; i < length; i += 1 ) {
338 file = this.data.includes[i];
339 $( '<tr>' )
340 .append( $( '<td>' ).text( file.name ) )
341 .append( $( '<td class="nr">' ).text( file.size ) )
342 .appendTo( $table );
343 }
344
345 return $table;
346 }
347 };
348
349 } )( jQuery, mediaWiki );