Dotclear

source: inc/admin/default-templates/js/jsToolBar/jsToolBar.js @ 1085:2b8ebf1d6f24

Revision 1085:2b8ebf1d6f24, 14.7 KB checked in by JcDenis, 13 years ago (diff)

Fix last merge from default branch

Line 
1/* ***** BEGIN LICENSE BLOCK *****
2 * This file is part of DotClear.
3 * Copyright (c) 2005 Nicolas Martin & Olivier Meunier and contributors. All
4 * rights reserved.
5 *
6 * DotClear is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * DotClear is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with DotClear; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 *
20 * ***** END LICENSE BLOCK *****
21*/
22
23function jsToolBar(textarea) {
24     if (!document.createElement) { return; }
25     
26     if (!textarea) { return; }
27     
28     if ((typeof(document["selection"]) == "undefined")
29     && (typeof(textarea["setSelectionRange"]) == "undefined")) {
30          return;
31     }
32     
33     this.textarea = textarea;
34     
35     this.editor = document.createElement('div');
36     this.editor.className = 'jstEditor';
37     
38     this.textarea.parentNode.insertBefore(this.editor,this.textarea);
39     this.editor.appendChild(this.textarea);
40     
41     this.toolbar = document.createElement("div");
42     this.toolbar.className = 'jstElements';
43     this.editor.parentNode.insertBefore(this.toolbar,this.editor);
44     
45     // Dragable resizing (only for gecko)
46     if (navigator.appName == 'Microsoft Internet Explorer')
47     {
48          if (this.editor.addEventListener)
49          {         
50               this.handle = document.createElement('div');
51               this.handle.className = 'jstHandle';
52               var dragStart = this.resizeDragStart;
53               var This = this;
54               this.handle.addEventListener('mousedown',function(event) { dragStart.call(This,event); },false);
55               this.editor.parentNode.insertBefore(this.handle,this.editor.nextSibling);
56          }
57     }
58     
59     this.context = null;
60     this.toolNodes = {}; // lorsque la toolbar est dessinée , cet objet est garni
61                         // de raccourcis vers les éléments DOM correspondants aux outils.
62};
63
64function jsButton(title, fn, scope, className) {
65     this.title = title || null;
66     this.fn = fn || function(){};
67     this.scope = scope || null;
68     this.className = className || null;
69};
70jsButton.prototype.draw = function() {
71     if (!this.scope) return null;
72     
73     var button = document.createElement('button');
74     button.setAttribute('type','button');
75     if (this.className) button.className = this.className;
76     button.title = this.title;
77     var span = document.createElement('span');
78     span.appendChild(document.createTextNode(this.title));
79     button.appendChild(span);
80     
81     if (this.icon != undefined) {
82          button.style.backgroundImage = 'url('+this.icon+')';
83     }
84     if (typeof(this.fn) == 'function') {
85          var This = this;
86          button.onclick = function() { try { This.fn.apply(This.scope, arguments) } catch (e) {} return false; };
87     }
88     return button;
89};
90
91function jsSpace(id) {
92     this.id = id || null;
93     this.width = null;
94};
95jsSpace.prototype.draw = function() {
96     var span = document.createElement('span');
97     if (this.id) span.id = this.id;
98     span.appendChild(document.createTextNode(String.fromCharCode(160)));
99     span.className = 'jstSpacer';
100     if (this.width) span.style.marginRight = this.width+'px';
101     
102     return span;
103};
104
105function jsCombo(title, options, scope, fn, className) {
106     this.title = title || null;
107     this.options = options || null;
108     this.scope = scope || null;
109     this.fn = fn || function(){};
110     this.className = className || null;
111};
112jsCombo.prototype.draw = function() {
113     if (!this.scope || !this.options) return null;
114     
115     var select = document.createElement('select');
116     if (this.className) select.className = className;
117     select.title = this.title;
118     
119     for (var o in this.options) {
120          //var opt = this.options[o];
121          var option = document.createElement('option');
122          option.value = o;
123          option.appendChild(document.createTextNode(this.options[o]));
124          select.appendChild(option);
125     }
126     
127     var This = this;
128     select.onchange = function() {
129          try { 
130               This.fn.call(This.scope, this.value);
131          } catch (e) { alert(e); }
132         
133          return false;
134     };
135     
136     return select;
137};
138
139
140jsToolBar.prototype = {
141     base_url: '',
142     mode: 'xhtml',
143     elements: {},
144     
145     getMode: function() {
146          return this.mode;
147     },
148     
149     setMode: function(mode) {
150          this.mode = mode || 'xhtml';
151     },
152     
153     switchMode: function(mode) {
154          mode = mode || 'xhtml';
155          this.draw(mode);
156     },
157     
158     button: function(toolName) {
159          var tool = this.elements[toolName];
160          if (typeof tool.fn[this.mode] != 'function') return null;
161          var b = new jsButton(tool.title, tool.fn[this.mode], this, 'jstb_'+toolName);
162          if (tool.icon != undefined) {
163               b.icon = tool.icon;
164          }
165          return b;
166     },
167     space: function(toolName) {
168          var tool = new jsSpace(toolName);
169          if (this.elements[toolName].width !== undefined) {
170               tool.width = this.elements[toolName].width;
171          }
172          return tool;
173     },
174     combo: function(toolName) {
175          var tool = this.elements[toolName];
176          var length = tool[this.mode].list.length;
177         
178          if (typeof tool[this.mode].fn != 'function' || length == 0) {
179               return null;
180          } else {
181               var options = {};
182               for (var i=0; i < length; i++) {
183                    var opt = tool[this.mode].list[i];
184                    options[opt] = tool.options[opt];
185               }
186               return new jsCombo(tool.title, options, this, tool[this.mode].fn);
187          }
188     },
189     draw: function(mode) {
190          this.setMode(mode);
191         
192          // Empty toolbar
193          while (this.toolbar.hasChildNodes()) {
194               this.toolbar.removeChild(this.toolbar.firstChild)
195          }
196          this.toolNodes = {}; // vide les raccourcis DOM/**/
197         
198          // Draw toolbar elements
199          var b, tool, newTool;
200         
201          for (var i in this.elements) {
202               b = this.elements[i];
203               
204               var disabled =
205               b.type == undefined || b.type == ''
206               || (b.disabled != undefined && b.disabled)
207               || (b.context != undefined && b.context != null && b.context != this.context);
208               
209               if (!disabled && typeof this[b.type] == 'function') {
210                    tool = this[b.type](i);
211                    if (tool) newTool = tool.draw();
212                    if (newTool) {
213                         this.toolNodes[i] = newTool; //mémorise l'accès DOM pour usage éventuel ultérieur
214                         this.toolbar.appendChild(newTool);
215                    }
216               }
217          }
218     },
219     
220     singleTag: function(stag,etag) {
221          stag = stag || null;
222          etag = etag || stag;
223         
224          if (!stag || !etag) { return; }
225         
226          this.encloseSelection(stag,etag);
227     },
228     
229     encloseSelection: function(prefix, suffix, fn) {
230          this.textarea.focus();
231         
232          prefix = prefix || '';
233          suffix = suffix || '';
234         
235          var start, end, sel, scrollPos, subst, res;
236         
237          if (typeof(document["selection"]) != "undefined") {
238               sel = document.selection.createRange().text;
239          } else if (typeof(this.textarea["setSelectionRange"]) != "undefined") {
240               start = this.textarea.selectionStart;
241               end = this.textarea.selectionEnd;
242               scrollPos = this.textarea.scrollTop;
243               sel = this.textarea.value.substring(start, end);
244          }
245         
246          if (sel.match(/ $/)) { // exclude ending space char, if any
247               sel = sel.substring(0, sel.length - 1);
248               suffix = suffix + " ";
249          }
250         
251          if (typeof(fn) == 'function') {
252               res = (sel) ? fn.call(this,sel) : fn('');
253          } else {
254               res = (sel) ? sel : '';
255          }
256         
257          subst = prefix + res + suffix;
258         
259          if (typeof(document["selection"]) != "undefined") {
260               var range = document.selection.createRange().text = subst;
261               this.textarea.caretPos -= suffix.length;
262          } else if (typeof(this.textarea["setSelectionRange"]) != "undefined") {
263               this.textarea.value = this.textarea.value.substring(0, start) + subst +
264               this.textarea.value.substring(end);
265               if (sel) {
266                    this.textarea.setSelectionRange(start + subst.length, start + subst.length);
267               } else {
268                    this.textarea.setSelectionRange(start + prefix.length, start + prefix.length);
269               }
270               this.textarea.scrollTop = scrollPos;
271          }
272     },
273     
274     stripBaseURL: function(url) {
275          if (this.base_url != '') {
276               var pos = url.indexOf(this.base_url);
277               if (pos == 0) {
278                    url = url.substr(this.base_url.length);
279               }
280          }
281         
282          return url;
283     }
284};
285
286/** Resizer
287-------------------------------------------------------- */
288jsToolBar.prototype.resizeSetStartH = function() {
289     this.dragStartH = this.textarea.offsetHeight + 0;
290};
291jsToolBar.prototype.resizeDragStart = function(event) {
292     var This = this;
293     this.dragStartY = event.clientY;
294     this.resizeSetStartH();
295     document.addEventListener('mousemove', this.dragMoveHdlr=function(event){This.resizeDragMove(event);}, false);
296     document.addEventListener('mouseup', this.dragStopHdlr=function(event){This.resizeDragStop(event);}, false);
297};
298
299jsToolBar.prototype.resizeDragMove = function(event) {
300     this.textarea.style.height = (this.dragStartH+event.clientY-this.dragStartY)+'px';
301};
302
303jsToolBar.prototype.resizeDragStop = function(event) {
304     document.removeEventListener('mousemove', this.dragMoveHdlr, false);
305     document.removeEventListener('mouseup', this.dragStopHdlr, false);
306};
307
308// Elements definition ------------------------------------
309// block format (paragraph, headers)
310jsToolBar.prototype.elements.blocks = {
311     type: 'combo',
312     title: 'block format',
313     options: {
314          none: '-- none --', // only for wysiwyg mode
315          nonebis: '- block format -', // only for xhtml mode
316          p: 'Paragraph',
317          h1: 'Header 1',
318          h2: 'Header 2',
319          h3: 'Header 3',
320          h4: 'Header 4',
321          h5: 'Header 5',
322          h6: 'Header 6'
323     },
324     xhtml: {
325          list: ['nonebis','p','h1','h2','h3','h4','h5','h6'],
326          fn: function(opt) {
327               if (opt=='nonebis')
328                    this.textarea.focus();
329               else
330                    try {this.singleTag('<'+opt+'>','</'+opt+'>');} catch(e){};
331               this.toolNodes.blocks.value = 'nonebis';
332          }
333     },
334     wiki: {
335          list: ['nonebis','h3','h4','h5'],
336          fn: function(opt) {
337               switch (opt) {
338                    case 'nonebis': this.textarea.focus(); break;
339                    case 'h3': this.encloseSelection('!!!'); break;
340                    case 'h4': this.encloseSelection('!!'); break;
341                    case 'h5': this.encloseSelection('!'); break;
342               }
343               this.toolNodes.blocks.value = 'nonebis';
344          }
345     }
346};
347
348// spacer
349jsToolBar.prototype.elements.space0 = {type: 'space'};
350
351// strong
352jsToolBar.prototype.elements.strong = {
353     type: 'button',
354     title: 'Strong emphasis',
355     fn: {
356          wiki: function() { this.singleTag('__') },
357          xhtml: function() { this.singleTag('<strong>','</strong>') }
358     }
359};
360
361// em
362jsToolBar.prototype.elements.em = {
363     type: 'button',
364     title: 'Emphasis',
365     fn: {
366          wiki: function() { this.singleTag("''") },
367          xhtml: function() { this.singleTag('<em>','</em>') }
368     }
369};
370
371// ins
372jsToolBar.prototype.elements.ins = {
373     type: 'button',
374     title: 'Inserted',
375     fn: {
376          wiki: function() { this.singleTag('++') },
377          xhtml: function() { this.singleTag('<ins>','</ins>') }
378     }
379};
380
381// del
382jsToolBar.prototype.elements.del = {
383     type: 'button',
384     title: 'Deleted',
385     fn: {
386          wiki: function() { this.singleTag('--') },
387          xhtml: function() { this.singleTag('<del>','</del>') }
388     }
389};
390
391// quote
392jsToolBar.prototype.elements.quote = {
393     type: 'button',
394     title: 'Inline quote',
395     fn: {
396          wiki: function() { this.singleTag('{{','}}') },
397          xhtml: function() { this.singleTag('<q>','</q>') }
398     }
399};
400
401// code
402jsToolBar.prototype.elements.code = {
403     type: 'button',
404     title: 'Code',
405     fn: {
406          wiki: function() { this.singleTag('@@') },
407          xhtml: function() { this.singleTag('<code>','</code>')}
408     }
409};
410
411// spacer
412jsToolBar.prototype.elements.space1 = {type: 'space'};
413
414// br
415jsToolBar.prototype.elements.br = {
416     type: 'button',
417     title: 'Line break',
418     fn: {
419          wiki: function() { this.encloseSelection("%%%\n",'') },
420          xhtml: function() { this.encloseSelection("<br />\n",'')}
421     }
422};
423
424// spacer
425jsToolBar.prototype.elements.space2 = {type: 'space'};
426
427// blockquote
428jsToolBar.prototype.elements.blockquote = {
429     type: 'button',
430     title: 'Blockquote',
431     fn: {
432          xhtml: function() { this.singleTag('<blockquote>','</blockquote>') },
433          wiki: function() {
434               this.encloseSelection("\n",'',
435               function(str) {
436                    str = str.replace(/\r/g,'');
437                    return '> '+str.replace(/\n/g,"\n> ");
438               });
439          }
440     }
441};
442
443// pre
444jsToolBar.prototype.elements.pre = {
445     type: 'button',
446     title: 'Preformated text',
447     fn: {
448          wiki: function() { this.singleTag("///\n","\n///") },
449          xhtml: function() { this.singleTag('<pre>','</pre>') }
450     }
451};
452
453// ul
454jsToolBar.prototype.elements.ul = {
455     type: 'button',
456     title: 'Unordered list',
457     fn: {
458          wiki: function() {
459               this.encloseSelection('','',function(str) {
460                    str = str.replace(/\r/g,'');
461                    return '* '+str.replace(/\n/g,"\n* ");
462               });
463          },
464          xhtml: function() {
465               this.encloseSelection('','',function(str) {
466                    str = str.replace(/\r/g,'');
467                    str = str.replace(/\n/g,"</li>\n <li>");
468                    return "<ul>\n <li>"+str+"</li>\n</ul>";
469               });
470          }
471     }
472};
473
474// ol
475jsToolBar.prototype.elements.ol = {
476     type: 'button',
477     title: 'Ordered list',
478     fn: {
479          wiki: function() {
480               this.encloseSelection('','',function(str) {
481                    str = str.replace(/\r/g,'');
482                    return '# '+str.replace(/\n/g,"\n# ");
483               });
484          },
485          xhtml: function() {
486               this.encloseSelection('','',function(str) {
487                    str = str.replace(/\r/g,'');
488                    str = str.replace(/\n/g,"</li>\n <li>");
489                    return "<ol>\n <li>"+str+"</li>\n</ol>";
490               });
491          }
492     }
493};
494
495// spacer
496jsToolBar.prototype.elements.space3 = {type: 'space'};
497
498// link
499jsToolBar.prototype.elements.link = {
500     type: 'button',
501     title: 'Link',
502     fn: {},
503     href_prompt: 'Please give page URL:',
504     hreflang_prompt: 'Language of this page:',
505     default_hreflang: '',
506     prompt: function(href,hreflang) {
507          href = href || '';
508          hreflang = hreflang || this.elements.link.default_hreflang;
509         
510          href = window.prompt(this.elements.link.href_prompt,href);
511          if (!href) { return false; }
512         
513          hreflang = window.prompt(this.elements.link.hreflang_prompt,
514          hreflang);
515         
516          return { href: this.stripBaseURL(href), hreflang: hreflang };
517     }
518};
519
520jsToolBar.prototype.elements.link.fn.xhtml = function() {
521     var link = this.elements.link.prompt.call(this);
522     if (link) {
523          var stag = '<a href="'+link.href+'"';
524          if (link.hreflang) { stag = stag+' hreflang="'+link.hreflang+'"'; }
525          stag = stag+'>';
526          var etag = '</a>';
527         
528          this.encloseSelection(stag,etag);
529     }
530};
531jsToolBar.prototype.elements.link.fn.wiki = function() {
532     var link = this.elements.link.prompt.call(this);
533     if (link) {
534          var stag = '[';
535          var etag = '|'+link.href;
536          if (link.hreflang) { etag = etag+'|'+link.hreflang; }
537          etag = etag+']';
538         
539          this.encloseSelection(stag,etag);
540     }
541};
542
543// img
544jsToolBar.prototype.elements.img = {
545          type: 'button',
546          title: 'External image',
547          src_prompt: 'Please give image URL:',
548          fn: {},
549          prompt: function(src) {
550               src = src || '';
551               return this.stripBaseURL(window.prompt(this.elements.img.src_prompt,src));
552          }
553};
554jsToolBar.prototype.elements.img.fn.xhtml = function() {
555     var src = this.elements.img.prompt.call(this);
556     if (src) {
557          this.encloseSelection('','',function(str) {
558               if (str) {
559                    return '<img src="'+src+'" alt="'+str+'" />';
560               } else {
561                    return '<img src="'+src+'" alt="" />';
562               }
563          });
564     }
565};
566jsToolBar.prototype.elements.img.fn.wiki = function() {
567     var src = this.elements.img.prompt.call(this);
568     if (src) {
569          this.encloseSelection('','',function(str) {
570               if (str) {
571                    return '(('+src+'|'+str+'))';
572               } else {
573                    return '(('+src+'))';
574               }
575          });
576     }
577};
Note: See TracBrowser for help on using the repository browser.

Sites map