Ticket #26046: 26046.diff
| File 26046.diff, 11.4 KB (added by , 13 years ago) |
|---|
-
src/wp-includes/js/quicktags.js
1 /* global adminpage, wpActiveEditor, quicktagsL10n, wpLink, fullscreen */ 1 2 /* 2 3 * Quicktags 3 4 * … … 21 22 // by Alex King 22 23 // http://www.alexking.org/ 23 24 24 var QTags, edButtons = [], edCanvas ,25 var QTags, edButtons = [], edCanvas; 25 26 27 /* jshint ignore:start */ 28 26 29 /** 27 30 * Back-compat 28 31 * 29 32 * Define all former global functions so plugins that hack quicktags.js directly don't cause fatal errors. 30 33 */ 31 edAddTag = function(){},34 var edAddTag = function(){}, 32 35 edCheckOpenTags = function(){}, 33 36 edCloseAllTags = function(){}, 34 37 edInsertImage = function(){}, … … 65 68 * Added for back compatibility, use QTags.addButton() as it gives more flexibility like type of button, button placement, etc. 66 69 * @see QTags.addButton() 67 70 */ 68 function edButton(id, display, tagStart, tagEnd, access , open) {71 function edButton(id, display, tagStart, tagEnd, access) { 69 72 return QTags.addButton( id, display, tagStart, tagEnd, access, '', -1 ); 70 73 } 71 74 75 /* jshint ignore:end */ 76 72 77 (function(){ 73 78 // private stuff is prefixed with an underscore 74 79 var _domReady = function(func) { 75 var t, i, DOMContentLoaded;80 var t, i, DOMContentLoaded, _tryReady; 76 81 77 if ( typeof jQuery != 'undefined' ) {82 if ( typeof jQuery !== 'undefined' ) { 78 83 jQuery(document).ready(func); 79 84 } else { 80 85 t = _domReady; … … 105 110 document.attachEvent('onreadystatechange', DOMContentLoaded); 106 111 window.attachEvent('onload', t.ready); 107 112 108 (function(){113 _tryReady = function() { 109 114 try { 110 document.documentElement.doScroll( "left");115 document.documentElement.doScroll('left'); 111 116 } catch(e) { 112 setTimeout( arguments.callee, 50);117 setTimeout(_tryReady, 50); 113 118 return; 114 119 } 115 120 116 121 t.ready(); 117 })(); 122 }; 123 _tryReady(); 118 124 } 119 125 120 126 t.eventAttached = true; … … 128 134 zeroise = function(number) { 129 135 var str = number.toString(); 130 136 131 if ( str.length < 2 ) 132 str = "0" + str; 137 if ( str.length < 2 ) { 138 str = '0' + str; 139 } 133 140 134 141 return str; 135 } 142 }; 136 143 137 144 return now.getUTCFullYear() + '-' + 138 145 zeroise( now.getUTCMonth() + 1 ) + '-' + … … 145 152 qt; 146 153 147 154 qt = QTags = function(settings) { 148 if ( typeof(settings) == 'string' )155 if ( typeof(settings) === 'string' ) { 149 156 settings = {id: settings}; 150 else if ( typeof(settings) != 'object' )157 } else if ( typeof(settings) !== 'object' ) { 151 158 return false; 159 } 152 160 153 161 var t = this, 154 162 id = settings.id, … … 156 164 name = 'qt_' + id, 157 165 tb, onclick, toolbar_id; 158 166 159 if ( !id || !canvas ) 167 if ( !id || !canvas ) { 160 168 return false; 169 } 161 170 162 171 t.name = name; 163 172 t.id = id; 164 173 t.canvas = canvas; 165 174 t.settings = settings; 166 175 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' ) ) { 168 177 // back compat hack :-( 169 178 edCanvas = canvas; 170 179 toolbar_id = 'ed_toolbar'; … … 185 194 var target = e.target || e.srcElement, visible = target.clientWidth || target.offsetWidth, i; 186 195 187 196 // don't call the callback on pressing the accesskey when the button is not visible 188 if ( !visible ) 197 if ( !visible ) { 189 198 return; 199 } 190 200 191 201 // as long as it has the class ed_button, execute the callback 192 202 if ( / ed_button /.test(' ' + target.className + ' ') ) { … … 194 204 t.canvas = canvas = document.getElementById(id); 195 205 i = target.id.replace(name + '_', ''); 196 206 197 if ( t.theButtons[i] ) 207 if ( t.theButtons[i] ) { 198 208 t.theButtons[i].callback.call(t.theButtons[i], target, canvas, t); 209 } 199 210 } 200 211 }; 201 212 … … 232 243 defaults = ',strong,em,link,block,del,ins,img,ul,ol,li,code,more,close,'; 233 244 234 245 for ( inst in t.instances ) { 235 if ( inst == 0 )246 if ( inst === 0 ) { 236 247 continue; 248 } 237 249 238 250 ed = t.instances[inst]; 239 251 canvas = ed.canvas; … … 244 256 use = ''; 245 257 246 258 // set buttons 247 if ( settings.buttons ) 259 if ( settings.buttons ) { 248 260 use = ','+settings.buttons+','; 261 } 249 262 250 263 for ( i in edButtons ) { 251 if ( !edButtons[i] ) 264 if ( !edButtons[i] ) { 252 265 continue; 266 } 253 267 254 268 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 ) { 256 270 continue; 271 } 257 272 258 if ( !edButtons[i].instance || edButtons[i].instance == inst ) {273 if ( !edButtons[i].instance || edButtons[i].instance === inst ) { 259 274 theButtons[id] = edButtons[i]; 260 275 261 if ( edButtons[i].html ) 276 if ( edButtons[i].html ) { 262 277 html += edButtons[i].html(name + '_'); 278 } 263 279 } 264 280 } 265 281 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 + '_'); 269 285 } 270 286 271 287 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 + '_'); 275 291 } 276 292 277 293 ed.toolbar.innerHTML = html; … … 309 325 qt.addButton = function( id, display, arg1, arg2, access_key, title, priority, instance ) { 310 326 var btn; 311 327 312 if ( !id || !display ) 328 if ( !id || !display ) { 313 329 return; 330 } 314 331 315 332 priority = priority || 0; 316 333 arg2 = arg2 || ''; … … 324 341 return; 325 342 } 326 343 327 if ( priority == -1 )// back-compat344 if ( priority === -1 ) { // back-compat 328 345 return btn; 346 } 329 347 330 348 if ( priority > 0 ) { 331 while ( typeof(edButtons[priority]) != 'undefined' ) {332 priority++ 349 while ( typeof(edButtons[priority]) !== 'undefined' ) { 350 priority++; 333 351 } 334 352 335 353 edButtons[priority] = btn; … … 337 355 edButtons[edButtons.length] = btn; 338 356 } 339 357 340 if ( this.buttonsInitDone ) 358 if ( this.buttonsInitDone ) { 341 359 this._buttonsInit(); // add the button HTML to all instances toolbars if addButton() was called too late 360 } 342 361 }; 343 362 344 363 qt.insertContent = function(content) { 345 364 var sel, startPos, endPos, scrollTop, text, canvas = document.getElementById(wpActiveEditor); 346 365 347 if ( !canvas ) 366 if ( !canvas ) { 348 367 return false; 368 } 349 369 350 370 if ( document.selection ) { //IE 351 371 canvas.focus(); 352 372 sel = document.selection.createRange(); 353 373 sel.text = content; 354 374 canvas.focus(); 355 } else if ( canvas.selectionStart || canvas.selectionStart == '0' ) { // FF, WebKit, Opera375 } else if ( canvas.selectionStart || canvas.selectionStart === '0' ) { // FF, WebKit, Opera 356 376 text = canvas.value; 357 377 startPos = canvas.selectionStart; 358 378 endPos = canvas.selectionEnd; … … 419 439 var t = this, i = 0, ret = false; 420 440 if ( ed.openTags ) { 421 441 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; 423 443 i ++; 424 444 } 425 445 } else { … … 434 454 canvas.focus(); 435 455 sel = document.selection.createRange(); 436 456 if ( sel.text.length > 0 ) { 437 if ( !t.tagEnd ) 457 if ( !t.tagEnd ) { 438 458 sel.text = sel.text + t.tagStart; 439 else459 } else { 440 460 sel.text = t.tagStart + sel.text + endTag; 461 } 441 462 } else { 442 463 if ( !t.tagEnd ) { 443 464 sel.text = t.tagStart; … … 450 471 } 451 472 } 452 473 canvas.focus(); 453 } else if ( canvas.selectionStart || canvas.selectionStart == '0' ) { // FF, WebKit, Opera474 } else if ( canvas.selectionStart || canvas.selectionStart === '0' ) { // FF, WebKit, Opera 454 475 startPos = canvas.selectionStart; 455 476 endPos = canvas.selectionEnd; 456 477 cursorPos = endPos; … … 458 479 l = v.substring(0, startPos); // left of the selection 459 480 r = v.substring(endPos, v.length); // right of the selection 460 481 i = v.substring(startPos, endPos); // inside the selection 461 if ( startPos != endPos ) {482 if ( startPos !== endPos ) { 462 483 if ( !t.tagEnd ) { 463 484 canvas.value = l + i + t.tagStart + r; // insert self closing tags after the selection 464 485 cursorPos += t.tagStart.length; … … 517 538 button = ed.getButton(tbo[tbo.length - 1]); 518 539 element = document.getElementById(ed.name + '_' + button.id); 519 540 520 if ( e ) 541 if ( e ) { 521 542 button.callback.call(button, element, c, ed); 522 else543 } else { 523 544 button.closeTag(element, ed); 545 } 524 546 } 525 547 } 526 548 }; … … 540 562 qt.LinkButton.prototype.callback = function(e, c, ed, defaultValue) { 541 563 var URL, t = this; 542 564 543 if ( typeof(wpLink) != 'undefined' ) {565 if ( typeof(wpLink) !== 'undefined' ) { 544 566 wpLink.open(); 545 567 return; 546 568 } 547 569 548 if ( ! defaultValue ) 570 if ( ! defaultValue ) { 549 571 defaultValue = 'http://'; 572 } 550 573 551 574 if ( t.isOpen(ed) === false ) { 552 URL = prompt(quicktagsL10n.enterURL, defaultValue);575 URL = window.prompt(quicktagsL10n.enterURL, defaultValue); 553 576 if ( URL ) { 554 577 t.tagStart = '<a href="' + URL + '">'; 555 578 qt.TagButton.prototype.callback.call(t, e, c, ed); … … 568 591 if ( ! defaultValue ) { 569 592 defaultValue = 'http://'; 570 593 } 571 var src = prompt(quicktagsL10n.enterImageURL, defaultValue), alt;594 var src = window.prompt(quicktagsL10n.enterImageURL, defaultValue), alt; 572 595 if ( src ) { 573 alt = prompt(quicktagsL10n.enterImageDescription, '');596 alt = window.prompt(quicktagsL10n.enterImageDescription, ''); 574 597 this.tagStart = '<img src="' + src + '" alt="' + alt + '" />'; 575 598 qt.TagButton.prototype.callback.call(this, e, c, ed); 576 599 } … … 581 604 }; 582 605 qt.FullscreenButton.prototype = new qt.Button(); 583 606 qt.FullscreenButton.prototype.callback = function(e, c) { 584 if ( !c.id || typeof(fullscreen) == 'undefined' )607 if ( !c.id || typeof(fullscreen) === 'undefined' ) { 585 608 return; 609 } 586 610 587 611 fullscreen.on(); 588 612 }; 589 613 590 614 qt.TextDirectionButton = function() { 591 qt.Button.call(this, 'textdirection', quicktagsL10n.textdirection, '', quicktagsL10n.toggleTextdirection) 615 qt.Button.call(this, 'textdirection', quicktagsL10n.textdirection, '', quicktagsL10n.toggleTextdirection); 592 616 }; 593 617 qt.TextDirectionButton.prototype = new qt.Button(); 594 618 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 ), 596 620 currentDirection = c.style.direction; 597 621 598 if ( ! currentDirection ) 622 if ( ! currentDirection ) { 599 623 currentDirection = ( isRTL ) ? 'rtl' : 'ltr'; 624 } 600 625 601 c.style.direction = ( 'rtl' == currentDirection ) ? 'ltr' : 'rtl';626 c.style.direction = ( 'rtl' === currentDirection ) ? 'ltr' : 'rtl'; 602 627 c.focus(); 603 } 628 }; 604 629 605 630 // ensure backward compatibility 606 631 edButtons[10] = new qt.TagButton('strong','b','<strong>','</strong>','b'); … … 615 640 edButtons[100] = new qt.TagButton('li','li','\t<li>','</li>\n','l'), 616 641 edButtons[110] = new qt.TagButton('code','code','<code>','</code>','c'), 617 642 edButtons[120] = new qt.TagButton('more','more','<!--more-->','','t'), 618 edButtons[140] = new qt.CloseButton() 643 edButtons[140] = new qt.CloseButton(); 619 644 620 645 })();