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