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