Changeset 3735:a3733e656d86 for plugins/dcLegacyEditor/js/jsToolBar
- Timestamp:
- 03/19/18 13:30:59 (8 years ago)
- Branch:
- default
- Location:
- plugins/dcLegacyEditor/js/jsToolBar
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
plugins/dcLegacyEditor/js/jsToolBar/jsToolBar.dotclear.js
r3361 r3735 1 /*global jsToolBar */ 2 'use strict'; 3 1 4 /* Change link button actions 2 5 -------------------------------------------------------- */ … … 6 9 7 10 jsToolBar.prototype.elements.link.popup = function(args) { 8 9 10 11 12 var url = this.elements.link.open_url+args;13 14 var p_win = window.open(url,'dc_popup',15 'alwaysRaised=yes,dependent=yes,toolbar=yes,height=420,width=520,'+16 11 window.the_toolbar = this; 12 args = args || ''; 13 14 this.elements.link.data = {}; 15 var url = this.elements.link.open_url + args; 16 17 window.open(url, 'dc_popup', 18 'alwaysRaised=yes,dependent=yes,toolbar=yes,height=420,width=520,' + 19 'menubar=no,resizable=yes,scrollbars=yes,status=no'); 17 20 }; 18 21 19 22 jsToolBar.prototype.elements.link.fn.wiki = function() { 20 this.elements.link.popup.call(this,'&hreflang='+this.elements.link.default_hreflang);23 this.elements.link.popup.call(this, '&hreflang=' + this.elements.link.default_hreflang); 21 24 }; 22 25 jsToolBar.prototype.elements.link.fncall.wiki = function() { 23 var data = this.elements.link.data; 24 25 if (data.href == '') { return; } 26 27 var etag = '|'+data.href; 28 if (data.hreflang) { etag += '|'+data.hreflang; } 29 30 if (data.title) { 31 if (!data.hreflang) { etag += '|'; } 32 etag += '|'+data.title; 33 } 34 35 if (data.content) { 36 this.encloseSelection('['+data.content,etag+']'); 37 } else { 38 this.encloseSelection('[',etag+']'); 39 } 26 var data = this.elements.link.data; 27 28 if (data.href == '') { 29 return; 30 } 31 32 var etag = '|' + data.href; 33 if (data.hreflang) { 34 etag += '|' + data.hreflang; 35 } 36 37 if (data.title) { 38 if (!data.hreflang) { 39 etag += '|'; 40 } 41 etag += '|' + data.title; 42 } 43 44 if (data.content) { 45 this.encloseSelection('[' + data.content, etag + ']'); 46 } else { 47 this.encloseSelection('[', etag + ']'); 48 } 40 49 }; 41 50 42 51 jsToolBar.prototype.elements.link.fn.xhtml = function() { 43 this.elements.link.popup.call(this,'&hreflang='+this.elements.link.default_hreflang);52 this.elements.link.popup.call(this, '&hreflang=' + this.elements.link.default_hreflang); 44 53 }; 45 54 jsToolBar.prototype.elements.link.fncall.xhtml = function() { 46 var data = this.elements.link.data; 47 48 if (data.href == '') { return; } 49 50 var stag = '<a href="'+data.href+'"'; 51 52 if (data.hreflang) { stag += ' hreflang="'+data.hreflang+'"'; } 53 if (data.title) { stag += ' title="'+data.title+'"'; } 54 stag += '>'; 55 var etag = '</a>'; 56 57 if (data.content) { 58 this.encloseSelection('','',function() { 59 return stag + data.content + etag; 60 }); 61 } else { 62 this.encloseSelection(stag,etag); 63 } 55 var data = this.elements.link.data; 56 57 if (data.href == '') { 58 return; 59 } 60 61 var stag = '<a href="' + data.href + '"'; 62 63 if (data.hreflang) { 64 stag += ' hreflang="' + data.hreflang + '"'; 65 } 66 if (data.title) { 67 stag += ' title="' + data.title + '"'; 68 } 69 stag += '>'; 70 var etag = '</a>'; 71 72 if (data.content) { 73 this.encloseSelection('', '', function() { 74 return stag + data.content + etag; 75 }); 76 } else { 77 this.encloseSelection(stag, etag); 78 } 64 79 }; 65 80 66 81 jsToolBar.prototype.elements.link.fn.wysiwyg = function() { 67 68 69 70 71 72 73 74 href= a.tag.href || '';75 76 77 78 79 this.elements.link.popup.call(this,'&href='+href+'&hreflang='+hreflang+'&title='+title);82 var href, title, hreflang; 83 href = title = hreflang = ''; 84 hreflang = this.elements.link.default_hreflang; 85 86 var a = this.getAncestor(); 87 88 if (a.tagName == 'a') { 89 href = a.tag.href || ''; 90 title = a.tag.title || ''; 91 hreflang = a.tag.hreflang || ''; 92 } 93 94 this.elements.link.popup.call(this, '&href=' + href + '&hreflang=' + hreflang + '&title=' + title); 80 95 }; 81 96 jsToolBar.prototype.elements.link.fncall.wysiwyg = function() { 82 var data = this.elements.link.data; 83 84 var a = this.getAncestor(); 85 86 if (a.tagName == 'a') { 87 if (data.href == '') { 88 // Remove link 89 this.replaceNodeByContent(a.tag); 90 this.iwin.focus(); 91 return; 92 } else { 93 // Update link 94 a.tag.href = data.href; 95 if (data.hreflang) { 96 a.tag.setAttribute('hreflang',data.hreflang); 97 } else { 98 a.tag.removeAttribute('hreflang'); 99 } 100 if (data.title) { 101 a.tag.setAttribute('title',data.title); 102 } else { 103 a.tag.removeAttribute('title'); 104 } 105 return; 106 } 107 } 108 109 // Create link 110 if (data.content) { 111 var n = document.createTextNode(data.content); 112 } else { 113 var n = this.getSelectedNode(); 114 } 115 var a = this.iwin.document.createElement('a'); 116 a.href = data.href; 117 if (data.hreflang) a.setAttribute('hreflang',data.hreflang); 118 if (data.title) a.setAttribute('title',data.title); 119 a.appendChild(n); 120 this.insertNode(a); 97 var data = this.elements.link.data; 98 99 var a = this.getAncestor(); 100 101 if (a.tagName == 'a') { 102 if (data.href == '') { 103 // Remove link 104 this.replaceNodeByContent(a.tag); 105 this.iwin.focus(); 106 return; 107 } else { 108 // Update link 109 a.tag.href = data.href; 110 if (data.hreflang) { 111 a.tag.setAttribute('hreflang', data.hreflang); 112 } else { 113 a.tag.removeAttribute('hreflang'); 114 } 115 if (data.title) { 116 a.tag.setAttribute('title', data.title); 117 } else { 118 a.tag.removeAttribute('title'); 119 } 120 return; 121 } 122 } 123 124 // Create link 125 var n; 126 if (data.content) { 127 n = document.createTextNode(data.content); 128 } else { 129 n = this.getSelectedNode(); 130 } 131 a = this.iwin.document.createElement('a'); 132 a.href = data.href; 133 if (data.hreflang) a.setAttribute('hreflang', data.hreflang); 134 if (data.title) a.setAttribute('title', data.title); 135 a.appendChild(n); 136 this.insertNode(a); 121 137 }; 122 138 jsToolBar.prototype.getAncestor = function() { 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 while (ancestorTagName!='a' && ancestorTagName!='body') {140 141 142 143 144 145 146 147 139 var res = {}; 140 var range, commonAncestorContainer; 141 142 if (this.iwin.getSelection) { //gecko 143 var selection = this.iwin.getSelection(); 144 range = selection.getRangeAt(0); 145 commonAncestorContainer = range.commonAncestorContainer; 146 while (commonAncestorContainer.nodeType != 1) { 147 commonAncestorContainer = commonAncestorContainer.parentNode; 148 } 149 } else { //ie 150 range = this.iwin.document.selection.createRange(); 151 commonAncestorContainer = range.parentElement(); 152 } 153 154 var ancestorTagName = commonAncestorContainer.tagName.toLowerCase(); 155 while (ancestorTagName != 'a' && ancestorTagName != 'body') { 156 commonAncestorContainer = commonAncestorContainer.parentNode; 157 ancestorTagName = commonAncestorContainer.tagName.toLowerCase(); 158 } 159 160 res.tag = commonAncestorContainer; 161 res.tagName = ancestorTagName; 162 163 return res; 148 164 }; 149 165 … … 151 167 -------------------------------------------------------- */ 152 168 jsToolBar.prototype.elements.img_select = { 153 154 155 156 157 158 159 160 161 162 163 164 var p_win = window.open(this.elements.img_select.open_url,'dc_popup',165 'alwaysRaised=yes,dependent=yes,toolbar=yes,height=500,width=760,'+166 167 169 type: 'button', 170 title: 'Image chooser', 171 accesskey: 'm', 172 fn: {}, 173 fncall: {}, 174 open_url: 'media.php?popup=1&plugin_id=dcLegacyEditor', 175 data: {}, 176 popup: function() { 177 window.the_toolbar = this; 178 this.elements.img_select.data = {}; 179 180 window.open(this.elements.img_select.open_url, 'dc_popup', 181 'alwaysRaised=yes,dependent=yes,toolbar=yes,height=500,width=760,' + 182 'menubar=no,resizable=yes,scrollbars=yes,status=no'); 183 } 168 184 }; 169 185 jsToolBar.prototype.elements.img_select.fn.wiki = function() { 170 186 this.elements.img_select.popup.call(this); 171 187 }; 172 188 jsToolBar.prototype.elements.img_select.fncall.wiki = function() { 173 var d = this.elements.img_select.data; 174 if (d.src == undefined) { return; } 175 176 this.encloseSelection('','',function(str) { 177 var alt = (str) ? str : d.title; 178 var res = '(('+d.src+'|'+alt; 179 180 if (d.alignment == 'left') { 181 res += '|L'; 182 } else if (d.alignment == 'right') { 183 res += '|R'; 184 } else if (d.alignment == 'center') { 185 res += '|C'; 186 } else if (d.description) { 187 res += '|'; 188 } 189 190 if (d.description) { 191 res += '|'+d.description; 192 } 193 194 res += '))'; 195 196 if (d.link) { 197 var ltitle = (alt) ? '||'+alt : ''; 198 res = '['+res+'|'+d.url+ltitle+']'; 199 } 200 201 return res; 202 }); 189 var d = this.elements.img_select.data; 190 if (d.src == undefined) { 191 return; 192 } 193 194 this.encloseSelection('', '', function(str) { 195 var alt = (str) ? str : d.title; 196 var res = '((' + d.src + '|' + alt; 197 198 if (d.alignment == 'left') { 199 res += '|L'; 200 } else if (d.alignment == 'right') { 201 res += '|R'; 202 } else if (d.alignment == 'center') { 203 res += '|C'; 204 } else if (d.description) { 205 res += '|'; 206 } 207 208 if (d.description) { 209 res += '|' + d.description; 210 } 211 212 res += '))'; 213 214 if (d.link) { 215 var ltitle = (alt) ? '||' + alt : ''; 216 res = '[' + res + '|' + d.url + ltitle + ']'; 217 } 218 219 return res; 220 }); 203 221 }; 204 222 jsToolBar.prototype.elements.img_select.fn.xhtml = function() { 205 223 this.elements.img_select.popup.call(this); 206 224 }; 207 225 jsToolBar.prototype.elements.img_select.fncall.xhtml = function() { 208 var d = this.elements.img_select.data; 209 if (d.src == undefined) { return; } 210 211 this.encloseSelection('','',function(str) { 212 var alt = (str) ? str : d.title; 213 var res = '<img src="'+d.src+'" alt="'+alt.replace('&','&').replace('>','>').replace('<','<').replace('"','"')+'"'; 214 215 if (d.alignment == 'left') { 216 res += ' style="float: left; margin: 0 1em 1em 0;"'; 217 } else if (d.alignment == 'right') { 218 res += ' style="float: right; margin: 0 0 1em 1em;"'; 219 } else if (d.alignment == 'center') { 220 res += ' style="margin: 0 auto; display: block;"'; 221 } 222 223 if (d.description) { 224 res += ' title="'+d.description.replace('&','&').replace('>','>').replace('<','<').replace('"','"')+'"'; 225 } 226 227 res += ' />'; 228 229 if (d.link) { 230 var ltitle = (alt) ? ' title="'+alt.replace('&','&').replace('>','>').replace('<','<').replace('"','"')+'"' : ''; 231 res = '<a href="'+d.url+'"'+ltitle+'>'+res+'</a>'; 232 } 233 234 return res; 235 }); 226 var d = this.elements.img_select.data; 227 if (d.src == undefined) { 228 return; 229 } 230 231 this.encloseSelection('', '', function(str) { 232 var alt = (str) ? str : d.title; 233 var res = '<img src="' + d.src + '" alt="' + alt.replace('&', '&').replace('>', '>').replace('<', '<').replace('"', '"') + '"'; 234 235 if (d.alignment == 'left') { 236 res += ' style="float: left; margin: 0 1em 1em 0;"'; 237 } else if (d.alignment == 'right') { 238 res += ' style="float: right; margin: 0 0 1em 1em;"'; 239 } else if (d.alignment == 'center') { 240 res += ' style="margin: 0 auto; display: block;"'; 241 } 242 243 if (d.description) { 244 res += ' title="' + d.description.replace('&', '&').replace('>', '>').replace('<', '<').replace('"', '"') + '"'; 245 } 246 247 res += ' />'; 248 249 if (d.link) { 250 var ltitle = (alt) ? ' title="' + alt.replace('&', '&').replace('>', '>').replace('<', '<').replace('"', '"') + '"' : ''; 251 res = '<a href="' + d.url + '"' + ltitle + '>' + res + '</a>'; 252 } 253 254 return res; 255 }); 236 256 }; 237 257 238 258 jsToolBar.prototype.elements.img.fn.wysiwyg = function() { 239 var src = this.elements.img.prompt.call(this); 240 if (!src) { return; } 241 242 var img = this.iwin.document.createElement('img'); 243 img.src = src; 244 img.setAttribute('alt',this.getSelectedText()); 245 246 this.insertNode(img); 259 var src = this.elements.img.prompt.call(this); 260 if (!src) { 261 return; 262 } 263 264 var img = this.iwin.document.createElement('img'); 265 img.src = src; 266 img.setAttribute('alt', this.getSelectedText()); 267 268 this.insertNode(img); 247 269 }; 248 270 249 271 jsToolBar.prototype.elements.img_select.fn.wysiwyg = function() { 250 272 this.elements.img_select.popup.call(this); 251 273 }; 252 274 jsToolBar.prototype.elements.img_select.fncall.wysiwyg = function() { 253 var d = this.elements.img_select.data; 254 var alt = (this.getSelectedText()) ? this.getSelectedText() : d.title; 255 if (d.src == undefined) { return; } 256 257 var img = this.iwin.document.createElement('img'); 258 img.src = d.src; 259 img.setAttribute('alt',alt); 260 261 262 if (d.alignment == 'left') { 263 if (img.style.styleFloat != undefined) { 264 img.style.styleFloat = 'left'; 265 } else { 266 img.style.cssFloat = 'left'; 267 } 268 img.style.marginTop = 0; 269 img.style.marginRight = '1em'; 270 img.style.marginBottom = '1em'; 271 img.style.marginLeft = 0; 272 } else if (d.alignment == 'right') { 273 if (img.style.styleFloat != undefined) { 274 img.style.styleFloat = 'right'; 275 } else { 276 img.style.cssFloat = 'right'; 277 } 278 img.style.marginTop = 0; 279 img.style.marginRight = 0; 280 img.style.marginBottom = '1em'; 281 img.style.marginLeft = '1em'; 282 } else if (d.alignment == 'center') { 283 img.style.marginTop = 0; 284 img.style.marginRight = 'auto'; 285 img.style.marginBottom = 0; 286 img.style.marginLeft = 'auto'; 287 img.style.display = 'block'; 288 } 289 290 if (d.description) { 291 img.setAttribute('title',d.description); 292 } 293 294 if (d.link) { 295 var a = this.iwin.document.createElement('a'); 296 a.href = d.url; 297 if (alt) { 298 a.setAttribute('title',alt); 299 } 300 a.appendChild(img); 301 this.insertNode(a); 302 } else { 303 this.insertNode(img); 304 } 275 var d = this.elements.img_select.data; 276 var alt = (this.getSelectedText()) ? this.getSelectedText() : d.title; 277 if (d.src == undefined) { 278 return; 279 } 280 281 var img = this.iwin.document.createElement('img'); 282 img.src = d.src; 283 img.setAttribute('alt', alt); 284 285 if (d.alignment == 'left') { 286 if (img.style.styleFloat != undefined) { 287 img.style.styleFloat = 'left'; 288 } else { 289 img.style.cssFloat = 'left'; 290 } 291 img.style.marginTop = 0; 292 img.style.marginRight = '1em'; 293 img.style.marginBottom = '1em'; 294 img.style.marginLeft = 0; 295 } else if (d.alignment == 'right') { 296 if (img.style.styleFloat != undefined) { 297 img.style.styleFloat = 'right'; 298 } else { 299 img.style.cssFloat = 'right'; 300 } 301 img.style.marginTop = 0; 302 img.style.marginRight = 0; 303 img.style.marginBottom = '1em'; 304 img.style.marginLeft = '1em'; 305 } else if (d.alignment == 'center') { 306 img.style.marginTop = 0; 307 img.style.marginRight = 'auto'; 308 img.style.marginBottom = 0; 309 img.style.marginLeft = 'auto'; 310 img.style.display = 'block'; 311 } 312 313 if (d.description) { 314 img.setAttribute('title', d.description); 315 } 316 317 if (d.link) { 318 var a = this.iwin.document.createElement('a'); 319 a.href = d.url; 320 if (alt) { 321 a.setAttribute('title', alt); 322 } 323 a.appendChild(img); 324 this.insertNode(a); 325 } else { 326 this.insertNode(img); 327 } 305 328 }; 306 329 307 330 // MP3 helpers 308 jsToolBar.prototype.elements.mp3_insert = { fncall: {}, data: {} }; 331 jsToolBar.prototype.elements.mp3_insert = { 332 fncall: {}, 333 data: {} 334 }; 309 335 jsToolBar.prototype.elements.mp3_insert.fncall.wiki = function() { 310 var d = this.elements.mp3_insert.data; 311 if (d.player == undefined) { return; } 312 313 this.encloseSelection('','',function(str) { 314 return '\n///html\n' + d.player + '///\n'; 315 }); 336 var d = this.elements.mp3_insert.data; 337 if (d.player == undefined) { 338 return; 339 } 340 341 this.encloseSelection('', '', function() { 342 return '\n///html\n' + d.player + '///\n'; 343 }); 316 344 }; 317 345 jsToolBar.prototype.elements.mp3_insert.fncall.xhtml = function() { 318 var d = this.elements.mp3_insert.data; 319 if (d.player == undefined) { return; } 320 321 this.encloseSelection('','',function(str) { 322 return '\n' + d.player + '\n'; 323 }); 346 var d = this.elements.mp3_insert.data; 347 if (d.player == undefined) { 348 return; 349 } 350 351 this.encloseSelection('', '', function() { 352 return '\n' + d.player + '\n'; 353 }); 324 354 }; 325 355 jsToolBar.prototype.elements.mp3_insert.fncall.wysiwyg = function() { 326 356 return; 327 357 }; 328 358 329 359 // FLV helpers 330 jsToolBar.prototype.elements.flv_insert = { fncall: {}, data: {} }; 360 jsToolBar.prototype.elements.flv_insert = { 361 fncall: {}, 362 data: {} 363 }; 331 364 jsToolBar.prototype.elements.flv_insert.fncall.wiki = function() { 332 var d = this.elements.flv_insert.data; 333 if (d.player == undefined) { return; } 334 335 this.encloseSelection('','',function(str) { 336 return '\n///html\n' + d.player + '///\n'; 337 }); 365 var d = this.elements.flv_insert.data; 366 if (d.player == undefined) { 367 return; 368 } 369 370 this.encloseSelection('', '', function() { 371 return '\n///html\n' + d.player + '///\n'; 372 }); 338 373 }; 339 374 jsToolBar.prototype.elements.flv_insert.fncall.xhtml = function() { 340 var d = this.elements.flv_insert.data; 341 if (d.player == undefined) { return; } 342 343 this.encloseSelection('','',function(str) { 344 return '\n' + d.player + '\n'; 345 }); 375 var d = this.elements.flv_insert.data; 376 if (d.player == undefined) { 377 return; 378 } 379 380 this.encloseSelection('', '', function() { 381 return '\n' + d.player + '\n'; 382 }); 346 383 }; 347 384 jsToolBar.prototype.elements.flv_insert.fncall.wysiwyg = function() { 348 return; 349 }; 350 385 return; 386 }; 351 387 352 388 /* Posts selector 353 389 -------------------------------------------------------- */ 354 390 jsToolBar.prototype.elements.post_link = { 355 356 357 358 359 360 361 362 363 364 var p_win = window.open(this.elements.post_link.open_url,'dc_popup',365 'alwaysRaised=yes,dependent=yes,toolbar=yes,height=500,width=760,'+366 367 391 type: 'button', 392 title: 'Link to an entry', 393 fn: {}, 394 open_url: 'popup_posts.php?plugin_id=dcLegacyEditor', 395 data: {}, 396 popup: function() { 397 window.the_toolbar = this; 398 this.elements.img_select.data = {}; 399 400 window.open(this.elements.post_link.open_url, 'dc_popup', 401 'alwaysRaised=yes,dependent=yes,toolbar=yes,height=500,width=760,' + 402 'menubar=no,resizable=yes,scrollbars=yes,status=no'); 403 } 368 404 }; 369 405 jsToolBar.prototype.elements.post_link.fn.wiki = function() { 370 406 this.elements.post_link.popup.call(this); 371 407 }; 372 408 jsToolBar.prototype.elements.post_link.fn.xhtml = function() { 373 409 this.elements.post_link.popup.call(this); 374 410 }; 375 411 jsToolBar.prototype.elements.post_link.fn.wysiwyg = function() { 376 412 this.elements.post_link.popup.call(this); 377 413 }; 378 414 379 415 // Last space element 380 416 jsToolBar.prototype.elements.space3 = { 381 type:'space',382 format:{383 wysiwyg:true,384 wiki:true,385 xhtml:true386 387 }; 417 type: 'space', 418 format: { 419 wysiwyg: true, 420 wiki: true, 421 xhtml: true 422 } 423 }; -
plugins/dcLegacyEditor/js/jsToolBar/jsToolBar.js
r3643 r3735 1 /*exported jsToolBar */ 2 'use strict'; 3 1 4 /* ***** BEGIN LICENSE BLOCK ***** 2 5 * This file is part of DotClear. … … 19 22 * 20 23 * ***** END LICENSE BLOCK ***** 21 */ 22 23 function jsToolBar(textarea) { 24 if (!document.createElement) { return; } 25 26 if (!textarea) { return; } 27 28 if ((typeof(document["selection"]) == "undefined") 29 && (typeof(textarea["setSelectionRange"]) == "undefined")) { 30 return; 31 } 32 33 this.textarea = textarea; 34 35 this.editor = document.createElement('div'); 36 this.editor.className = 'jstEditor'; 37 38 this.textarea.parentNode.insertBefore(this.editor,this.textarea); 39 this.editor.appendChild(this.textarea); 40 41 this.toolbar = document.createElement("div"); 42 this.toolbar.className = 'jstElements'; 43 44 if (this.toolbar_bottom) { 45 this.editor.parentNode.insertBefore(this.toolbar,this.editor.nextSibling); 46 } else { 47 this.editor.parentNode.insertBefore(this.toolbar,this.editor); 48 } 49 50 // Dragable resizing (only for gecko) 51 if (navigator.appName == 'Microsoft Internet Explorer') 52 { 53 if (this.editor.addEventListener) 54 { 55 this.handle = document.createElement('div'); 56 this.handle.className = 'jstHandle'; 57 var dragStart = this.resizeDragStart; 58 var This = this; 59 this.handle.addEventListener('mousedown',function(event) { dragStart.call(This,event); },false); 60 this.editor.parentNode.insertBefore(this.handle,this.editor.nextSibling); 61 } 62 } 63 64 this.context = null; 65 this.toolNodes = {}; // lorsque la toolbar est dessinée , cet objet est garni 66 // de raccourcis vers les éléments DOM correspondants aux outils. 67 }; 68 69 function jsButton(title, fn, scope, className, accesskey) { 70 this.title = title || null; 71 this.fn = fn || function(){}; 72 this.scope = scope || null; 73 this.className = className || null; 74 this.accesskey = accesskey || null; 24 */ 25 26 var jsToolBar = function(textarea) { 27 if (!document.createElement) { 28 return; 29 } 30 31 if (!textarea) { 32 return; 33 } 34 35 if ((typeof(document.selection) == "undefined") && (typeof(textarea.setSelectionRange) == "undefined")) { 36 return; 37 } 38 39 this.textarea = textarea; 40 41 this.editor = document.createElement('div'); 42 this.editor.className = 'jstEditor'; 43 44 this.textarea.parentNode.insertBefore(this.editor, this.textarea); 45 this.editor.appendChild(this.textarea); 46 47 this.toolbar = document.createElement("div"); 48 this.toolbar.className = 'jstElements'; 49 50 if (this.toolbar_bottom) { 51 this.editor.parentNode.insertBefore(this.toolbar, this.editor.nextSibling); 52 } else { 53 this.editor.parentNode.insertBefore(this.toolbar, this.editor); 54 } 55 56 // Dragable resizing (only for gecko) 57 if (navigator.appName == 'Microsoft Internet Explorer') { 58 if (this.editor.addEventListener) { 59 this.handle = document.createElement('div'); 60 this.handle.className = 'jstHandle'; 61 var dragStart = this.resizeDragStart; 62 var This = this; 63 this.handle.addEventListener('mousedown', function(event) { 64 dragStart.call(This, event); 65 }, false); 66 this.editor.parentNode.insertBefore(this.handle, this.editor.nextSibling); 67 } 68 } 69 70 this.context = null; 71 this.toolNodes = {}; // lorsque la toolbar est dessinée , cet objet est garni 72 // de raccourcis vers les éléments DOM correspondants aux outils. 73 }; 74 75 var jsButton = function(title, fn, scope, className, accesskey) { 76 this.title = title || null; 77 this.fn = fn || function() {}; 78 this.scope = scope || null; 79 this.className = className || null; 80 this.accesskey = accesskey || null; 75 81 }; 76 82 jsButton.prototype.draw = function() { 77 if (!this.scope) return null; 78 79 var button = document.createElement('button'); 80 button.setAttribute('type','button'); 81 if (this.className) button.className = this.className; 82 button.title = this.title; 83 if (this.accesskey) button.accessKey = this.accesskey; 84 var span = document.createElement('span'); 85 span.appendChild(document.createTextNode(this.title)); 86 button.appendChild(span); 87 88 if (this.icon != undefined) { 89 button.style.backgroundImage = 'url('+this.icon+')'; 90 } 91 if (typeof(this.fn) == 'function') { 92 var This = this; 93 button.onclick = function() { try { This.fn.apply(This.scope, arguments) } catch (e) {} return false; }; 94 } 95 return button; 96 }; 97 98 function jsSpace(id) { 99 this.id = id || null; 100 this.width = null; 83 if (!this.scope) return null; 84 85 var button = document.createElement('button'); 86 button.setAttribute('type', 'button'); 87 if (this.className) button.className = this.className; 88 button.title = this.title; 89 if (this.accesskey) button.accessKey = this.accesskey; 90 var span = document.createElement('span'); 91 span.appendChild(document.createTextNode(this.title)); 92 button.appendChild(span); 93 94 if (this.icon != undefined) { 95 button.style.backgroundImage = 'url(' + this.icon + ')'; 96 } 97 if (typeof(this.fn) == 'function') { 98 var This = this; 99 button.onclick = function() { 100 try { 101 This.fn.apply(This.scope, arguments); 102 } catch (e) {} 103 return false; 104 }; 105 } 106 return button; 107 }; 108 109 var jsSpace = function(id) { 110 this.id = id || null; 111 this.width = null; 101 112 }; 102 113 jsSpace.prototype.draw = function() { 103 104 105 106 107 if (this.width) span.style.marginRight = this.width+'px';108 109 110 }; 111 112 function jsCombo(title, options, scope, fn, className) {113 114 115 116 this.fn = fn || function(){};117 114 var span = document.createElement('span'); 115 if (this.id) span.id = this.id; 116 span.appendChild(document.createTextNode(String.fromCharCode(160))); 117 span.className = 'jstSpacer'; 118 if (this.width) span.style.marginRight = this.width + 'px'; 119 120 return span; 121 }; 122 123 var jsCombo = function(title, options, scope, fn, className) { 124 this.title = title || null; 125 this.options = options || null; 126 this.scope = scope || null; 127 this.fn = fn || function() {}; 128 this.className = className || null; 118 129 }; 119 130 jsCombo.prototype.draw = function() { 120 if (!this.scope || !this.options) return null; 121 122 var select = document.createElement('select'); 123 if (this.className) select.className = className; 124 select.title = this.title; 125 126 for (var o in this.options) { 127 //var opt = this.options[o]; 128 var option = document.createElement('option'); 129 option.value = o; 130 option.appendChild(document.createTextNode(this.options[o])); 131 select.appendChild(option); 132 } 133 134 var This = this; 135 select.onchange = function() { 136 try { 137 This.fn.call(This.scope, this.value); 138 } catch (e) { alert(e); } 139 140 return false; 141 }; 142 143 return select; 144 }; 145 131 if (!this.scope || !this.options) return null; 132 133 var select = document.createElement('select'); 134 if (this.className) select.className = this.className; 135 select.title = this.title; 136 137 for (var o in this.options) { 138 //var opt = this.options[o]; 139 var option = document.createElement('option'); 140 option.value = o; 141 option.appendChild(document.createTextNode(this.options[o])); 142 select.appendChild(option); 143 } 144 145 var This = this; 146 select.onchange = function() { 147 try { 148 This.fn.call(This.scope, this.value); 149 } catch (e) { 150 window.alert(e); 151 } 152 153 return false; 154 }; 155 156 return select; 157 }; 146 158 147 159 jsToolBar.prototype = { 148 base_url: '', 149 mode: 'xhtml', 150 elements: {}, 151 toolbar_bottom: false, 152 153 getMode: function() { 154 return this.mode; 155 }, 156 157 setMode: function(mode) { 158 this.mode = mode || 'xhtml'; 159 }, 160 161 switchMode: function(mode) { 162 mode = mode || 'xhtml'; 163 this.draw(mode); 164 }, 165 166 button: function(toolName) { 167 var tool = this.elements[toolName]; 168 if (typeof tool.fn[this.mode] != 'function') return null; 169 var b = new jsButton(tool.title, tool.fn[this.mode], this, 'jstb_'+toolName, tool.accesskey); 170 if (tool.icon != undefined) { 171 b.icon = tool.icon; 172 } 173 return b; 174 }, 175 space: function(toolName) { 176 var tool = new jsSpace(toolName); 177 if (this.elements[toolName].format != undefined && !this.elements[toolName].format[this.mode]) return null; 178 if (this.elements[toolName].width !== undefined) { 179 tool.width = this.elements[toolName].width; 180 } 181 return tool; 182 }, 183 combo: function(toolName) { 184 var tool = this.elements[toolName]; 185 186 if( tool[this.mode] != undefined) { 187 188 var length = tool[this.mode].list.length; 189 190 if (typeof tool[this.mode].fn != 'function' || length == 0) { 191 return null; 192 } else { 193 var options = {}; 194 for (var i=0; i < length; i++) { 195 var opt = tool[this.mode].list[i]; 196 options[opt] = tool.options[opt]; 197 } 198 return new jsCombo(tool.title, options, this, tool[this.mode].fn); 199 } 200 201 } 202 203 }, 204 draw: function(mode) { 205 this.setMode(mode); 206 207 // Empty toolbar 208 while (this.toolbar.hasChildNodes()) { 209 this.toolbar.removeChild(this.toolbar.firstChild) 210 } 211 this.toolNodes = {}; // vide les raccourcis DOM/**/ 212 213 // Draw toolbar elements 214 var b, tool, newTool; 215 216 for (var i in this.elements) { 217 b = this.elements[i]; 218 219 var disabled = 220 b.type == undefined || b.type == '' 221 || (b.disabled != undefined && b.disabled) 222 || (b.context != undefined && b.context != null && b.context != this.context); 223 224 if (!disabled && typeof this[b.type] == 'function') { 225 tool = this[b.type](i); 226 if (tool) newTool = tool.draw(); 227 if (newTool) { 228 this.toolNodes[i] = newTool; //mémorise l'accès DOM pour usage éventuel ultérieur 229 this.toolbar.appendChild(newTool); 230 } 231 } 232 } 233 }, 234 235 singleTag: function(stag,etag) { 236 stag = stag || null; 237 etag = etag || stag; 238 239 if (!stag || !etag) { return; } 240 241 this.encloseSelection(stag,etag); 242 }, 243 244 encloseSelection: function(prefix, suffix, fn) { 245 this.textarea.focus(); 246 247 prefix = prefix || ''; 248 suffix = suffix || ''; 249 250 var start, end, sel, scrollPos, subst, res; 251 252 if (typeof(document["selection"]) != "undefined") { 253 sel = document.selection.createRange().text; 254 } else if (typeof(this.textarea["setSelectionRange"]) != "undefined") { 255 start = this.textarea.selectionStart; 256 end = this.textarea.selectionEnd; 257 scrollPos = this.textarea.scrollTop; 258 sel = this.textarea.value.substring(start, end); 259 } 260 261 if (sel.match(/ $/)) { // exclude ending space char, if any 262 sel = sel.substring(0, sel.length - 1); 263 suffix = suffix + " "; 264 } 265 266 if (typeof(fn) == 'function') { 267 res = (sel) ? fn.call(this,sel) : fn(''); 268 } else { 269 res = (sel) ? sel : ''; 270 } 271 272 subst = prefix + res + suffix; 273 274 if (typeof(document["selection"]) != "undefined") { 275 var range = document.selection.createRange().text = subst; 276 this.textarea.caretPos -= suffix.length; 277 } else if (typeof(this.textarea["setSelectionRange"]) != "undefined") { 278 this.textarea.value = this.textarea.value.substring(0, start) + subst + 279 this.textarea.value.substring(end); 280 if (sel) { 281 this.textarea.setSelectionRange(start + subst.length, start + subst.length); 282 } else { 283 if (typeof(fn) != 'function') { 284 this.textarea.setSelectionRange(start + prefix.length, start + prefix.length); 285 } 286 } 287 this.textarea.scrollTop = scrollPos; 288 } 289 }, 290 291 stripBaseURL: function(url) { 292 if (this.base_url != '') { 293 var pos = url.indexOf(this.base_url); 294 if (pos == 0) { 295 url = url.substr(this.base_url.length); 296 } 297 } 298 299 return url; 300 } 160 base_url: '', 161 mode: 'xhtml', 162 elements: {}, 163 toolbar_bottom: false, 164 165 getMode: function() { 166 return this.mode; 167 }, 168 169 setMode: function(mode) { 170 this.mode = mode || 'xhtml'; 171 }, 172 173 switchMode: function(mode) { 174 mode = mode || 'xhtml'; 175 this.draw(mode); 176 }, 177 178 button: function(toolName) { 179 var tool = this.elements[toolName]; 180 if (typeof tool.fn[this.mode] != 'function') return null; 181 var b = new jsButton(tool.title, tool.fn[this.mode], this, 'jstb_' + toolName, tool.accesskey); 182 if (tool.icon != undefined) { 183 b.icon = tool.icon; 184 } 185 return b; 186 }, 187 space: function(toolName) { 188 var tool = new jsSpace(toolName); 189 if (this.elements[toolName].format != undefined && !this.elements[toolName].format[this.mode]) return null; 190 if (this.elements[toolName].width !== undefined) { 191 tool.width = this.elements[toolName].width; 192 } 193 return tool; 194 }, 195 combo: function(toolName) { 196 var tool = this.elements[toolName]; 197 198 if (tool[this.mode] != undefined) { 199 200 var length = tool[this.mode].list.length; 201 202 if (typeof tool[this.mode].fn != 'function' || length == 0) { 203 return null; 204 } else { 205 var options = {}; 206 for (var i = 0; i < length; i++) { 207 var opt = tool[this.mode].list[i]; 208 options[opt] = tool.options[opt]; 209 } 210 return new jsCombo(tool.title, options, this, tool[this.mode].fn); 211 } 212 213 } 214 215 }, 216 draw: function(mode) { 217 this.setMode(mode); 218 219 // Empty toolbar 220 while (this.toolbar.hasChildNodes()) { 221 this.toolbar.removeChild(this.toolbar.firstChild); 222 } 223 this.toolNodes = {}; // vide les raccourcis DOM/**/ 224 225 // Draw toolbar elements 226 var b, tool, newTool; 227 228 for (var i in this.elements) { 229 b = this.elements[i]; 230 231 var disabled = 232 b.type == undefined || b.type == '' || 233 (b.disabled != undefined && b.disabled) || 234 (b.context != undefined && b.context != null && b.context != this.context); 235 236 if (!disabled && typeof this[b.type] == 'function') { 237 tool = this[b.type](i); 238 if (tool) newTool = tool.draw(); 239 if (newTool) { 240 this.toolNodes[i] = newTool; //mémorise l'accès DOM pour usage éventuel ultérieur 241 this.toolbar.appendChild(newTool); 242 } 243 } 244 } 245 }, 246 247 singleTag: function(stag, etag) { 248 stag = stag || null; 249 etag = etag || stag; 250 251 if (!stag || !etag) { 252 return; 253 } 254 255 this.encloseSelection(stag, etag); 256 }, 257 258 encloseSelection: function(prefix, suffix, fn) { 259 this.textarea.focus(); 260 261 prefix = prefix || ''; 262 suffix = suffix || ''; 263 264 var start, end, sel, scrollPos, subst, res; 265 266 if (typeof(document.selection) != "undefined") { 267 sel = document.selection.createRange().text; 268 } else if (typeof(this.textarea.setSelectionRange) != "undefined") { 269 start = this.textarea.selectionStart; 270 end = this.textarea.selectionEnd; 271 scrollPos = this.textarea.scrollTop; 272 sel = this.textarea.value.substring(start, end); 273 } 274 275 if (sel.match(/ $/)) { // exclude ending space char, if any 276 sel = sel.substring(0, sel.length - 1); 277 suffix = suffix + " "; 278 } 279 280 if (typeof(fn) == 'function') { 281 res = (sel) ? fn.call(this, sel) : fn(''); 282 } else { 283 res = (sel) ? sel : ''; 284 } 285 286 subst = prefix + res + suffix; 287 288 if (typeof(document.selection) != "undefined") { 289 document.selection.createRange().text = subst; 290 this.textarea.caretPos -= suffix.length; 291 } else if (typeof(this.textarea.setSelectionRange) != "undefined") { 292 this.textarea.value = this.textarea.value.substring(0, start) + subst + 293 this.textarea.value.substring(end); 294 if (sel) { 295 this.textarea.setSelectionRange(start + subst.length, start + subst.length); 296 } else { 297 if (typeof(fn) != 'function') { 298 this.textarea.setSelectionRange(start + prefix.length, start + prefix.length); 299 } 300 } 301 this.textarea.scrollTop = scrollPos; 302 } 303 }, 304 305 stripBaseURL: function(url) { 306 if (this.base_url != '') { 307 var pos = url.indexOf(this.base_url); 308 if (pos == 0) { 309 url = url.substr(this.base_url.length); 310 } 311 } 312 313 return url; 314 } 301 315 }; 302 316 … … 304 318 -------------------------------------------------------- */ 305 319 jsToolBar.prototype.resizeSetStartH = function() { 306 320 this.dragStartH = this.textarea.offsetHeight + 0; 307 321 }; 308 322 jsToolBar.prototype.resizeDragStart = function(event) { 309 var This = this; 310 this.dragStartY = event.clientY; 311 this.resizeSetStartH(); 312 document.addEventListener('mousemove', this.dragMoveHdlr=function(event){This.resizeDragMove(event);}, false); 313 document.addEventListener('mouseup', this.dragStopHdlr=function(event){This.resizeDragStop(event);}, false); 323 var This = this; 324 this.dragStartY = event.clientY; 325 this.resizeSetStartH(); 326 document.addEventListener('mousemove', this.dragMoveHdlr = function(event) { 327 This.resizeDragMove(event); 328 }, false); 329 document.addEventListener('mouseup', this.dragStopHdlr = function(event) { 330 This.resizeDragStop(event); 331 }, false); 314 332 }; 315 333 316 334 jsToolBar.prototype.resizeDragMove = function(event) { 317 this.textarea.style.height = (this.dragStartH+event.clientY-this.dragStartY)+'px';318 }; 319 320 jsToolBar.prototype.resizeDragStop = function( event) {321 322 335 this.textarea.style.height = (this.dragStartH + event.clientY - this.dragStartY) + 'px'; 336 }; 337 338 jsToolBar.prototype.resizeDragStop = function() { 339 document.removeEventListener('mousemove', this.dragMoveHdlr, false); 340 document.removeEventListener('mouseup', this.dragStopHdlr, false); 323 341 }; 324 342 … … 326 344 // block format (paragraph, headers) 327 345 jsToolBar.prototype.elements.blocks = { 328 type: 'combo', 329 title: 'block format', 330 options: { 331 none: '-- none --', // only for wysiwyg mode 332 nonebis: '- block format -', // only for xhtml mode 333 p: 'Paragraph', 334 h1: 'Header 1', 335 h2: 'Header 2', 336 h3: 'Header 3', 337 h4: 'Header 4', 338 h5: 'Header 5', 339 h6: 'Header 6' 340 }, 341 xhtml: { 342 list: ['nonebis','p','h1','h2','h3','h4','h5','h6'], 343 fn: function(opt) { 344 if (opt=='nonebis') 345 this.textarea.focus(); 346 else 347 try {this.singleTag('<'+opt+'>','</'+opt+'>');} catch(e){}; 348 this.toolNodes.blocks.value = 'nonebis'; 349 } 350 }, 351 wiki: { 352 list: ['nonebis','h3','h4','h5'], 353 fn: function(opt) { 354 switch (opt) { 355 case 'nonebis': this.textarea.focus(); break; 356 case 'h3': this.encloseSelection('!!!'); break; 357 case 'h4': this.encloseSelection('!!'); break; 358 case 'h5': this.encloseSelection('!'); break; 359 } 360 this.toolNodes.blocks.value = 'nonebis'; 361 } 362 } 346 type: 'combo', 347 title: 'block format', 348 options: { 349 none: '-- none --', // only for wysiwyg mode 350 nonebis: '- block format -', // only for xhtml mode 351 p: 'Paragraph', 352 h1: 'Header 1', 353 h2: 'Header 2', 354 h3: 'Header 3', 355 h4: 'Header 4', 356 h5: 'Header 5', 357 h6: 'Header 6' 358 }, 359 xhtml: { 360 list: ['nonebis', 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'], 361 fn: function(opt) { 362 if (opt == 'nonebis') 363 this.textarea.focus(); 364 else 365 try { 366 this.singleTag('<' + opt + '>', '</' + opt + '>'); 367 } catch (e) {} 368 this.toolNodes.blocks.value = 'nonebis'; 369 } 370 }, 371 wiki: { 372 list: ['nonebis', 'h3', 'h4', 'h5'], 373 fn: function(opt) { 374 switch (opt) { 375 case 'nonebis': 376 this.textarea.focus(); 377 break; 378 case 'h3': 379 this.encloseSelection('!!!'); 380 break; 381 case 'h4': 382 this.encloseSelection('!!'); 383 break; 384 case 'h5': 385 this.encloseSelection('!'); 386 break; 387 } 388 this.toolNodes.blocks.value = 'nonebis'; 389 } 390 } 363 391 }; 364 392 365 393 // spacer 366 394 jsToolBar.prototype.elements.space0 = { 367 type:'space',368 format:{369 wysiwyg:true,370 wiki:true,371 xhtml:true372 395 type: 'space', 396 format: { 397 wysiwyg: true, 398 wiki: true, 399 xhtml: true 400 } 373 401 }; 374 402 375 403 // strong 376 404 jsToolBar.prototype.elements.strong = { 377 type: 'button', 378 title: 'Strong emphasis', 379 fn: { 380 wiki: function() { this.singleTag('__') }, 381 xhtml: function() { this.singleTag('<strong>','</strong>') } 382 } 405 type: 'button', 406 title: 'Strong emphasis', 407 fn: { 408 wiki: function() { 409 this.singleTag('__'); 410 }, 411 xhtml: function() { 412 this.singleTag('<strong>', '</strong>'); 413 } 414 } 383 415 }; 384 416 385 417 // em 386 418 jsToolBar.prototype.elements.em = { 387 type: 'button', 388 title: 'Emphasis', 389 fn: { 390 wiki: function() { this.singleTag("''") }, 391 xhtml: function() { this.singleTag('<em>','</em>') } 392 } 419 type: 'button', 420 title: 'Emphasis', 421 fn: { 422 wiki: function() { 423 this.singleTag("''"); 424 }, 425 xhtml: function() { 426 this.singleTag('<em>', '</em>'); 427 } 428 } 393 429 }; 394 430 395 431 // ins 396 432 jsToolBar.prototype.elements.ins = { 397 type: 'button', 398 title: 'Inserted', 399 fn: { 400 wiki: function() { this.singleTag('++') }, 401 xhtml: function() { this.singleTag('<ins>','</ins>') } 402 } 433 type: 'button', 434 title: 'Inserted', 435 fn: { 436 wiki: function() { 437 this.singleTag('++'); 438 }, 439 xhtml: function() { 440 this.singleTag('<ins>', '</ins>'); 441 } 442 } 403 443 }; 404 444 405 445 // del 406 446 jsToolBar.prototype.elements.del = { 407 type: 'button', 408 title: 'Deleted', 409 fn: { 410 wiki: function() { this.singleTag('--') }, 411 xhtml: function() { this.singleTag('<del>','</del>') } 412 } 447 type: 'button', 448 title: 'Deleted', 449 fn: { 450 wiki: function() { 451 this.singleTag('--'); 452 }, 453 xhtml: function() { 454 this.singleTag('<del>', '</del>'); 455 } 456 } 413 457 }; 414 458 415 459 // quote 416 460 jsToolBar.prototype.elements.quote = { 417 type: 'button', 418 title: 'Inline quote', 419 fn: { 420 wiki: function() { this.singleTag('{{','}}') }, 421 xhtml: function() { this.singleTag('<q>','</q>') } 422 } 461 type: 'button', 462 title: 'Inline quote', 463 fn: { 464 wiki: function() { 465 this.singleTag('{{', '}}'); 466 }, 467 xhtml: function() { 468 this.singleTag('<q>', '</q>'); 469 } 470 } 423 471 }; 424 472 425 473 // code 426 474 jsToolBar.prototype.elements.code = { 427 type: 'button', 428 title: 'Code', 429 fn: { 430 wiki: function() { this.singleTag('@@') }, 431 xhtml: function() { this.singleTag('<code>','</code>')} 432 } 475 type: 'button', 476 title: 'Code', 477 fn: { 478 wiki: function() { 479 this.singleTag('@@'); 480 }, 481 xhtml: function() { 482 this.singleTag('<code>', '</code>'); 483 } 484 } 433 485 }; 434 486 435 487 // code 436 488 jsToolBar.prototype.elements.mark = { 437 type: 'button', 438 title: 'Mark', 439 fn: { 440 wiki: function() { this.singleTag('""') }, 441 xhtml: function() { this.singleTag('<mark>','</mark>')} 442 } 489 type: 'button', 490 title: 'Mark', 491 fn: { 492 wiki: function() { 493 this.singleTag('""'); 494 }, 495 xhtml: function() { 496 this.singleTag('<mark>', '</mark>'); 497 } 498 } 443 499 }; 444 500 445 501 // spacer 446 502 jsToolBar.prototype.elements.space1 = { 447 type:'space',448 format:{449 wysiwyg:true,450 wiki:true,451 xhtml:true452 503 type: 'space', 504 format: { 505 wysiwyg: true, 506 wiki: true, 507 xhtml: true 508 } 453 509 }; 454 510 455 511 // br 456 512 jsToolBar.prototype.elements.br = { 457 type: 'button', 458 title: 'Line break', 459 fn: { 460 wiki: function() { this.encloseSelection("%%%\n",'') }, 461 xhtml: function() { this.encloseSelection("<br />\n",'')} 462 } 513 type: 'button', 514 title: 'Line break', 515 fn: { 516 wiki: function() { 517 this.encloseSelection("%%%\n", ''); 518 }, 519 xhtml: function() { 520 this.encloseSelection("<br />\n", ''); 521 } 522 } 463 523 }; 464 524 465 525 // spacer 466 526 jsToolBar.prototype.elements.space2 = { 467 type:'space',468 format:{469 wysiwyg:true,470 wiki:true,471 xhtml:true472 527 type: 'space', 528 format: { 529 wysiwyg: true, 530 wiki: true, 531 xhtml: true 532 } 473 533 }; 474 534 475 535 // blockquote 476 536 jsToolBar.prototype.elements.blockquote = { 477 type: 'button', 478 title: 'Blockquote', 479 fn: { 480 xhtml: function() { this.singleTag('<blockquote>','</blockquote>') }, 481 wiki: function() { 482 this.encloseSelection("\n",'', 483 function(str) { 484 str = str.replace(/\r/g,''); 485 return '> '+str.replace(/\n/g,"\n> "); 486 }); 487 } 488 } 537 type: 'button', 538 title: 'Blockquote', 539 fn: { 540 xhtml: function() { 541 this.singleTag('<blockquote>', '</blockquote>'); 542 }, 543 wiki: function() { 544 this.encloseSelection("\n", '', 545 function(str) { 546 str = str.replace(/\r/g, ''); 547 return '> ' + str.replace(/\n/g, "\n> "); 548 }); 549 } 550 } 489 551 }; 490 552 491 553 // pre 492 554 jsToolBar.prototype.elements.pre = { 493 type: 'button', 494 title: 'Preformated text', 495 fn: { 496 wiki: function() { this.singleTag("///\n","\n///") }, 497 xhtml: function() { this.singleTag('<pre>','</pre>') } 498 } 555 type: 'button', 556 title: 'Preformated text', 557 fn: { 558 wiki: function() { 559 this.singleTag("///\n", "\n///"); 560 }, 561 xhtml: function() { 562 this.singleTag('<pre>', '</pre>'); 563 } 564 } 499 565 }; 500 566 501 567 // ul 502 568 jsToolBar.prototype.elements.ul = { 503 504 505 506 507 this.encloseSelection('','',function(str) {508 str = str.replace(/\r/g,'');509 return '* '+str.replace(/\n/g,"\n* ");510 511 512 513 this.encloseSelection('','',function(str) {514 str = str.replace(/\r/g,'');515 str = str.replace(/\n/g,"</li>\n <li>");516 return "<ul>\n <li>"+str+"</li>\n</ul>";517 518 519 569 type: 'button', 570 title: 'Unordered list', 571 fn: { 572 wiki: function() { 573 this.encloseSelection('', '', function(str) { 574 str = str.replace(/\r/g, ''); 575 return '* ' + str.replace(/\n/g, "\n* "); 576 }); 577 }, 578 xhtml: function() { 579 this.encloseSelection('', '', function(str) { 580 str = str.replace(/\r/g, ''); 581 str = str.replace(/\n/g, "</li>\n <li>"); 582 return "<ul>\n <li>" + str + "</li>\n</ul>"; 583 }); 584 } 585 } 520 586 }; 521 587 522 588 // ol 523 589 jsToolBar.prototype.elements.ol = { 524 525 526 527 528 this.encloseSelection('','',function(str) {529 str = str.replace(/\r/g,'');530 return '# '+str.replace(/\n/g,"\n# ");531 532 533 534 this.encloseSelection('','',function(str) {535 str = str.replace(/\r/g,'');536 str = str.replace(/\n/g,"</li>\n <li>");537 return "<ol>\n <li>"+str+"</li>\n</ol>";538 539 540 590 type: 'button', 591 title: 'Ordered list', 592 fn: { 593 wiki: function() { 594 this.encloseSelection('', '', function(str) { 595 str = str.replace(/\r/g, ''); 596 return '# ' + str.replace(/\n/g, "\n# "); 597 }); 598 }, 599 xhtml: function() { 600 this.encloseSelection('', '', function(str) { 601 str = str.replace(/\r/g, ''); 602 str = str.replace(/\n/g, "</li>\n <li>"); 603 return "<ol>\n <li>" + str + "</li>\n</ol>"; 604 }); 605 } 606 } 541 607 }; 542 608 543 609 // spacer 544 610 jsToolBar.prototype.elements.space3 = { 545 type:'space',546 format:{547 wysiwyg:true,548 wiki:true,549 xhtml:true550 611 type: 'space', 612 format: { 613 wysiwyg: true, 614 wiki: true, 615 xhtml: true 616 } 551 617 }; 552 618 553 619 // link 554 620 jsToolBar.prototype.elements.link = { 555 type: 'button', 556 title: 'Link', 557 fn: {}, 558 accesskey: 'l', 559 href_prompt: 'Please give page URL:', 560 hreflang_prompt: 'Language of this page:', 561 default_hreflang: '', 562 prompt: function(href,hreflang) { 563 href = href || ''; 564 hreflang = hreflang || this.elements.link.default_hreflang; 565 566 href = window.prompt(this.elements.link.href_prompt,href); 567 if (!href) { return false; } 568 569 hreflang = window.prompt(this.elements.link.hreflang_prompt, 570 hreflang); 571 572 return { href: this.stripBaseURL(href), hreflang: hreflang }; 573 } 621 type: 'button', 622 title: 'Link', 623 fn: {}, 624 accesskey: 'l', 625 href_prompt: 'Please give page URL:', 626 hreflang_prompt: 'Language of this page:', 627 default_hreflang: '', 628 prompt: function(href, hreflang) { 629 href = href || ''; 630 hreflang = hreflang || this.elements.link.default_hreflang; 631 632 href = window.prompt(this.elements.link.href_prompt, href); 633 if (!href) { 634 return false; 635 } 636 637 hreflang = window.prompt(this.elements.link.hreflang_prompt, 638 hreflang); 639 640 return { 641 href: this.stripBaseURL(href), 642 hreflang: hreflang 643 }; 644 } 574 645 }; 575 646 576 647 jsToolBar.prototype.elements.link.fn.xhtml = function() { 577 var link = this.elements.link.prompt.call(this); 578 if (link) { 579 var stag = '<a href="'+link.href+'"'; 580 if (link.hreflang) { stag = stag+' hreflang="'+link.hreflang+'"'; } 581 stag = stag+'>'; 582 var etag = '</a>'; 583 584 this.encloseSelection(stag,etag); 585 } 648 var link = this.elements.link.prompt.call(this); 649 if (link) { 650 var stag = '<a href="' + link.href + '"'; 651 if (link.hreflang) { 652 stag = stag + ' hreflang="' + link.hreflang + '"'; 653 } 654 stag = stag + '>'; 655 var etag = '</a>'; 656 657 this.encloseSelection(stag, etag); 658 } 586 659 }; 587 660 jsToolBar.prototype.elements.link.fn.wiki = function() { 588 var link = this.elements.link.prompt.call(this); 589 if (link) { 590 var stag = '['; 591 var etag = '|'+link.href; 592 if (link.hreflang) { etag = etag+'|'+link.hreflang; } 593 etag = etag+']'; 594 595 this.encloseSelection(stag,etag); 596 } 661 var link = this.elements.link.prompt.call(this); 662 if (link) { 663 var stag = '['; 664 var etag = '|' + link.href; 665 if (link.hreflang) { 666 etag = etag + '|' + link.hreflang; 667 } 668 etag = etag + ']'; 669 670 this.encloseSelection(stag, etag); 671 } 597 672 }; 598 673 599 674 // img 600 675 jsToolBar.prototype.elements.img = { 601 602 603 604 605 606 607 return this.stripBaseURL(window.prompt(this.elements.img.src_prompt,src));608 676 type: 'button', 677 title: 'External image', 678 src_prompt: 'Please give image URL:', 679 fn: {}, 680 prompt: function(src) { 681 src = src || ''; 682 return this.stripBaseURL(window.prompt(this.elements.img.src_prompt, src)); 683 } 609 684 }; 610 685 jsToolBar.prototype.elements.img.fn.xhtml = function() { 611 612 613 this.encloseSelection('','',function(str) {614 615 return '<img src="'+src+'" alt="'+str+'" />';616 617 return '<img src="'+src+'" alt="" />';618 619 620 686 var src = this.elements.img.prompt.call(this); 687 if (src) { 688 this.encloseSelection('', '', function(str) { 689 if (str) { 690 return '<img src="' + src + '" alt="' + str + '" />'; 691 } else { 692 return '<img src="' + src + '" alt="" />'; 693 } 694 }); 695 } 621 696 }; 622 697 jsToolBar.prototype.elements.img.fn.wiki = function() { 623 624 625 this.encloseSelection('','',function(str) {626 627 return '(('+src+'|'+str+'))';628 629 return '(('+src+'))';630 631 632 633 }; 698 var src = this.elements.img.prompt.call(this); 699 if (src) { 700 this.encloseSelection('', '', function(str) { 701 if (str) { 702 return '((' + src + '|' + str + '))'; 703 } else { 704 return '((' + src + '))'; 705 } 706 }); 707 } 708 }; -
plugins/dcLegacyEditor/js/jsToolBar/jsToolBar.wysiwyg.js
r3669 r3735 1 /*global jsToolBar, dotclear_htmlFontSize, chainHandler */ 2 'use strict'; 3 1 4 /* ***** BEGIN LICENSE BLOCK ***** 2 5 * This file is part of DotClear. … … 19 22 * 20 23 * ***** END LICENSE BLOCK ***** 21 */24 */ 22 25 23 26 jsToolBar.prototype.can_wwg = (document.designMode != undefined); … … 31 34 jsToolBar.prototype.drawToolBar = jsToolBar.prototype.draw; 32 35 jsToolBar.prototype.draw = function(mode) { 33 34 35 36 37 38 39 40 41 36 mode = mode || 'xhtml'; 37 38 if (this.can_wwg) { 39 this.mode = 'wysiwyg'; 40 this.drawToolBar('wysiwyg'); 41 this.initWindow(); 42 } else { 43 this.drawToolBar(mode); 44 } 42 45 }; 43 46 44 47 jsToolBar.prototype.switchMode = function(mode) { 45 46 47 48 49 50 51 52 53 54 55 56 57 58 48 mode = mode || 'xhtml'; 49 50 if (mode == 'xhtml') { 51 this.wwg_mode = true; 52 this.draw(mode); 53 } else { 54 if (this.wwg_mode) { 55 this.syncContents('iframe'); 56 } 57 this.wwg_mode = false; 58 this.removeEditor(); 59 this.textarea.style.display = ''; 60 this.drawToolBar(mode); 61 } 59 62 }; 60 63 61 64 jsToolBar.prototype.syncContents = function(from) { 62 from = from || 'textarea'; 63 var This = this; 64 if (from == 'textarea') { 65 initContent(); 66 } else { 67 this.validBlockquote(); 68 var html = this.applyHtmlFilters(this.ibody.innerHTML); 69 if (html == '<br />') { html = '<p></p>'; } 70 this.textarea.value = html; 71 } 72 73 function initContent() { 74 if (!This.iframe.contentWindow.document || !This.iframe.contentWindow.document.body) { 75 setTimeout(initContent, 1); 76 return; 77 } 78 This.ibody = This.iframe.contentWindow.document.body; 79 80 if (This.textarea.value != '' && This.textarea.value != '<p></p>') { 81 This.ibody.innerHTML = This.applyWysiwygFilters(This.textarea.value); 82 if (This.ibody.createTextRange) { //cursor at the begin for IE 83 var IErange = This.ibody.createTextRange(); 84 IErange.execCommand("SelectAll"); 85 IErange.collapse(); 86 IErange.select(); 87 } 88 } else { 89 var idoc = This.iwin.document; 90 var para = idoc.createElement('p'); 91 para.appendChild(idoc.createElement('br')); 92 while (idoc.body.hasChildNodes()) { 93 idoc.body.removeChild(idoc.body.lastChild); 94 } 95 idoc.body.appendChild(para); 96 } 97 } 65 from = from || 'textarea'; 66 var This = this; 67 if (from == 'textarea') { 68 initContent(); 69 } else { 70 this.validBlockquote(); 71 var html = this.applyHtmlFilters(this.ibody.innerHTML); 72 if (html == '<br />') { 73 html = '<p></p>'; 74 } 75 this.textarea.value = html; 76 } 77 78 function initContent() { 79 if (!This.iframe.contentWindow.document || !This.iframe.contentWindow.document.body) { 80 setTimeout(initContent, 1); 81 return; 82 } 83 This.ibody = This.iframe.contentWindow.document.body; 84 85 if (This.textarea.value != '' && This.textarea.value != '<p></p>') { 86 This.ibody.innerHTML = This.applyWysiwygFilters(This.textarea.value); 87 if (This.ibody.createTextRange) { //cursor at the begin for IE 88 var IErange = This.ibody.createTextRange(); 89 IErange.execCommand("SelectAll"); 90 IErange.collapse(); 91 IErange.select(); 92 } 93 } else { 94 var idoc = This.iwin.document; 95 var para = idoc.createElement('p'); 96 para.appendChild(idoc.createElement('br')); 97 while (idoc.body.hasChildNodes()) { 98 idoc.body.removeChild(idoc.body.lastChild); 99 } 100 idoc.body.appendChild(para); 101 } 102 } 98 103 }; 99 104 jsToolBar.prototype.htmlFilters = { 100 tagsoup: function(str){101 102 105 tagsoup: function(str) { 106 return this.tagsoup2xhtml(str); 107 } 103 108 }; 104 109 jsToolBar.prototype.applyHtmlFilters = function(str) { 105 106 107 108 110 for (var fn in this.htmlFilters) { 111 str = this.htmlFilters[fn].call(this, str); 112 } 113 return str; 109 114 }; 110 115 jsToolBar.prototype.wysiwygFilters = {}; 111 jsToolBar.prototype.applyWysiwygFilters = function( 112 113 114 115 116 jsToolBar.prototype.applyWysiwygFilters = function(str) { 117 for (var fn in this.wysiwygFilters) { 118 str = this.wysiwygFilters[fn].call(this, str); 119 } 120 return str; 116 121 }; 117 122 118 123 jsToolBar.prototype.switchEdit = function() { 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 124 if (this.wwg_mode) { 125 this.textarea.style.display = ''; 126 this.iframe.style.display = 'none'; 127 this.syncContents('iframe'); 128 this.drawToolBar('xhtml'); 129 this.wwg_mode = false; 130 this.focusEditor(); 131 } else { 132 this.iframe.style.display = ''; 133 this.textarea.style.display = 'none'; 134 this.syncContents('textarea'); 135 this.drawToolBar('wysiwyg'); 136 this.wwg_mode = true; 137 this.focusEditor(); 138 } 139 this.setSwitcher(); 135 140 }; 136 141 137 142 /** Creates iframe for editor, inits a blank document 138 */143 */ 139 144 jsToolBar.prototype.initWindow = function() { 140 var This = this; 141 142 this.iframe = document.createElement('iframe'); 143 this.textarea.parentNode.insertBefore(this.iframe,this.textarea.nextSibling); 144 145 this.switcher = document.createElement('ul'); 146 this.switcher.className = 'jstSwitcher'; 147 this.editor.appendChild(this.switcher); 148 149 this.iframe.height = this.textarea.offsetHeight + 0; 150 this.iframe.width = this.textarea.offsetWidth + 0; 151 152 if (this.textarea.tabIndex != undefined) { 153 this.iframe.tabIndex = this.textarea.tabIndex; 154 } 155 156 function initIframe() { 157 var doc = This.iframe.contentWindow.document; 158 if (!doc) { 159 setTimeout(initIframe,1); 160 return false; 161 } 162 163 doc.open(); 164 var html = 165 '<html>\n'+ 166 '<head>\n'+ 167 '<link rel="stylesheet" href="style/default.css" type="text/css" media="screen" />' 168 '<style type="text/css">'+This.iframe_css+'</style>\n'+ 169 (This.base_url != '' ? '<base href="'+This.base_url+'" />' : '')+ 170 '</head>\n'+ 171 '<body>\n'+ 172 '</body>\n'+ 173 '</html>'; 174 175 doc.write(html); 176 doc.close(); 177 if (document.all) { // for IE 178 doc.designMode = 'on'; 179 // warning : doc is now inaccessible for IE6 sp1 180 } 181 182 if (typeof dotclear_htmlFontSize !== 'undefined') { 183 doc.documentElement.style.setProperty('--html-font-size',dotclear_htmlFontSize); 184 } 185 186 This.iwin = This.iframe.contentWindow; 187 188 This.syncContents('textarea'); 189 190 if (This.wwg_mode == undefined) { 191 This.wwg_mode = true; 192 } 193 194 if (This.wwg_mode) { 195 This.textarea.style.display = 'none'; 196 } else { 197 This.iframe.style.display = 'none'; 198 } 199 200 // update textarea on submit 201 if (This.textarea.form) { 202 chainHandler(This.textarea.form,'onsubmit', function() { 203 if (This.wwg_mode) { 204 This.syncContents('iframe'); 205 } 206 }); 207 } 208 209 for (var evt in This.iwinEvents) { 210 var event = This.iwinEvents[evt]; 211 This.addIwinEvent(This.iframe.contentWindow.document, event.type, event.fn, This); 212 } 213 214 This.setSwitcher(); 215 try { This.iwin.document.designMode = 'on'; } catch (e) {}; // Firefox needs this 216 217 return true; 218 } 219 initIframe(); 145 var This = this; 146 147 this.iframe = document.createElement('iframe'); 148 this.textarea.parentNode.insertBefore(this.iframe, this.textarea.nextSibling); 149 150 this.switcher = document.createElement('ul'); 151 this.switcher.className = 'jstSwitcher'; 152 this.editor.appendChild(this.switcher); 153 154 this.iframe.height = this.textarea.offsetHeight + 0; 155 this.iframe.width = this.textarea.offsetWidth + 0; 156 157 if (this.textarea.tabIndex != undefined) { 158 this.iframe.tabIndex = this.textarea.tabIndex; 159 } 160 161 function initIframe() { 162 var doc = This.iframe.contentWindow.document; 163 if (!doc) { 164 setTimeout(initIframe, 1); 165 return false; 166 } 167 168 doc.open(); 169 var html = 170 '<html>\n' + 171 '<head>\n' + 172 '<link rel="stylesheet" href="style/default.css" type="text/css" media="screen" />' + 173 '<style type="text/css">' + This.iframe_css + '</style>\n' + 174 (This.base_url != '' ? '<base href="' + This.base_url + '" />' : '') + 175 '</head>\n' + 176 '<body>\n' + 177 '</body>\n' + 178 '</html>'; 179 180 doc.write(html); 181 doc.close(); 182 if (document.all) { // for IE 183 doc.designMode = 'on'; 184 // warning : doc is now inaccessible for IE6 sp1 185 } 186 187 if (typeof dotclear_htmlFontSize !== 'undefined') { 188 doc.documentElement.style.setProperty('--html-font-size', dotclear_htmlFontSize); 189 } 190 191 This.iwin = This.iframe.contentWindow; 192 193 This.syncContents('textarea'); 194 195 if (This.wwg_mode == undefined) { 196 This.wwg_mode = true; 197 } 198 199 if (This.wwg_mode) { 200 This.textarea.style.display = 'none'; 201 } else { 202 This.iframe.style.display = 'none'; 203 } 204 205 // update textarea on submit 206 if (This.textarea.form) { 207 chainHandler(This.textarea.form, 'onsubmit', function() { 208 if (This.wwg_mode) { 209 This.syncContents('iframe'); 210 } 211 }); 212 } 213 214 for (var evt in This.iwinEvents) { 215 var event = This.iwinEvents[evt]; 216 This.addIwinEvent(This.iframe.contentWindow.document, event.type, event.fn, This); 217 } 218 219 This.setSwitcher(); 220 try { 221 This.iwin.document.designMode = 'on'; 222 } catch (e) {} // Firefox needs this 223 224 return true; 225 } 226 initIframe(); 220 227 }; 221 228 jsToolBar.prototype.addIwinEvent = function(target, type, fn, scope) { 222 var myFn = function(e){fn.call(scope, e)}; 223 addEvent(target, type, myFn, true); 224 // fix memory leak 225 addEvent(scope.iwin, 'unload', function(){ 226 removeEvent(target, type, myFn, true); 227 }, true); 229 var myFn = function(e) { 230 fn.call(scope, e); 231 }; 232 addEvent(target, type, myFn, true); 233 // fix memory leak 234 addEvent(scope.iwin, 'unload', function() { 235 removeEvent(target, type, myFn, true); 236 }, true); 228 237 }; 229 238 jsToolBar.prototype.iwinEvents = { 230 block1: { 231 type: 'mouseup', 232 fn: function(){ this.adjustBlockLevelCombo() } 233 }, 234 block2: { 235 type: 'keyup', 236 fn: function(){ this.adjustBlockLevelCombo() } 237 } 239 block1: { 240 type: 'mouseup', 241 fn: function() { 242 this.adjustBlockLevelCombo(); 243 } 244 }, 245 block2: { 246 type: 'keyup', 247 fn: function() { 248 this.adjustBlockLevelCombo(); 249 } 250 } 238 251 }; 239 252 240 253 /** Insert a mode switcher after editor area 241 */254 */ 242 255 jsToolBar.prototype.switcher_visual_title = 'visual'; 243 256 jsToolBar.prototype.switcher_source_title = 'source'; 244 257 jsToolBar.prototype.setSwitcher = function() { 245 while (this.switcher.hasChildNodes()) { 246 this.switcher.removeChild(this.switcher.firstChild); 247 } 248 249 var This = this; 250 function setLink(title,link) { 251 var li = document.createElement('li'); 252 if (link) { 253 var a = document.createElement('a'); 254 a.href = '#'; 255 a.editor = This; 256 a.onclick = function() { this.editor.switchEdit(); return false; }; 257 a.appendChild(document.createTextNode(title)); 258 } else { 259 li.className = 'jstSwitcherCurrent'; 260 a = document.createTextNode(title); 261 } 262 263 li.appendChild(a); 264 This.switcher.appendChild(li); 265 } 266 267 setLink(this.switcher_visual_title,!this.wwg_mode); 268 setLink(this.switcher_source_title,this.wwg_mode); 258 while (this.switcher.hasChildNodes()) { 259 this.switcher.removeChild(this.switcher.firstChild); 260 } 261 262 var This = this; 263 264 function setLink(title, link) { 265 var li = document.createElement('li'); 266 var a; 267 if (link) { 268 a = document.createElement('a'); 269 a.href = '#'; 270 a.editor = This; 271 a.onclick = function() { 272 this.editor.switchEdit(); 273 return false; 274 }; 275 a.appendChild(document.createTextNode(title)); 276 } else { 277 li.className = 'jstSwitcherCurrent'; 278 a = document.createTextNode(title); 279 } 280 281 li.appendChild(a); 282 This.switcher.appendChild(li); 283 } 284 285 setLink(this.switcher_visual_title, !this.wwg_mode); 286 setLink(this.switcher_source_title, this.wwg_mode); 269 287 }; 270 288 271 289 /** Removes editor area and mode switcher 272 */290 */ 273 291 jsToolBar.prototype.removeEditor = function() { 274 275 276 277 278 279 280 281 292 if (this.iframe != null) { 293 this.iframe.parentNode.removeChild(this.iframe); 294 this.iframe = null; 295 } 296 297 if (this.switcher != undefined && this.switcher.parentNode != undefined) { 298 this.switcher.parentNode.removeChild(this.switcher); 299 } 282 300 }; 283 301 284 302 /** Focus on the editor area 285 */303 */ 286 304 jsToolBar.prototype.focusEditor = function() { 287 if (this.wwg_mode) { 288 try { this.iwin.document.designMode = 'on'; } catch (e) {}; // Firefox needs this 289 var This = this; 290 setTimeout(function() {This.iframe.contentWindow.focus()},1); 291 } else { 292 this.textarea.focus(); 293 } 305 if (this.wwg_mode) { 306 try { 307 this.iwin.document.designMode = 'on'; 308 } catch (e) {} // Firefox needs this 309 var This = this; 310 setTimeout(function() { 311 This.iframe.contentWindow.focus(); 312 }, 1); 313 } else { 314 this.textarea.focus(); 315 } 294 316 }; 295 317 296 318 /** Resizer 297 */319 */ 298 320 jsToolBar.prototype.resizeSetStartH = function() { 299 300 301 302 303 321 if (this.wwg_mode && this.iframe != undefined) { 322 this.dragStartH = this.iframe.offsetHeight; 323 return; 324 } 325 this.dragStartH = this.textarea.offsetHeight + 0; 304 326 }; 305 327 jsToolBar.prototype.resizeDragMove = function(event) { 306 var new_height = (this.dragStartH+event.clientY-this.dragStartY)+'px';307 308 309 310 328 var new_height = (this.dragStartH + event.clientY - this.dragStartY) + 'px'; 329 if (this.iframe != undefined) { 330 this.iframe.style.height = new_height; 331 } 332 this.textarea.style.height = new_height; 311 333 }; 312 334 … … 314 336 -------------------------------------------------------- */ 315 337 /** Replaces current selection by given node 316 */338 */ 317 339 jsToolBar.prototype.insertNode = function(node) { 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 340 var range; 341 342 if (this.iwin.getSelection) { // Gecko 343 var sel = this.iwin.getSelection(); 344 range = sel.getRangeAt(0); 345 346 // deselect all ranges 347 sel.removeAllRanges(); 348 349 // empty range 350 range.deleteContents(); 351 352 // Insert node 353 range.insertNode(node); 354 355 range.selectNodeContents(node); 356 range.setEndAfter(node); 357 if (range.endContainer.childNodes.length > range.endOffset && 358 range.endContainer.nodeType != Node.TEXT_NODE) { 359 range.setEnd(range.endContainer.childNodes[range.endOffset], 0); 360 } else { 361 range.setEnd(range.endContainer.childNodes[0]); 362 } 363 sel.addRange(range); 364 365 sel.collapseToEnd(); 366 } else { // IE 367 // lambda element 368 var p = this.iwin.document.createElement('div'); 369 p.appendChild(node); 370 range = this.iwin.document.selection.createRange(); 371 range.execCommand('delete'); 372 // insert innerHTML from element 373 range.pasteHTML(p.innerHTML); 374 range.collapse(false); 375 range.select(); 376 } 377 this.iwin.focus(); 356 378 }; 357 379 358 380 /** Returns a document fragment with selected nodes 359 */381 */ 360 382 jsToolBar.prototype.getSelectedNode = function() { 361 if (this.iwin.getSelection) { // Gecko 362 var sel = this.iwin.getSelection(); 363 var range = sel.getRangeAt(0); 364 var content = range.cloneContents(); 365 } else { // IE 366 var sel = this.iwin.document.selection; 367 var d = this.iwin.document.createElement('div'); 368 d.innerHTML = sel.createRange().htmlText; 369 var content = this.iwin.document.createDocumentFragment(); 370 for (var i=0; i < d.childNodes.length; i++) { 371 content.appendChild(d.childNodes[i].cloneNode(true)); 372 } 373 } 374 return content; 383 var sel; 384 var content; 385 if (this.iwin.getSelection) { // Gecko 386 sel = this.iwin.getSelection(); 387 var range = sel.getRangeAt(0); 388 content = range.cloneContents(); 389 } else { // IE 390 sel = this.iwin.document.selection; 391 var d = this.iwin.document.createElement('div'); 392 d.innerHTML = sel.createRange().htmlText; 393 content = this.iwin.document.createDocumentFragment(); 394 for (var i = 0; i < d.childNodes.length; i++) { 395 content.appendChild(d.childNodes[i].cloneNode(true)); 396 } 397 } 398 return content; 375 399 }; 376 400 377 401 /** Returns string representation for selected node 378 */402 */ 379 403 jsToolBar.prototype.getSelectedText = function() { 380 381 382 383 384 385 404 if (this.iwin.getSelection) { // Gecko 405 return this.iwin.getSelection().toString(); 406 } else { // IE 407 var range = this.iwin.document.selection.createRange(); 408 return range.text; 409 } 386 410 }; 387 411 388 412 jsToolBar.prototype.replaceNodeByContent = function(node) { 389 390 for (var i=0; i < node.childNodes.length; i++) {391 392 393 413 var content = this.iwin.document.createDocumentFragment(); 414 for (var i = 0; i < node.childNodes.length; i++) { 415 content.appendChild(node.childNodes[i].cloneNode(true)); 416 } 417 node.parentNode.replaceChild(content, node); 394 418 }; 395 419 396 420 jsToolBar.prototype.getBlockLevel = function() { 397 var blockElts = ['p','h1','h2','h3','h4','h5','h6'];398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 while (arrayIndexOf(blockElts, ancestorTagName)==-1 && ancestorTagName!='body') {414 415 416 417 418 421 var blockElts = ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6']; 422 423 var range, commonAncestorContainer; 424 if (this.iwin.getSelection) { //gecko 425 var selection = this.iwin.getSelection(); 426 range = selection.getRangeAt(0); 427 commonAncestorContainer = range.commonAncestorContainer; 428 while (commonAncestorContainer.nodeType != 1) { 429 commonAncestorContainer = commonAncestorContainer.parentNode; 430 } 431 } else { //ie 432 range = this.iwin.document.selection.createRange(); 433 commonAncestorContainer = range.parentElement(); 434 } 435 436 var ancestorTagName = commonAncestorContainer.tagName.toLowerCase(); 437 while (arrayIndexOf(blockElts, ancestorTagName) == -1 && ancestorTagName != 'body') { 438 commonAncestorContainer = commonAncestorContainer.parentNode; 439 ancestorTagName = commonAncestorContainer.tagName.toLowerCase(); 440 } 441 if (ancestorTagName == 'body') return null; 442 else return commonAncestorContainer; 419 443 }; 420 444 jsToolBar.prototype.adjustBlockLevelCombo = function() { 421 422 423 424 425 426 427 445 var blockLevel = this.getBlockLevel(); 446 if (blockLevel !== null) 447 this.toolNodes.blocks.value = blockLevel.tagName.toLowerCase(); 448 else { 449 if (this.mode == 'wysiwyg') this.toolNodes.blocks.value = 'none'; 450 if (this.mode == 'xhtml') this.toolNodes.blocks.value = 'nonebis'; 451 } 428 452 }; 429 453 … … 431 455 -------------------------------------------------------- */ 432 456 jsToolBar.prototype.simpleCleanRegex = new Array( 433 /* Remove every tags we don't need */ 434 [/<meta[\w\W]*?>/gim,''], 435 [/<style[\w\W]*?>[\w\W]*?<\/style>/gim, ''], 436 [/<\/?font[\w\W]*?>/gim, ''], 437 438 439 /* Replacements */ 440 [/<(\/?)(B|b|STRONG)([\s>\/])/g, "<$1strong$3"], 441 [/<(\/?)(I|i|EM)([\s>\/])/g, "<$1em$3"], 442 [/<IMG ([^>]*?[^\/])>/gi, "<img $1 />"], 443 [/<INPUT ([^>]*?[^\/])>/gi, "<input $1 />"], 444 [/<COL ([^>]*?[^\/])>/gi, "<col $1 />"], 445 [/<AREA ([^>]*?[^\/])>/gi, "<area $1 />"], 446 [/<PARAM ([^>]*?[^\/])>/gi, "<param $1 />"], 447 [/<HR ([^>]*?[^\/])>/gi, "<hr $1/>"], 448 [/<BR ([^>]*?[^\/])>/gi, "<br $1/>"], 449 [/<(\/?)U([\s>\/])/gi, "<$1ins$2"], 450 [/<(\/?)STRIKE([\s>\/])/gi, "<$1del$2"], 451 [/<span style="font-weight: normal;">([\w\W]*?)<\/span>/gm, "$1"], 452 [/<span style="font-weight: bold;">([\w\W]*?)<\/span>/gm, "<strong>$1</strong>"], 453 [/<span style="font-style: italic;">([\w\W]*?)<\/span>/gm, "<em>$1</em>"], 454 [/<span style="text-decoration: underline;">([\w\W]*?)<\/span>/gm, "<ins>$1</ins>"], 455 [/<span style="text-decoration: line-through;">([\w\W]*?)<\/span>/gm, "<del>$1</del>"], 456 [/<span style="text-decoration: underline line-through;">([\w\W]*?)<\/span>/gm, "<del><ins>$1</ins></del>"], 457 [/<span style="(font-weight: bold; ?|font-style: italic; ?){2}">([\w\W]*?)<\/span>/gm, "<strong><em>$2</em></strong>"], 458 [/<span style="(font-weight: bold; ?|text-decoration: underline; ?){2}">([\w\W]*?)<\/span>/gm, "<ins><strong>$2</strong></ins>"], 459 [/<span style="(font-weight: italic; ?|text-decoration: underline; ?){2}">([\w\W]*?)<\/span>/gm, "<ins><em>$2</em></ins>"], 460 [/<span style="(font-weight: bold; ?|text-decoration: line-through; ?){2}">([\w\W]*?)<\/span>/gm, "<del><strong>$2</strong></del>"], 461 [/<span style="(font-weight: italic; ?|text-decoration: line-through; ?){2}">([\w\W]*?)<\/span>/gm, "<del><em>$2</em></del>"], 462 [/<span style="(font-weight: bold; ?|font-style: italic; ?|text-decoration: underline; ?){3}">([\w\W]*?)<\/span>/gm, "<ins><strong><em>$2</em></strong></ins>"], 463 [/<span style="(font-weight: bold; ?|font-style: italic; ?|text-decoration: line-through; ?){3}">([\w\W]*?)<\/span>/gm, "<del><strong><em>$2</em></strong></del>"], 464 [/<span style="(font-weight: bold; ?|font-style: italic; ?|text-decoration: underline line-through; ?){3}">([\w\W]*?)<\/span>/gm, "<del><ins><strong><em>$2</em></strong></ins></del>"], 465 [/<strong style="font-weight: normal;">([\w\W]*?)<\/strong>/gm, "$1"], 466 [/<([a-z]+) style="font-weight: normal;">([\w\W]*?)<\/\1>/gm, "<$1>$2</$1>"], 467 [/<([a-z]+) style="font-weight: bold;">([\w\W]*?)<\/\1>/gm, "<$1><strong>$2</strong></$1>"], 468 [/<([a-z]+) style="font-style: italic;">([\w\W]*?)<\/\1>/gm, "<$1><em>$2</em></$1>"], 469 [/<([a-z]+) style="text-decoration: underline;">([\w\W]*?)<\/\1>/gm, "<ins><$1>$2</$1></ins>"], 470 [/<([a-z]+) style="text-decoration: line-through;">([\w\W]*?)<\/\1>/gm, "<del><$1>$2</$1></del>"], 471 [/<([a-z]+) style="text-decoration: underline line-through;">([\w\W]*?)<\/\1>/gm, "<del><ins><$1>$2</$1></ins></del>"], 472 [/<([a-z]+) style="(font-weight: bold; ?|font-style: italic; ?){2}">([\w\W]*?)<\/\1>/gm, "<$1><strong><em>$3</em></strong></$1>"], 473 [/<([a-z]+) style="(font-weight: bold; ?|text-decoration: underline; ?){2}">([\w\W]*?)<\/\1>/gm, "<ins><$1><strong>$3</strong></$1></ins>"], 474 [/<([a-z]+) style="(font-weight: italic; ?|text-decoration: underline; ?){2}">([\w\W]*?)<\/\1>/gm, "<ins><$1><em>$3</em></$1></ins>"], 475 [/<([a-z]+) style="(font-weight: bold; ?|text-decoration: line-through; ?){2}">([\w\W]*?)<\/\1>/gm, "<del><$1><strong>$3</strong></$1></del>"], 476 [/<([a-z]+) style="(font-weight: italic; ?|text-decoration: line-through; ?){2}">([\w\W]*?)<\/\1>/gm, "<del><$1><em>$3</em></$1></del>"], 477 [/<([a-z]+) style="(font-weight: bold; ?|font-style: italic; ?|text-decoration: underline; ?){3}">([\w\W]*?)<\/\1>/gm, "<ins><$1><strong><em>$3</em></strong></$1></ins>"], 478 [/<([a-z]+) style="(font-weight: bold; ?|font-style: italic; ?|text-decoration: line-through; ?){3}">([\w\W]*?)<\/\1>/gm, "<del><$1><strong><em>$3</em></strong></$1></del>"], 479 [/<([a-z]+) style="(font-weight: bold; ?|font-style: italic; ?|text-decoration: underline line-through; ?){3}">([\w\W]*?)<\/\1>/gm, "<del><ins><$1><strong><em>$3</em></strong></$1></ins></del>"], 480 [/<p><blockquote>(.*)(\n)+<\/blockquote><\/p>/i,"<blockquote>$1</blockquote>\n"], 481 /* mise en forme identique contigue */ 482 [/<\/(strong|em|ins|del|q|code)>(\s*?)<\1>/gim, "$2"], 483 [/<(br|BR)>/g, "<br />"], 484 [/<(hr|HR)>/g, "<hr />"], 485 /* opera est trop strict ;)) */ 486 [/([^\s])\/>/g, "$1 />"], 487 /* br intempestifs de fin de block */ 488 [/<br \/>\s*<\/(h1|h2|h3|h4|h5|h6|ul|ol|li|p|blockquote|div)/gi, "</$1"], 489 [/<\/(h1|h2|h3|h4|h5|h6|ul|ol|li|p|blockquote)>([^\n\u000B\r\f])/gi, "</$1>\n$2"], 490 [/<hr style="width: 100%; height: 2px;" \/>/g, "<hr />"] 457 /* Remove every tags we don't need */ 458 [/<meta[\w\W]*?>/gim, ''], [/<style[\w\W]*?>[\w\W]*?<\/style>/gim, ''], [/<\/?font[\w\W]*?>/gim, ''], 459 460 /* Replacements */ 461 [/<(\/?)(B|b|STRONG)([\s>\/])/g, "<$1strong$3"], [/<(\/?)(I|i|EM)([\s>\/])/g, "<$1em$3"], [/<IMG ([^>]*?[^\/])>/gi, "<img $1 />"], [/<INPUT ([^>]*?[^\/])>/gi, "<input $1 />"], [/<COL ([^>]*?[^\/])>/gi, "<col $1 />"], [/<AREA ([^>]*?[^\/])>/gi, "<area $1 />"], [/<PARAM ([^>]*?[^\/])>/gi, "<param $1 />"], [/<HR ([^>]*?[^\/])>/gi, "<hr $1/>"], [/<BR ([^>]*?[^\/])>/gi, "<br $1/>"], [/<(\/?)U([\s>\/])/gi, "<$1ins$2"], [/<(\/?)STRIKE([\s>\/])/gi, "<$1del$2"], [/<span style="font-weight: normal;">([\w\W]*?)<\/span>/gm, "$1"], [/<span style="font-weight: bold;">([\w\W]*?)<\/span>/gm, "<strong>$1</strong>"], [/<span style="font-style: italic;">([\w\W]*?)<\/span>/gm, "<em>$1</em>"], [/<span style="text-decoration: underline;">([\w\W]*?)<\/span>/gm, "<ins>$1</ins>"], [/<span style="text-decoration: line-through;">([\w\W]*?)<\/span>/gm, "<del>$1</del>"], [/<span style="text-decoration: underline line-through;">([\w\W]*?)<\/span>/gm, "<del><ins>$1</ins></del>"], [/<span style="(font-weight: bold; ?|font-style: italic; ?){2}">([\w\W]*?)<\/span>/gm, "<strong><em>$2</em></strong>"], [/<span style="(font-weight: bold; ?|text-decoration: underline; ?){2}">([\w\W]*?)<\/span>/gm, "<ins><strong>$2</strong></ins>"], [/<span style="(font-weight: italic; ?|text-decoration: underline; ?){2}">([\w\W]*?)<\/span>/gm, "<ins><em>$2</em></ins>"], [/<span style="(font-weight: bold; ?|text-decoration: line-through; ?){2}">([\w\W]*?)<\/span>/gm, "<del><strong>$2</strong></del>"], [/<span style="(font-weight: italic; ?|text-decoration: line-through; ?){2}">([\w\W]*?)<\/span>/gm, "<del><em>$2</em></del>"], [/<span style="(font-weight: bold; ?|font-style: italic; ?|text-decoration: underline; ?){3}">([\w\W]*?)<\/span>/gm, "<ins><strong><em>$2</em></strong></ins>"], [/<span style="(font-weight: bold; ?|font-style: italic; ?|text-decoration: line-through; ?){3}">([\w\W]*?)<\/span>/gm, "<del><strong><em>$2</em></strong></del>"], [/<span style="(font-weight: bold; ?|font-style: italic; ?|text-decoration: underline line-through; ?){3}">([\w\W]*?)<\/span>/gm, "<del><ins><strong><em>$2</em></strong></ins></del>"], [/<strong style="font-weight: normal;">([\w\W]*?)<\/strong>/gm, "$1"], [/<([a-z]+) style="font-weight: normal;">([\w\W]*?)<\/\1>/gm, "<$1>$2</$1>"], [/<([a-z]+) style="font-weight: bold;">([\w\W]*?)<\/\1>/gm, "<$1><strong>$2</strong></$1>"], [/<([a-z]+) style="font-style: italic;">([\w\W]*?)<\/\1>/gm, "<$1><em>$2</em></$1>"], [/<([a-z]+) style="text-decoration: underline;">([\w\W]*?)<\/\1>/gm, "<ins><$1>$2</$1></ins>"], [/<([a-z]+) style="text-decoration: line-through;">([\w\W]*?)<\/\1>/gm, "<del><$1>$2</$1></del>"], [/<([a-z]+) style="text-decoration: underline line-through;">([\w\W]*?)<\/\1>/gm, "<del><ins><$1>$2</$1></ins></del>"], [/<([a-z]+) style="(font-weight: bold; ?|font-style: italic; ?){2}">([\w\W]*?)<\/\1>/gm, "<$1><strong><em>$3</em></strong></$1>"], [/<([a-z]+) style="(font-weight: bold; ?|text-decoration: underline; ?){2}">([\w\W]*?)<\/\1>/gm, "<ins><$1><strong>$3</strong></$1></ins>"], [/<([a-z]+) style="(font-weight: italic; ?|text-decoration: underline; ?){2}">([\w\W]*?)<\/\1>/gm, "<ins><$1><em>$3</em></$1></ins>"], [/<([a-z]+) style="(font-weight: bold; ?|text-decoration: line-through; ?){2}">([\w\W]*?)<\/\1>/gm, "<del><$1><strong>$3</strong></$1></del>"], [/<([a-z]+) style="(font-weight: italic; ?|text-decoration: line-through; ?){2}">([\w\W]*?)<\/\1>/gm, "<del><$1><em>$3</em></$1></del>"], [/<([a-z]+) style="(font-weight: bold; ?|font-style: italic; ?|text-decoration: underline; ?){3}">([\w\W]*?)<\/\1>/gm, "<ins><$1><strong><em>$3</em></strong></$1></ins>"], [/<([a-z]+) style="(font-weight: bold; ?|font-style: italic; ?|text-decoration: line-through; ?){3}">([\w\W]*?)<\/\1>/gm, "<del><$1><strong><em>$3</em></strong></$1></del>"], [/<([a-z]+) style="(font-weight: bold; ?|font-style: italic; ?|text-decoration: underline line-through; ?){3}">([\w\W]*?)<\/\1>/gm, "<del><ins><$1><strong><em>$3</em></strong></$1></ins></del>"], [/<p><blockquote>(.*)(\n)+<\/blockquote><\/p>/i, "<blockquote>$1</blockquote>\n"], 462 /* mise en forme identique contigue */ 463 [/<\/(strong|em|ins|del|q|code)>(\s*?)<\1>/gim, "$2"], [/<(br|BR)>/g, "<br />"], [/<(hr|HR)>/g, "<hr />"], 464 /* opera est trop strict ;)) */ 465 [/([^\s])\/>/g, "$1 />"], 466 /* br intempestifs de fin de block */ 467 [/<br \/>\s*<\/(h1|h2|h3|h4|h5|h6|ul|ol|li|p|blockquote|div)/gi, "</$1"], [/<\/(h1|h2|h3|h4|h5|h6|ul|ol|li|p|blockquote)>([^\n\u000B\r\f])/gi, "</$1>\n$2"], [/<hr style="width: 100%; height: 2px;" \/>/g, "<hr />"] 491 468 ); 492 469 493 470 /** Cleanup HTML code 494 */471 */ 495 472 jsToolBar.prototype.tagsoup2xhtml = function(html) { 496 497 498 499 500 501 502 while ( /(<[^\/!]>|<[^\/!][^>]*[^\/]>)\s*<\/[^>]*[^-]>/.test(html)) {503 504 505 506 507 508 509 510 511 512 513 514 while (myRegexp.test(html)) {515 516 517 function (str, val1, val2, val3){518 519 return str.replace(tamponRegex, val2+'"'+val3+'"');520 521 ) 522 523 524 525 526 while (/(<[^>]+style=(["'])[^>]+[\s:]+)0(pt|px)(\2|\s|;)/.test(html)) {527 html = html.replace(/(<[^>]+style=(["'])[^>]+[\s:]+)0(pt|px)(\2|\s|;)/gi, "$1"+"0$4");528 529 530 531 532 html = html.replace(/\r\n/g,"\n");533 534 535 536 537 html = html.replace(/^\s+/gm,'');538 html = html.replace(/\s+$/gm,'');539 540 541 473 for (var reg in this.simpleCleanRegex) { 474 html = html.replace(this.simpleCleanRegex[reg][0], this.simpleCleanRegex[reg][1]); 475 } 476 /* tags vides */ 477 /* note : on tente de ne pas tenir compte des commentaires html, ceux-ci 478 permettent entre autre d'inserer des commentaires conditionnels pour ie */ 479 while (/(<[^\/!]>|<[^\/!][^>]*[^\/]>)\s*<\/[^>]*[^-]>/.test(html)) { 480 html = html.replace(/(<[^\/!]>|<[^\/!][^>]*[^\/]>)\s*<\/[^>]*[^-]>/g, ""); 481 } 482 483 /* tous les tags en minuscule */ 484 html = html.replace(/<(\/?)([A-Z0-9]+)/g, 485 function(match0, match1, match2) { 486 return "<" + match1 + match2.toLowerCase(); 487 }); 488 489 /* IE laisse souvent des attributs sans guillemets */ 490 var myRegexp = /<[^>]+((\s+\w+\s*=\s*)([^"'][\w~@+$,%\/:.#?=&;!*()-]*))[^>]*?>/; 491 while (myRegexp.test(html)) { 492 html = html.replace( 493 myRegexp, 494 function(str, val1, val2, val3) { 495 var tamponRegex = new RegExp(regexpEscape(val1)); 496 return str.replace(tamponRegex, val2 + '"' + val3 + '"'); 497 } 498 ); 499 } 500 501 /* les navigateurs rajoutent une unite aux longueurs css nulles */ 502 /* note: a ameliorer ! */ 503 while (/(<[^>]+style=(["'])[^>]+[\s:]+)0(pt|px)(\2|\s|;)/.test(html)) { 504 html = html.replace(/(<[^>]+style=(["'])[^>]+[\s:]+)0(pt|px)(\2|\s|;)/gi, "$1" + "0$4"); 505 } 506 507 /* correction des fins de lignes : le textarea edite contient des \n 508 * le wysiwyg des \r\n , et le textarea mis a jour SANS etre affiche des \r\n ! */ 509 html = html.replace(/\r\n/g, "\n"); 510 511 /* Trim only if there's no pre tag */ 512 var pattern_pre = /<pre>[\s\S]*<\/pre>/gi; 513 if (!pattern_pre.test(html)) { 514 html = html.replace(/^\s+/gm, ''); 515 html = html.replace(/\s+$/gm, ''); 516 } 517 518 return html; 542 519 }; 543 520 jsToolBar.prototype.validBlockquote = function() { 544 var blockElts = ['address','blockquote','dl','div','fieldset','form','h1', 545 'h2','h3','h4','h5','h6','hr','ol','p','pre','table','ul']; 546 var BQs = this.iwin.document.getElementsByTagName('blockquote'); 547 var bqChilds; 548 549 for (var bq = 0; bq < BQs.length; bq++) { 550 bqChilds = BQs[bq].childNodes; 551 var frag = this.iwin.document.createDocumentFragment(); 552 for (var i = (bqChilds.length-1); i >= 0; i--) { 553 if (bqChilds[i].nodeType == 1 && // Node.ELEMENT_NODE 554 arrayIndexOf(blockElts, bqChilds[i].tagName.toLowerCase()) >= 0) 555 { 556 if (frag.childNodes.length > 0) { 557 var p = this.iwin.document.createElement('p'); 558 p.appendChild(frag); 559 BQs[bq].replaceChild(p, bqChilds[i+1]); 560 frag = this.iwin.document.createDocumentFragment(); 561 } 562 } else { 563 if (frag.childNodes.length > 0) BQs[bq].removeChild(bqChilds[i+1]); 564 frag.insertBefore(bqChilds[i].cloneNode(true), frag.firstChild); 565 } 566 } 567 if (frag.childNodes.length > 0) { 568 var p = this.iwin.document.createElement('p'); 569 p.appendChild(frag); 570 BQs[bq].replaceChild(p, bqChilds[0]); 571 } 572 } 521 var blockElts = ['address', 'blockquote', 'dl', 'div', 'fieldset', 'form', 'h1', 522 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'ol', 'p', 'pre', 'table', 'ul' 523 ]; 524 var BQs = this.iwin.document.getElementsByTagName('blockquote'); 525 var bqChilds; 526 var p; 527 528 for (var bq = 0; bq < BQs.length; bq++) { 529 bqChilds = BQs[bq].childNodes; 530 var frag = this.iwin.document.createDocumentFragment(); 531 for (var i = (bqChilds.length - 1); i >= 0; i--) { 532 if (bqChilds[i].nodeType == 1 && // Node.ELEMENT_NODE 533 arrayIndexOf(blockElts, bqChilds[i].tagName.toLowerCase()) >= 0) { 534 if (frag.childNodes.length > 0) { 535 p = this.iwin.document.createElement('p'); 536 p.appendChild(frag); 537 BQs[bq].replaceChild(p, bqChilds[i + 1]); 538 frag = this.iwin.document.createDocumentFragment(); 539 } 540 } else { 541 if (frag.childNodes.length > 0) BQs[bq].removeChild(bqChilds[i + 1]); 542 frag.insertBefore(bqChilds[i].cloneNode(true), frag.firstChild); 543 } 544 } 545 if (frag.childNodes.length > 0) { 546 p = this.iwin.document.createElement('p'); 547 p.appendChild(frag); 548 BQs[bq].replaceChild(p, bqChilds[0]); 549 } 550 } 573 551 }; 574 552 575 553 /* Removing text formating */ 576 554 jsToolBar.prototype.removeFormatRegexp = new Array( 577 [/(<[a-z][^>]*)margin\s*:[^;]*;/mg, "$1"], 578 [/(<[a-z][^>]*)margin-bottom\s*:[^;]*;/mg, "$1"], 579 [/(<[a-z][^>]*)margin-left\s*:[^;]*;/mg, "$1"], 580 [/(<[a-z][^>]*)margin-right\s*:[^;]*;/mg, "$1"], 581 [/(<[a-z][^>]*)margin-top\s*:[^;]*;/mg, "$1"], 582 583 [/(<[a-z][^>]*)padding\s*:[^;]*;/mg, "$1"], 584 [/(<[a-z][^>]*)padding-bottom\s*:[^;]*;/mg, "$1"], 585 [/(<[a-z][^>]*)padding-left\s*:[^;]*;/mg, "$1"], 586 [/(<[a-z][^>]*)padding-right\s*:[^;]*;/mg, "$1"], 587 [/(<[a-z][^>]*)padding-top\s*:[^;]*;/mg, "$1"], 588 589 [/(<[a-z][^>]*)font\s*:[^;]*;/mg, "$1"], 590 [/(<[a-z][^>]*)font-family\s*:[^;]*;/mg, "$1"], 591 [/(<[a-z][^>]*)font-size\s*:[^;]*;/mg, "$1"], 592 [/(<[a-z][^>]*)font-style\s*:[^;]*;/mg, "$1"], 593 [/(<[a-z][^>]*)font-variant\s*:[^;]*;/mg, "$1"], 594 [/(<[a-z][^>]*)font-weight\s*:[^;]*;/mg, "$1"], 595 596 [/(<[a-z][^>]*)color\s*:[^;]*;/mg, "$1"] 555 [/(<[a-z][^>]*)margin\s*:[^;]*;/mg, "$1"], [/(<[a-z][^>]*)margin-bottom\s*:[^;]*;/mg, "$1"], [/(<[a-z][^>]*)margin-left\s*:[^;]*;/mg, "$1"], [/(<[a-z][^>]*)margin-right\s*:[^;]*;/mg, "$1"], [/(<[a-z][^>]*)margin-top\s*:[^;]*;/mg, "$1"], 556 557 [/(<[a-z][^>]*)padding\s*:[^;]*;/mg, "$1"], [/(<[a-z][^>]*)padding-bottom\s*:[^;]*;/mg, "$1"], [/(<[a-z][^>]*)padding-left\s*:[^;]*;/mg, "$1"], [/(<[a-z][^>]*)padding-right\s*:[^;]*;/mg, "$1"], [/(<[a-z][^>]*)padding-top\s*:[^;]*;/mg, "$1"], 558 559 [/(<[a-z][^>]*)font\s*:[^;]*;/mg, "$1"], [/(<[a-z][^>]*)font-family\s*:[^;]*;/mg, "$1"], [/(<[a-z][^>]*)font-size\s*:[^;]*;/mg, "$1"], [/(<[a-z][^>]*)font-style\s*:[^;]*;/mg, "$1"], [/(<[a-z][^>]*)font-variant\s*:[^;]*;/mg, "$1"], [/(<[a-z][^>]*)font-weight\s*:[^;]*;/mg, "$1"], 560 561 [/(<[a-z][^>]*)color\s*:[^;]*;/mg, "$1"] 597 562 ); 598 563 599 564 jsToolBar.prototype.removeTextFormating = function(html) { 600 601 602 603 604 605 html = html.replace(/style="\s*?"/mgi,'');606 565 for (var reg in this.removeFormatRegexp) { 566 html = html.replace(this.removeFormatRegexp[reg][0], this.removeFormatRegexp[reg][1]); 567 } 568 569 html = this.tagsoup2xhtml(html); 570 html = html.replace(/style="\s*?"/mgi, ''); 571 return html; 607 572 }; 608 573 … … 610 575 -------------------------------------------------------- */ 611 576 jsToolBar.prototype.elements.blocks.wysiwyg = { 612 list: ['none','p','h1','h2','h3','h4','h5','h6'], 613 fn: function(opt) { 614 if (opt=='none') { 615 var blockLevel = this.getBlockLevel(); 616 if (blockLevel !== null) { 617 this.replaceNodeByContent(blockLevel); 618 } 619 this.iwin.focus(); 620 } else { 621 try { this.iwin.document.execCommand('formatblock',false,'<'+opt+'>'); 622 } catch(e){}; 623 this.iwin.focus(); 624 } 625 } 577 list: ['none', 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'], 578 fn: function(opt) { 579 if (opt == 'none') { 580 var blockLevel = this.getBlockLevel(); 581 if (blockLevel !== null) { 582 this.replaceNodeByContent(blockLevel); 583 } 584 this.iwin.focus(); 585 } else { 586 try { 587 this.iwin.document.execCommand('formatblock', false, '<' + opt + '>'); 588 } catch (e) {} 589 this.iwin.focus(); 590 } 591 } 626 592 }; 627 593 628 594 jsToolBar.prototype.elements.strong.fn.wysiwyg = function() { 629 630 595 this.iwin.document.execCommand('bold', false, null); 596 this.iwin.focus(); 631 597 }; 632 598 633 599 jsToolBar.prototype.elements.em.fn.wysiwyg = function() { 634 635 600 this.iwin.document.execCommand('italic', false, null); 601 this.iwin.focus(); 636 602 }; 637 603 638 604 jsToolBar.prototype.elements.ins.fn.wysiwyg = function() { 639 640 605 this.iwin.document.execCommand('underline', false, null); 606 this.iwin.focus(); 641 607 }; 642 608 643 609 jsToolBar.prototype.elements.del.fn.wysiwyg = function() { 644 645 610 this.iwin.document.execCommand('strikethrough', false, null); 611 this.iwin.focus(); 646 612 }; 647 613 648 614 jsToolBar.prototype.elements.quote.fn.wysiwyg = function() { 649 650 651 652 615 var n = this.getSelectedNode(); 616 var q = this.iwin.document.createElement('q'); 617 q.appendChild(n); 618 this.insertNode(q); 653 619 }; 654 620 655 621 jsToolBar.prototype.elements.code.fn.wysiwyg = function() { 656 657 658 659 622 var n = this.getSelectedNode(); 623 var code = this.iwin.document.createElement('code'); 624 code.appendChild(n); 625 this.insertNode(code); 660 626 }; 661 627 662 628 jsToolBar.prototype.elements.mark.fn.wysiwyg = function() { 663 664 665 666 629 var n = this.getSelectedNode(); 630 var mark = this.iwin.document.createElement('mark'); 631 mark.appendChild(n); 632 this.insertNode(mark); 667 633 }; 668 634 669 635 jsToolBar.prototype.elements.br.fn.wysiwyg = function() { 670 671 636 var n = this.iwin.document.createElement('br'); 637 this.insertNode(n); 672 638 }; 673 639 674 640 jsToolBar.prototype.elements.blockquote.fn.wysiwyg = function() { 675 676 677 678 641 var n = this.getSelectedNode(); 642 var q = this.iwin.document.createElement('blockquote'); 643 q.appendChild(n); 644 this.insertNode(q); 679 645 }; 680 646 681 647 jsToolBar.prototype.elements.pre.fn.wysiwyg = function() { 682 this.iwin.document.execCommand('formatblock',false,'<pre>');683 648 this.iwin.document.execCommand('formatblock', false, '<pre>'); 649 this.iwin.focus(); 684 650 }; 685 651 686 652 jsToolBar.prototype.elements.ul.fn.wysiwyg = function() { 687 this.iwin.document.execCommand('insertunorderedlist',false,null);688 653 this.iwin.document.execCommand('insertunorderedlist', false, null); 654 this.iwin.focus(); 689 655 }; 690 656 691 657 jsToolBar.prototype.elements.ol.fn.wysiwyg = function() { 692 this.iwin.document.execCommand('insertorderedlist',false,null);693 658 this.iwin.document.execCommand('insertorderedlist', false, null); 659 this.iwin.focus(); 694 660 }; 695 661 696 662 jsToolBar.prototype.elements.link.fn.wysiwyg = function() { 697 var href, hreflang; 698 var range, commonAncestorContainer; 699 if (this.iwin.getSelection) { //gecko 700 var selection = this.iwin.getSelection(); 701 range = selection.getRangeAt(0); 702 commonAncestorContainer = range.commonAncestorContainer; 703 while (commonAncestorContainer.nodeType != 1) { 704 commonAncestorContainer = commonAncestorContainer.parentNode; 705 } 706 } else { //ie 707 range = this.iwin.document.selection.createRange(); 708 commonAncestorContainer = range.parentElement(); 709 } 710 711 var ancestorTagName = commonAncestorContainer.tagName.toLowerCase(); 712 while (ancestorTagName!='a' && ancestorTagName!='body') { 713 commonAncestorContainer = commonAncestorContainer.parentNode; 714 ancestorTagName = commonAncestorContainer.tagName.toLowerCase(); 715 } 716 717 // Update or remove link? 718 if (ancestorTagName == 'a') { 719 href = commonAncestorContainer.href || ''; 720 hreflang = commonAncestorContainer.hreflang || ''; 721 } 722 723 href = window.prompt(this.elements.link.href_prompt,href); 724 725 // Remove link 726 if (ancestorTagName == 'a' && href=='') { 727 this.replaceNodeByContent(commonAncestorContainer); 728 } 729 if (!href) return; // user cancel 730 731 hreflang = window.prompt(this.elements.link.hreflang_prompt, hreflang); 732 733 // Update link 734 if (ancestorTagName == 'a' && href) { 735 commonAncestorContainer.setAttribute('href', href); 736 if (hreflang) { 737 commonAncestorContainer.setAttribute('hreflang', hreflang); 738 } else { 739 commonAncestorContainer.removeAttribute('hreflang'); 740 } 741 return; 742 } 743 744 // Create link 745 var n = this.getSelectedNode(); 746 var a = this.iwin.document.createElement('a'); 747 a.href = href; 748 if (hreflang) a.setAttribute('hreflang',hreflang); 749 a.appendChild(n); 750 this.insertNode(a); 751 }; 752 753 663 var href, hreflang; 664 var range, commonAncestorContainer; 665 if (this.iwin.getSelection) { //gecko 666 var selection = this.iwin.getSelection(); 667 range = selection.getRangeAt(0); 668 commonAncestorContainer = range.commonAncestorContainer; 669 while (commonAncestorContainer.nodeType != 1) { 670 commonAncestorContainer = commonAncestorContainer.parentNode; 671 } 672 } else { //ie 673 range = this.iwin.document.selection.createRange(); 674 commonAncestorContainer = range.parentElement(); 675 } 676 677 var ancestorTagName = commonAncestorContainer.tagName.toLowerCase(); 678 while (ancestorTagName != 'a' && ancestorTagName != 'body') { 679 commonAncestorContainer = commonAncestorContainer.parentNode; 680 ancestorTagName = commonAncestorContainer.tagName.toLowerCase(); 681 } 682 683 // Update or remove link? 684 if (ancestorTagName == 'a') { 685 href = commonAncestorContainer.href || ''; 686 hreflang = commonAncestorContainer.hreflang || ''; 687 } 688 689 href = window.prompt(this.elements.link.href_prompt, href); 690 691 // Remove link 692 if (ancestorTagName == 'a' && href == '') { 693 this.replaceNodeByContent(commonAncestorContainer); 694 } 695 if (!href) return; // user cancel 696 697 hreflang = window.prompt(this.elements.link.hreflang_prompt, hreflang); 698 699 // Update link 700 if (ancestorTagName == 'a' && href) { 701 commonAncestorContainer.setAttribute('href', href); 702 if (hreflang) { 703 commonAncestorContainer.setAttribute('hreflang', hreflang); 704 } else { 705 commonAncestorContainer.removeAttribute('hreflang'); 706 } 707 return; 708 } 709 710 // Create link 711 var n = this.getSelectedNode(); 712 var a = this.iwin.document.createElement('a'); 713 a.href = href; 714 if (hreflang) a.setAttribute('hreflang', hreflang); 715 a.appendChild(n); 716 this.insertNode(a); 717 }; 754 718 755 719 // Remove format and Toggle 756 720 jsToolBar.prototype.elements.removeFormat = { 757 758 759 721 type: 'button', 722 title: 'Remove text formating', 723 fn: {} 760 724 }; 761 725 jsToolBar.prototype.elements.removeFormat.disabled = !jsToolBar.prototype.can_wwg; 762 726 jsToolBar.prototype.elements.removeFormat.fn.xhtml = function() { 763 764 765 727 var html = this.textarea.value; 728 html = this.removeTextFormating(html); 729 this.textarea.value = html; 766 730 }; 767 731 jsToolBar.prototype.elements.removeFormat.fn.wysiwyg = function() { 768 769 770 732 var html = this.iwin.document.body.innerHTML; 733 html = this.removeTextFormating(html); 734 this.iwin.document.body.innerHTML = html; 771 735 }; 772 736 /** Utilities 773 737 -------------------------------------------------------- */ 774 function arrayIndexOf(aArray, aValue){ 775 if (typeof Array.indexOf == 'function') { 776 return aArray.indexOf(aValue); 777 } else { 778 var index = -1; 779 var l = aArray.length; 780 for (var i = 0; i < l ; i++) { 781 if (aArray[i] === aValue) { 782 index = i; 783 break; 784 } 785 } 786 return index; 787 } 788 }; 738 function arrayIndexOf(aArray, aValue) { 739 if (typeof Array.indexOf == 'function') { 740 return aArray.indexOf(aValue); 741 } else { 742 var index = -1; 743 var l = aArray.length; 744 for (var i = 0; i < l; i++) { 745 if (aArray[i] === aValue) { 746 index = i; 747 break; 748 } 749 } 750 return index; 751 } 752 } 753 789 754 function addEvent(obj, evType, fn, useCapture) { 790 if (obj.addEventListener){ 791 obj.addEventListener(evType, fn, useCapture); 792 return true; 793 } else if (obj.attachEvent){ 794 var r = obj.attachEvent("on"+evType, fn); 795 return r; 796 } else { 797 return false; 798 } 799 }; 755 if (obj.addEventListener) { 756 obj.addEventListener(evType, fn, useCapture); 757 return true; 758 } else if (obj.attachEvent) { 759 var r = obj.attachEvent("on" + evType, fn); 760 return r; 761 } else { 762 return false; 763 } 764 } 765 800 766 function removeEvent(obj, evType, fn, useCapture) { 801 if (obj.removeEventListener){ 802 obj.removeEventListener(evType, fn, useCapture); 803 return true; 804 } else if (obj.detachEvent){ 805 var r = obj.detachEvent("on"+evType, fn); 806 return r; 807 } else { 808 return false; 809 } 810 }; 767 if (obj.removeEventListener) { 768 obj.removeEventListener(evType, fn, useCapture); 769 return true; 770 } else if (obj.detachEvent) { 771 var r = obj.detachEvent("on" + evType, fn); 772 return r; 773 } else { 774 return false; 775 } 776 } 777 811 778 function regexpEscape(s) { 812 return s.replace(/([\\\^\$*+[\]?{}.=!:(|)])/g,"\\$1") 813 } ;779 return s.replace(/([\\\^\$*+[\]?{}.=!:(|)])/g, "\\$1"); 780 } -
plugins/dcLegacyEditor/js/jsToolBar/popup_link.js
r2614 r3735 1 /*global $ */ 2 'use strict'; 3 1 4 $(function() { 2 5 $('#link-insert-cancel').click(function() { -
plugins/dcLegacyEditor/js/jsToolBar/popup_posts.js
r2614 r3735 1 /*global $ */ 2 'use strict'; 3 1 4 $(function() { 2 5 $('#link-insert-cancel').click(function() {
Note: See TracChangeset
for help on using the changeset viewer.