Dotclear

source: inc/admin/default-templates/js/jsToolBar/jsToolBar.js @ 1056:b67f949a98f8

Revision 1056:b67f949a98f8, 14.7 KB checked in by JcDenis, 13 years ago (diff)
  • Twig know loaded in dcCore
  • Admin context now loaded in admin prepend and avavailable under $_ctx
  • Copy admin theme features in default-templates
  • New URL argument to serve theme file as ?tf=my_theme_file
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','h2','h3','h4','h5'],
336          fn: function(opt) {
337               switch (opt) {
338                    case 'nonebis': this.textarea.focus(); break;
339                    case 'h2': this.encloseSelection('!!!!'); break;
340                    case 'h3': this.encloseSelection('!!!'); break;
341                    case 'h4': this.encloseSelection('!!'); break;
342                    case 'h5': this.encloseSelection('!'); break;
343               }
344               this.toolNodes.blocks.value = 'nonebis';
345          }
346     }
347};
348
349// spacer
350jsToolBar.prototype.elements.space0 = {type: 'space'};
351
352// strong
353jsToolBar.prototype.elements.strong = {
354     type: 'button',
355     title: 'Strong emphasis',
356     fn: {
357          wiki: function() { this.singleTag('__') },
358          xhtml: function() { this.singleTag('<strong>','</strong>') }
359     }
360};
361
362// em
363jsToolBar.prototype.elements.em = {
364     type: 'button',
365     title: 'Emphasis',
366     fn: {
367          wiki: function() { this.singleTag("''") },
368          xhtml: function() { this.singleTag('<em>','</em>') }
369     }
370};
371
372// ins
373jsToolBar.prototype.elements.ins = {
374     type: 'button',
375     title: 'Inserted',
376     fn: {
377          wiki: function() { this.singleTag('++') },
378          xhtml: function() { this.singleTag('<ins>','</ins>') }
379     }
380};
381
382// del
383jsToolBar.prototype.elements.del = {
384     type: 'button',
385     title: 'Deleted',
386     fn: {
387          wiki: function() { this.singleTag('--') },
388          xhtml: function() { this.singleTag('<del>','</del>') }
389     }
390};
391
392// quote
393jsToolBar.prototype.elements.quote = {
394     type: 'button',
395     title: 'Inline quote',
396     fn: {
397          wiki: function() { this.singleTag('{{','}}') },
398          xhtml: function() { this.singleTag('<q>','</q>') }
399     }
400};
401
402// code
403jsToolBar.prototype.elements.code = {
404     type: 'button',
405     title: 'Code',
406     fn: {
407          wiki: function() { this.singleTag('@@') },
408          xhtml: function() { this.singleTag('<code>','</code>')}
409     }
410};
411
412// spacer
413jsToolBar.prototype.elements.space1 = {type: 'space'};
414
415// br
416jsToolBar.prototype.elements.br = {
417     type: 'button',
418     title: 'Line break',
419     fn: {
420          wiki: function() { this.encloseSelection("%%%\n",'') },
421          xhtml: function() { this.encloseSelection("<br />\n",'')}
422     }
423};
424
425// spacer
426jsToolBar.prototype.elements.space2 = {type: 'space'};
427
428// blockquote
429jsToolBar.prototype.elements.blockquote = {
430     type: 'button',
431     title: 'Blockquote',
432     fn: {
433          xhtml: function() { this.singleTag('<blockquote>','</blockquote>') },
434          wiki: function() {
435               this.encloseSelection("\n",'',
436               function(str) {
437                    str = str.replace(/\r/g,'');
438                    return '> '+str.replace(/\n/g,"\n> ");
439               });
440          }
441     }
442};
443
444// pre
445jsToolBar.prototype.elements.pre = {
446     type: 'button',
447     title: 'Preformated text',
448     fn: {
449          wiki: function() { this.singleTag("///\n","\n///") },
450          xhtml: function() { this.singleTag('<pre>','</pre>') }
451     }
452};
453
454// ul
455jsToolBar.prototype.elements.ul = {
456     type: 'button',
457     title: 'Unordered list',
458     fn: {
459          wiki: function() {
460               this.encloseSelection('','',function(str) {
461                    str = str.replace(/\r/g,'');
462                    return '* '+str.replace(/\n/g,"\n* ");
463               });
464          },
465          xhtml: function() {
466               this.encloseSelection('','',function(str) {
467                    str = str.replace(/\r/g,'');
468                    str = str.replace(/\n/g,"</li>\n <li>");
469                    return "<ul>\n <li>"+str+"</li>\n</ul>";
470               });
471          }
472     }
473};
474
475// ol
476jsToolBar.prototype.elements.ol = {
477     type: 'button',
478     title: 'Ordered list',
479     fn: {
480          wiki: function() {
481               this.encloseSelection('','',function(str) {
482                    str = str.replace(/\r/g,'');
483                    return '# '+str.replace(/\n/g,"\n# ");
484               });
485          },
486          xhtml: function() {
487               this.encloseSelection('','',function(str) {
488                    str = str.replace(/\r/g,'');
489                    str = str.replace(/\n/g,"</li>\n <li>");
490                    return "<ol>\n <li>"+str+"</li>\n</ol>";
491               });
492          }
493     }
494};
495
496// spacer
497jsToolBar.prototype.elements.space3 = {type: 'space'};
498
499// link
500jsToolBar.prototype.elements.link = {
501     type: 'button',
502     title: 'Link',
503     fn: {},
504     href_prompt: 'Please give page URL:',
505     hreflang_prompt: 'Language of this page:',
506     default_hreflang: '',
507     prompt: function(href,hreflang) {
508          href = href || '';
509          hreflang = hreflang || this.elements.link.default_hreflang;
510         
511          href = window.prompt(this.elements.link.href_prompt,href);
512          if (!href) { return false; }
513         
514          hreflang = window.prompt(this.elements.link.hreflang_prompt,
515          hreflang);
516         
517          return { href: this.stripBaseURL(href), hreflang: hreflang };
518     }
519};
520
521jsToolBar.prototype.elements.link.fn.xhtml = function() {
522     var link = this.elements.link.prompt.call(this);
523     if (link) {
524          var stag = '<a href="'+link.href+'"';
525          if (link.hreflang) { stag = stag+' hreflang="'+link.hreflang+'"'; }
526          stag = stag+'>';
527          var etag = '</a>';
528         
529          this.encloseSelection(stag,etag);
530     }
531};
532jsToolBar.prototype.elements.link.fn.wiki = function() {
533     var link = this.elements.link.prompt.call(this);
534     if (link) {
535          var stag = '[';
536          var etag = '|'+link.href;
537          if (link.hreflang) { etag = etag+'|'+link.hreflang; }
538          etag = etag+']';
539         
540          this.encloseSelection(stag,etag);
541     }
542};
543
544// img
545jsToolBar.prototype.elements.img = {
546          type: 'button',
547          title: 'External image',
548          src_prompt: 'Please give image URL:',
549          fn: {},
550          prompt: function(src) {
551               src = src || '';
552               return this.stripBaseURL(window.prompt(this.elements.img.src_prompt,src));
553          }
554};
555jsToolBar.prototype.elements.img.fn.xhtml = function() {
556     var src = this.elements.img.prompt.call(this);
557     if (src) {
558          this.encloseSelection('','',function(str) {
559               if (str) {
560                    return '<img src="'+src+'" alt="'+str+'" />';
561               } else {
562                    return '<img src="'+src+'" alt="" />';
563               }
564          });
565     }
566};
567jsToolBar.prototype.elements.img.fn.wiki = function() {
568     var src = this.elements.img.prompt.call(this);
569     if (src) {
570          this.encloseSelection('','',function(str) {
571               if (str) {
572                    return '(('+src+'|'+str+'))';
573               } else {
574                    return '(('+src+'))';
575               }
576          });
577     }
578};
Note: See TracBrowser for help on using the repository browser.

Sites map