You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jstoolbar.js 11KB

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