annotate .svn/pristine/f0/f06528e84b5415a40a72b9b0d4c4587fbdcb8aec.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

Merge from branch "live"
author Chris Cannam
date Tue, 09 Sep 2014 09:34:53 +0100
parents cbb26bc654de
children
rev   line source
Chris@909 1 /* ***** BEGIN LICENSE BLOCK *****
Chris@909 2 * This file is part of DotClear.
Chris@909 3 * Copyright (c) 2005 Nicolas Martin & Olivier Meunier and contributors. All
Chris@909 4 * rights reserved.
Chris@909 5 *
Chris@909 6 * DotClear is free software; you can redistribute it and/or modify
Chris@909 7 * it under the terms of the GNU General Public License as published by
Chris@909 8 * the Free Software Foundation; either version 2 of the License, or
Chris@909 9 * (at your option) any later version.
Chris@909 10 *
Chris@909 11 * DotClear is distributed in the hope that it will be useful,
Chris@909 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@909 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@909 14 * GNU General Public License for more details.
Chris@909 15 *
Chris@909 16 * You should have received a copy of the GNU General Public License
Chris@909 17 * along with DotClear; if not, write to the Free Software
Chris@909 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Chris@909 19 *
Chris@909 20 * ***** END LICENSE BLOCK *****
Chris@909 21 */
Chris@909 22
Chris@909 23 /* Modified by JP LANG for textile formatting */
Chris@909 24
Chris@909 25 function jsToolBar(textarea) {
Chris@909 26 if (!document.createElement) { return; }
Chris@909 27
Chris@909 28 if (!textarea) { return; }
Chris@909 29
Chris@909 30 if ((typeof(document["selection"]) == "undefined")
Chris@909 31 && (typeof(textarea["setSelectionRange"]) == "undefined")) {
Chris@909 32 return;
Chris@909 33 }
Chris@909 34
Chris@909 35 this.textarea = textarea;
Chris@909 36
Chris@909 37 this.editor = document.createElement('div');
Chris@909 38 this.editor.className = 'jstEditor';
Chris@909 39
Chris@909 40 this.textarea.parentNode.insertBefore(this.editor,this.textarea);
Chris@909 41 this.editor.appendChild(this.textarea);
Chris@909 42
Chris@909 43 this.toolbar = document.createElement("div");
Chris@909 44 this.toolbar.className = 'jstElements';
Chris@909 45 this.editor.parentNode.insertBefore(this.toolbar,this.editor);
Chris@909 46
Chris@909 47 // Dragable resizing (only for gecko)
Chris@909 48 if (this.editor.addEventListener)
Chris@909 49 {
Chris@909 50 this.handle = document.createElement('div');
Chris@909 51 this.handle.className = 'jstHandle';
Chris@909 52 var dragStart = this.resizeDragStart;
Chris@909 53 var This = this;
Chris@909 54 this.handle.addEventListener('mousedown',function(event) { dragStart.call(This,event); },false);
Chris@909 55 // fix memory leak in Firefox (bug #241518)
Chris@909 56 window.addEventListener('unload',function() {
Chris@909 57 var del = This.handle.parentNode.removeChild(This.handle);
Chris@909 58 delete(This.handle);
Chris@909 59 },false);
Chris@909 60
Chris@909 61 this.editor.parentNode.insertBefore(this.handle,this.editor.nextSibling);
Chris@909 62 }
Chris@909 63
Chris@909 64 this.context = null;
Chris@909 65 this.toolNodes = {}; // lorsque la toolbar est dessinée , cet objet est garni
Chris@909 66 // de raccourcis vers les éléments DOM correspondants aux outils.
Chris@909 67 }
Chris@909 68
Chris@909 69 function jsButton(title, fn, scope, className) {
Chris@909 70 if(typeof jsToolBar.strings == 'undefined') {
Chris@909 71 this.title = title || null;
Chris@909 72 } else {
Chris@909 73 this.title = jsToolBar.strings[title] || title || null;
Chris@909 74 }
Chris@909 75 this.fn = fn || function(){};
Chris@909 76 this.scope = scope || null;
Chris@909 77 this.className = className || null;
Chris@909 78 }
Chris@909 79 jsButton.prototype.draw = function() {
Chris@909 80 if (!this.scope) return null;
Chris@909 81
Chris@909 82 var button = document.createElement('button');
Chris@909 83 button.setAttribute('type','button');
Chris@909 84 button.tabIndex = 200;
Chris@909 85 if (this.className) button.className = this.className;
Chris@909 86 button.title = this.title;
Chris@909 87 var span = document.createElement('span');
Chris@909 88 span.appendChild(document.createTextNode(this.title));
Chris@909 89 button.appendChild(span);
Chris@909 90
Chris@909 91 if (this.icon != undefined) {
Chris@909 92 button.style.backgroundImage = 'url('+this.icon+')';
Chris@909 93 }
Chris@909 94 if (typeof(this.fn) == 'function') {
Chris@909 95 var This = this;
Chris@909 96 button.onclick = function() { try { This.fn.apply(This.scope, arguments) } catch (e) {} return false; };
Chris@909 97 }
Chris@909 98 return button;
Chris@909 99 }
Chris@909 100
Chris@909 101 function jsSpace(id) {
Chris@909 102 this.id = id || null;
Chris@909 103 this.width = null;
Chris@909 104 }
Chris@909 105 jsSpace.prototype.draw = function() {
Chris@909 106 var span = document.createElement('span');
Chris@909 107 if (this.id) span.id = this.id;
Chris@909 108 span.appendChild(document.createTextNode(String.fromCharCode(160)));
Chris@909 109 span.className = 'jstSpacer';
Chris@909 110 if (this.width) span.style.marginRight = this.width+'px';
Chris@909 111
Chris@909 112 return span;
Chris@909 113 }
Chris@909 114
Chris@909 115 function jsCombo(title, options, scope, fn, className) {
Chris@909 116 this.title = title || null;
Chris@909 117 this.options = options || null;
Chris@909 118 this.scope = scope || null;
Chris@909 119 this.fn = fn || function(){};
Chris@909 120 this.className = className || null;
Chris@909 121 }
Chris@909 122 jsCombo.prototype.draw = function() {
Chris@909 123 if (!this.scope || !this.options) return null;
Chris@909 124
Chris@909 125 var select = document.createElement('select');
Chris@909 126 if (this.className) select.className = className;
Chris@909 127 select.title = this.title;
Chris@909 128
Chris@909 129 for (var o in this.options) {
Chris@909 130 //var opt = this.options[o];
Chris@909 131 var option = document.createElement('option');
Chris@909 132 option.value = o;
Chris@909 133 option.appendChild(document.createTextNode(this.options[o]));
Chris@909 134 select.appendChild(option);
Chris@909 135 }
Chris@909 136
Chris@909 137 var This = this;
Chris@909 138 select.onchange = function() {
Chris@909 139 try {
Chris@909 140 This.fn.call(This.scope, this.value);
Chris@909 141 } catch (e) { alert(e); }
Chris@909 142
Chris@909 143 return false;
Chris@909 144 }
Chris@909 145
Chris@909 146 return select;
Chris@909 147 }
Chris@909 148
Chris@909 149
Chris@909 150 jsToolBar.prototype = {
Chris@909 151 base_url: '',
Chris@909 152 mode: 'wiki',
Chris@909 153 elements: {},
Chris@909 154 help_link: '',
Chris@909 155
Chris@909 156 getMode: function() {
Chris@909 157 return this.mode;
Chris@909 158 },
Chris@909 159
Chris@909 160 setMode: function(mode) {
Chris@909 161 this.mode = mode || 'wiki';
Chris@909 162 },
Chris@909 163
Chris@909 164 switchMode: function(mode) {
Chris@909 165 mode = mode || 'wiki';
Chris@909 166 this.draw(mode);
Chris@909 167 },
Chris@909 168
Chris@909 169 setHelpLink: function(link) {
Chris@909 170 this.help_link = link;
Chris@909 171 },
Chris@909 172
Chris@909 173 button: function(toolName) {
Chris@909 174 var tool = this.elements[toolName];
Chris@909 175 if (typeof tool.fn[this.mode] != 'function') return null;
Chris@909 176 var b = new jsButton(tool.title, tool.fn[this.mode], this, 'jstb_'+toolName);
Chris@909 177 if (tool.icon != undefined) b.icon = tool.icon;
Chris@909 178 return b;
Chris@909 179 },
Chris@909 180 space: function(toolName) {
Chris@909 181 var tool = new jsSpace(toolName)
Chris@909 182 if (this.elements[toolName].width !== undefined)
Chris@909 183 tool.width = this.elements[toolName].width;
Chris@909 184 return tool;
Chris@909 185 },
Chris@909 186 combo: function(toolName) {
Chris@909 187 var tool = this.elements[toolName];
Chris@909 188 var length = tool[this.mode].list.length;
Chris@909 189
Chris@909 190 if (typeof tool[this.mode].fn != 'function' || length == 0) {
Chris@909 191 return null;
Chris@909 192 } else {
Chris@909 193 var options = {};
Chris@909 194 for (var i=0; i < length; i++) {
Chris@909 195 var opt = tool[this.mode].list[i];
Chris@909 196 options[opt] = tool.options[opt];
Chris@909 197 }
Chris@909 198 return new jsCombo(tool.title, options, this, tool[this.mode].fn);
Chris@909 199 }
Chris@909 200 },
Chris@909 201 draw: function(mode) {
Chris@909 202 this.setMode(mode);
Chris@909 203
Chris@909 204 // Empty toolbar
Chris@909 205 while (this.toolbar.hasChildNodes()) {
Chris@909 206 this.toolbar.removeChild(this.toolbar.firstChild)
Chris@909 207 }
Chris@909 208 this.toolNodes = {}; // vide les raccourcis DOM/**/
Chris@909 209
Chris@909 210 var h = document.createElement('div');
Chris@909 211 h.className = 'help'
Chris@909 212 h.innerHTML = this.help_link;
Chris@909 213 '<a href="/help/wiki_syntax.html" onclick="window.open(\'/help/wiki_syntax.html\', \'\', \'resizable=yes, location=no, width=300, height=640, menubar=no, status=no, scrollbars=yes\'); return false;">Aide</a>';
Chris@909 214 this.toolbar.appendChild(h);
Chris@909 215
Chris@909 216 // Draw toolbar elements
Chris@909 217 var b, tool, newTool;
Chris@909 218
Chris@909 219 for (var i in this.elements) {
Chris@909 220 b = this.elements[i];
Chris@909 221
Chris@909 222 var disabled =
Chris@909 223 b.type == undefined || b.type == ''
Chris@909 224 || (b.disabled != undefined && b.disabled)
Chris@909 225 || (b.context != undefined && b.context != null && b.context != this.context);
Chris@909 226
Chris@909 227 if (!disabled && typeof this[b.type] == 'function') {
Chris@909 228 tool = this[b.type](i);
Chris@909 229 if (tool) newTool = tool.draw();
Chris@909 230 if (newTool) {
Chris@909 231 this.toolNodes[i] = newTool; //mémorise l'accès DOM pour usage éventuel ultérieur
Chris@909 232 this.toolbar.appendChild(newTool);
Chris@909 233 }
Chris@909 234 }
Chris@909 235 }
Chris@909 236 },
Chris@909 237
Chris@909 238 singleTag: function(stag,etag) {
Chris@909 239 stag = stag || null;
Chris@909 240 etag = etag || stag;
Chris@909 241
Chris@909 242 if (!stag || !etag) { return; }
Chris@909 243
Chris@909 244 this.encloseSelection(stag,etag);
Chris@909 245 },
Chris@909 246
Chris@909 247 encloseLineSelection: function(prefix, suffix, fn) {
Chris@909 248 this.textarea.focus();
Chris@909 249
Chris@909 250 prefix = prefix || '';
Chris@909 251 suffix = suffix || '';
Chris@909 252
Chris@909 253 var start, end, sel, scrollPos, subst, res;
Chris@909 254
Chris@909 255 if (typeof(document["selection"]) != "undefined") {
Chris@909 256 sel = document.selection.createRange().text;
Chris@909 257 } else if (typeof(this.textarea["setSelectionRange"]) != "undefined") {
Chris@909 258 start = this.textarea.selectionStart;
Chris@909 259 end = this.textarea.selectionEnd;
Chris@909 260 scrollPos = this.textarea.scrollTop;
Chris@909 261 // go to the start of the line
Chris@909 262 start = this.textarea.value.substring(0, start).replace(/[^\r\n]*$/g,'').length;
Chris@909 263 // go to the end of the line
Chris@909 264 end = this.textarea.value.length - this.textarea.value.substring(end, this.textarea.value.length).replace(/^[^\r\n]*/, '').length;
Chris@909 265 sel = this.textarea.value.substring(start, end);
Chris@909 266 }
Chris@909 267
Chris@909 268 if (sel.match(/ $/)) { // exclude ending space char, if any
Chris@909 269 sel = sel.substring(0, sel.length - 1);
Chris@909 270 suffix = suffix + " ";
Chris@909 271 }
Chris@909 272
Chris@909 273 if (typeof(fn) == 'function') {
Chris@909 274 res = (sel) ? fn.call(this,sel) : fn('');
Chris@909 275 } else {
Chris@909 276 res = (sel) ? sel : '';
Chris@909 277 }
Chris@909 278
Chris@909 279 subst = prefix + res + suffix;
Chris@909 280
Chris@909 281 if (typeof(document["selection"]) != "undefined") {
Chris@909 282 document.selection.createRange().text = subst;
Chris@909 283 var range = this.textarea.createTextRange();
Chris@909 284 range.collapse(false);
Chris@909 285 range.move('character', -suffix.length);
Chris@909 286 range.select();
Chris@909 287 } else if (typeof(this.textarea["setSelectionRange"]) != "undefined") {
Chris@909 288 this.textarea.value = this.textarea.value.substring(0, start) + subst +
Chris@909 289 this.textarea.value.substring(end);
Chris@909 290 if (sel) {
Chris@909 291 this.textarea.setSelectionRange(start + subst.length, start + subst.length);
Chris@909 292 } else {
Chris@909 293 this.textarea.setSelectionRange(start + prefix.length, start + prefix.length);
Chris@909 294 }
Chris@909 295 this.textarea.scrollTop = scrollPos;
Chris@909 296 }
Chris@909 297 },
Chris@909 298
Chris@909 299 encloseSelection: function(prefix, suffix, fn) {
Chris@909 300 this.textarea.focus();
Chris@909 301
Chris@909 302 prefix = prefix || '';
Chris@909 303 suffix = suffix || '';
Chris@909 304
Chris@909 305 var start, end, sel, scrollPos, subst, res;
Chris@909 306
Chris@909 307 if (typeof(document["selection"]) != "undefined") {
Chris@909 308 sel = document.selection.createRange().text;
Chris@909 309 } else if (typeof(this.textarea["setSelectionRange"]) != "undefined") {
Chris@909 310 start = this.textarea.selectionStart;
Chris@909 311 end = this.textarea.selectionEnd;
Chris@909 312 scrollPos = this.textarea.scrollTop;
Chris@909 313 sel = this.textarea.value.substring(start, end);
Chris@909 314 }
Chris@909 315
Chris@909 316 if (sel.match(/ $/)) { // exclude ending space char, if any
Chris@909 317 sel = sel.substring(0, sel.length - 1);
Chris@909 318 suffix = suffix + " ";
Chris@909 319 }
Chris@909 320
Chris@909 321 if (typeof(fn) == 'function') {
Chris@909 322 res = (sel) ? fn.call(this,sel) : fn('');
Chris@909 323 } else {
Chris@909 324 res = (sel) ? sel : '';
Chris@909 325 }
Chris@909 326
Chris@909 327 subst = prefix + res + suffix;
Chris@909 328
Chris@909 329 if (typeof(document["selection"]) != "undefined") {
Chris@909 330 document.selection.createRange().text = subst;
Chris@909 331 var range = this.textarea.createTextRange();
Chris@909 332 range.collapse(false);
Chris@909 333 range.move('character', -suffix.length);
Chris@909 334 range.select();
Chris@909 335 // this.textarea.caretPos -= suffix.length;
Chris@909 336 } else if (typeof(this.textarea["setSelectionRange"]) != "undefined") {
Chris@909 337 this.textarea.value = this.textarea.value.substring(0, start) + subst +
Chris@909 338 this.textarea.value.substring(end);
Chris@909 339 if (sel) {
Chris@909 340 this.textarea.setSelectionRange(start + subst.length, start + subst.length);
Chris@909 341 } else {
Chris@909 342 this.textarea.setSelectionRange(start + prefix.length, start + prefix.length);
Chris@909 343 }
Chris@909 344 this.textarea.scrollTop = scrollPos;
Chris@909 345 }
Chris@909 346 },
Chris@909 347
Chris@909 348 stripBaseURL: function(url) {
Chris@909 349 if (this.base_url != '') {
Chris@909 350 var pos = url.indexOf(this.base_url);
Chris@909 351 if (pos == 0) {
Chris@909 352 url = url.substr(this.base_url.length);
Chris@909 353 }
Chris@909 354 }
Chris@909 355
Chris@909 356 return url;
Chris@909 357 }
Chris@909 358 };
Chris@909 359
Chris@909 360 /** Resizer
Chris@909 361 -------------------------------------------------------- */
Chris@909 362 jsToolBar.prototype.resizeSetStartH = function() {
Chris@909 363 this.dragStartH = this.textarea.offsetHeight + 0;
Chris@909 364 };
Chris@909 365 jsToolBar.prototype.resizeDragStart = function(event) {
Chris@909 366 var This = this;
Chris@909 367 this.dragStartY = event.clientY;
Chris@909 368 this.resizeSetStartH();
Chris@909 369 document.addEventListener('mousemove', this.dragMoveHdlr=function(event){This.resizeDragMove(event);}, false);
Chris@909 370 document.addEventListener('mouseup', this.dragStopHdlr=function(event){This.resizeDragStop(event);}, false);
Chris@909 371 };
Chris@909 372
Chris@909 373 jsToolBar.prototype.resizeDragMove = function(event) {
Chris@909 374 this.textarea.style.height = (this.dragStartH+event.clientY-this.dragStartY)+'px';
Chris@909 375 };
Chris@909 376
Chris@909 377 jsToolBar.prototype.resizeDragStop = function(event) {
Chris@909 378 document.removeEventListener('mousemove', this.dragMoveHdlr, false);
Chris@909 379 document.removeEventListener('mouseup', this.dragStopHdlr, false);
Chris@909 380 };