1 // new edit toolbar used with permission
3 // http://www.alexking.org/
5 var edButtons = new Array(), edLinks = new Array(), edOpenTags = new Array(), now = new Date(), datetime;
7 function edButton(id, display, tagStart, tagEnd, access, open) {
8 this.id = id; // used to name the toolbar button
9 this.display = display; // label on button
10 this.tagStart = tagStart; // open tag
11 this.tagEnd = tagEnd; // close tag
12 this.access = access; // access key
13 this.open = open; // set to -1 if tag does not need to be closed
16 function zeroise(number, threshold) {
17 // FIXME: or we could use an implementation of printf in js here
18 var str = number.toString();
19 if (number < 0) { str = str.substr(1, str.length) }
20 while (str.length < threshold) { str = "0" + str }
21 if (number < 0) { str = '-' + str }
25 datetime = now.getUTCFullYear() + '-' +
26 zeroise(now.getUTCMonth() + 1, 2) + '-' +
27 zeroise(now.getUTCDate(), 2) + 'T' +
28 zeroise(now.getUTCHours(), 2) + ':' +
29 zeroise(now.getUTCMinutes(), 2) + ':' +
30 zeroise(now.getUTCSeconds() ,2) +
33 edButtons[edButtons.length] =
34 new edButton('ed_strong'
41 edButtons[edButtons.length] =
49 edButtons[edButtons.length] =
50 new edButton('ed_link'
57 edButtons[edButtons.length] =
58 new edButton('ed_block'
66 edButtons[edButtons.length] =
69 ,'<del datetime="' + datetime + '">'
74 edButtons[edButtons.length] =
77 ,'<ins datetime="' + datetime + '">'
82 edButtons[edButtons.length] =
91 edButtons[edButtons.length] =
99 edButtons[edButtons.length] =
107 edButtons[edButtons.length] =
115 edButtons[edButtons.length] =
116 new edButton('ed_code'
123 edButtons[edButtons.length] =
124 new edButton('ed_more'
132 edButtons[edButtons.length] =
133 new edButton('ed_next'
147 edLinks[edLinks.length] = new edLink('WordPress'
148 ,'http://wordpress.org/'
151 edLinks[edLinks.length] = new edLink('alexking.org'
152 ,'http://www.alexking.org/'
155 function edShowButton(button, i) {
156 if (button.id == 'ed_img') {
157 document.write('<input type="button" id="' + button.id + '" accesskey="' + button.access + '" class="ed_button" onclick="edInsertImage(edCanvas);" value="' + button.display + '" />');
159 else if (button.id == 'ed_link') {
160 document.write('<input type="button" id="' + button.id + '" accesskey="' + button.access + '" class="ed_button" onclick="edInsertLink(edCanvas, ' + i + ');" value="' + button.display + '" />');
163 document.write('<input type="button" id="' + button.id + '" accesskey="' + button.access + '" class="ed_button" onclick="edInsertTag(edCanvas, ' + i + ');" value="' + button.display + '" />');
167 function edShowLinks() {
168 var tempStr = '<select onchange="edQuickLink(this.options[this.selectedIndex].value, this);"><option value="-1" selected>' + quicktagsL10n.quickLinks + '</option>', i;
169 for (i = 0; i < edLinks.length; i++) {
170 tempStr += '<option value="' + i + '">' + edLinks[i].display + '</option>';
172 tempStr += '</select>';
173 document.write(tempStr);
176 function edAddTag(button) {
177 if (edButtons[button].tagEnd != '') {
178 edOpenTags[edOpenTags.length] = button;
179 document.getElementById(edButtons[button].id).value = '/' + document.getElementById(edButtons[button].id).value;
183 function edRemoveTag(button) {
184 for (var i = 0; i < edOpenTags.length; i++) {
185 if (edOpenTags[i] == button) {
186 edOpenTags.splice(i, 1);
187 document.getElementById(edButtons[button].id).value = document.getElementById(edButtons[button].id).value.replace('/', '');
192 function edCheckOpenTags(button) {
194 for (i = 0; i < edOpenTags.length; i++) {
195 if (edOpenTags[i] == button) {
200 return true; // tag found
203 return false; // tag not found
207 function edCloseAllTags() {
208 var count = edOpenTags.length, o;
209 for (o = 0; o < count; o++) {
210 edInsertTag(edCanvas, edOpenTags[edOpenTags.length - 1]);
214 function edQuickLink(i, thisSelect) {
216 var newWin = '', tempStr;
217 if (edLinks[i].newWin == 1) {
218 newWin = ' target="_blank"';
220 tempStr = '<a href="' + edLinks[i].URL + '"' + newWin + '>'
223 thisSelect.selectedIndex = 0;
224 edInsertContent(edCanvas, tempStr);
227 thisSelect.selectedIndex = 0;
231 function edSpell(myField) {
232 var word = '', sel, startPos, endPos;
233 if (document.selection) {
235 sel = document.selection.createRange();
236 if (sel.text.length > 0) {
240 else if (myField.selectionStart || myField.selectionStart == '0') {
241 startPos = myField.selectionStart;
242 endPos = myField.selectionEnd;
243 if (startPos != endPos) {
244 word = myField.value.substring(startPos, endPos);
248 word = prompt(quicktagsL10n.wordLookup, '');
250 if (word !== null && /^\w[\w ]*$/.test(word)) {
251 window.open('http://www.answers.com/' + escape(word));
255 function edToolbar() {
256 document.write('<div id="ed_toolbar">');
257 for (var i = 0; i < edButtons.length; i++) {
258 edShowButton(edButtons[i], i);
260 document.write('<input type="button" id="ed_spell" class="ed_button" onclick="edSpell(edCanvas);" title="' + quicktagsL10n.dictionaryLookup + '" value="' + quicktagsL10n.lookup + '" />');
261 document.write('<input type="button" id="ed_close" class="ed_button" onclick="edCloseAllTags();" title="' + quicktagsL10n.closeAllOpenTags + '" value="' + quicktagsL10n.closeTags + '" />');
262 // edShowLinks(); // disabled by default
263 document.write('</div>');
268 function edInsertTag(myField, i) {
270 if (document.selection) {
272 var sel = document.selection.createRange();
273 if (sel.text.length > 0) {
274 sel.text = edButtons[i].tagStart + sel.text + edButtons[i].tagEnd;
277 if (!edCheckOpenTags(i) || edButtons[i].tagEnd == '') {
278 sel.text = edButtons[i].tagStart;
282 sel.text = edButtons[i].tagEnd;
288 //MOZILLA/NETSCAPE support
289 else if (myField.selectionStart || myField.selectionStart == '0') {
290 var startPos = myField.selectionStart, endPos = myField.selectionEnd, cursorPos = endPos, scrollTop = myField.scrollTop;
292 if (startPos != endPos) {
293 myField.value = myField.value.substring(0, startPos)
294 + edButtons[i].tagStart
295 + myField.value.substring(startPos, endPos)
296 + edButtons[i].tagEnd
297 + myField.value.substring(endPos, myField.value.length);
298 cursorPos += edButtons[i].tagStart.length + edButtons[i].tagEnd.length;
301 if (!edCheckOpenTags(i) || edButtons[i].tagEnd == '') {
302 myField.value = myField.value.substring(0, startPos)
303 + edButtons[i].tagStart
304 + myField.value.substring(endPos, myField.value.length);
306 cursorPos = startPos + edButtons[i].tagStart.length;
309 myField.value = myField.value.substring(0, startPos)
310 + edButtons[i].tagEnd
311 + myField.value.substring(endPos, myField.value.length);
313 cursorPos = startPos + edButtons[i].tagEnd.length;
317 myField.selectionStart = cursorPos;
318 myField.selectionEnd = cursorPos;
319 myField.scrollTop = scrollTop;
322 if (!edCheckOpenTags(i) || edButtons[i].tagEnd == '') {
323 myField.value += edButtons[i].tagStart;
327 myField.value += edButtons[i].tagEnd;
334 function edInsertContent(myField, myValue) {
335 var sel, startPos, endPos, scrollTop;
338 if (document.selection) {
340 sel = document.selection.createRange();
344 //MOZILLA/NETSCAPE support
345 else if (myField.selectionStart || myField.selectionStart == '0') {
346 startPos = myField.selectionStart;
347 endPos = myField.selectionEnd;
348 scrollTop = myField.scrollTop;
349 myField.value = myField.value.substring(0, startPos)
351 + myField.value.substring(endPos, myField.value.length);
353 myField.selectionStart = startPos + myValue.length;
354 myField.selectionEnd = startPos + myValue.length;
355 myField.scrollTop = scrollTop;
357 myField.value += myValue;
362 function edInsertLink(myField, i, defaultValue) {
364 defaultValue = 'http://';
366 if (!edCheckOpenTags(i)) {
367 var URL = prompt(quicktagsL10n.enterURL, defaultValue);
369 edButtons[i].tagStart = '<a href="' + URL + '">';
370 edInsertTag(myField, i);
374 edInsertTag(myField, i);
378 function edInsertImage(myField) {
379 var myValue = prompt(quicktagsL10n.enterImageURL, 'http://');
381 myValue = '<img src="'
383 + '" alt="' + prompt(quicktagsL10n.enterImageDescription, '')
385 edInsertContent(myField, myValue);
390 // Allow multiple instances.
391 // Name = unique value, id = textarea id, container = container div.
392 // Can disable some buttons by passing comma delimited string as 4th param.
393 var QTags = function(name, id, container, disabled) {
394 var t = this, cont = document.getElementById(container), i, tag, tb, html, sel;
399 t.Canvas = document.getElementById(id);
401 if ( ! t.Canvas || ! cont )
404 disabled = ( typeof disabled != 'undefined' ) ? ','+disabled+',' : '';
406 t.edShowButton = function(button, i) {
407 if ( disabled && (disabled.indexOf(','+button.display+',') != -1) )
409 else if ( button.id == name+'_img' )
410 return '<input type="button" id="' + button.id + '" accesskey="' + button.access + '" class="ed_button" onclick="edInsertImage('+name+'.Canvas);" value="' + button.display + '" />';
411 else if (button.id == name+'_link')
412 return '<input type="button" id="' + button.id + '" accesskey="' + button.access + '" class="ed_button" onclick="'+name+'.edInsertLink('+i+');" value="'+button.display+'" />';
414 return '<input type="button" id="' + button.id + '" accesskey="'+button.access+'" class="ed_button" onclick="'+name+'.edInsertTag('+i+');" value="'+button.display+'" />';
417 t.edAddTag = function(button) {
418 if ( t.Buttons[button].tagEnd != '' ) {
419 t.OpenTags[t.OpenTags.length] = button;
420 document.getElementById(t.Buttons[button].id).value = '/' + document.getElementById(t.Buttons[button].id).value;
424 t.edRemoveTag = function(button) {
425 for ( i = 0; i < t.OpenTags.length; i++ ) {
426 if ( t.OpenTags[i] == button ) {
427 t.OpenTags.splice(i, 1);
428 document.getElementById(t.Buttons[button].id).value = document.getElementById(t.Buttons[button].id).value.replace('/', '');
433 t.edCheckOpenTags = function(button) {
435 for ( var i = 0; i < t.OpenTags.length; i++ ) {
436 if ( t.OpenTags[i] == button )
439 if ( tag > 0 ) return true; // tag found
440 else return false; // tag not found
443 this.edCloseAllTags = function() {
444 var count = t.OpenTags.length;
445 for ( var o = 0; o < count; o++ )
446 t.edInsertTag(t.OpenTags[t.OpenTags.length - 1]);
449 this.edQuickLink = function(i, thisSelect) {
451 var newWin = '', tempStr;
452 if ( Links[i].newWin == 1 ) {
453 newWin = ' target="_blank"';
455 tempStr = '<a href="' + Links[i].URL + '"' + newWin + '>'
458 thisSelect.selectedIndex = 0;
459 edInsertContent(t.Canvas, tempStr);
461 thisSelect.selectedIndex = 0;
466 t.edInsertTag = function(i) {
468 if ( document.selection ) {
470 sel = document.selection.createRange();
471 if ( sel.text.length > 0 ) {
472 sel.text = t.Buttons[i].tagStart + sel.text + t.Buttons[i].tagEnd;
474 if ( ! t.edCheckOpenTags(i) || t.Buttons[i].tagEnd == '' ) {
475 sel.text = t.Buttons[i].tagStart;
478 sel.text = t.Buttons[i].tagEnd;
483 } else if ( t.Canvas.selectionStart || t.Canvas.selectionStart == '0' ) { //MOZILLA/NETSCAPE support
484 var startPos = t.Canvas.selectionStart, endPos = t.Canvas.selectionEnd, cursorPos = endPos, scrollTop = t.Canvas.scrollTop;
486 if ( startPos != endPos ) {
487 t.Canvas.value = t.Canvas.value.substring(0, startPos)
488 + t.Buttons[i].tagStart
489 + t.Canvas.value.substring(startPos, endPos)
490 + t.Buttons[i].tagEnd
491 + t.Canvas.value.substring(endPos, t.Canvas.value.length);
492 cursorPos += t.Buttons[i].tagStart.length + t.Buttons[i].tagEnd.length;
494 if ( !t.edCheckOpenTags(i) || t.Buttons[i].tagEnd == '' ) {
495 t.Canvas.value = t.Canvas.value.substring(0, startPos)
496 + t.Buttons[i].tagStart
497 + t.Canvas.value.substring(endPos, t.Canvas.value.length);
499 cursorPos = startPos + t.Buttons[i].tagStart.length;
501 t.Canvas.value = t.Canvas.value.substring(0, startPos)
502 + t.Buttons[i].tagEnd
503 + t.Canvas.value.substring(endPos, t.Canvas.value.length);
505 cursorPos = startPos + t.Buttons[i].tagEnd.length;
509 t.Canvas.selectionStart = cursorPos;
510 t.Canvas.selectionEnd = cursorPos;
511 t.Canvas.scrollTop = scrollTop;
513 if ( ! t.edCheckOpenTags(i) || t.Buttons[i].tagEnd == '' ) {
514 t.Canvas.value += Buttons[i].tagStart;
517 t.Canvas.value += Buttons[i].tagEnd;
524 this.edInsertLink = function(i, defaultValue) {
525 if ( ! defaultValue )
526 defaultValue = 'http://';
528 if ( ! t.edCheckOpenTags(i) ) {
529 var URL = prompt(quicktagsL10n.enterURL, defaultValue);
531 t.Buttons[i].tagStart = '<a href="' + URL + '">';
539 this.edInsertImage = function() {
540 var myValue = prompt(quicktagsL10n.enterImageURL, 'http://');
542 myValue = '<img src="'
544 + '" alt="' + prompt(quicktagsL10n.enterImageDescription, '')
546 edInsertContent(t.Canvas, myValue);
550 t.Buttons[t.Buttons.length] = new edButton(name+'_strong','b','<strong>','</strong>','b');
551 t.Buttons[t.Buttons.length] = new edButton(name+'_em','i','<em>','</em>','i');
552 t.Buttons[t.Buttons.length] = new edButton(name+'_link','link','','</a>','a'); // special case
553 t.Buttons[t.Buttons.length] = new edButton(name+'_block','b-quote','\n\n<blockquote>','</blockquote>\n\n','q');
554 t.Buttons[t.Buttons.length] = new edButton(name+'_del','del','<del datetime="' + datetime + '">','</del>','d');
555 t.Buttons[t.Buttons.length] = new edButton(name+'_ins','ins','<ins datetime="' + datetime + '">','</ins>','s');
556 t.Buttons[t.Buttons.length] = new edButton(name+'_img','img','','','m',-1); // special case
557 t.Buttons[t.Buttons.length] = new edButton(name+'_ul','ul','<ul>\n','</ul>\n\n','u');
558 t.Buttons[t.Buttons.length] = new edButton(name+'_ol','ol','<ol>\n','</ol>\n\n','o');
559 t.Buttons[t.Buttons.length] = new edButton(name+'_li','li','\t<li>','</li>\n','l');
560 t.Buttons[t.Buttons.length] = new edButton(name+'_code','code','<code>','</code>','c');
561 t.Buttons[t.Buttons.length] = new edButton(name+'_more','more','<!--more-->','','t',-1);
562 // t.Buttons[t.Buttons.length] = new edButton(name+'_next','page','<!--nextpage-->','','p',-1);
564 tb = document.createElement('div');
565 tb.id = name+'_qtags';
567 html = '<div id="'+name+'_toolbar">';
568 for (i = 0; i < t.Buttons.length; i++)
569 html += t.edShowButton(t.Buttons[i], i);
571 html += '<input type="button" id="'+name+'_ed_spell" class="ed_button" onclick="edSpell('+name+'.Canvas);" title="' + quicktagsL10n.dictionaryLookup + '" value="' + quicktagsL10n.lookup + '" />';
572 html += '<input type="button" id="'+name+'_ed_close" class="ed_button" onclick="'+name+'.edCloseAllTags();" title="' + quicktagsL10n.closeAllOpenTags + '" value="' + quicktagsL10n.closeTags + '" /></div>';
575 cont.parentNode.insertBefore(tb, cont);