@@ -0,0 +1,442 | |||||
|
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 | ||||
|
23 | function 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 (this.editor.addEventListener) | |||
|
47 | { | |||
|
48 | this.handle = document.createElement('div'); | |||
|
49 | this.handle.className = 'jstHandle'; | |||
|
50 | var dragStart = this.resizeDragStart; | |||
|
51 | var This = this; | |||
|
52 | this.handle.addEventListener('mousedown',function(event) { dragStart.call(This,event); },false); | |||
|
53 | // fix memory leak in Firefox (bug #241518) | |||
|
54 | window.addEventListener('unload',function() { | |||
|
55 | var del = This.handle.parentNode.removeChild(This.handle); | |||
|
56 | delete(This.handle); | |||
|
57 | },false); | |||
|
58 | ||||
|
59 | this.editor.parentNode.insertBefore(this.handle,this.editor.nextSibling); | |||
|
60 | } | |||
|
61 | ||||
|
62 | this.context = null; | |||
|
63 | this.toolNodes = {}; // lorsque la toolbar est dessinΓ©e , cet objet est garni | |||
|
64 | // de raccourcis vers les Γ©lΓ©ments DOM correspondants aux outils. | |||
|
65 | } | |||
|
66 | ||||
|
67 | function jsButton(title, fn, scope, className) { | |||
|
68 | this.title = title || null; | |||
|
69 | this.fn = fn || function(){}; | |||
|
70 | this.scope = scope || null; | |||
|
71 | this.className = className || null; | |||
|
72 | } | |||
|
73 | jsButton.prototype.draw = function() { | |||
|
74 | if (!this.scope) return null; | |||
|
75 | ||||
|
76 | var button = document.createElement('button'); | |||
|
77 | button.setAttribute('type','button'); | |||
|
78 | if (this.className) button.className = this.className; | |||
|
79 | button.title = this.title; | |||
|
80 | var span = document.createElement('span'); | |||
|
81 | span.appendChild(document.createTextNode(this.title)); | |||
|
82 | button.appendChild(span); | |||
|
83 | ||||
|
84 | if (this.icon != undefined) { | |||
|
85 | button.style.backgroundImage = 'url('+this.icon+')'; | |||
|
86 | } | |||
|
87 | if (typeof(this.fn) == 'function') { | |||
|
88 | var This = this; | |||
|
89 | button.onclick = function() { try { This.fn.apply(This.scope, arguments) } catch (e) {} return false; }; | |||
|
90 | } | |||
|
91 | return button; | |||
|
92 | } | |||
|
93 | ||||
|
94 | function jsSpace(id) { | |||
|
95 | this.id = id || null; | |||
|
96 | this.width = null; | |||
|
97 | } | |||
|
98 | jsSpace.prototype.draw = function() { | |||
|
99 | var span = document.createElement('span'); | |||
|
100 | if (this.id) span.id = this.id; | |||
|
101 | span.appendChild(document.createTextNode(String.fromCharCode(160))); | |||
|
102 | span.className = 'jstSpacer'; | |||
|
103 | if (this.width) span.style.marginRight = this.width+'px'; | |||
|
104 | ||||
|
105 | return span; | |||
|
106 | } | |||
|
107 | ||||
|
108 | function jsCombo(title, options, scope, fn, className) { | |||
|
109 | this.title = title || null; | |||
|
110 | this.options = options || null; | |||
|
111 | this.scope = scope || null; | |||
|
112 | this.fn = fn || function(){}; | |||
|
113 | this.className = className || null; | |||
|
114 | } | |||
|
115 | jsCombo.prototype.draw = function() { | |||
|
116 | if (!this.scope || !this.options) return null; | |||
|
117 | ||||
|
118 | var select = document.createElement('select'); | |||
|
119 | if (this.className) select.className = className; | |||
|
120 | select.title = this.title; | |||
|
121 | ||||
|
122 | for (var o in this.options) { | |||
|
123 | //var opt = this.options[o]; | |||
|
124 | var option = document.createElement('option'); | |||
|
125 | option.value = o; | |||
|
126 | option.appendChild(document.createTextNode(this.options[o])); | |||
|
127 | select.appendChild(option); | |||
|
128 | } | |||
|
129 | ||||
|
130 | var This = this; | |||
|
131 | select.onchange = function() { | |||
|
132 | try { | |||
|
133 | This.fn.call(This.scope, this.value); | |||
|
134 | } catch (e) { alert(e); } | |||
|
135 | ||||
|
136 | return false; | |||
|
137 | } | |||
|
138 | ||||
|
139 | return select; | |||
|
140 | } | |||
|
141 | ||||
|
142 | ||||
|
143 | jsToolBar.prototype = { | |||
|
144 | base_url: '', | |||
|
145 | mode: 'wiki', | |||
|
146 | elements: {}, | |||
|
147 | ||||
|
148 | getMode: function() { | |||
|
149 | return this.mode; | |||
|
150 | }, | |||
|
151 | ||||
|
152 | setMode: function(mode) { | |||
|
153 | this.mode = mode || 'wiki'; | |||
|
154 | }, | |||
|
155 | ||||
|
156 | switchMode: function(mode) { | |||
|
157 | mode = mode || 'wiki'; | |||
|
158 | this.draw(mode); | |||
|
159 | }, | |||
|
160 | ||||
|
161 | button: function(toolName) { | |||
|
162 | var tool = this.elements[toolName]; | |||
|
163 | if (typeof tool.fn[this.mode] != 'function') return null; | |||
|
164 | var b = new jsButton(tool.title, tool.fn[this.mode], this, 'jstb_'+toolName); | |||
|
165 | if (tool.icon != undefined) b.icon = tool.icon; | |||
|
166 | return b; | |||
|
167 | }, | |||
|
168 | space: function(toolName) { | |||
|
169 | var tool = new jsSpace(toolName) | |||
|
170 | if (this.elements[toolName].width !== undefined) | |||
|
171 | tool.width = this.elements[toolName].width; | |||
|
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 | -------------------------------------------------------- */ | |||
|
288 | jsToolBar.prototype.resizeSetStartH = function() { | |||
|
289 | this.dragStartH = this.textarea.offsetHeight + 0; | |||
|
290 | }; | |||
|
291 | jsToolBar.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 | ||||
|
299 | jsToolBar.prototype.resizeDragMove = function(event) { | |||
|
300 | this.textarea.style.height = (this.dragStartH+event.clientY-this.dragStartY)+'px'; | |||
|
301 | }; | |||
|
302 | ||||
|
303 | jsToolBar.prototype.resizeDragStop = function(event) { | |||
|
304 | document.removeEventListener('mousemove', this.dragMoveHdlr, false); | |||
|
305 | document.removeEventListener('mouseup', this.dragStopHdlr, false); | |||
|
306 | }; | |||
|
307 | ||||
|
308 | // Elements definition ------------------------------------ | |||
|
309 | ||||
|
310 | // strong | |||
|
311 | jsToolBar.prototype.elements.strong = { | |||
|
312 | type: 'button', | |||
|
313 | title: 'Strong emphasis', | |||
|
314 | fn: { | |||
|
315 | wiki: function() { this.singleTag('*') } | |||
|
316 | } | |||
|
317 | } | |||
|
318 | ||||
|
319 | // em | |||
|
320 | jsToolBar.prototype.elements.em = { | |||
|
321 | type: 'button', | |||
|
322 | title: 'Emphasis', | |||
|
323 | fn: { | |||
|
324 | wiki: function() { this.singleTag("_") } | |||
|
325 | } | |||
|
326 | } | |||
|
327 | ||||
|
328 | // ins | |||
|
329 | jsToolBar.prototype.elements.ins = { | |||
|
330 | type: 'button', | |||
|
331 | title: 'Inserted', | |||
|
332 | fn: { | |||
|
333 | wiki: function() { this.singleTag('+') } | |||
|
334 | } | |||
|
335 | } | |||
|
336 | ||||
|
337 | // del | |||
|
338 | jsToolBar.prototype.elements.del = { | |||
|
339 | type: 'button', | |||
|
340 | title: 'Deleted', | |||
|
341 | fn: { | |||
|
342 | wiki: function() { this.singleTag('-') } | |||
|
343 | } | |||
|
344 | } | |||
|
345 | ||||
|
346 | // quote | |||
|
347 | jsToolBar.prototype.elements.quote = { | |||
|
348 | type: 'button', | |||
|
349 | title: 'Inline quote', | |||
|
350 | fn: { | |||
|
351 | wiki: function() { this.singleTag('{{','}}') } | |||
|
352 | } | |||
|
353 | } | |||
|
354 | ||||
|
355 | // code | |||
|
356 | jsToolBar.prototype.elements.code = { | |||
|
357 | type: 'button', | |||
|
358 | title: 'Code', | |||
|
359 | fn: { | |||
|
360 | wiki: function() { this.singleTag('@') } | |||
|
361 | } | |||
|
362 | } | |||
|
363 | ||||
|
364 | // spacer | |||
|
365 | jsToolBar.prototype.elements.space1 = {type: 'space'} | |||
|
366 | ||||
|
367 | // br | |||
|
368 | jsToolBar.prototype.elements.br = { | |||
|
369 | type: 'button', | |||
|
370 | title: 'Line break', | |||
|
371 | fn: { | |||
|
372 | wiki: function() { this.encloseSelection("%%%\n",'') } | |||
|
373 | } | |||
|
374 | } | |||
|
375 | ||||
|
376 | // spacer | |||
|
377 | jsToolBar.prototype.elements.space2 = {type: 'space'} | |||
|
378 | ||||
|
379 | // ul | |||
|
380 | jsToolBar.prototype.elements.ul = { | |||
|
381 | type: 'button', | |||
|
382 | title: 'Unordered list', | |||
|
383 | fn: { | |||
|
384 | wiki: function() { | |||
|
385 | this.encloseSelection('','',function(str) { | |||
|
386 | str = str.replace(/\r/g,''); | |||
|
387 | return '* '+str.replace(/\n/g,"\n* "); | |||
|
388 | }); | |||
|
389 | } | |||
|
390 | } | |||
|
391 | } | |||
|
392 | ||||
|
393 | // ol | |||
|
394 | jsToolBar.prototype.elements.ol = { | |||
|
395 | type: 'button', | |||
|
396 | title: 'Ordered list', | |||
|
397 | fn: { | |||
|
398 | wiki: function() { | |||
|
399 | this.encloseSelection('','',function(str) { | |||
|
400 | str = str.replace(/\r/g,''); | |||
|
401 | return '# '+str.replace(/\n/g,"\n# "); | |||
|
402 | }); | |||
|
403 | } | |||
|
404 | } | |||
|
405 | } | |||
|
406 | ||||
|
407 | // spacer | |||
|
408 | jsToolBar.prototype.elements.space3 = {type: 'space'} | |||
|
409 | ||||
|
410 | // link | |||
|
411 | jsToolBar.prototype.elements.link = { | |||
|
412 | type: 'button', | |||
|
413 | title: 'Link', | |||
|
414 | fn: {}, | |||
|
415 | href_prompt: 'Please give page URL:', | |||
|
416 | hreflang_prompt: 'Language of this page:', | |||
|
417 | default_hreflang: '', | |||
|
418 | prompt: function(href,hreflang) { | |||
|
419 | href = href || ''; | |||
|
420 | hreflang = hreflang || this.elements.link.default_hreflang; | |||
|
421 | ||||
|
422 | href = window.prompt(this.elements.link.href_prompt,href); | |||
|
423 | if (!href) { return false; } | |||
|
424 | ||||
|
425 | hreflang = window.prompt(this.elements.link.hreflang_prompt, | |||
|
426 | hreflang); | |||
|
427 | ||||
|
428 | return { href: this.stripBaseURL(href), hreflang: hreflang }; | |||
|
429 | } | |||
|
430 | } | |||
|
431 | ||||
|
432 | jsToolBar.prototype.elements.link.fn.wiki = function() { | |||
|
433 | var link = this.elements.link.prompt.call(this); | |||
|
434 | if (link) { | |||
|
435 | var stag = '['; | |||
|
436 | var etag = '|'+link.href; | |||
|
437 | if (link.hreflang) { etag = etag+'|'+link.hreflang; } | |||
|
438 | etag = etag+']'; | |||
|
439 | ||||
|
440 | this.encloseSelection(stag,etag); | |||
|
441 | } | |||
|
442 | }; |
General Comments 0
You need to be logged in to leave comments.
Login now