]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/quicktags.dev.js
Wordpress 3.2.1-scripts
[autoinstalls/wordpress.git] / wp-includes / js / quicktags.dev.js
1 // new edit toolbar used with permission
2 // by Alex King
3 // http://www.alexking.org/
4
5 var edButtons = new Array(), edLinks = new Array(), edOpenTags = new Array(), now = new Date(), datetime;
6
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
14 }
15
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 }
22         return str;
23 }
24
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) +
31 '+00:00';
32
33 edButtons[edButtons.length] =
34 new edButton('ed_strong'
35 ,'b'
36 ,'<strong>'
37 ,'</strong>'
38 ,'b'
39 );
40
41 edButtons[edButtons.length] =
42 new edButton('ed_em'
43 ,'i'
44 ,'<em>'
45 ,'</em>'
46 ,'i'
47 );
48
49 edButtons[edButtons.length] =
50 new edButton('ed_link'
51 ,'link'
52 ,''
53 ,'</a>'
54 ,'a'
55 ); // special case
56
57 edButtons[edButtons.length] =
58 new edButton('ed_block'
59 ,'b-quote'
60 ,'\n\n<blockquote>'
61 ,'</blockquote>\n\n'
62 ,'q'
63 );
64
65
66 edButtons[edButtons.length] =
67 new edButton('ed_del'
68 ,'del'
69 ,'<del datetime="' + datetime + '">'
70 ,'</del>'
71 ,'d'
72 );
73
74 edButtons[edButtons.length] =
75 new edButton('ed_ins'
76 ,'ins'
77 ,'<ins datetime="' + datetime + '">'
78 ,'</ins>'
79 ,'s'
80 );
81
82 edButtons[edButtons.length] =
83 new edButton('ed_img'
84 ,'img'
85 ,''
86 ,''
87 ,'m'
88 ,-1
89 ); // special case
90
91 edButtons[edButtons.length] =
92 new edButton('ed_ul'
93 ,'ul'
94 ,'<ul>\n'
95 ,'</ul>\n\n'
96 ,'u'
97 );
98
99 edButtons[edButtons.length] =
100 new edButton('ed_ol'
101 ,'ol'
102 ,'<ol>\n'
103 ,'</ol>\n\n'
104 ,'o'
105 );
106
107 edButtons[edButtons.length] =
108 new edButton('ed_li'
109 ,'li'
110 ,'\t<li>'
111 ,'</li>\n'
112 ,'l'
113 );
114
115 edButtons[edButtons.length] =
116 new edButton('ed_code'
117 ,'code'
118 ,'<code>'
119 ,'</code>'
120 ,'c'
121 );
122
123 edButtons[edButtons.length] =
124 new edButton('ed_more'
125 ,'more'
126 ,'<!--more-->'
127 ,''
128 ,'t'
129 ,-1
130 );
131 /*
132 edButtons[edButtons.length] =
133 new edButton('ed_next'
134 ,'page'
135 ,'<!--nextpage-->'
136 ,''
137 ,'p'
138 ,-1
139 );
140 */
141 function edLink() {
142         this.display = '';
143         this.URL = '';
144         this.newWin = 0;
145 }
146
147 edLinks[edLinks.length] = new edLink('WordPress'
148                                     ,'http://wordpress.org/'
149                                     );
150
151 edLinks[edLinks.length] = new edLink('alexking.org'
152                                     ,'http://www.alexking.org/'
153                                     );
154
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 + '" />');
158         }
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 + '" />');
161         }
162         else {
163                 document.write('<input type="button" id="' + button.id + '" accesskey="' + button.access + '" class="ed_button" onclick="edInsertTag(edCanvas, ' + i + ');" value="' + button.display + '"  />');
164         }
165 }
166
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>';
171         }
172         tempStr += '</select>';
173         document.write(tempStr);
174 }
175
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;
180         }
181 }
182
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('/', '');
188                 }
189         }
190 }
191
192 function edCheckOpenTags(button) {
193         var tag = 0, i;
194         for (i = 0; i < edOpenTags.length; i++) {
195                 if (edOpenTags[i] == button) {
196                         tag++;
197                 }
198         }
199         if (tag > 0) {
200                 return true; // tag found
201         }
202         else {
203                 return false; // tag not found
204         }
205 }
206
207 function edCloseAllTags() {
208         var count = edOpenTags.length, o;
209         for (o = 0; o < count; o++) {
210                 edInsertTag(edCanvas, edOpenTags[edOpenTags.length - 1]);
211         }
212 }
213
214 function edQuickLink(i, thisSelect) {
215         if (i > -1) {
216                 var newWin = '', tempStr;
217                 if (edLinks[i].newWin == 1) {
218                         newWin = ' target="_blank"';
219                 }
220                 tempStr = '<a href="' + edLinks[i].URL + '"' + newWin + '>'
221                             + edLinks[i].display
222                             + '</a>';
223                 thisSelect.selectedIndex = 0;
224                 edInsertContent(edCanvas, tempStr);
225         }
226         else {
227                 thisSelect.selectedIndex = 0;
228         }
229 }
230
231 function edSpell(myField) {
232         var word = '', sel, startPos, endPos;
233         if (document.selection) {
234                 myField.focus();
235             sel = document.selection.createRange();
236                 if (sel.text.length > 0) {
237                         word = sel.text;
238                 }
239         }
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);
245                 }
246         }
247         if (word == '') {
248                 word = prompt(quicktagsL10n.wordLookup, '');
249         }
250         if (word !== null && /^\w[\w ]*$/.test(word)) {
251                 window.open('http://www.answers.com/' + escape(word));
252         }
253 }
254
255 function edToolbar() {
256         document.write('<div id="ed_toolbar">');
257         for (var i = 0; i < edButtons.length; i++) {
258                 edShowButton(edButtons[i], i);
259         }
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         document.write('<input type="button" id="ed_fullscreen" class="ed_button" onclick="fullscreen.on();" title="' + quicktagsL10n.toggleFullscreen + '" value="' + quicktagsL10n.fullscreen + '" />');
263 //      edShowLinks(); // disabled by default
264         document.write('</div>');
265 }
266
267 // insertion code
268
269 function edInsertTag(myField, i) {
270         //IE support
271         if (document.selection) {
272                 myField.focus();
273             var sel = document.selection.createRange();
274                 if (sel.text.length > 0) {
275                         sel.text = edButtons[i].tagStart + sel.text + edButtons[i].tagEnd;
276                 }
277                 else {
278                         if (!edCheckOpenTags(i) || edButtons[i].tagEnd == '') {
279                                 sel.text = edButtons[i].tagStart;
280                                 edAddTag(i);
281                         }
282                         else {
283                                 sel.text = edButtons[i].tagEnd;
284                                 edRemoveTag(i);
285                         }
286                 }
287                 myField.focus();
288         }
289         //MOZILLA/NETSCAPE support
290         else if (myField.selectionStart || myField.selectionStart == '0') {
291                 var startPos = myField.selectionStart, endPos = myField.selectionEnd, cursorPos = endPos, scrollTop = myField.scrollTop;
292
293                 if (startPos != endPos) {
294                         myField.value = myField.value.substring(0, startPos)
295                                       + edButtons[i].tagStart
296                                       + myField.value.substring(startPos, endPos)
297                                       + edButtons[i].tagEnd
298                                       + myField.value.substring(endPos, myField.value.length);
299                         cursorPos += edButtons[i].tagStart.length + edButtons[i].tagEnd.length;
300                 }
301                 else {
302                         if (!edCheckOpenTags(i) || edButtons[i].tagEnd == '') {
303                                 myField.value = myField.value.substring(0, startPos)
304                                               + edButtons[i].tagStart
305                                               + myField.value.substring(endPos, myField.value.length);
306                                 edAddTag(i);
307                                 cursorPos = startPos + edButtons[i].tagStart.length;
308                         }
309                         else {
310                                 myField.value = myField.value.substring(0, startPos)
311                                               + edButtons[i].tagEnd
312                                               + myField.value.substring(endPos, myField.value.length);
313                                 edRemoveTag(i);
314                                 cursorPos = startPos + edButtons[i].tagEnd.length;
315                         }
316                 }
317                 myField.focus();
318                 myField.selectionStart = cursorPos;
319                 myField.selectionEnd = cursorPos;
320                 myField.scrollTop = scrollTop;
321         }
322         else {
323                 if (!edCheckOpenTags(i) || edButtons[i].tagEnd == '') {
324                         myField.value += edButtons[i].tagStart;
325                         edAddTag(i);
326                 }
327                 else {
328                         myField.value += edButtons[i].tagEnd;
329                         edRemoveTag(i);
330                 }
331                 myField.focus();
332         }
333 }
334
335 function edInsertContent(myField, myValue) {
336         var sel, startPos, endPos, scrollTop;
337
338         //IE support
339         if (document.selection) {
340                 myField.focus();
341                 sel = document.selection.createRange();
342                 sel.text = myValue;
343                 myField.focus();
344         }
345         //MOZILLA/NETSCAPE support
346         else if (myField.selectionStart || myField.selectionStart == '0') {
347                 startPos = myField.selectionStart;
348                 endPos = myField.selectionEnd;
349                 scrollTop = myField.scrollTop;
350                 myField.value = myField.value.substring(0, startPos)
351                               + myValue
352                       + myField.value.substring(endPos, myField.value.length);
353                 myField.focus();
354                 myField.selectionStart = startPos + myValue.length;
355                 myField.selectionEnd = startPos + myValue.length;
356                 myField.scrollTop = scrollTop;
357         } else {
358                 myField.value += myValue;
359                 myField.focus();
360         }
361 }
362
363 function edInsertLink(myField, i, defaultValue) {
364         if ( 'object' == typeof(wpLink) ) {
365                 wpLink.open();
366         } else {
367                 if (!defaultValue) {
368                         defaultValue = 'http://';
369                 }
370                 if (!edCheckOpenTags(i)) {
371                         var URL = prompt(quicktagsL10n.enterURL, defaultValue);
372                         if (URL) {
373                                 edButtons[i].tagStart = '<a href="' + URL + '">';
374                                 edInsertTag(myField, i);
375                         }
376                 }
377                 else {
378                         edInsertTag(myField, i);
379                 }
380         }
381 }
382
383 function edInsertImage(myField) {
384         var myValue = prompt(quicktagsL10n.enterImageURL, 'http://');
385         if (myValue) {
386                 myValue = '<img src="'
387                                 + myValue
388                                 + '" alt="' + prompt(quicktagsL10n.enterImageDescription, '')
389                                 + '" />';
390                 edInsertContent(myField, myValue);
391         }
392 }
393
394
395 // Allow multiple instances.
396 // Name = unique value, id = textarea id, container = container div.
397 // Can disable some buttons by passing comma delimited string as 4th param.
398 var QTags = function(name, id, container, disabled) {
399         var t = this, cont = document.getElementById(container), i, tag, tb, html, sel;
400
401         t.Buttons = [];
402         t.Links = [];
403         t.OpenTags = [];
404         t.Canvas = document.getElementById(id);
405
406         if ( ! t.Canvas || ! cont )
407                 return;
408
409         disabled = ( typeof disabled != 'undefined' ) ? ','+disabled+',' : '';
410
411         t.edShowButton = function(button, i) {
412                 if ( disabled && (disabled.indexOf(','+button.display+',') != -1) )
413                         return '';
414                 else if ( button.id == name+'_img' )
415                         return '<input type="button" id="' + button.id + '" accesskey="' + button.access + '" class="ed_button" onclick="edInsertImage('+name+'.Canvas);" value="' + button.display + '" />';
416                 else if (button.id == name+'_link')
417                         return '<input type="button" id="' + button.id + '" accesskey="' + button.access + '" class="ed_button" onclick="'+name+'.edInsertLink('+i+');" value="'+button.display+'" />';
418                 else
419                         return '<input type="button" id="' + button.id + '" accesskey="'+button.access+'" class="ed_button" onclick="'+name+'.edInsertTag('+i+');" value="'+button.display+'" />';
420         };
421
422         t.edAddTag = function(button) {
423                 if ( t.Buttons[button].tagEnd != '' ) {
424                         t.OpenTags[t.OpenTags.length] = button;
425                         document.getElementById(t.Buttons[button].id).value = '/' + document.getElementById(t.Buttons[button].id).value;
426                 }
427         };
428
429         t.edRemoveTag = function(button) {
430                 for ( i = 0; i < t.OpenTags.length; i++ ) {
431                         if ( t.OpenTags[i] == button ) {
432                                 t.OpenTags.splice(i, 1);
433                                 document.getElementById(t.Buttons[button].id).value = document.getElementById(t.Buttons[button].id).value.replace('/', '');
434                         }
435                 }
436         };
437
438         t.edCheckOpenTags = function(button) {
439                 tag = 0;
440                 for ( var i = 0; i < t.OpenTags.length; i++ ) {
441                         if ( t.OpenTags[i] == button )
442                                 tag++;
443                 }
444                 if ( tag > 0 ) return true; // tag found
445                 else return false; // tag not found
446         };
447
448         this.edCloseAllTags = function() {
449                 var count = t.OpenTags.length;
450                 for ( var o = 0; o < count; o++ )
451                         t.edInsertTag(t.OpenTags[t.OpenTags.length - 1]);
452         };
453
454         this.edQuickLink = function(i, thisSelect) {
455                 if ( i > -1 ) {
456                         var newWin = '', tempStr;
457                         if ( Links[i].newWin == 1 ) {
458                                 newWin = ' target="_blank"';
459                         }
460                         tempStr = '<a href="' + Links[i].URL + '"' + newWin + '>'
461                                     + Links[i].display
462                                     + '</a>';
463                         thisSelect.selectedIndex = 0;
464                         edInsertContent(t.Canvas, tempStr);
465                 } else {
466                         thisSelect.selectedIndex = 0;
467                 }
468         };
469
470         // insertion code
471         t.edInsertTag = function(i) {
472                 //IE support
473                 if ( document.selection ) {
474                         t.Canvas.focus();
475                     sel = document.selection.createRange();
476                         if ( sel.text.length > 0 ) {
477                                 sel.text = t.Buttons[i].tagStart + sel.text + t.Buttons[i].tagEnd;
478                         } else {
479                                 if ( ! t.edCheckOpenTags(i) || t.Buttons[i].tagEnd == '' ) {
480                                         sel.text = t.Buttons[i].tagStart;
481                                         t.edAddTag(i);
482                                 } else {
483                                         sel.text = t.Buttons[i].tagEnd;
484                                         t.edRemoveTag(i);
485                                 }
486                         }
487                         t.Canvas.focus();
488                 } else if ( t.Canvas.selectionStart || t.Canvas.selectionStart == '0' ) { //MOZILLA/NETSCAPE support
489                         var startPos = t.Canvas.selectionStart, endPos = t.Canvas.selectionEnd, cursorPos = endPos, 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         tb = document.createElement('div');
570         tb.id = name+'_qtags';
571
572         html = '<div id="'+name+'_toolbar">';
573         for (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 };