Projects : mp-wp : mp-wp_genesis
1 | // new edit toolbar used with permission |
2 | // by Alex King |
3 | // http://www.alexking.org/ |
4 | |
5 | var edButtons = new Array(); |
6 | var edLinks = new Array(); |
7 | var edOpenTags = new Array(); |
8 | |
9 | function edButton(id, display, tagStart, tagEnd, access, open) { |
10 | this.id = id; // used to name the toolbar button |
11 | this.display = display; // label on button |
12 | this.tagStart = tagStart; // open tag |
13 | this.tagEnd = tagEnd; // close tag |
14 | this.access = access; // access key |
15 | this.open = open; // set to -1 if tag does not need to be closed |
16 | } |
17 | |
18 | function zeroise(number, threshold) { |
19 | // FIXME: or we could use an implementation of printf in js here |
20 | var str = number.toString(); |
21 | if (number < 0) { str = str.substr(1, str.length) } |
22 | while (str.length < threshold) { str = "0" + str } |
23 | if (number < 0) { str = '-' + str } |
24 | return str; |
25 | } |
26 | |
27 | var now = new Date(); |
28 | var datetime = now.getUTCFullYear() + '-' + |
29 | zeroise(now.getUTCMonth() + 1, 2) + '-' + |
30 | zeroise(now.getUTCDate(), 2) + 'T' + |
31 | zeroise(now.getUTCHours(), 2) + ':' + |
32 | zeroise(now.getUTCMinutes(), 2) + ':' + |
33 | zeroise(now.getUTCSeconds() ,2) + |
34 | '+00:00'; |
35 | |
36 | edButtons[edButtons.length] = |
37 | new edButton('ed_strong' |
38 | ,'b' |
39 | ,'<strong>' |
40 | ,'</strong>' |
41 | ,'b' |
42 | ); |
43 | |
44 | edButtons[edButtons.length] = |
45 | new edButton('ed_em' |
46 | ,'i' |
47 | ,'<em>' |
48 | ,'</em>' |
49 | ,'i' |
50 | ); |
51 | |
52 | edButtons[edButtons.length] = |
53 | new edButton('ed_link' |
54 | ,'link' |
55 | ,'' |
56 | ,'</a>' |
57 | ,'a' |
58 | ); // special case |
59 | |
60 | edButtons[edButtons.length] = |
61 | new edButton('ed_block' |
62 | ,'b-quote' |
63 | ,'\n\n<blockquote>' |
64 | ,'</blockquote>\n\n' |
65 | ,'q' |
66 | ); |
67 | |
68 | |
69 | edButtons[edButtons.length] = |
70 | new edButton('ed_del' |
71 | ,'del' |
72 | ,'<del datetime="' + datetime + '">' |
73 | ,'</del>' |
74 | ,'d' |
75 | ); |
76 | |
77 | edButtons[edButtons.length] = |
78 | new edButton('ed_ins' |
79 | ,'ins' |
80 | ,'<ins datetime="' + datetime + '">' |
81 | ,'</ins>' |
82 | ,'s' |
83 | ); |
84 | |
85 | edButtons[edButtons.length] = |
86 | new edButton('ed_img' |
87 | ,'img' |
88 | ,'' |
89 | ,'' |
90 | ,'m' |
91 | ,-1 |
92 | ); // special case |
93 | |
94 | edButtons[edButtons.length] = |
95 | new edButton('ed_ul' |
96 | ,'ul' |
97 | ,'<ul>\n' |
98 | ,'</ul>\n\n' |
99 | ,'u' |
100 | ); |
101 | |
102 | edButtons[edButtons.length] = |
103 | new edButton('ed_ol' |
104 | ,'ol' |
105 | ,'<ol>\n' |
106 | ,'</ol>\n\n' |
107 | ,'o' |
108 | ); |
109 | |
110 | edButtons[edButtons.length] = |
111 | new edButton('ed_li' |
112 | ,'li' |
113 | ,'\t<li>' |
114 | ,'</li>\n' |
115 | ,'l' |
116 | ); |
117 | |
118 | edButtons[edButtons.length] = |
119 | new edButton('ed_code' |
120 | ,'code' |
121 | ,'<code>' |
122 | ,'</code>' |
123 | ,'c' |
124 | ); |
125 | |
126 | edButtons[edButtons.length] = |
127 | new edButton('ed_more' |
128 | ,'more' |
129 | ,'<!--more-->' |
130 | ,'' |
131 | ,'t' |
132 | ,-1 |
133 | ); |
134 | /* |
135 | edButtons[edButtons.length] = |
136 | new edButton('ed_next' |
137 | ,'page' |
138 | ,'<!--nextpage-->' |
139 | ,'' |
140 | ,'p' |
141 | ,-1 |
142 | ); |
143 | */ |
144 | function edLink() { |
145 | this.display = ''; |
146 | this.URL = ''; |
147 | this.newWin = 0; |
148 | } |
149 | |
150 | edLinks[edLinks.length] = new edLink('WordPress' |
151 | ,'http://wordpress.org/' |
152 | ); |
153 | |
154 | edLinks[edLinks.length] = new edLink('alexking.org' |
155 | ,'http://www.alexking.org/' |
156 | ); |
157 | |
158 | function edShowButton(button, i) { |
159 | if (button.id == 'ed_img') { |
160 | document.write('<input type="button" id="' + button.id + '" accesskey="' + button.access + '" class="ed_button" onclick="edInsertImage(edCanvas);" value="' + button.display + '" />'); |
161 | } |
162 | else if (button.id == 'ed_link') { |
163 | document.write('<input type="button" id="' + button.id + '" accesskey="' + button.access + '" class="ed_button" onclick="edInsertLink(edCanvas, ' + i + ');" value="' + button.display + '" />'); |
164 | } |
165 | else { |
166 | document.write('<input type="button" id="' + button.id + '" accesskey="' + button.access + '" class="ed_button" onclick="edInsertTag(edCanvas, ' + i + ');" value="' + button.display + '" />'); |
167 | } |
168 | } |
169 | |
170 | function edShowLinks() { |
171 | var tempStr = '<select onchange="edQuickLink(this.options[this.selectedIndex].value, this);"><option value="-1" selected>' + quicktagsL10n.quickLinks + '</option>'; |
172 | for (i = 0; i < edLinks.length; i++) { |
173 | tempStr += '<option value="' + i + '">' + edLinks[i].display + '</option>'; |
174 | } |
175 | tempStr += '</select>'; |
176 | document.write(tempStr); |
177 | } |
178 | |
179 | function edAddTag(button) { |
180 | if (edButtons[button].tagEnd != '') { |
181 | edOpenTags[edOpenTags.length] = button; |
182 | document.getElementById(edButtons[button].id).value = '/' + document.getElementById(edButtons[button].id).value; |
183 | } |
184 | } |
185 | |
186 | function edRemoveTag(button) { |
187 | for (i = 0; i < edOpenTags.length; i++) { |
188 | if (edOpenTags[i] == button) { |
189 | edOpenTags.splice(i, 1); |
190 | document.getElementById(edButtons[button].id).value = document.getElementById(edButtons[button].id).value.replace('/', ''); |
191 | } |
192 | } |
193 | } |
194 | |
195 | function edCheckOpenTags(button) { |
196 | var tag = 0; |
197 | for (i = 0; i < edOpenTags.length; i++) { |
198 | if (edOpenTags[i] == button) { |
199 | tag++; |
200 | } |
201 | } |
202 | if (tag > 0) { |
203 | return true; // tag found |
204 | } |
205 | else { |
206 | return false; // tag not found |
207 | } |
208 | } |
209 | |
210 | function edCloseAllTags() { |
211 | var count = edOpenTags.length; |
212 | for (o = 0; o < count; o++) { |
213 | edInsertTag(edCanvas, edOpenTags[edOpenTags.length - 1]); |
214 | } |
215 | } |
216 | |
217 | function edQuickLink(i, thisSelect) { |
218 | if (i > -1) { |
219 | var newWin = ''; |
220 | if (edLinks[i].newWin == 1) { |
221 | newWin = ' target="_blank"'; |
222 | } |
223 | var tempStr = '<a href="' + edLinks[i].URL + '"' + newWin + '>' |
224 | + edLinks[i].display |
225 | + '</a>'; |
226 | thisSelect.selectedIndex = 0; |
227 | edInsertContent(edCanvas, tempStr); |
228 | } |
229 | else { |
230 | thisSelect.selectedIndex = 0; |
231 | } |
232 | } |
233 | |
234 | function edSpell(myField) { |
235 | var word = ''; |
236 | if (document.selection) { |
237 | myField.focus(); |
238 | var sel = document.selection.createRange(); |
239 | if (sel.text.length > 0) { |
240 | word = sel.text; |
241 | } |
242 | } |
243 | else if (myField.selectionStart || myField.selectionStart == '0') { |
244 | var startPos = myField.selectionStart; |
245 | var endPos = myField.selectionEnd; |
246 | if (startPos != endPos) { |
247 | word = myField.value.substring(startPos, endPos); |
248 | } |
249 | } |
250 | if (word == '') { |
251 | word = prompt(quicktagsL10n.wordLookup, ''); |
252 | } |
253 | if (word !== null && /^\w[\w ]*$/.test(word)) { |
254 | window.open('http://www.answers.com/' + escape(word)); |
255 | } |
256 | } |
257 | |
258 | function edToolbar() { |
259 | document.write('<div id="ed_toolbar">'); |
260 | for (i = 0; i < edButtons.length; i++) { |
261 | edShowButton(edButtons[i], i); |
262 | } |
263 | document.write('<input type="button" id="ed_spell" class="ed_button" onclick="edSpell(edCanvas);" title="' + quicktagsL10n.dictionaryLookup + '" value="' + quicktagsL10n.lookup + '" />'); |
264 | document.write('<input type="button" id="ed_close" class="ed_button" onclick="edCloseAllTags();" title="' + quicktagsL10n.closeAllOpenTags + '" value="' + quicktagsL10n.closeTags + '" />'); |
265 | // edShowLinks(); // disabled by default |
266 | document.write('</div>'); |
267 | } |
268 | |
269 | // insertion code |
270 | |
271 | function edInsertTag(myField, i) { |
272 | //IE support |
273 | if (document.selection) { |
274 | myField.focus(); |
275 | sel = document.selection.createRange(); |
276 | if (sel.text.length > 0) { |
277 | sel.text = edButtons[i].tagStart + sel.text + edButtons[i].tagEnd; |
278 | } |
279 | else { |
280 | if (!edCheckOpenTags(i) || edButtons[i].tagEnd == '') { |
281 | sel.text = edButtons[i].tagStart; |
282 | edAddTag(i); |
283 | } |
284 | else { |
285 | sel.text = edButtons[i].tagEnd; |
286 | edRemoveTag(i); |
287 | } |
288 | } |
289 | myField.focus(); |
290 | } |
291 | //MOZILLA/NETSCAPE support |
292 | else if (myField.selectionStart || myField.selectionStart == '0') { |
293 | var startPos = myField.selectionStart; |
294 | var endPos = myField.selectionEnd; |
295 | var cursorPos = endPos; |
296 | var scrollTop = myField.scrollTop; |
297 | |
298 | if (startPos != endPos) { |
299 | myField.value = myField.value.substring(0, startPos) |
300 | + edButtons[i].tagStart |
301 | + myField.value.substring(startPos, endPos) |
302 | + edButtons[i].tagEnd |
303 | + myField.value.substring(endPos, myField.value.length); |
304 | cursorPos += edButtons[i].tagStart.length + edButtons[i].tagEnd.length; |
305 | } |
306 | else { |
307 | if (!edCheckOpenTags(i) || edButtons[i].tagEnd == '') { |
308 | myField.value = myField.value.substring(0, startPos) |
309 | + edButtons[i].tagStart |
310 | + myField.value.substring(endPos, myField.value.length); |
311 | edAddTag(i); |
312 | cursorPos = startPos + edButtons[i].tagStart.length; |
313 | } |
314 | else { |
315 | myField.value = myField.value.substring(0, startPos) |
316 | + edButtons[i].tagEnd |
317 | + myField.value.substring(endPos, myField.value.length); |
318 | edRemoveTag(i); |
319 | cursorPos = startPos + edButtons[i].tagEnd.length; |
320 | } |
321 | } |
322 | myField.focus(); |
323 | myField.selectionStart = cursorPos; |
324 | myField.selectionEnd = cursorPos; |
325 | myField.scrollTop = scrollTop; |
326 | } |
327 | else { |
328 | if (!edCheckOpenTags(i) || edButtons[i].tagEnd == '') { |
329 | myField.value += edButtons[i].tagStart; |
330 | edAddTag(i); |
331 | } |
332 | else { |
333 | myField.value += edButtons[i].tagEnd; |
334 | edRemoveTag(i); |
335 | } |
336 | myField.focus(); |
337 | } |
338 | } |
339 | |
340 | function edInsertContent(myField, myValue) { |
341 | //IE support |
342 | if (document.selection) { |
343 | myField.focus(); |
344 | sel = document.selection.createRange(); |
345 | sel.text = myValue; |
346 | myField.focus(); |
347 | } |
348 | //MOZILLA/NETSCAPE support |
349 | else if (myField.selectionStart || myField.selectionStart == '0') { |
350 | var startPos = myField.selectionStart; |
351 | var endPos = myField.selectionEnd; |
352 | myField.value = myField.value.substring(0, startPos) |
353 | + myValue |
354 | + myField.value.substring(endPos, myField.value.length); |
355 | myField.focus(); |
356 | myField.selectionStart = startPos + myValue.length; |
357 | myField.selectionEnd = startPos + myValue.length; |
358 | } else { |
359 | myField.value += myValue; |
360 | myField.focus(); |
361 | } |
362 | } |
363 | |
364 | function edInsertLink(myField, i, defaultValue) { |
365 | if (!defaultValue) { |
366 | defaultValue = 'http://'; |
367 | } |
368 | if (!edCheckOpenTags(i)) { |
369 | var URL = prompt(quicktagsL10n.enterURL, defaultValue); |
370 | if (URL) { |
371 | edButtons[i].tagStart = '<a href="' + URL + '">'; |
372 | edInsertTag(myField, i); |
373 | } |
374 | } |
375 | else { |
376 | edInsertTag(myField, i); |
377 | } |
378 | } |
379 | |
380 | function edInsertImage(myField) { |
381 | var myValue = prompt(quicktagsL10n.enterImageURL, 'http://'); |
382 | if (myValue) { |
383 | myValue = '<img src="' |
384 | + myValue |
385 | + '" alt="' + prompt(quicktagsL10n.enterImageDescription, '') |
386 | + '" />'; |
387 | edInsertContent(myField, myValue); |
388 | } |
389 | } |
390 | |
391 | |
392 | // Allow multiple instances. |
393 | // Name = unique value, id = textarea id, container = container div. |
394 | // Can disable some buttons by passing comma delimited string as 4th param. |
395 | var QTags = function(name, id, container, disabled) { |
396 | var t = this, cont = document.getElementById(container); |
397 | |
398 | t.Buttons = []; |
399 | t.Links = []; |
400 | t.OpenTags = []; |
401 | t.Canvas = document.getElementById(id); |
402 | |
403 | if ( ! t.Canvas || ! cont ) |
404 | return; |
405 | |
406 | disabled = ( typeof disabled != 'undefined' ) ? ','+disabled+',' : ''; |
407 | |
408 | t.edShowButton = function(button, i) { |
409 | if ( disabled && (disabled.indexOf(','+button.display+',') != -1) ) |
410 | return ''; |
411 | else if ( button.id == name+'_img' ) |
412 | return '<input type="button" id="' + button.id + '" accesskey="' + button.access + '" class="ed_button" onclick="edInsertImage('+name+'.Canvas);" value="' + button.display + '" />'; |
413 | else if (button.id == name+'_link') |
414 | return '<input type="button" id="' + button.id + '" accesskey="' + button.access + '" class="ed_button" onclick="'+name+'.edInsertLink('+i+');" value="'+button.display+'" />'; |
415 | else |
416 | return '<input type="button" id="' + button.id + '" accesskey="'+button.access+'" class="ed_button" onclick="'+name+'.edInsertTag('+i+');" value="'+button.display+'" />'; |
417 | }; |
418 | |
419 | t.edAddTag = function(button) { |
420 | if ( t.Buttons[button].tagEnd != '' ) { |
421 | t.OpenTags[t.OpenTags.length] = button; |
422 | document.getElementById(t.Buttons[button].id).value = '/' + document.getElementById(t.Buttons[button].id).value; |
423 | } |
424 | }; |
425 | |
426 | t.edRemoveTag = function(button) { |
427 | for ( var i = 0; i < t.OpenTags.length; i++ ) { |
428 | if ( t.OpenTags[i] == button ) { |
429 | t.OpenTags.splice(i, 1); |
430 | document.getElementById(t.Buttons[button].id).value = document.getElementById(t.Buttons[button].id).value.replace('/', ''); |
431 | } |
432 | } |
433 | }; |
434 | |
435 | t.edCheckOpenTags = function(button) { |
436 | var tag = 0; |
437 | for ( var i = 0; i < t.OpenTags.length; i++ ) { |
438 | if ( t.OpenTags[i] == button ) |
439 | tag++; |
440 | } |
441 | if ( tag > 0 ) return true; // tag found |
442 | else return false; // tag not found |
443 | }; |
444 | |
445 | this.edCloseAllTags = function() { |
446 | var count = t.OpenTags.length; |
447 | for ( var o = 0; o < count; o++ ) |
448 | t.edInsertTag(t.OpenTags[t.OpenTags.length - 1]); |
449 | }; |
450 | |
451 | this.edQuickLink = function(i, thisSelect) { |
452 | if ( i > -1 ) { |
453 | var newWin = ''; |
454 | if ( Links[i].newWin == 1 ) { |
455 | newWin = ' target="_blank"'; |
456 | } |
457 | var tempStr = '<a href="' + Links[i].URL + '"' + newWin + '>' |
458 | + Links[i].display |
459 | + '</a>'; |
460 | thisSelect.selectedIndex = 0; |
461 | edInsertContent(t.Canvas, tempStr); |
462 | } else { |
463 | thisSelect.selectedIndex = 0; |
464 | } |
465 | }; |
466 | |
467 | // insertion code |
468 | t.edInsertTag = function(i) { |
469 | //IE support |
470 | if ( document.selection ) { |
471 | t.Canvas.focus(); |
472 | sel = document.selection.createRange(); |
473 | if ( sel.text.length > 0 ) { |
474 | sel.text = t.Buttons[i].tagStart + sel.text + t.Buttons[i].tagEnd; |
475 | } else { |
476 | if ( ! t.edCheckOpenTags(i) || t.Buttons[i].tagEnd == '' ) { |
477 | sel.text = t.Buttons[i].tagStart; |
478 | t.edAddTag(i); |
479 | } else { |
480 | sel.text = t.Buttons[i].tagEnd; |
481 | t.edRemoveTag(i); |
482 | } |
483 | } |
484 | t.Canvas.focus(); |
485 | } else if ( t.Canvas.selectionStart || t.Canvas.selectionStart == '0' ) { //MOZILLA/NETSCAPE support |
486 | var startPos = t.Canvas.selectionStart; |
487 | var endPos = t.Canvas.selectionEnd; |
488 | var cursorPos = endPos; |
489 | var scrollTop = t.Canvas.scrollTop; |
490 | |
491 | if ( startPos != endPos ) { |
492 | t.Canvas.value = t.Canvas.value.substring(0, startPos) |
493 | + t.Buttons[i].tagStart |
494 | + t.Canvas.value.substring(startPos, endPos) |
495 | + t.Buttons[i].tagEnd |
496 | + t.Canvas.value.substring(endPos, t.Canvas.value.length); |
497 | cursorPos += t.Buttons[i].tagStart.length + t.Buttons[i].tagEnd.length; |
498 | } else { |
499 | if ( !t.edCheckOpenTags(i) || t.Buttons[i].tagEnd == '' ) { |
500 | t.Canvas.value = t.Canvas.value.substring(0, startPos) |
501 | + t.Buttons[i].tagStart |
502 | + t.Canvas.value.substring(endPos, t.Canvas.value.length); |
503 | t.edAddTag(i); |
504 | cursorPos = startPos + t.Buttons[i].tagStart.length; |
505 | } else { |
506 | t.Canvas.value = t.Canvas.value.substring(0, startPos) |
507 | + t.Buttons[i].tagEnd |
508 | + t.Canvas.value.substring(endPos, t.Canvas.value.length); |
509 | t.edRemoveTag(i); |
510 | cursorPos = startPos + t.Buttons[i].tagEnd.length; |
511 | } |
512 | } |
513 | t.Canvas.focus(); |
514 | t.Canvas.selectionStart = cursorPos; |
515 | t.Canvas.selectionEnd = cursorPos; |
516 | t.Canvas.scrollTop = scrollTop; |
517 | } else { |
518 | if ( ! t.edCheckOpenTags(i) || t.Buttons[i].tagEnd == '' ) { |
519 | t.Canvas.value += Buttons[i].tagStart; |
520 | t.edAddTag(i); |
521 | } else { |
522 | t.Canvas.value += Buttons[i].tagEnd; |
523 | t.edRemoveTag(i); |
524 | } |
525 | t.Canvas.focus(); |
526 | } |
527 | }; |
528 | |
529 | this.edInsertLink = function(i, defaultValue) { |
530 | if ( ! defaultValue ) |
531 | defaultValue = 'http://'; |
532 | |
533 | if ( ! t.edCheckOpenTags(i) ) { |
534 | var URL = prompt(quicktagsL10n.enterURL, defaultValue); |
535 | if ( URL ) { |
536 | t.Buttons[i].tagStart = '<a href="' + URL + '">'; |
537 | t.edInsertTag(i); |
538 | } |
539 | } else { |
540 | t.edInsertTag(i); |
541 | } |
542 | }; |
543 | |
544 | this.edInsertImage = function() { |
545 | var myValue = prompt(quicktagsL10n.enterImageURL, 'http://'); |
546 | if ( myValue ) { |
547 | myValue = '<img src="' |
548 | + myValue |
549 | + '" alt="' + prompt(quicktagsL10n.enterImageDescription, '') |
550 | + '" />'; |
551 | edInsertContent(t.Canvas, myValue); |
552 | } |
553 | }; |
554 | |
555 | t.Buttons[t.Buttons.length] = new edButton(name+'_strong','b','<strong>','</strong>','b'); |
556 | t.Buttons[t.Buttons.length] = new edButton(name+'_em','i','<em>','</em>','i'); |
557 | t.Buttons[t.Buttons.length] = new edButton(name+'_link','link','','</a>','a'); // special case |
558 | t.Buttons[t.Buttons.length] = new edButton(name+'_block','b-quote','\n\n<blockquote>','</blockquote>\n\n','q'); |
559 | t.Buttons[t.Buttons.length] = new edButton(name+'_del','del','<del datetime="' + datetime + '">','</del>','d'); |
560 | t.Buttons[t.Buttons.length] = new edButton(name+'_ins','ins','<ins datetime="' + datetime + '">','</ins>','s'); |
561 | t.Buttons[t.Buttons.length] = new edButton(name+'_img','img','','','m',-1); // special case |
562 | t.Buttons[t.Buttons.length] = new edButton(name+'_ul','ul','<ul>\n','</ul>\n\n','u'); |
563 | t.Buttons[t.Buttons.length] = new edButton(name+'_ol','ol','<ol>\n','</ol>\n\n','o'); |
564 | t.Buttons[t.Buttons.length] = new edButton(name+'_li','li','\t<li>','</li>\n','l'); |
565 | t.Buttons[t.Buttons.length] = new edButton(name+'_code','code','<code>','</code>','c'); |
566 | t.Buttons[t.Buttons.length] = new edButton(name+'_more','more','<!--more-->','','t',-1); |
567 | // t.Buttons[t.Buttons.length] = new edButton(name+'_next','page','<!--nextpage-->','','p',-1); |
568 | |
569 | var tb = document.createElement('div'); |
570 | tb.id = name+'_qtags'; |
571 | |
572 | var html = '<div id="'+name+'_toolbar">'; |
573 | for (var i = 0; i < t.Buttons.length; i++) |
574 | html += t.edShowButton(t.Buttons[i], i); |
575 | |
576 | html += '<input type="button" id="'+name+'_ed_spell" class="ed_button" onclick="edSpell('+name+'.Canvas);" title="' + quicktagsL10n.dictionaryLookup + '" value="' + quicktagsL10n.lookup + '" />'; |
577 | html += '<input type="button" id="'+name+'_ed_close" class="ed_button" onclick="'+name+'.edCloseAllTags();" title="' + quicktagsL10n.closeAllOpenTags + '" value="' + quicktagsL10n.closeTags + '" /></div>'; |
578 | |
579 | tb.innerHTML = html; |
580 | cont.parentNode.insertBefore(tb, cont); |
581 | |
582 | }; |