mw.toolbar: Clean up the API of the classic toolbar.
* Several methods were added here recently during 1.20 development
that should not have been public methods.
* Also in the creation of this new module (replacing the old
mwCustomEditButtons) a design flaw was made. Instead of using
a key-value pair object, the signature was changed to a tentacle
function with 7 (for callers, unnamed) arguments.
* Changed it back with the compatibility fix the other way around.
So everything is backwards compatible.
* Moved to local scope:
- buttons queue
- $toolbar
- insertButton
These were recently introduced during 1.20 development but not
meant to be public. When used too early or too late from outside
the module it will break or be ignored. For example $toolbar is
false before dom ready, buttons queue is ignored after domready,
insertButton will break if called before dom ready because the
target element doesn't exist yet. These are not bugs, but result
of calling internal methods before they are initialized.
The public API takes care of these state differences by using
the queue and the dom ready handler.
Scripts should (and do) only use the addButton API.
* Kept:
- addButton
- insertTags
- init (empty b/c function, was already there)
* Improved:
- addButton: Now takes an object as well, just like
mwCustomEditButtons used to do.
- Cache Array.prototype.slice instead of re-grabbing from
a new dummy array.
- Store buttons[i] in a local variable in both cases, not just
for legacy. Saves 2 property lookups. Minor gain, but
in this case it was already going to be stored in a local
variable, so might as well do it in the other case.
* Fixes:
- Clear queue array after it has been used. Though in practice
it should never happen that it is iterated over twice, just in
case.
- Added comment to init() function explaining where it is used.
- Updated closure arguments per code conventions.
- Made it a position-top module so that it actually can be used
before the document is ready.
* Example usages tested:
<code>
// Legacy way from wikibits.js:
// Has to be done before document ready
window.mwCustomEditButtons[window.mwCustomEditButtons.length] = {
imageFile: 'http://placehold.it/23x22',
speedTip: 'tool tip',
tagOpen: 'x-',
tagClose: '-y'
};
// mw.toolbar: List of arguments
mw.toolbar.addButton( 'http://placehold.it/23x22', 'tooltip', 'x-', '-y' );
// mw.toolbar: Object
mw.toolbar.addButton({
imageFile: 'http://placehold.it/23x22',
speedTip: 'tool tip',
tagOpen: 'x-',
tagClose: '-y'
});
</code>
Change-Id: Id19819707c937c2c3144ad8177b75baa46f5073c