1// JS QuickTags version 1.3.1 2// 3// Copyright (c) 2002-2008 Alex King 4// http://alexking.org/projects/js-quicktags 5// 6// Thanks to Greg Heo <[email protected]> for his changes 7// to support multiple toolbars per page. 8// 9// Licensed under the LGPL license 10// http://www.gnu.org/copyleft/lesser.html 11// 12// ********************************************************************** 13// This program is distributed in the hope that it will be useful, but 14// WITHOUT ANY WARRANTY; without even the implied warranty of 15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 16// ********************************************************************** 17// 18// This JavaScript will insert the tags below at the cursor position in IE and 19// Gecko-based browsers (Mozilla, Camino, Firefox, Netscape). For browsers that 20// do not support inserting at the cursor position (older versions of Safari, 21// OmniWeb) it appends the tags to the end of the content. 22// 23// Pass the ID of the <textarea> element to the edToolbar and function. 24// 25// Example: 26// 27// <script type="text/javascript">edToolbar('canvas');</script> 28// <textarea id="canvas" rows="20" cols="50"></textarea> 29// 30 31var dictionaryUrl = 'http://www.ninjawords.com/'; 32 33// other options include: 34// 35// var dictionaryUrl = 'http://www.answers.com/'; 36// var dictionaryUrl = 'http://www.dictionary.com/'; 37 38var edButtons = new Array(); 39var edLinks = new Array(); 40var edOpenTags = new Array(); 41 42function edButton(id, display, tagStart, tagEnd, access, open) { 43 this.id = id; // used to name the toolbar button 44 this.display = display; // label on button 45 this.tagStart = tagStart; // open tag 46 this.tagEnd = tagEnd; // close tag 47 this.access = access; // set to -1 if tag does not need to be closed 48 this.open = open; // set to -1 if tag does not need to be closed 49} 50 51edButtons.push( 52 new edButton( 53 'ed_bold' 54 ,'B' 55 ,'<strong>' 56 ,'</strong>' 57 ,'b' 58 ) 59); 60 61edButtons.push( 62 new edButton( 63 'ed_italic' 64 ,'I' 65 ,'<em>' 66 ,'</em>' 67 ,'i' 68 ) 69); 70 71edButtons.push( 72 new edButton( 73 'ed_link' 74 ,'Link' 75 ,'' 76 ,'</a>' 77 ,'a' 78 ) 79); // special case 80 81edButtons.push( 82 new edButton( 83 'ed_block' 84 ,'B-QUOTE' 85 ,'<blockquote>' 86 ,'</blockquote>' 87 ,'q' 88 ) 89); 90 91edButtons.push( 92 new edButton( 93 'ed_img' 94 ,'IMG' 95 ,'' 96 ,'' 97 ,'m' 98 ,-1 99 ) 100); // special case 101 102edButtons.push( 103 new edButton( 104 'ed_ul' 105 ,'UL' 106 ,'<ul>\n' 107 ,'</ul>\n\n' 108 ,'u' 109 ) 110); 111 112edButtons.push( 113 new edButton( 114 'ed_ol' 115 ,'OL' 116 ,'<ol>\n' 117 ,'</ol>\n\n' 118 ,'o' 119 ) 120); 121 122edButtons.push( 123 new edButton( 124 'ed_li' 125 ,'LI' 126 ,'\t<li>' 127 ,'</li>\n' 128 ,'l' 129 ) 130); 131 132edButtons.push( 133 new edButton( 134 'ed_code' 135 ,'CODE' 136 ,'<code>' 137 ,'</code>' 138 ,'c' 139 ) 140); 141 142 143 144 145 146var extendedStart = edButtons.length; 147 148function edLink(display, URL, newWin) { 149 this.display = display; 150 this.URL = URL; 151 if (!newWin) { 152 newWin = 0; 153 } 154 this.newWin = newWin; 155} 156 157 158edLinks[edLinks.length] = new edLink('alexking.org' 159 ,'http://www.alexking.org/' 160 ); 161 162function edShowButton(which, button, i) { 163 if (button.access) { 164 var accesskey = ' accesskey = "' + button.access + '"' 165 } 166 else { 167 var accesskey = ''; 168 } 169 switch (button.id) { 170 case 'ed_img':
171 document.write('<input type="button" id="' + button.id + '_' + which + '" ' + accesskey + ' class="ed_button ' + button.id + '" onclick="edInsertImage(\'' + which + '\');" value="' + button.display + '" />'); 172 break; 173 case 'ed_link': 174 document.write('<input type="button" id="' + button.id + '_' + which + '" ' + accesskey + ' class="ed_button ' + button.id + '" onclick="edInsertLink(\'' + which + '\', ' + i + ');" value="' + button.display + '" />'); 175 break; 176 case 'ed_ext_link': 177 document.write('<input type="button" id="' + button.id + '_' + which + '" ' + accesskey + ' class="ed_button ' + button.id + '" onclick="edInsertExtLink(\'' + which + '\', ' + i + ');" value="' + button.display + '" />'); 178 break; 179 case 'ed_footnote': 180 document.write('<input type="button" id="' + button.id + '_' + which + '" ' + accesskey + ' class="ed_button ' + button.id + '" onclick="edInsertFootnote(\'' + which + '\');" value="' + button.display + '" />'); 181 break; 182 case 'ed_via': 183 document.write('<input type="button" id="' + button.id + '_' + which + '" ' + accesskey + ' class="ed_button ' + button.id + '" onclick="edInsertVia(\'' + which + '\');" value="' + button.display + '" />'); 184 break; 185 default:
186 document.write('<input type="button" id="' + button.id + '_' + which + '" ' + accesskey + ' class="ed_button ' + button.id + '" onclick="edInsertTag(\'' + which + '\', ' + i + ');" value="' + button.display + '" />'); 187 break; 188 } 189} 190 191function edShowLinks() { 192 var tempStr = '<select onchange="edQuickLink(this.options[this.selectedIndex].value, this);"><option value="-1" selected>(Quick Links)</option>'; 193 for (i = 0; i < edLinks.length; i++) { 194 tempStr += '<option value="' + i + '">' + edLinks[i].display + '</option>'; 195 } 196 tempStr += '</select>'; 197 document.write(tempStr); 198} 199 200function edAddTag(which, button) { 201 if (edButtons[button].tagEnd != '') { 202 edOpenTags[which][edOpenTags[which].length] = button; 203 document.getElementById(edButtons[button].id + '_' + which).value = '/' + document.getElementById(edButtons[button].id + '_' + which).value; 204 } 205} 206 207function edRemoveTag(which, button) { 208 for (i = 0; i < edOpenTags[which].length; i++) { 209 if (edOpenTags[which][i] == button) { 210 edOpenTags[which].splice(i, 1); 211 document.getElementById(edButtons[button].id + '_' + which).value = document.getElementById(edButtons[button].id + '_' + which).value.replace('/', ''); 212 } 213 } 214} 215 216function edCheckOpenTags(which, button) { 217 var tag = 0; 218 for (i = 0; i < edOpenTags[which].length; i++) { 219 if (edOpenTags[which][i] == button) { 220 tag++; 221 } 222 } 223 if (tag > 0) { 224 return true; // tag found 225 } 226 else { 227 return false; // tag not found 228 } 229} 230 231function edCloseAllTags(which) { 232 var count = edOpenTags[which].length; 233 for (o = 0; o < count; o++) { 234 edInsertTag(which, edOpenTags[which][edOpenTags[which].length - 1]); 235 } 236} 237 238function edQuickLink(i, thisSelect) { 239 if (i > -1) { 240 var newWin = ''; 241 if (edLinks[i].newWin == 1) { 242 newWin = ' target="_blank"'; 243 } 244 var tempStr = '<a href="' + edLinks[i].URL + '"' + newWin + '>' 245 + edLinks[i].display 246 + '</a>'; 247 thisSelect.selectedIndex = 0; 248 edInsertContent(edCanvas, tempStr); 249 } 250 else { 251 thisSelect.selectedIndex = 0; 252 } 253} 254 255function edSpell(which) { 256 myField = document.getElementById(which); 257 var word = ''; 258 if (document.selection) { 259 myField.focus(); 260 var sel = document.selection.createRange(); 261 if (sel.text.length > 0) { 262 word = sel.text; 263 } 264 } 265 else if (myField.selectionStart || myField.selectionStart == '0') { 266 var startPos = myField.selectionStart; 267 var endPos = myField.selectionEnd; 268 if (startPos != endPos) { 269 word = myField.value.substring(startPos, endPos); 270 } 271 } 272 if (word == '') { 273 word = prompt('Enter a word to look up:', ''); 274 } 275 if (word != '') { 276 window.open(dictionaryUrl + escape(word)); 277 } 278} 279 280function edToolbar(which) {
281 document.write('<div id="ed_toolbar_' + which + '"><span>'); 282 for (i = 0; i < extendedStart; i++) { 283 edShowButton(which, edButtons[i], i); 284 } 285 if (edShowExtraCookie()) { 286 document.write( 287 '<input type="button" id="ed_close_' + which + '" class="ed_button ed_closetags" onclick="edCloseAllTags(\'' + which + '\');" value="Close Tags" />' 288 + '<input type="button" id="ed_spell_' + which + '" class="ed_button ed_spell" onclick="edSpell(\'' + which + '\');" value="Lookup" />' 289 ); 290 } 291 else { 292 document.write( 293 '<input type="button" id="ed_close_' + which + '" class="ed_button ed_closetags" onclick="edCloseAllTags(\'' + which + '\');" value="Close Tags" />' 294 + '<input type="button" id="ed_spell_' + which + '" class="ed_button ed_spell" onclick="edSpell(\'' + which + '\');" value="Lookup" />' 295 ); 296 } 297 for (i = extendedStart; i < edButtons.length; i++) { 298 //edShowButton(which, edButtons[i], i); 299 } 300 document.write('</span>'); 301// edShowLinks(); 302 document.write('</div>'); 303 edOpenTags[which] = new Array(); 304} 305 306function edShowExtra(which) { 307 document.getElementById('ed_extra_show_' + which).style.visibility = 'hidden'; 308 document.getElementById('ed_extra_buttons_' + which).style.display = 'block'; 309 edSetCookie( 310 'js_quicktags_extra' 311 , 'show' 312 , new Date("December 31, 2100") 313 ); 314} 315 316function edHideExtra(which) { 317 document.getElementById('ed_extra_buttons_' + which).style.display = 'none'; 318 document.getElementById('ed_extra_show_' + which).style.visibility = 'visible'; 319 edSetCookie( 320 'js_quicktags_extra' 321 , 'hide' 322 , new Date("December 31, 2100") 323 ); 324} 325 326// insertion code 327 328function edInsertTag(which, i) { 329 myField = document.getElementById(which); 330 //IE support 331 if (document.selection) { 332 myField.focus(); 333 sel = document.selection.createRange(); 334 if (sel.text.length > 0) { 335 sel.text = edButtons[i].tagStart + sel.text + edButtons[i].tagEnd; 336 } 337 else { 338 if (!edCheckOpenTags(which, i) || edButtons[i].tagEnd == '') { 339 sel.text = edButtons[i].tagStart; 340 edAddTag(which, i); 341 } 342 else { 343 sel.text = edButtons[i].tagEnd; 344 edRemoveTag(which, i); 345 } 346 } 347 myField.focus(); 348 } 349 //MOZILLA/NETSCAPE support 350 else if (myField.selectionStart || myField.selectionStart == '0') { 351 var startPos = myField.selectionStart; 352 var endPos = myField.selectionEnd; 353 var cursorPos = endPos; 354 var scrollTop = myField.scrollTop; 355 if (startPos != endPos) { 356 myField.value = myField.value.substring(0, startPos) 357 + edButtons[i].tagStart 358 + myField.value.substring(startPos, endPos) 359 + edButtons[i].tagEnd 360 + myField.value.substring(endPos, myField.value.length); 361 cursorPos += edButtons[i].tagStart.length + edButtons[i].tagEnd.length; 362 } 363 else { 364 if (!edCheckOpenTags(which, i) || edButtons[i].tagEnd == '') { 365 myField.value = myField.value.substring(0, startPos) 366 + edButtons[i].tagStart 367 + myField.value.substring(endPos, myField.value.length); 368 edAddTag(which, i); 369 cursorPos = startPos + edButtons[i].tagStart.length; 370 } 371 else { 372 myField.value = myField.value.substring(0, startPos) 373 + edButtons[i].tagEnd 374 + myField.value.substring(endPos, myField.value.length); 375 edRemoveTag(which, i); 376 cursorPos = startPos + edButtons[i].tagEnd.length; 377 } 378 } 379 myField.focus(); 380 myField.selectionStart = cursorPos; 381 myField.selectionEnd = cursorPos; 382 myField.scrollTop = scrollTop; 383 } 384 else { 385 if (!edCheckOpenTags(which, i) || edButtons[i].tagEnd == '') { 386 myField.value += edButtons[i].tagStart; 387 edAddTag(which, i); 388 } 389 else { 390 myField.value += edButtons[i].tagEnd; 391 edRemoveTag(which, i); 392 } 393 myField.focus(); 394 } 395} 396 397function edInsertContent(which, myValue) { 398 myField = document.getElementById(which); 399 //IE support 400 if (document.selection) { 401 myField.focus(); 402 sel = document.selection.createRange(); 403 sel.text = myValue; 404 myField.focus(); 405 } 406 //MOZILLA/NETSCAPE support 407 else if (myField.selectionStart || myField.selectionStart == '0') { 408 var startPos = myField.selectionStart; 409 var endPos = myField.selectionEnd; 410 var scrollTop = myField.scrollTop; 411 myField.value = myField.value.substring(0, startPos) 412 + myValue 413 + myField.value.substring(endPos, myField.value.length); 414 myField.focus(); 415 myField.selectionStart = startPos + myValue.length; 416 myField.selectionEnd = startPos + myValue.length; 417 myField.scrollTop = scrollTop; 418 } else { 419 myField.value += myValue; 420 myField.focus(); 421 } 422} 423 424function edInsertLink(which, i, defaultValue) { 425 myField = document.getElementById(which); 426 if (!defaultValue) { 427 defaultValue = 'http://'; 428 } 429 if (!edCheckOpenTags(which, i)) { 430 var URL = prompt('Enter the URL' ,defaultValue); 431 if (URL) { 432 edButtons[i].tagStart = '<a href="' + URL + '">'; 433 edInsertTag(which, i); 434 } 435 } 436 else { 437 edInsertTag(which, i); 438 } 439} 440 441function edInsertExtLink(which, i, defaultValue) { 442 myField = document.getElementById(which); 443 if (!defaultValue) { 444 defaultValue = 'http://'; 445 } 446 if (!edCheckOpenTags(which, i)) { 447 var URL = prompt('Enter the URL' ,defaultValue); 448 if (URL) { 449 edButtons[i].tagStart = '<a href="' + URL + '" rel="external">'; 450 edInsertTag(which, i); 451 } 452 } 453 else { 454 edInsertTag(which, i); 455 } 456} 457 458function edInsertImage(which) { 459 myField = document.getElementById(which); 460 var myValue = prompt('Enter the URL of the image', 'http://'); 461 if (myValue) { 462 myValue = '<img src="' 463 + myValue 464 + '" alt="' + prompt('Enter a description of the image', '') 465 + '" />'; 466 edInsertContent(which, myValue); 467 } 468} 469 470function edInsertFootnote(which) { 471 myField = document.getElementById(which); 472 var note = prompt('Enter the footnote:', ''); 473 if (!note || note == '') { 474 return false;
475 } 476 var now = new Date; 477 var fnId = 'fn' + now.getTime(); 478 var fnStart = myField.value.indexOf('<ol class="footnotes">'); 479 if (fnStart != -1) { 480 var fnStr1 = myField.value.substring(0, fnStart) 481 var fnStr2 = myField.value.substring(fnStart, myField.value.length) 482 var count = countInstances(fnStr2, '<li id="') + 1; 483 } 484 else { 485 var count = 1; 486 } 487 var count = '<sup><a href="#' + fnId + 'n" id="' + fnId + '" class="footnote">' + count + '</a></sup>'; 488 edInsertContent(which, count); 489 if (fnStart != -1) { 490 fnStr1 = myField.value.substring(0, fnStart + count.length) 491 fnStr2 = myField.value.substring(fnStart + count.length, myField.value.length) 492 } 493 else { 494 var fnStr1 = myField.value; 495 var fnStr2 = "\n\n" + '<ol class="footnotes">' + "\n" 496 + '</ol>' + "\n"; 497 } 498 var footnote = ' <li id="' + fnId + 'n">' + note + ' [<a href="#' + fnId + '">back</a>]</li>' + "\n" 499 + '</ol>'; 500 myField.value = fnStr1 + fnStr2.replace('</ol>', footnote); 501} 502 503function countInstances(string, substr) { 504 var count = string.split(substr); 505 return count.length - 1; 506} 507 508function edInsertVia(which) { 509 myField = document.getElementById(which); 510 var myValue = prompt('Enter the URL of the source link', 'http://'); 511 if (myValue) { 512 myValue = '(Thanks <a href="' + myValue + '" rel="external">' 513 + prompt('Enter the name of the source', '') 514 + '</a>)'; 515 edInsertContent(which, myValue); 516 } 517} 518 519 520function edSetCookie(name, value, expires, path, domain) { 521 document.cookie= name + "=" + escape(value) + 522 ((expires) ? "; expires=" + expires.toGMTString() : "") + 523 ((path) ? "; path=" + path : "") + 524 ((domain) ? "; domain=" + domain : ""); 525} 526 527function edShowExtraCookie() { 528 var cookies = document.cookie.split(';'); 529 for (var i=0;i < cookies.length; i++) { 530 var cookieData = cookies[i]; 531 while (cookieData.charAt(0) ==' ') { 532 cookieData = cookieData.substring(1, cookieData.length); 533 } 534 if (cookieData.indexOf('js_quicktags_extra') == 0) { 535 if (cookieData.substring(19, cookieData.length) == 'show') { 536 return true; 537 } 538 else { 539 return false; 540 } 541 } 542 } 543 return false; 544}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.