Improve quicktags back-compat, add method instance.getButtonElement(id), see #16695

git-svn-id: http://svn.automattic.com/wordpress/trunk@18611 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
azaozz 2011-08-26 23:30:01 +00:00
parent 07d81969bd
commit a8df969ac9
3 changed files with 86 additions and 55 deletions

View File

@ -52,7 +52,7 @@ function edInsertContent(bah, txt) {
* @see QTags.addButton() * @see QTags.addButton()
*/ */
function edButton(id, display, tagStart, tagEnd, access, open) { function edButton(id, display, tagStart, tagEnd, access, open) {
return QTags.addButton( id, display, tagStart, tagEnd, open, access ); return QTags.addButton( id, display, tagStart, tagEnd, open, access, '', -1 );
} }
(function(){ (function(){
@ -140,12 +140,9 @@ function edButton(id, display, tagStart, tagEnd, access, open) {
var t = this, var t = this,
id = settings.id, id = settings.id,
buttons = {},
theButtons = {},
canvas = document.getElementById(id), canvas = document.getElementById(id),
name = 'qt_' + id, name = 'qt_' + id,
html = '', tb, onclick, toolbar_id;
i, tb, qb, btn, onclick, toolbar_id;
if ( !id || !canvas ) if ( !id || !canvas )
return false; return false;
@ -153,17 +150,9 @@ function edButton(id, display, tagStart, tagEnd, access, open) {
t.name = name; t.name = name;
t.id = id; t.id = id;
t.canvas = canvas; t.canvas = canvas;
t.settings = settings;
// default buttons
for ( i in edButtons ) {
if ( !edButtons[i] )
continue;
buttons[edButtons[i].id] = edButtons[i];
}
if ( id == 'content' && adminpage && ( adminpage == 'post-new-php' || adminpage == 'post-php' ) ) { if ( id == 'content' && adminpage && ( adminpage == 'post-new-php' || adminpage == 'post-php' ) ) {
buttons['fullscreen'] = new qt.FullscreenButton();
// back compat hack :-( // back compat hack :-(
edCanvas = canvas; edCanvas = canvas;
toolbar_id = 'ed_toolbar'; toolbar_id = 'ed_toolbar';
@ -171,8 +160,87 @@ function edButton(id, display, tagStart, tagEnd, access, open) {
toolbar_id = name + '_toolbar'; toolbar_id = name + '_toolbar';
} }
tb = document.createElement('div');
tb.id = toolbar_id;
tb.className = 'quicktags-toolbar';
canvas.parentNode.insertBefore(tb, canvas);
t.toolbar = tb;
// listen for click events
onclick = function(e) {
e = e || window.event;
var target = e.target || e.srcElement, i;
// as long as it has the class ed_button, execute the callback
if ( /\s+ed_button\s+/.test(' ' + target.className + ' ' ) ) {
// we have to reassign canvas here
t.canvas = canvas = document.getElementById(id);
i = target.id.replace(name + '_', '');
if ( t.theButtons[i] )
t.theButtons[i].callback.call(t.theButtons[i], target, canvas, t);
}
};
if ( tb.addEventListener ) {
tb.addEventListener('click', onclick, false);
} else if ( tb.attachEvent ) {
tb.attachEvent('onclick', onclick);
}
t.getButton = function(id) {
return t.theButtons[id];
};
t.getButtonElement = function(id) {
return document.getElementById(name + '_' + id);
};
qt.instances[id] = t;
if ( !qt.instances[0] ) {
qt.instances[0] = qt.instances[id];
_domReady( function(){ qt._buttonsInit(); } );
}
};
qt.instances = {};
qt.registerButton = function(id, btnClass) {
_customButtons[id] = btnClass;
};
qt.getInstance = function(id) {
return qt.instances[id];
};
qt._buttonsInit = function() {
var t = this, instance, canvas, name, settings, buttons = {}, theButtons = {}, html = '', id, i, qb, btn;;
for ( id in t.instances ) {
if ( id == 0 )
continue;
instance = t.instances[id];
canvas = instance.canvas;
name = instance.name;
settings = instance.settings;
// set buttons
for ( i in edButtons ) {
if ( !edButtons[i] )
continue;
buttons[edButtons[i].id] = edButtons[i];
}
if ( id == 'content' && adminpage && ( adminpage == 'post-new-php' || adminpage == 'post-php' ) )
buttons['fullscreen'] = new qt.FullscreenButton();
// add custom buttons // add custom buttons
for ( i in t._customButtons ) { for ( i in t._customButtons ) {
if ( !buttons[i] )
buttons[i] = new t._customButtons[i](); buttons[i] = new t._customButtons[i]();
} }
@ -201,55 +269,10 @@ function edButton(id, display, tagStart, tagEnd, access, open) {
for ( i in theButtons ) for ( i in theButtons )
html += theButtons[i].html(name + '_'); html += theButtons[i].html(name + '_');
tb = document.createElement('div'); instance.toolbar.innerHTML = html;
tb.id = toolbar_id; instance.theButtons = theButtons;
tb.className = 'quicktags-toolbar';
canvas.parentNode.insertBefore(tb, canvas);
tb.innerHTML = html;
t.toolbar = tb;
// listen for click events
onclick = function(e) {
e = e || window.event;
var target = e.target || e.srcElement, i;
// as long as it has the class ed_button, execute the callback
if ( /\s+ed_button\s+/.test(' ' + target.className + ' ' ) ) {
// we have to reassign canvas here
t.canvas = canvas = document.getElementById(id);
i = target.id.replace(name + '_', '');
if ( theButtons[i] )
theButtons[i].callback.call(theButtons[i], target, canvas, t);
} }
}; t.buttonsInitDone = true;
if ( tb.addEventListener ) {
tb.addEventListener('click', onclick, false);
} else if ( tb.attachEvent ) {
tb.attachEvent('onclick', onclick);
}
t.getButton = function(id) {
return buttons[id];
};
qt.instances[id] = t;
if ( !qt.instances[0] )
qt.instances[0] = qt.instances[id];
};
qt.instances = {};
qt.registerButton = function(id, btnClass) {
_customButtons[id] = btnClass;
};
qt.getInstance = function(id) {
return qt.instances[id];
}; };
/** /**
@ -283,16 +306,21 @@ function edButton(id, display, tagStart, tagEnd, access, open) {
if ( !id || !display ) if ( !id || !display )
return; return;
priority = priority || 0;
if ( typeof(arg1) == 'function' ) { if ( typeof(arg1) == 'function' ) {
btn = new qt.Button(id, display, access, title); btn = new qt.Button(id, display, access, title);
btn.callback = arg1; btn.callback = arg1;
} else if ( typeof(arg1) == 'string' && arg1 && arg2 ) { } else if ( arg1 && arg2 && typeof(arg1) == 'string' ) {
btn = new qt.TagButton(id, display, arg1, arg2, access, arg3, title); btn = new qt.TagButton(id, display, arg1, arg2, access, arg3, title);
} else { } else {
return; return;
} }
if ( priority ) { if ( priority == -1 ) // back-compat
return btn;
if ( priority > 0 ) {
while ( typeof(edButtons[priority]) != 'undefined' ) { while ( typeof(edButtons[priority]) != 'undefined' ) {
priority++ priority++
} }
@ -301,6 +329,9 @@ function edButton(id, display, tagStart, tagEnd, access, open) {
} else { } else {
edButtons[edButtons.length] = btn; edButtons[edButtons.length] = btn;
} }
if ( this.buttonsInitDone )
this._buttonsInit(); // add the button HTML to all instances toolbars if addButton() was called too late
}; };
qt.insertContent = function(content) { qt.insertContent = function(content) {

File diff suppressed because one or more lines are too long

View File

@ -68,7 +68,7 @@ function wp_default_scripts( &$scripts ) {
$scripts->add( 'sack', "/wp-includes/js/tw-sack$suffix.js", false, '1.6.1', 1 ); $scripts->add( 'sack', "/wp-includes/js/tw-sack$suffix.js", false, '1.6.1', 1 );
$scripts->add( 'quicktags', "/wp-includes/js/quicktags$suffix.js", false, '20110820', 1 ); $scripts->add( 'quicktags', "/wp-includes/js/quicktags$suffix.js", false, '20110826', 1 );
$scripts->add_script_data( 'quicktags', 'quicktagsL10n', array( $scripts->add_script_data( 'quicktags', 'quicktagsL10n', array(
'wordLookup' => __('Enter a word to look up:'), 'wordLookup' => __('Enter a word to look up:'),
'dictionaryLookup' => esc_attr(__('Dictionary lookup')), 'dictionaryLookup' => esc_attr(__('Dictionary lookup')),