toolbar.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * This file is part of DotClear.
  3. * Copyright (c) 2004 Olivier Meunier and contributors. All rights
  4. * 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. function dcToolBar(textarea,format,img_path)
  22. {
  23. this.addButton = function() {}
  24. this.addSpace = function() {}
  25. this.draw = function() {}
  26. this.btStrong = function() {}
  27. this.btEm = function() {}
  28. this.btIns = function() {}
  29. this.btDel = function() {}
  30. this.btQ = function() {}
  31. this.btCode = function() {}
  32. this.btBr = function() {}
  33. this.btBquote = function() {}
  34. this.btPre = function() {}
  35. this.btList = function() {}
  36. this.btLink = function() {}
  37. this.btImgLink = function() {}
  38. this.btImg = function() {}
  39. this.insImg = function() {}
  40. if (!document.createElement) {
  41. return;
  42. }
  43. if ((typeof(document["selection"]) == "undefined")
  44. && (typeof(textarea["setSelectionRange"]) == "undefined")) {
  45. return;
  46. }
  47. var toolbar = document.createElement("div");
  48. toolbar.id = "dctoolbar";
  49. function getFormat() {
  50. if (format.value == 'wiki') {
  51. return 'wiki';
  52. } else {
  53. return 'html';
  54. }
  55. }
  56. function addButton(src, title, fn) {
  57. var i = document.createElement('img');
  58. i.src = src;
  59. i.title = title;
  60. i.onclick = function() { try { fn() } catch (e) { } return false };
  61. i.tabIndex = 400;
  62. toolbar.appendChild(i);
  63. addSpace(2);
  64. }
  65. function addSpace(w)
  66. {
  67. s = document.createElement('span');
  68. s.style.padding='0 '+w+'px 0 0';
  69. s.appendChild(document.createTextNode(' '));
  70. toolbar.appendChild(s);
  71. }
  72. function encloseSelection(prefix, suffix, fn) {
  73. textarea.focus();
  74. var start, end, sel, scrollPos, subst;
  75. if (typeof(document["selection"]) != "undefined") {
  76. sel = document.selection.createRange().text;
  77. } else if (typeof(textarea["setSelectionRange"]) != "undefined") {
  78. start = textarea.selectionStart;
  79. end = textarea.selectionEnd;
  80. scrollPos = textarea.scrollTop;
  81. sel = textarea.value.substring(start, end);
  82. }
  83. if (sel.match(/ $/)) { // exclude ending space char, if any
  84. sel = sel.substring(0, sel.length - 1);
  85. suffix = suffix + " ";
  86. }
  87. if (typeof(fn) == 'function') {
  88. var res = (sel) ? fn(sel) : fn('');
  89. } else {
  90. var res = (sel) ? sel : '';
  91. }
  92. subst = prefix + res + suffix;
  93. if (typeof(document["selection"]) != "undefined") {
  94. var range = document.selection.createRange().text = subst;
  95. textarea.caretPos -= suffix.length;
  96. } else if (typeof(textarea["setSelectionRange"]) != "undefined") {
  97. textarea.value = textarea.value.substring(0, start) + subst +
  98. textarea.value.substring(end);
  99. if (sel) {
  100. textarea.setSelectionRange(start + subst.length, start + subst.length);
  101. } else {
  102. textarea.setSelectionRange(start + prefix.length, start + prefix.length);
  103. }
  104. textarea.scrollTop = scrollPos;
  105. }
  106. }
  107. function draw(msg) {
  108. p = document.createElement('em');
  109. p.style.display='block';
  110. p.style.margin='-0.5em 0 0.5em 0';
  111. p.appendChild(document.createTextNode(msg));
  112. textarea.parentNode.insertBefore(p, textarea);
  113. textarea.parentNode.insertBefore(toolbar, textarea);
  114. }
  115. // ---
  116. function singleTag(wtag,htag,wetag) {
  117. if (getFormat() == 'wiki') {
  118. var stag = wtag;
  119. var etag = (wetag) ? wetag : wtag;
  120. } else {
  121. var stag = '<'+htag+'>';
  122. var etag = '</'+htag+'>';
  123. }
  124. encloseSelection(stag,etag);
  125. }
  126. function btStrong(label) {
  127. addButton(img_path+'bt_strong.png',label,
  128. function() { singleTag('__','strong'); });
  129. }
  130. function btEm(label) {
  131. addButton(img_path+'bt_em.png',label,
  132. function() { singleTag("''",'em'); });
  133. }
  134. function btIns(label) {
  135. addButton(img_path+'bt_ins.png',label,
  136. function() { singleTag('++','ins'); });
  137. }
  138. function btDel(label) {
  139. addButton(img_path+'bt_del.png',label,
  140. function() { singleTag('--','del'); });
  141. }
  142. function btQ(label) {
  143. addButton(img_path+'bt_quote.png',label,
  144. function() { singleTag('{{','q','}}'); });
  145. }
  146. function btCode(label) {
  147. addButton(img_path+'bt_code.png',label,
  148. function() { singleTag('@@','code'); });
  149. }
  150. function btBr(label) {
  151. addButton(img_path+'bt_br.png',label,
  152. function() {
  153. var tag = getFormat() == 'wiki' ? "%%%\n" : "<br />\n";
  154. encloseSelection('',tag);
  155. });
  156. }
  157. function btBquote(label) {
  158. addButton(img_path+'bt_bquote.png',label,
  159. function() {
  160. encloseSelection("\n",'',
  161. function(str) {
  162. if (getFormat() == 'wiki') {
  163. str = str.replace(/\r/g,'');
  164. return '> '+str.replace(/\n/g,"\n> ");
  165. } else {
  166. return "<blockquote>"+str+"</blockquote>\n";
  167. }
  168. });
  169. });
  170. }
  171. function btPre(label) {
  172. addButton(img_path+'bt_pre.png',label,
  173. function() {
  174. encloseSelection("\n",'',
  175. function(str) {
  176. if (getFormat() == 'wiki') {
  177. str = str.replace(/\r/g,'');
  178. return ' '+str.replace(/\n/g,"\n ");
  179. } else {
  180. return "<pre>"+str+"</pre>\n";
  181. }
  182. });
  183. });
  184. }
  185. function btList(label,type) {
  186. var img = (type == 'ul') ? 'bt_ul.png' : 'bt_ol.png';
  187. var wtag = (type == 'ul') ? '*' : '#';
  188. addButton(img_path+img,label,
  189. function() {
  190. encloseSelection("",'',
  191. function(str) {
  192. if (getFormat() == 'wiki') {
  193. str = str.replace(/\r/g,'');
  194. return wtag+' '+str.replace(/\n/g,"\n"+wtag+' ');
  195. } else {
  196. str = str.replace(/\r/g,'');
  197. str = str.replace(/\n/g,"</li>\n <li>");
  198. return "<"+type+">\n <li>"+str+"</li>\n</"+type+">";
  199. }
  200. });
  201. });
  202. }
  203. function btLink(label,msg_url,msg_lang,default_lang) {
  204. addButton(img_path+'bt_link.png',label,
  205. function() {
  206. var href = window.prompt(msg_url,'');
  207. if (!href) { return; }
  208. var hreflang = window.prompt(msg_lang,default_lang);
  209. if (getFormat() == 'wiki') {
  210. stag = '[';
  211. var etag = '|'+href;
  212. if (hreflang) { etag = etag+'|'+hreflang; }
  213. etag = etag+']';
  214. } else {
  215. var stag = '<a href="'+href+'"';
  216. if (hreflang) { stag = stag+' hreflang="'+hreflang+'"'; }
  217. stag = stag+'>';
  218. etag = '</a>';
  219. }
  220. encloseSelection(stag,etag);
  221. });
  222. }
  223. function btImgLink(label,msg_src)
  224. {
  225. addButton(img_path+'bt_img_link.png',label,
  226. function() {
  227. encloseSelection('','',
  228. function(str) {
  229. var src = window.prompt(msg_src,'');
  230. if (!src) { return str; }
  231. if (getFormat() == 'wiki') {
  232. if (str) {
  233. return '(('+src+'|'+str+'))';
  234. } else {
  235. return '(('+src+'))';
  236. }
  237. } else {
  238. if (str) {
  239. return '<img src="'+src+'" alt="'+str+'" />';
  240. } else {
  241. return '<img src="'+src+'" alt="" />';
  242. }
  243. }
  244. });
  245. });
  246. }
  247. function btImg(label,url)
  248. {
  249. addButton(img_path+'bt_img.png',label,
  250. function() {
  251. popup(url);
  252. });
  253. }
  254. function insImg(src)
  255. {
  256. if (document.all) {
  257. textarea.focus();
  258. if (getFormat() == 'wiki') {
  259. textarea.value = textarea.value+'(('+src+'))';
  260. } else {
  261. textarea.value = textarea.value+'<img src="'+src+'" alt="" />';
  262. }
  263. } else {
  264. encloseSelection('','',
  265. function(str) {
  266. if (getFormat() == 'wiki') {
  267. if (str) {
  268. return '(('+src+'|'+str+'))';
  269. } else {
  270. return '(('+src+'))';
  271. }
  272. } else {
  273. if (str) {
  274. return '<img src="'+src+'" alt="'+str+'" />';
  275. } else {
  276. return '<img src="'+src+'" alt="" />';
  277. }
  278. }
  279. });
  280. }
  281. }
  282. // methods
  283. this.addButton = addButton;
  284. this.addSpace = addSpace;
  285. this.draw = draw;
  286. this.btStrong = btStrong;
  287. this.btEm = btEm;
  288. this.btIns = btIns;
  289. this.btDel = btDel;
  290. this.btQ = btQ;
  291. this.btCode = btCode;
  292. this.btBr = btBr;
  293. this.btBquote = btBquote;
  294. this.btPre = btPre;
  295. this.btList = btList;
  296. this.btLink = btLink;
  297. this.btImgLink = btImgLink;
  298. this.btImg = btImg;
  299. this.insImg = insImg;
  300. }