--- /dev/null
+/* ***** BEGIN LICENSE BLOCK *****\r
+ * This file is part of DotClear.\r
+ * Copyright (c) 2005 Nicolas Martin & Olivier Meunier and contributors. All\r
+ * rights reserved.\r
+ *\r
+ * DotClear is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ * \r
+ * DotClear is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * GNU General Public License for more details.\r
+ * \r
+ * You should have received a copy of the GNU General Public License\r
+ * along with DotClear; if not, write to the Free Software\r
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r
+ *\r
+ * ***** END LICENSE BLOCK *****\r
+*/\r
+\r
+function jsToolBar(textarea) {\r
+ if (!document.createElement) { return; }\r
+ \r
+ if (!textarea) { return; }\r
+ \r
+ if ((typeof(document["selection"]) == "undefined")\r
+ && (typeof(textarea["setSelectionRange"]) == "undefined")) {\r
+ return;\r
+ }\r
+ \r
+ this.textarea = textarea;\r
+ \r
+ this.editor = document.createElement('div');\r
+ this.editor.className = 'jstEditor';\r
+ \r
+ this.textarea.parentNode.insertBefore(this.editor,this.textarea);\r
+ this.editor.appendChild(this.textarea);\r
+ \r
+ this.toolbar = document.createElement("div");\r
+ this.toolbar.className = 'jstElements';\r
+ this.editor.parentNode.insertBefore(this.toolbar,this.editor);\r
+ \r
+ // Dragable resizing (only for gecko)\r
+ if (this.editor.addEventListener)\r
+ {\r
+ this.handle = document.createElement('div');\r
+ this.handle.className = 'jstHandle';\r
+ var dragStart = this.resizeDragStart;\r
+ var This = this;\r
+ this.handle.addEventListener('mousedown',function(event) { dragStart.call(This,event); },false);\r
+ // fix memory leak in Firefox (bug #241518)\r
+ window.addEventListener('unload',function() { \r
+ var del = This.handle.parentNode.removeChild(This.handle);\r
+ delete(This.handle);\r
+ },false);\r
+ \r
+ this.editor.parentNode.insertBefore(this.handle,this.editor.nextSibling);\r
+ }\r
+ \r
+ this.context = null;\r
+ this.toolNodes = {}; // lorsque la toolbar est dessinée , cet objet est garni \r
+ // de raccourcis vers les éléments DOM correspondants aux outils.\r
+}\r
+\r
+function jsButton(title, fn, scope, className) {\r
+ this.title = title || null;\r
+ this.fn = fn || function(){};\r
+ this.scope = scope || null;\r
+ this.className = className || null;\r
+}\r
+jsButton.prototype.draw = function() {\r
+ if (!this.scope) return null;\r
+ \r
+ var button = document.createElement('button');\r
+ button.setAttribute('type','button');\r
+ if (this.className) button.className = this.className;\r
+ button.title = this.title;\r
+ var span = document.createElement('span');\r
+ span.appendChild(document.createTextNode(this.title));\r
+ button.appendChild(span);\r
+ \r
+ if (this.icon != undefined) {\r
+ button.style.backgroundImage = 'url('+this.icon+')';\r
+ }\r
+ if (typeof(this.fn) == 'function') {\r
+ var This = this;\r
+ button.onclick = function() { try { This.fn.apply(This.scope, arguments) } catch (e) {} return false; };\r
+ }\r
+ return button;\r
+}\r
+\r
+function jsSpace(id) {\r
+ this.id = id || null;\r
+ this.width = null;\r
+}\r
+jsSpace.prototype.draw = function() {\r
+ var span = document.createElement('span');\r
+ if (this.id) span.id = this.id;\r
+ span.appendChild(document.createTextNode(String.fromCharCode(160)));\r
+ span.className = 'jstSpacer';\r
+ if (this.width) span.style.marginRight = this.width+'px';\r
+ \r
+ return span;\r
+} \r
+\r
+function jsCombo(title, options, scope, fn, className) {\r
+ this.title = title || null;\r
+ this.options = options || null;\r
+ this.scope = scope || null;\r
+ this.fn = fn || function(){};\r
+ this.className = className || null;\r
+}\r
+jsCombo.prototype.draw = function() {\r
+ if (!this.scope || !this.options) return null;\r
+\r
+ var select = document.createElement('select');\r
+ if (this.className) select.className = className;\r
+ select.title = this.title;\r
+ \r
+ for (var o in this.options) {\r
+ //var opt = this.options[o];\r
+ var option = document.createElement('option');\r
+ option.value = o;\r
+ option.appendChild(document.createTextNode(this.options[o]));\r
+ select.appendChild(option);\r
+ }\r
+\r
+ var This = this;\r
+ select.onchange = function() {\r
+ try { \r
+ This.fn.call(This.scope, this.value);\r
+ } catch (e) { alert(e); }\r
+\r
+ return false;\r
+ }\r
+\r
+ return select;\r
+}\r
+\r
+\r
+jsToolBar.prototype = {\r
+ base_url: '',\r
+ mode: 'wiki',\r
+ elements: {},\r
+ \r
+ getMode: function() {\r
+ return this.mode;\r
+ },\r
+ \r
+ setMode: function(mode) {\r
+ this.mode = mode || 'wiki';\r
+ },\r
+ \r
+ switchMode: function(mode) {\r
+ mode = mode || 'wiki';\r
+ this.draw(mode);\r
+ },\r
+ \r
+ button: function(toolName) {\r
+ var tool = this.elements[toolName];\r
+ if (typeof tool.fn[this.mode] != 'function') return null;\r
+ var b = new jsButton(tool.title, tool.fn[this.mode], this, 'jstb_'+toolName);\r
+ if (tool.icon != undefined) b.icon = tool.icon;\r
+ return b;\r
+ },\r
+ space: function(toolName) {\r
+ var tool = new jsSpace(toolName)\r
+ if (this.elements[toolName].width !== undefined)\r
+ tool.width = this.elements[toolName].width;\r
+ return tool;\r
+ },\r
+ combo: function(toolName) {\r
+ var tool = this.elements[toolName];\r
+ var length = tool[this.mode].list.length;\r
+\r
+ if (typeof tool[this.mode].fn != 'function' || length == 0) {\r
+ return null;\r
+ } else {\r
+ var options = {};\r
+ for (var i=0; i < length; i++) {\r
+ var opt = tool[this.mode].list[i];\r
+ options[opt] = tool.options[opt];\r
+ }\r
+ return new jsCombo(tool.title, options, this, tool[this.mode].fn);\r
+ }\r
+ },\r
+ draw: function(mode) {\r
+ this.setMode(mode);\r
+ \r
+ // Empty toolbar\r
+ while (this.toolbar.hasChildNodes()) {\r
+ this.toolbar.removeChild(this.toolbar.firstChild)\r
+ }\r
+ this.toolNodes = {}; // vide les raccourcis DOM/**/\r
+ \r
+ // Draw toolbar elements\r
+ var b, tool, newTool;\r
+ \r
+ for (var i in this.elements) {\r
+ b = this.elements[i];\r
+\r
+ var disabled =\r
+ b.type == undefined || b.type == ''\r
+ || (b.disabled != undefined && b.disabled)\r
+ || (b.context != undefined && b.context != null && b.context != this.context);\r
+ \r
+ if (!disabled && typeof this[b.type] == 'function') {\r
+ tool = this[b.type](i);\r
+ if (tool) newTool = tool.draw();\r
+ if (newTool) {\r
+ this.toolNodes[i] = newTool; //mémorise l'accès DOM pour usage éventuel ultérieur\r
+ this.toolbar.appendChild(newTool);\r
+ }\r
+ }\r
+ }\r
+ },\r
+ \r
+ singleTag: function(stag,etag) {\r
+ stag = stag || null;\r
+ etag = etag || stag;\r
+ \r
+ if (!stag || !etag) { return; }\r
+ \r
+ this.encloseSelection(stag,etag);\r
+ },\r
+ \r
+ encloseSelection: function(prefix, suffix, fn) {\r
+ this.textarea.focus();\r
+ \r
+ prefix = prefix || '';\r
+ suffix = suffix || '';\r
+ \r
+ var start, end, sel, scrollPos, subst, res;\r
+ \r
+ if (typeof(document["selection"]) != "undefined") {\r
+ sel = document.selection.createRange().text;\r
+ } else if (typeof(this.textarea["setSelectionRange"]) != "undefined") {\r
+ start = this.textarea.selectionStart;\r
+ end = this.textarea.selectionEnd;\r
+ scrollPos = this.textarea.scrollTop;\r
+ sel = this.textarea.value.substring(start, end);\r
+ }\r
+ \r
+ if (sel.match(/ $/)) { // exclude ending space char, if any\r
+ sel = sel.substring(0, sel.length - 1);\r
+ suffix = suffix + " ";\r
+ }\r
+ \r
+ if (typeof(fn) == 'function') {\r
+ res = (sel) ? fn.call(this,sel) : fn('');\r
+ } else {\r
+ res = (sel) ? sel : '';\r
+ }\r
+ \r
+ subst = prefix + res + suffix;\r
+ \r
+ if (typeof(document["selection"]) != "undefined") {\r
+ var range = document.selection.createRange().text = subst;\r
+ this.textarea.caretPos -= suffix.length;\r
+ } else if (typeof(this.textarea["setSelectionRange"]) != "undefined") {\r
+ this.textarea.value = this.textarea.value.substring(0, start) + subst +\r
+ this.textarea.value.substring(end);\r
+ if (sel) {\r
+ this.textarea.setSelectionRange(start + subst.length, start + subst.length);\r
+ } else {\r
+ this.textarea.setSelectionRange(start + prefix.length, start + prefix.length);\r
+ }\r
+ this.textarea.scrollTop = scrollPos;\r
+ }\r
+ },\r
+ \r
+ stripBaseURL: function(url) {\r
+ if (this.base_url != '') {\r
+ var pos = url.indexOf(this.base_url);\r
+ if (pos == 0) {\r
+ url = url.substr(this.base_url.length);\r
+ }\r
+ }\r
+ \r
+ return url;\r
+ }\r
+};\r
+\r
+/** Resizer\r
+-------------------------------------------------------- */\r
+jsToolBar.prototype.resizeSetStartH = function() {\r
+ this.dragStartH = this.textarea.offsetHeight + 0;\r
+};\r
+jsToolBar.prototype.resizeDragStart = function(event) {\r
+ var This = this;\r
+ this.dragStartY = event.clientY;\r
+ this.resizeSetStartH();\r
+ document.addEventListener('mousemove', this.dragMoveHdlr=function(event){This.resizeDragMove(event);}, false);\r
+ document.addEventListener('mouseup', this.dragStopHdlr=function(event){This.resizeDragStop(event);}, false);\r
+};\r
+\r
+jsToolBar.prototype.resizeDragMove = function(event) {\r
+ this.textarea.style.height = (this.dragStartH+event.clientY-this.dragStartY)+'px';\r
+};\r
+\r
+jsToolBar.prototype.resizeDragStop = function(event) {\r
+ document.removeEventListener('mousemove', this.dragMoveHdlr, false);\r
+ document.removeEventListener('mouseup', this.dragStopHdlr, false);\r
+};\r
+\r
+// Elements definition ------------------------------------\r
+\r
+// strong\r
+jsToolBar.prototype.elements.strong = {\r
+ type: 'button',\r
+ title: 'Strong emphasis',\r
+ fn: {\r
+ wiki: function() { this.singleTag('*') }\r
+ }\r
+}\r
+\r
+// em\r
+jsToolBar.prototype.elements.em = {\r
+ type: 'button',\r
+ title: 'Emphasis',\r
+ fn: {\r
+ wiki: function() { this.singleTag("_") }\r
+ }\r
+}\r
+\r
+// ins\r
+jsToolBar.prototype.elements.ins = {\r
+ type: 'button',\r
+ title: 'Inserted',\r
+ fn: {\r
+ wiki: function() { this.singleTag('+') }\r
+ }\r
+}\r
+\r
+// del\r
+jsToolBar.prototype.elements.del = {\r
+ type: 'button',\r
+ title: 'Deleted',\r
+ fn: {\r
+ wiki: function() { this.singleTag('-') }\r
+ }\r
+}\r
+\r
+// quote\r
+jsToolBar.prototype.elements.quote = {\r
+ type: 'button',\r
+ title: 'Inline quote',\r
+ fn: {\r
+ wiki: function() { this.singleTag('{{','}}') }\r
+ }\r
+}\r
+\r
+// code\r
+jsToolBar.prototype.elements.code = {\r
+ type: 'button',\r
+ title: 'Code',\r
+ fn: {\r
+ wiki: function() { this.singleTag('@') }\r
+ }\r
+}\r
+\r
+// spacer\r
+jsToolBar.prototype.elements.space1 = {type: 'space'}\r
+\r
+// br\r
+jsToolBar.prototype.elements.br = {\r
+ type: 'button',\r
+ title: 'Line break',\r
+ fn: {\r
+ wiki: function() { this.encloseSelection("%%%\n",'') }\r
+ }\r
+}\r
+\r
+// spacer\r
+jsToolBar.prototype.elements.space2 = {type: 'space'}\r
+\r
+// ul\r
+jsToolBar.prototype.elements.ul = {\r
+ type: 'button',\r
+ title: 'Unordered list',\r
+ fn: {\r
+ wiki: function() {\r
+ this.encloseSelection('','',function(str) {\r
+ str = str.replace(/\r/g,'');\r
+ return '* '+str.replace(/\n/g,"\n* ");\r
+ });\r
+ }\r
+ }\r
+}\r
+\r
+// ol\r
+jsToolBar.prototype.elements.ol = {\r
+ type: 'button',\r
+ title: 'Ordered list',\r
+ fn: {\r
+ wiki: function() {\r
+ this.encloseSelection('','',function(str) {\r
+ str = str.replace(/\r/g,'');\r
+ return '# '+str.replace(/\n/g,"\n# ");\r
+ });\r
+ }\r
+ }\r
+}\r
+\r
+// spacer\r
+jsToolBar.prototype.elements.space3 = {type: 'space'}\r
+\r
+// link\r
+jsToolBar.prototype.elements.link = {\r
+ type: 'button',\r
+ title: 'Link',\r
+ fn: {},\r
+ href_prompt: 'Please give page URL:',\r
+ hreflang_prompt: 'Language of this page:',\r
+ default_hreflang: '',\r
+ prompt: function(href,hreflang) {\r
+ href = href || '';\r
+ hreflang = hreflang || this.elements.link.default_hreflang;\r
+ \r
+ href = window.prompt(this.elements.link.href_prompt,href);\r
+ if (!href) { return false; }\r
+ \r
+ hreflang = window.prompt(this.elements.link.hreflang_prompt,\r
+ hreflang);\r
+ \r
+ return { href: this.stripBaseURL(href), hreflang: hreflang };\r
+ }\r
+}\r
+\r
+jsToolBar.prototype.elements.link.fn.wiki = function() {\r
+ var link = this.elements.link.prompt.call(this);\r
+ if (link) {\r
+ var stag = '[';\r
+ var etag = '|'+link.href;\r
+ if (link.hreflang) { etag = etag+'|'+link.hreflang; }\r
+ etag = etag+']';\r
+ \r
+ this.encloseSelection(stag,etag);\r
+ }\r
+};\r