X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/177fd6fefd2e3d5a0ea6591c71d660cabdb3c1a4..7f1521bf193b382565eb753043c161f4cb3fcda7:/wp-includes/js/quicktags.js diff --git a/wp-includes/js/quicktags.js b/wp-includes/js/quicktags.js index 90b0e7ef..9b2f8c8d 100644 --- a/wp-includes/js/quicktags.js +++ b/wp-includes/js/quicktags.js @@ -1,389 +1,683 @@ +/* global adminpage, wpActiveEditor, quicktagsL10n, wpLink, prompt */ +/* + * Quicktags + * + * This is the HTML editor in WordPress. It can be attached to any textarea and will + * append a toolbar above it. This script is self-contained (does not require external libraries). + * + * Run quicktags(settings) to initialize it, where settings is an object containing up to 3 properties: + * settings = { + * id : 'my_id', the HTML ID of the textarea, required + * buttons: '' Comma separated list of the names of the default buttons to show. Optional. + * Current list of default button names: 'strong,em,link,block,del,ins,img,ul,ol,li,code,more,close'; + * } + * + * The settings can also be a string quicktags_id. + * + * quicktags_id string The ID of the textarea that will be the editor canvas + * buttons string Comma separated list of the default buttons names that will be shown in that instance. + */ + // new edit toolbar used with permission // by Alex King // http://www.alexking.org/ -var edButtons = new Array(); -var edLinks = new Array(); -var edOpenTags = new Array(); - -function edButton(id, display, tagStart, tagEnd, access, open) { - this.id = id; // used to name the toolbar button - this.display = display; // label on button - this.tagStart = tagStart; // open tag - this.tagEnd = tagEnd; // close tag - this.access = access; // access key - this.open = open; // set to -1 if tag does not need to be closed +var QTags, edCanvas, + edButtons = []; + +/* jshint ignore:start */ + +/** + * Back-compat + * + * Define all former global functions so plugins that hack quicktags.js directly don't cause fatal errors. + */ +var edAddTag = function(){}, +edCheckOpenTags = function(){}, +edCloseAllTags = function(){}, +edInsertImage = function(){}, +edInsertLink = function(){}, +edInsertTag = function(){}, +edLink = function(){}, +edQuickLink = function(){}, +edRemoveTag = function(){}, +edShowButton = function(){}, +edShowLinks = function(){}, +edSpell = function(){}, +edToolbar = function(){}; + +/** + * Initialize new instance of the Quicktags editor + */ +function quicktags(settings) { + return new QTags(settings); } -function zeroise(number, threshold) { - // FIXME: or we could use an implementation of printf in js here - var str = number.toString(); - if (number < 0) { str = str.substr(1, str.length) } - while (str.length < threshold) { str = "0" + str } - if (number < 0) { str = '-' + str } - return str; +/** + * Inserts content at the caret in the active editor (textarea) + * + * Added for back compatibility + * @see QTags.insertContent() + */ +function edInsertContent(bah, txt) { + return QTags.insertContent(txt); } -var now = new Date(); -var datetime = now.getUTCFullYear() + '-' + -zeroise(now.getUTCMonth() + 1, 2) + '-' + -zeroise(now.getUTCDate(), 2) + 'T' + -zeroise(now.getUTCHours(), 2) + ':' + -zeroise(now.getUTCMinutes(), 2) + ':' + -zeroise(now.getUTCSeconds() ,2) + -'+00:00'; - -edButtons[edButtons.length] = -new edButton('ed_strong' -,'b' -,'' -,'' -,'b' -); - -edButtons[edButtons.length] = -new edButton('ed_em' -,'i' -,'' -,'' -,'i' -); - -edButtons[edButtons.length] = -new edButton('ed_link' -,'link' -,'' -,'' -,'a' -); // special case - -edButtons[edButtons.length] = -new edButton('ed_block' -,'b-quote' -,'\n\n
' -,'\n\n' -,'q' -); - - -edButtons[edButtons.length] = -new edButton('ed_del' -,'del' -,'
'
-,'
'
-,'c'
-);
-
-edButtons[edButtons.length] =
-new edButton('ed_more'
-,'more'
-,''
-,''
-,'t'
-,-1
-);
-/*
-edButtons[edButtons.length] =
-new edButton('ed_next'
-,'page'
-,''
-,''
-,'p'
-,-1
-);
-*/
-function edLink() {
- this.display = '';
- this.URL = '';
- this.newWin = 0;
+/**
+ * Adds a button to all instances of the editor
+ *
+ * Added for back compatibility, use QTags.addButton() as it gives more flexibility like type of button, button placement, etc.
+ * @see QTags.addButton()
+ */
+function edButton(id, display, tagStart, tagEnd, access) {
+ return QTags.addButton( id, display, tagStart, tagEnd, access, '', -1 );
}
-edLinks[edLinks.length] = new edLink('WordPress'
- ,'http://wordpress.org/'
- );
-
-edLinks[edLinks.length] = new edLink('alexking.org'
- ,'http://www.alexking.org/'
- );
-
-function edShowButton(button, i) {
- if (button.id == 'ed_img') {
- document.write('');
- }
- else if (button.id == 'ed_link') {
- document.write('');
- }
- else {
- document.write('');
- }
-}
+/* jshint ignore:end */
+
+(function(){
+ // private stuff is prefixed with an underscore
+ var _domReady = function(func) {
+ var t, i, DOMContentLoaded, _tryReady;
+
+ if ( typeof jQuery !== 'undefined' ) {
+ jQuery(document).ready(func);
+ } else {
+ t = _domReady;
+ t.funcs = [];
+
+ t.ready = function() {
+ if ( ! t.isReady ) {
+ t.isReady = true;
+ for ( i = 0; i < t.funcs.length; i++ ) {
+ t.funcs[i]();
+ }
+ }
+ };
+
+ if ( t.isReady ) {
+ func();
+ } else {
+ t.funcs.push(func);
+ }
-function edShowLinks() {
- var tempStr = '';
- document.write(tempStr);
-}
+ if ( ! t.eventAttached ) {
+ if ( document.addEventListener ) {
+ DOMContentLoaded = function(){document.removeEventListener('DOMContentLoaded', DOMContentLoaded, false);t.ready();};
+ document.addEventListener('DOMContentLoaded', DOMContentLoaded, false);
+ window.addEventListener('load', t.ready, false);
+ } else if ( document.attachEvent ) {
+ DOMContentLoaded = function(){if (document.readyState === 'complete'){ document.detachEvent('onreadystatechange', DOMContentLoaded);t.ready();}};
+ document.attachEvent('onreadystatechange', DOMContentLoaded);
+ window.attachEvent('onload', t.ready);
+
+ _tryReady = function() {
+ try {
+ document.documentElement.doScroll('left');
+ } catch(e) {
+ setTimeout(_tryReady, 50);
+ return;
+ }
+
+ t.ready();
+ };
+ _tryReady();
+ }
+
+ t.eventAttached = true;
+ }
+ }
+ },
-function edAddTag(button) {
- if (edButtons[button].tagEnd != '') {
- edOpenTags[edOpenTags.length] = button;
- document.getElementById(edButtons[button].id).value = '/' + document.getElementById(edButtons[button].id).value;
- }
-}
+ _datetime = (function() {
+ var now = new Date(), zeroise;
+
+ zeroise = function(number) {
+ var str = number.toString();
+
+ if ( str.length < 2 ) {
+ str = '0' + str;
+ }
-function edRemoveTag(button) {
- for (i = 0; i < edOpenTags.length; i++) {
- if (edOpenTags[i] == button) {
- edOpenTags.splice(i, 1);
- document.getElementById(edButtons[button].id).value = document.getElementById(edButtons[button].id).value.replace('/', '');
+ return str;
+ };
+
+ return now.getUTCFullYear() + '-' +
+ zeroise( now.getUTCMonth() + 1 ) + '-' +
+ zeroise( now.getUTCDate() ) + 'T' +
+ zeroise( now.getUTCHours() ) + ':' +
+ zeroise( now.getUTCMinutes() ) + ':' +
+ zeroise( now.getUTCSeconds() ) +
+ '+00:00';
+ })(),
+ qt;
+
+ qt = QTags = function(settings) {
+ if ( typeof(settings) === 'string' ) {
+ settings = {id: settings};
+ } else if ( typeof(settings) !== 'object' ) {
+ return false;
}
- }
-}
-function edCheckOpenTags(button) {
- var tag = 0;
- for (i = 0; i < edOpenTags.length; i++) {
- if (edOpenTags[i] == button) {
- tag++;
+ var t = this,
+ id = settings.id,
+ canvas = document.getElementById(id),
+ name = 'qt_' + id,
+ tb, onclick, toolbar_id;
+
+ if ( !id || !canvas ) {
+ return false;
}
- }
- if (tag > 0) {
- return true; // tag found
- }
- else {
- return false; // tag not found
- }
-}
-function edCloseAllTags() {
- var count = edOpenTags.length;
- for (o = 0; o < count; o++) {
- edInsertTag(edCanvas, edOpenTags[edOpenTags.length - 1]);
- }
-}
+ t.name = name;
+ t.id = id;
+ t.canvas = canvas;
+ t.settings = settings;
+
+ if ( id === 'content' && typeof(adminpage) === 'string' && ( adminpage === 'post-new-php' || adminpage === 'post-php' ) ) {
+ // back compat hack :-(
+ edCanvas = canvas;
+ toolbar_id = 'ed_toolbar';
+ } else {
+ toolbar_id = name + '_toolbar';
+ }
+
+ tb = document.createElement('div');
+ tb.id = toolbar_id;
+ tb.className = 'quicktags-toolbar';
+ tb.onclick = function() {
+ window.wpActiveEditor = id;
+ };
+
+ 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, visible = target.clientWidth || target.offsetWidth, i;
+
+ // don't call the callback on pressing the accesskey when the button is not visible
+ if ( !visible ) {
+ return;
+ }
+
+ // as long as it has the class ed_button, execute the callback
+ if ( / ed_button /.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);
+ }
+ }
+ };
-function edQuickLink(i, thisSelect) {
- if (i > -1) {
- var newWin = '';
- if (edLinks[i].newWin == 1) {
- newWin = ' target="_blank"';
+ if ( tb.addEventListener ) {
+ tb.addEventListener('click', onclick, false);
+ } else if ( tb.attachEvent ) {
+ tb.attachEvent('onclick', onclick);
}
- var tempStr = ''
- + edLinks[i].display
- + '';
- thisSelect.selectedIndex = 0;
- edInsertContent(edCanvas, tempStr);
- }
- else {
- thisSelect.selectedIndex = 0;
- }
-}
-function edSpell(myField) {
- var word = '';
- if (document.selection) {
- myField.focus();
- var sel = document.selection.createRange();
- if (sel.text.length > 0) {
- word = sel.text;
+ 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.getInstance = function(id) {
+ return qt.instances[id];
+ };
+
+ qt._buttonsInit = function() {
+ var t = this, canvas, name, settings, theButtons, html, inst, ed, id, i, use,
+ defaults = ',strong,em,link,block,del,ins,img,ul,ol,li,code,more,close,';
+
+ for ( inst in t.instances ) {
+ if ( '0' === inst ) {
+ continue;
+ }
+
+ ed = t.instances[inst];
+ canvas = ed.canvas;
+ name = ed.name;
+ settings = ed.settings;
+ html = '';
+ theButtons = {};
+ use = '';
+
+ // set buttons
+ if ( settings.buttons ) {
+ use = ','+settings.buttons+',';
+ }
+
+ for ( i in edButtons ) {
+ if ( !edButtons[i] ) {
+ continue;
+ }
+
+ id = edButtons[i].id;
+ if ( use && defaults.indexOf( ',' + id + ',' ) !== -1 && use.indexOf( ',' + id + ',' ) === -1 ) {
+ continue;
+ }
+
+ if ( !edButtons[i].instance || edButtons[i].instance === inst ) {
+ theButtons[id] = edButtons[i];
+
+ if ( edButtons[i].html ) {
+ html += edButtons[i].html(name + '_');
+ }
+ }
+ }
+
+ if ( use && use.indexOf(',fullscreen,') !== -1 ) {
+ theButtons.fullscreen = new qt.FullscreenButton();
+ html += theButtons.fullscreen.html(name + '_');
+ }
+
+ if ( use && use.indexOf(',dfw,') !== -1 ) {
+ theButtons.dfw = new qt.DFWButton();
+ html += theButtons.dfw.html( name + '_' );
+ }
+
+ if ( 'rtl' === document.getElementsByTagName('html')[0].dir ) {
+ theButtons.textdirection = new qt.TextDirectionButton();
+ html += theButtons.textdirection.html(name + '_');
+ }
+
+ ed.toolbar.innerHTML = html;
+ ed.theButtons = theButtons;
+
+ if ( typeof jQuery !== 'undefined' ) {
+ jQuery( document ).triggerHandler( 'quicktags-init', [ ed ] );
+ }
}
- }
- else if (myField.selectionStart || myField.selectionStart == '0') {
- var startPos = myField.selectionStart;
- var endPos = myField.selectionEnd;
- if (startPos != endPos) {
- word = myField.value.substring(startPos, endPos);
+ t.buttonsInitDone = true;
+ };
+
+ /**
+ * Main API function for adding a button to Quicktags
+ *
+ * Adds qt.Button or qt.TagButton depending on the args. The first three args are always required.
+ * To be able to add button(s) to Quicktags, your script should be enqueued as dependent
+ * on "quicktags" and outputted in the footer. If you are echoing JS directly from PHP,
+ * use add_action( 'admin_print_footer_scripts', 'output_my_js', 100 ) or add_action( 'wp_footer', 'output_my_js', 100 )
+ *
+ * Minimum required to add a button that calls an external function:
+ * QTags.addButton( 'my_id', 'my button', my_callback );
+ * function my_callback() { alert('yeah!'); }
+ *
+ * Minimum required to add a button that inserts a tag:
+ * QTags.addButton( 'my_id', 'my button', '', '' );
+ * QTags.addButton( 'my_id2', 'my button', '','\n\n'), + edButtons[50] = new qt.TagButton('del','del','
','
'),
+ edButtons[120] = new qt.TagButton('more','more','\n\n',''),
+ edButtons[140] = new qt.CloseButton();
+
+})();