* Fix r111983 (bug 34662): make mw.toolbar.addButton() work even after DOM ready
authorTim Starling <tstarling@users.mediawiki.org>
Sun, 26 Feb 2012 22:46:40 +0000 (22:46 +0000)
committerTim Starling <tstarling@users.mediawiki.org>
Sun, 26 Feb 2012 22:46:40 +0000 (22:46 +0000)
* Don't use an empty array as the default for $toolbar, use false which will throw an exception on a logic error.
* Fix exception handling in mw.loader: have handlePending() rethrow its exception so that the exception handler in execute() can run, so that the exception gets logged. But don't rethrow the exception in execute(), because that may break other modules.

resources/mediawiki.action/mediawiki.action.edit.js
resources/mediawiki/mediawiki.js

index 499aba6..c5e38ff 100644 (file)
@@ -3,12 +3,17 @@
        var currentFocused = $( '#wpTextbox1' );
 
        mw.toolbar = {
-               $toolbar : [],
+               $toolbar : false,
                buttons : [],
+               isReady : false,
                // If you want to add buttons, use
                // mw.toolbar.addButton( imageFile, speedTip, tagOpen, tagClose, sampleText, imageId, selectText );
                addButton : function() {
-                       this.buttons.push( [].slice.call( arguments ) );
+                       if ( isReady ) {
+                               this.insertButton.apply( this, arguments );
+                       } else {
+                               this.buttons.push( [].slice.call( arguments ) );
+                       }       
                },
                insertButton : function( imageFile, speedTip, tagOpen, tagClose, sampleText, imageId, selectText ) {
                        var image = $('<img>', {
                init : function() {},
 
                onReady : function() {
-                       mw.toolbar.$toolbar = $( '#toolbar' );
+                       this.$toolbar = $( '#toolbar' );
+                       isReady = true;
                        // Legacy
                        // Merge buttons from mwCustomEditButtons
                        var buttons = [].concat( this.buttons, window.mwCustomEditButtons );
                        for ( var i = 0; i < buttons.length; i++ ) {
                                if ( $.isArray( buttons[i] ) ) {
                                        // Passes our button array as arguments
-                                       mw.toolbar.insertButton.apply( this, buttons[i] );
+                                       this.insertButton.apply( this, buttons[i] );
                                } else {
                                        // Legacy mwCustomEditButtons is an object
                                        var c = buttons[i];
-                                       mw.toolbar.insertButton( c.imageFile, c.speedTip, c.tagOpen, c.tagClose, c.sampleText, c.imageId, c.selectText );
+                                       this.insertButton( c.imageFile, c.speedTip, c.tagOpen, 
+                                               c.tagClose, c.sampleText, c.imageId, c.selectText );
                                }
                        }
                        return true;
index c7e584b..4aee9eb 100644 (file)
@@ -630,6 +630,7 @@ var mw = ( function ( $, undefined ) {
                                                        j -= 1;
                                                }
                                        }
+                                       throw e;
                                }
                        }
        
@@ -785,7 +786,6 @@ var mw = ( function ( $, undefined ) {
                                                console.log( 'mw.loader::execute> Exception thrown by ' + module + ': ' + e.message );
                                        }
                                        registry[module].state = 'error';
-                                       throw e;
                                }
                        }