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