Make WordPress Core

Ticket #26046: 26046.diff

File 26046.diff, 11.4 KB (added by kovshenin, 13 years ago)
  • src/wp-includes/js/quicktags.js

     
     1/* global adminpage, wpActiveEditor, quicktagsL10n, wpLink, fullscreen */
    12/*
    23 * Quicktags
    34 *
     
    2122// by Alex King
    2223// http://www.alexking.org/
    2324
    24 var QTags, edButtons = [], edCanvas,
     25var QTags, edButtons = [], edCanvas;
    2526
     27/* jshint ignore:start */
     28
    2629/**
    2730 * Back-compat
    2831 *
    2932 * Define all former global functions so plugins that hack quicktags.js directly don't cause fatal errors.
    3033 */
    31 edAddTag = function(){},
     34var edAddTag = function(){},
    3235edCheckOpenTags = function(){},
    3336edCloseAllTags = function(){},
    3437edInsertImage = function(){},
     
    6568 * Added for back compatibility, use QTags.addButton() as it gives more flexibility like type of button, button placement, etc.
    6669 * @see QTags.addButton()
    6770 */
    68 function edButton(id, display, tagStart, tagEnd, access, open) {
     71function edButton(id, display, tagStart, tagEnd, access) {
    6972        return QTags.addButton( id, display, tagStart, tagEnd, access, '', -1 );
    7073}
    7174
     75/* jshint ignore:end */
     76
    7277(function(){
    7378        // private stuff is prefixed with an underscore
    7479        var _domReady = function(func) {
    75                 var t, i,  DOMContentLoaded;
     80                var t, i, DOMContentLoaded, _tryReady;
    7681
    77                 if ( typeof jQuery != 'undefined' ) {
     82                if ( typeof jQuery !== 'undefined' ) {
    7883                        jQuery(document).ready(func);
    7984                } else {
    8085                        t = _domReady;
     
    105110                                        document.attachEvent('onreadystatechange', DOMContentLoaded);
    106111                                        window.attachEvent('onload', t.ready);
    107112
    108                                         (function(){
     113                                        _tryReady = function() {
    109114                                                try {
    110                                                         document.documentElement.doScroll("left");
     115                                                        document.documentElement.doScroll('left');
    111116                                                } catch(e) {
    112                                                         setTimeout(arguments.callee, 50);
     117                                                        setTimeout(_tryReady, 50);
    113118                                                        return;
    114119                                                }
    115120
    116121                                                t.ready();
    117                                         })();
     122                                        };
     123                                        _tryReady();
    118124                                }
    119125
    120126                                t.eventAttached = true;
     
    128134                zeroise = function(number) {
    129135                        var str = number.toString();
    130136
    131                         if ( str.length < 2 )
    132                                 str = "0" + str;
     137                        if ( str.length < 2 ) {
     138                                str = '0' + str;
     139                        }
    133140
    134141                        return str;
    135                 }
     142                };
    136143
    137144                return now.getUTCFullYear() + '-' +
    138145                        zeroise( now.getUTCMonth() + 1 ) + '-' +
     
    145152        qt;
    146153
    147154        qt = QTags = function(settings) {
    148                 if ( typeof(settings) == 'string' )
     155                if ( typeof(settings) === 'string' ) {
    149156                        settings = {id: settings};
    150                 else if ( typeof(settings) != 'object' )
     157                } else if ( typeof(settings) !== 'object' ) {
    151158                        return false;
     159                }
    152160
    153161                var t = this,
    154162                        id = settings.id,
     
    156164                        name = 'qt_' + id,
    157165                        tb, onclick, toolbar_id;
    158166
    159                 if ( !id || !canvas )
     167                if ( !id || !canvas ) {
    160168                        return false;
     169                }
    161170
    162171                t.name = name;
    163172                t.id = id;
    164173                t.canvas = canvas;
    165174                t.settings = settings;
    166175
    167                 if ( id == 'content' && typeof(adminpage) == 'string' && ( adminpage == 'post-new-php' || adminpage == 'post-php' ) ) {
     176                if ( id === 'content' && typeof(adminpage) === 'string' && ( adminpage === 'post-new-php' || adminpage === 'post-php' ) ) {
    168177                        // back compat hack :-(
    169178                        edCanvas = canvas;
    170179                        toolbar_id = 'ed_toolbar';
     
    185194                        var target = e.target || e.srcElement, visible = target.clientWidth || target.offsetWidth, i;
    186195
    187196                        // don't call the callback on pressing the accesskey when the button is not visible
    188                         if ( !visible )
     197                        if ( !visible ) {
    189198                                return;
     199                        }
    190200
    191201                        // as long as it has the class ed_button, execute the callback
    192202                        if ( / ed_button /.test(' ' + target.className + ' ') ) {
     
    194204                                t.canvas = canvas = document.getElementById(id);
    195205                                i = target.id.replace(name + '_', '');
    196206
    197                                 if ( t.theButtons[i] )
     207                                if ( t.theButtons[i] ) {
    198208                                        t.theButtons[i].callback.call(t.theButtons[i], target, canvas, t);
     209                                }
    199210                        }
    200211                };
    201212
     
    232243                        defaults = ',strong,em,link,block,del,ins,img,ul,ol,li,code,more,close,';
    233244
    234245                for ( inst in t.instances ) {
    235                         if ( inst == 0 )
     246                        if ( inst === 0 ) {
    236247                                continue;
     248                        }
    237249
    238250                        ed = t.instances[inst];
    239251                        canvas = ed.canvas;
     
    244256                        use = '';
    245257
    246258                        // set buttons
    247                         if ( settings.buttons )
     259                        if ( settings.buttons ) {
    248260                                use = ','+settings.buttons+',';
     261                        }
    249262
    250263                        for ( i in edButtons ) {
    251                                 if ( !edButtons[i] )
     264                                if ( !edButtons[i] ) {
    252265                                        continue;
     266                                }
    253267
    254268                                id = edButtons[i].id;
    255                                 if ( use && defaults.indexOf(','+id+',') != -1 && use.indexOf(','+id+',') == -1 )
     269                                if ( use && defaults.indexOf(','+id+',') !== -1 && use.indexOf(','+id+',') === -1 ) {
    256270                                        continue;
     271                                }
    257272
    258                                 if ( !edButtons[i].instance || edButtons[i].instance == inst ) {
     273                                if ( !edButtons[i].instance || edButtons[i].instance === inst ) {
    259274                                        theButtons[id] = edButtons[i];
    260275
    261                                         if ( edButtons[i].html )
     276                                        if ( edButtons[i].html ) {
    262277                                                html += edButtons[i].html(name + '_');
     278                                        }
    263279                                }
    264280                        }
    265281
    266                         if ( use && use.indexOf(',fullscreen,') != -1 ) {
    267                                 theButtons['fullscreen'] = new qt.FullscreenButton();
    268                                 html += theButtons['fullscreen'].html(name + '_');
     282                        if ( use && use.indexOf(',fullscreen,') !== -1 ) {
     283                                theButtons.fullscreen = new qt.FullscreenButton();
     284                                html += theButtons.fullscreen.html(name + '_');
    269285                        }
    270286
    271287
    272                         if ( 'rtl' == document.getElementsByTagName('html')[0].dir ) {
    273                                 theButtons['textdirection'] = new qt.TextDirectionButton();
    274                                 html += theButtons['textdirection'].html(name + '_');
     288                        if ( 'rtl' === document.getElementsByTagName('html')[0].dir ) {
     289                                theButtons.textdirection = new qt.TextDirectionButton();
     290                                html += theButtons.textdirection.html(name + '_');
    275291                        }
    276292
    277293                        ed.toolbar.innerHTML = html;
     
    309325        qt.addButton = function( id, display, arg1, arg2, access_key, title, priority, instance ) {
    310326                var btn;
    311327
    312                 if ( !id || !display )
     328                if ( !id || !display ) {
    313329                        return;
     330                }
    314331
    315332                priority = priority || 0;
    316333                arg2 = arg2 || '';
     
    324341                        return;
    325342                }
    326343
    327                 if ( priority == -1 ) // back-compat
     344                if ( priority === -1 ) { // back-compat
    328345                        return btn;
     346                }
    329347
    330348                if ( priority > 0 ) {
    331                         while ( typeof(edButtons[priority]) != 'undefined' ) {
    332                                 priority++
     349                        while ( typeof(edButtons[priority]) !== 'undefined' ) {
     350                                priority++;
    333351                        }
    334352
    335353                        edButtons[priority] = btn;
     
    337355                        edButtons[edButtons.length] = btn;
    338356                }
    339357
    340                 if ( this.buttonsInitDone )
     358                if ( this.buttonsInitDone ) {
    341359                        this._buttonsInit(); // add the button HTML to all instances toolbars if addButton() was called too late
     360                }
    342361        };
    343362
    344363        qt.insertContent = function(content) {
    345364                var sel, startPos, endPos, scrollTop, text, canvas = document.getElementById(wpActiveEditor);
    346365
    347                 if ( !canvas )
     366                if ( !canvas ) {
    348367                        return false;
     368                }
    349369
    350370                if ( document.selection ) { //IE
    351371                        canvas.focus();
    352372                        sel = document.selection.createRange();
    353373                        sel.text = content;
    354374                        canvas.focus();
    355                 } else if ( canvas.selectionStart || canvas.selectionStart == '0' ) { // FF, WebKit, Opera
     375                } else if ( canvas.selectionStart || canvas.selectionStart === '0' ) { // FF, WebKit, Opera
    356376                        text = canvas.value;
    357377                        startPos = canvas.selectionStart;
    358378                        endPos = canvas.selectionEnd;
     
    419439                var t = this, i = 0, ret = false;
    420440                if ( ed.openTags ) {
    421441                        while ( ret === false && i < ed.openTags.length ) {
    422                                 ret = ed.openTags[i] == t.id ? i : false;
     442                                ret = ed.openTags[i] === t.id ? i : false;
    423443                                i ++;
    424444                        }
    425445                } else {
     
    434454                        canvas.focus();
    435455                        sel = document.selection.createRange();
    436456                        if ( sel.text.length > 0 ) {
    437                                 if ( !t.tagEnd )
     457                                if ( !t.tagEnd ) {
    438458                                        sel.text = sel.text + t.tagStart;
    439                                 else
     459                                } else {
    440460                                        sel.text = t.tagStart + sel.text + endTag;
     461                                }
    441462                        } else {
    442463                                if ( !t.tagEnd ) {
    443464                                        sel.text = t.tagStart;
     
    450471                                }
    451472                        }
    452473                        canvas.focus();
    453                 } else if ( canvas.selectionStart || canvas.selectionStart == '0' ) { // FF, WebKit, Opera
     474                } else if ( canvas.selectionStart || canvas.selectionStart === '0' ) { // FF, WebKit, Opera
    454475                        startPos = canvas.selectionStart;
    455476                        endPos = canvas.selectionEnd;
    456477                        cursorPos = endPos;
     
    458479                        l = v.substring(0, startPos); // left of the selection
    459480                        r = v.substring(endPos, v.length); // right of the selection
    460481                        i = v.substring(startPos, endPos); // inside the selection
    461                         if ( startPos != endPos ) {
     482                        if ( startPos !== endPos ) {
    462483                                if ( !t.tagEnd ) {
    463484                                        canvas.value = l + i + t.tagStart + r; // insert self closing tags after the selection
    464485                                        cursorPos += t.tagStart.length;
     
    517538                                button = ed.getButton(tbo[tbo.length - 1]);
    518539                                element = document.getElementById(ed.name + '_' + button.id);
    519540
    520                                 if ( e )
     541                                if ( e ) {
    521542                                        button.callback.call(button, element, c, ed);
    522                                 else
     543                                } else {
    523544                                        button.closeTag(element, ed);
     545                                }
    524546                        }
    525547                }
    526548        };
     
    540562        qt.LinkButton.prototype.callback = function(e, c, ed, defaultValue) {
    541563                var URL, t = this;
    542564
    543                 if ( typeof(wpLink) != 'undefined' ) {
     565                if ( typeof(wpLink) !== 'undefined' ) {
    544566                        wpLink.open();
    545567                        return;
    546568                }
    547569
    548                 if ( ! defaultValue )
     570                if ( ! defaultValue ) {
    549571                        defaultValue = 'http://';
     572                }
    550573
    551574                if ( t.isOpen(ed) === false ) {
    552                         URL = prompt(quicktagsL10n.enterURL, defaultValue);
     575                        URL = window.prompt(quicktagsL10n.enterURL, defaultValue);
    553576                        if ( URL ) {
    554577                                t.tagStart = '<a href="' + URL + '">';
    555578                                qt.TagButton.prototype.callback.call(t, e, c, ed);
     
    568591                if ( ! defaultValue ) {
    569592                        defaultValue = 'http://';
    570593                }
    571                 var src = prompt(quicktagsL10n.enterImageURL, defaultValue), alt;
     594                var src = window.prompt(quicktagsL10n.enterImageURL, defaultValue), alt;
    572595                if ( src ) {
    573                         alt = prompt(quicktagsL10n.enterImageDescription, '');
     596                        alt = window.prompt(quicktagsL10n.enterImageDescription, '');
    574597                        this.tagStart = '<img src="' + src + '" alt="' + alt + '" />';
    575598                        qt.TagButton.prototype.callback.call(this, e, c, ed);
    576599                }
     
    581604        };
    582605        qt.FullscreenButton.prototype = new qt.Button();
    583606        qt.FullscreenButton.prototype.callback = function(e, c) {
    584                 if ( !c.id || typeof(fullscreen) == 'undefined' )
     607                if ( !c.id || typeof(fullscreen) === 'undefined' ) {
    585608                        return;
     609                }
    586610
    587611                fullscreen.on();
    588612        };
    589613
    590614        qt.TextDirectionButton = function() {
    591                 qt.Button.call(this, 'textdirection', quicktagsL10n.textdirection, '', quicktagsL10n.toggleTextdirection)
     615                qt.Button.call(this, 'textdirection', quicktagsL10n.textdirection, '', quicktagsL10n.toggleTextdirection);
    592616        };
    593617        qt.TextDirectionButton.prototype = new qt.Button();
    594618        qt.TextDirectionButton.prototype.callback = function(e, c) {
    595                 var isRTL = ( 'rtl' == document.getElementsByTagName('html')[0].dir ),
     619                var isRTL = ( 'rtl' === document.getElementsByTagName('html')[0].dir ),
    596620                        currentDirection = c.style.direction;
    597621
    598                 if ( ! currentDirection )
     622                if ( ! currentDirection ) {
    599623                        currentDirection = ( isRTL ) ? 'rtl' : 'ltr';
     624                }
    600625
    601                 c.style.direction = ( 'rtl' == currentDirection ) ? 'ltr' : 'rtl';
     626                c.style.direction = ( 'rtl' === currentDirection ) ? 'ltr' : 'rtl';
    602627                c.focus();
    603         }
     628        };
    604629
    605630        // ensure backward compatibility
    606631        edButtons[10] = new qt.TagButton('strong','b','<strong>','</strong>','b');
     
    615640        edButtons[100] = new qt.TagButton('li','li','\t<li>','</li>\n','l'),
    616641        edButtons[110] = new qt.TagButton('code','code','<code>','</code>','c'),
    617642        edButtons[120] = new qt.TagButton('more','more','<!--more-->','','t'),
    618         edButtons[140] = new qt.CloseButton()
     643        edButtons[140] = new qt.CloseButton();
    619644
    620645})();