mediawiki.log.js: Refactor
authorKrinkle <krinkle@users.mediawiki.org>
Sat, 10 Sep 2011 22:18:39 +0000 (22:18 +0000)
committerKrinkle <krinkle@users.mediawiki.org>
Sat, 10 Sep 2011 22:18:39 +0000 (22:18 +0000)
* Add @author info etc. to the file header
* Merge var statements
* remove single quotes around object keys where not needed
* Changing bottom padding on <body> to be relatively added instead of absolutely (body might have a bottom padding already!)
* Add return statement after reaching out to window.console. No need for the rest of the function to be executed in that case. outdenting the rest after the new return statement back one tab (thus removing the else-case)

resources/mediawiki/mediawiki.log.js

index 38f3411..9c3d890 100644 (file)
@@ -1,67 +1,74 @@
-/*
- * Implementation for mediaWiki.log stub
+/**
+ * Logger for MediaWiki javascript.
+ * Implements the stub left by the main 'mediawiki' module.
+ *
+ * @author Michael Dale <mdale@wikimedia.org>
+ * @author Trevor Parscal <tparscal@wikimedia.org>
  */
 
 (function( $ ) {
 
        /**
-        * Log output to the console.
+        * Logs a message to the console.
         *
-        * In the case that the browser does not have a console available, one is created by appending a
-        * <div> element to the bottom of the body and then appending a <div> element to that for each
-        * message.
+        * In the case the browser does not have a console API, a console is created on-the-fly by appending
+        * a <div id="mw-log-console"> element to the bottom of the body and then appending this and future
+        * messages to that, instead of the console.
         *
-        * @author Michael Dale <mdale@wikimedia.org>
-        * @author Trevor Parscal <tparscal@wikimedia.org>
-        * @param logmsg string Message to output to console.
+        * @param logmsg {String} Messages to output to the console.
         */
        mw.log = function( logmsg ) {
                // Allow log messages to use a configured prefix to identify the source window (ie. frame)
                if ( mw.config.exists( 'mw.log.prefix' ) ) {
                        logmsg = mw.config.get( 'mw.log.prefix' ) + '> ' + logmsg;
                }
+
                // Try to use an existing console
                if ( window.console !== undefined && $.isFunction( window.console.log ) ) {
                        window.console.log( logmsg );
-               } else {
-                       // Set timestamp
-                       var d = new Date();
-                       var time = ( d.getHours() < 10 ? '0' + d.getHours() : d.getHours() ) +
+                       return;
+               }
+
+               // If there is no console, use our own log box
+
+                       var     d = new Date(),
+                               // Create HH:MM:SS.MIL timestamp
+                               time = ( d.getHours() < 10 ? '0' + d.getHours() : d.getHours() ) +
                                 ':' + ( d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes() ) +
                                 ':' + ( d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds() ) +
-                                '.' + ( d.getMilliseconds() < 10 ? '00' + d.getMilliseconds() : ( d.getMilliseconds() < 100 ? '0' + d.getMilliseconds() : d.getMilliseconds() ) );
-                       // Show a log box for console-less browsers
-                       var $log = $( '#mw-log-console' );
+                                '.' + ( d.getMilliseconds() < 10 ? '00' + d.getMilliseconds() : ( d.getMilliseconds() < 100 ? '0' + d.getMilliseconds() : d.getMilliseconds() ) ),
+                                $log = $( '#mw-log-console' );
+       
                        if ( !$log.length ) {
-                               $log = $( '<div id="mw-log-console"></div>' )
-                                       .css( {
-                                               'position': 'fixed',
-                                               'overflow': 'auto',
-                                               'z-index': 500,
-                                               'bottom': '0px',
-                                               'left': '0px',
-                                               'right': '0px',
-                                               'height': '150px',
-                                               'background-color': 'white',
-                                               'border-top': 'solid 2px #ADADAD'
+                               $log = $( '<div id="mw-log-console"></div>' ).css( {
+                                               position: 'fixed',
+                                               overflow: 'auto',
+                                               zIndex: 500,
+                                               bottom: '0px',
+                                               left: '0px',
+                                               right: '0px',
+                                               height: '150px',
+                                               backgroundColor: 'white',
+                                               borderTop: 'solid 2px #ADADAD'
                                        } );
                                $( 'body' )
-                                       .css( 'padding-bottom', '150px' ) // don't hide anything
+                                       // Since the position is fixed, make sure we don't hide any actual content.
+                                       // Increase padding to account for #mw-log-console.
+                                       .css( 'paddingBottom', '+=150px' )
                                        .append( $log );
                        }
                        $log.append(
                                $( '<div></div>' )
                                        .css( {
-                                               'border-bottom': 'solid 1px #DDDDDD',
-                                               'font-size': 'small',
-                                               'font-family': 'monospace',
-                                               'white-space': 'pre-wrap',
-                                               'padding': '0.125em 0.25em'
+                                               borderBottom: 'solid 1px #DDDDDD',
+                                               fontSize: 'small',
+                                               fontFamily: 'monospace',
+                                               whiteSpace: 'pre-wrap',
+                                               padding: '0.125em 0.25em'
                                        } )
                                        .text( logmsg )
-                                       .prepend( '<span style="float:right">[' + time + ']</span>' )
-                       );
-               }
+                                       .prepend( '<span style="float: right;">[' + time + ']</span>' )
+               );
        };
 
-})(jQuery);
+})( jQuery );