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