[ Index ] |
|
Code source de WordPress 2.1.2 |
1 /** 2 * $RCSfile: editor_plugin_src.js,v $ 3 * $Revision: 1.4 $ 4 * $Date: 2006/03/24 17:24:50 $ 5 * 6 * @author Moxiecode 7 * @copyright Copyright © 2004-2006, Moxiecode Systems AB, All rights reserved. 8 */ 9 10 tinyMCE.importPluginLanguagePack('spellchecker', 'en,sv,nn,nb'); 11 12 // Plucin static class 13 var TinyMCE_SpellCheckerPlugin = { 14 _contextMenu : new TinyMCE_Menu(), 15 _menu : new TinyMCE_Menu(), 16 _counter : 0, 17 18 getInfo : function() { 19 return { 20 longname : 'Spellchecker', 21 author : 'Moxiecode Systems AB', 22 authorurl : 'http://tinymce.moxiecode.com', 23 infourl : 'http://tinymce.moxiecode.com/tinymce/docs/plugin_spellchecker.html', 24 version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion 25 }; 26 }, 27 28 handleEvent : function(e) { 29 var elm = tinyMCE.isMSIE ? e.srcElement : e.target; 30 var inst = tinyMCE.selectedInstance, args = ''; 31 var self = TinyMCE_SpellCheckerPlugin; 32 var cm = self._contextMenu; 33 var p, p2, x, y, sx, sy, h, elm; 34 35 // Handle click on word 36 if ((e.type == "click" || e.type == "contextmenu") && elm) { 37 do { 38 if (tinyMCE.getAttrib(elm, 'class') == "mceItemHiddenSpellWord") { 39 inst.spellCheckerElm = elm; 40 41 // Setup arguments 42 args += 'id=' + inst.editorId + "|" + (++self._counter); 43 args += '&cmd=suggest&check=' + escape(elm.innerHTML); 44 args += '&lang=' + escape(inst.spellCheckerLang); 45 46 elm = inst.spellCheckerElm; 47 p = tinyMCE.getAbsPosition(inst.iframeElement); 48 p2 = tinyMCE.getAbsPosition(elm); 49 h = parseInt(elm.offsetHeight); 50 sx = inst.getBody().scrollLeft; 51 sy = inst.getBody().scrollTop; 52 x = p.absLeft + p2.absLeft - sx; 53 y = p.absTop + p2.absTop - sy + h; 54 55 cm.clear(); 56 cm.addTitle(tinyMCE.getLang('lang_spellchecker_wait', '', true)); 57 cm.show(); 58 cm.moveTo(x, y); 59 60 inst.selection.selectNode(elm, false, false); 61 62 self._sendAjax(self.baseURL + "/tinyspell.php", self._ajaxResponse, 'post', args); 63 64 tinyMCE.cancelEvent(e); 65 return false; 66 } 67 } while ((elm = elm.parentNode)); 68 } 69 70 return true; 71 }, 72 73 initInstance : function(inst) { 74 var self = TinyMCE_SpellCheckerPlugin, m = self._menu, cm = self._contextMenu, e; 75 76 tinyMCE.importCSS(inst.getDoc(), tinyMCE.baseURL + "/plugins/spellchecker/css/content.css"); 77 78 if (!tinyMCE.hasMenu('spellcheckercontextmenu')) { 79 tinyMCE.importCSS(document, tinyMCE.baseURL + "/plugins/spellchecker/css/spellchecker.css"); 80 81 cm.init({drop_menu : false}); 82 tinyMCE.addMenu('spellcheckercontextmenu', cm); 83 } 84 85 if (!tinyMCE.hasMenu('spellcheckermenu')) { 86 m.init({}); 87 tinyMCE.addMenu('spellcheckermenu', m); 88 } 89 90 inst.spellCheckerLang = 'en'; 91 self._buildSettingsMenu(inst, null); 92 93 e = self._getBlockBoxLayer(inst).create('div', 'mceBlockBox', document.getElementById(inst.editorId + '_parent')); 94 self._getMsgBoxLayer(inst).create('div', 'mceMsgBox', document.getElementById(inst.editorId + '_parent')); 95 }, 96 97 _getMsgBoxLayer : function(inst) { 98 if (!inst.spellCheckerMsgBoxL) 99 inst.spellCheckerMsgBoxL = new TinyMCE_Layer(inst.editorId + '_spellcheckerMsgBox', false); 100 101 return inst.spellCheckerMsgBoxL; 102 }, 103 104 _getBlockBoxLayer : function(inst) { 105 if (!inst.spellCheckerBoxL) 106 inst.spellCheckerBoxL = new TinyMCE_Layer(inst.editorId + '_spellcheckerBlockBox', false); 107 108 return inst.spellCheckerBoxL; 109 }, 110 111 _buildSettingsMenu : function(inst, lang) { 112 var i, ar = tinyMCE.getParam('spellchecker_languages', '+English=en').split(','), p; 113 var self = TinyMCE_SpellCheckerPlugin, m = self._menu, c; 114 115 m.clear(); 116 m.addTitle(tinyMCE.getLang('lang_spellchecker_langs', '', true)); 117 118 for (i=0; i<ar.length; i++) { 119 if (ar[i] != '') { 120 p = ar[i].split('='); 121 c = 'mceMenuCheckItem'; 122 123 if (p[0].charAt(0) == '+') { 124 p[0] = p[0].substring(1); 125 126 if (lang == null) { 127 c = 'mceMenuSelectedItem'; 128 inst.spellCheckerLang = p[1]; 129 } 130 } 131 132 if (lang == p[1]) 133 c = 'mceMenuSelectedItem'; 134 135 m.add({text : p[0], js : "tinyMCE.execInstanceCommand('" + inst.editorId + "','mceSpellCheckerSetLang',false,'" + p[1] + "');", class_name : c}); 136 } 137 } 138 }, 139 140 setupContent : function(editor_id, body, doc) { 141 TinyMCE_SpellCheckerPlugin._removeWords(doc); 142 }, 143 144 getControlHTML : function(cn) { 145 switch (cn) { 146 case "spellchecker": 147 return TinyMCE_SpellCheckerPlugin._getMenuButtonHTML(cn, 'lang_spellchecker_desc', '{$pluginurl}/images/spellchecker.gif', 'lang_spellchecker_desc', 'mceSpellCheckerMenu', 'mceSpellCheck'); 148 } 149 150 return ""; 151 }, 152 153 /** 154 * Returns the HTML code for a normal button control. 155 * 156 * @param {string} id Button control id, this will be the suffix for the element id, the prefix is the editor id. 157 * @param {string} lang Language variable key name to insert as the title/alt of the button image. 158 * @param {string} img Image URL to insert, {$themeurl} and {$pluginurl} will be replaced. 159 * @param {string} mlang Language variable key name to insert as the title/alt of the menu button image. 160 * @param {string} mid Menu by id to display when the menu button is pressed. 161 * @param {string} cmd Command to execute when the user clicks the button. 162 * @param {string} ui Optional user interface boolean for command. 163 * @param {string} val Optional value for command. 164 * @return HTML code for a normal button based in input information. 165 * @type string 166 */ 167 _getMenuButtonHTML : function(id, lang, img, mlang, mid, cmd, ui, val) { 168 var h = '', m, x; 169 170 cmd = 'tinyMCE.hideMenus();tinyMCE.execInstanceCommand(\'{$editor_id}\',\'' + cmd + '\''; 171 172 if (typeof(ui) != "undefined" && ui != null) 173 cmd += ',' + ui; 174 175 if (typeof(val) != "undefined" && val != null) 176 cmd += ",'" + val + "'"; 177 178 cmd += ');'; 179 180 // Use tilemaps when enabled and found and never in MSIE since it loads the tile each time from cache if cahce is disabled 181 if (tinyMCE.getParam('button_tile_map') && (!tinyMCE.isMSIE || tinyMCE.isOpera) && (m = tinyMCE.buttonMap[id]) != null && (tinyMCE.getParam("language") == "en" || img.indexOf('$lang') == -1)) { 182 // Tiled button 183 x = 0 - (m * 20) == 0 ? '0' : 0 - (m * 20); 184 h += '<a id="{$editor_id}_' + id + '" href="javascript:' + cmd + '" onclick="' + cmd + 'return false;" onmousedown="return false;" class="mceTiledButton mceButtonNormal" target="_self">'; 185 h += '<img src="{$themeurl}/images/spacer.gif" style="background-position: ' + x + 'px 0" title="{$' + lang + '}" />'; 186 h += '<img src="{$themeurl}/images/button_menu.gif" title="{$' + lang + '}" class="mceMenuButton" onclick="' + mcmd + 'return false;" />'; 187 h += '</a>'; 188 } else { 189 if (tinyMCE.isMSIE && !tinyMCE.isOpera) 190 h += '<span id="{$editor_id}_' + id + '" class="mceMenuButton" onmouseover="tinyMCE.plugins.spellchecker._menuButtonEvent(\'over\',this);" onmouseout="tinyMCE.plugins.spellchecker._menuButtonEvent(\'out\',this);">'; 191 else 192 h += '<span id="{$editor_id}_' + id + '" class="mceMenuButton">'; 193 194 h += '<a href="javascript:' + cmd + '" onclick="' + cmd + 'return false;" onmousedown="return false;" class="mceMenuButtonNormal" target="_self">'; 195 h += '<img src="' + img + '" title="{$' + lang + '}" /></a>'; 196 h += '<a href="#" onclick="tinyMCE.plugins.spellchecker._toggleMenu(\'{$editor_id}\',\'' + mid + '\');return false;" onmousedown="return false;"><img src="{$themeurl}/images/button_menu.gif" title="{$' + lang + '}" class="mceMenuButton" />'; 197 h += '</a></span>'; 198 } 199 200 return h; 201 }, 202 203 _menuButtonEvent : function(e, o) { 204 if (o.className == 'mceMenuButtonFocus') 205 return; 206 207 if (e == 'over') 208 o.className = o.className + ' mceMenuHover'; 209 else 210 o.className = o.className.replace(/\s.*$/, ''); 211 }, 212 213 _toggleMenu : function(editor_id, id) { 214 var self = TinyMCE_SpellCheckerPlugin; 215 var e = document.getElementById(editor_id + '_spellchecker'); 216 var inst = tinyMCE.getInstanceById(editor_id); 217 218 if (self._menu.isVisible()) { 219 tinyMCE.hideMenus(); 220 return; 221 } 222 223 tinyMCE.lastMenuBtnClass = e.className.replace(/\s.*$/, ''); 224 tinyMCE.switchClass(editor_id + '_spellchecker', 'mceMenuButtonFocus'); 225 226 self._menu.moveRelativeTo(e, 'bl'); 227 self._menu.moveBy(tinyMCE.isMSIE && !tinyMCE.isOpera ? 0 : 1, -1); 228 229 if (tinyMCE.isOpera) 230 self._menu.moveBy(0, -2); 231 232 self._onMenuEvent(inst, self._menu, 'show'); 233 234 self._menu.show(); 235 236 tinyMCE.lastSelectedMenuBtn = editor_id + '_spellchecker'; 237 }, 238 239 _onMenuEvent : function(inst, m, n) { 240 TinyMCE_SpellCheckerPlugin._buildSettingsMenu(inst, inst.spellCheckerLang); 241 }, 242 243 execCommand : function(editor_id, element, command, user_interface, value) { 244 var inst = tinyMCE.getInstanceById(editor_id), self = TinyMCE_SpellCheckerPlugin, args = '', co, bb, mb, nl, i, e; 245 246 // Handle commands 247 switch (command) { 248 case "mceSpellCheck": 249 if (!inst.spellcheckerOn) { 250 inst.spellCheckerBookmark = inst.selection.getBookmark(); 251 252 // Setup arguments 253 args += 'id=' + inst.editorId + "|" + (++self._counter); 254 args += '&cmd=spell&check=' + escape(self._getWordList(inst.getBody())).replace(/%20/g, '+'); 255 args += '&lang=' + escape(inst.spellCheckerLang); 256 257 co = document.getElementById(inst.editorId + '_parent').firstChild; 258 bb = self._getBlockBoxLayer(inst); 259 bb.moveRelativeTo(co, 'tl'); 260 bb.resizeTo(co.offsetWidth, co.offsetHeight); 261 bb.show(); 262 263 // Setup message box 264 mb = self._getMsgBoxLayer(inst); 265 e = mb.getElement(); 266 e.innerHTML = '<span>' + tinyMCE.getLang('lang_spellchecker_swait', '', true) + '</span>'; 267 mb.show(); 268 mb.moveRelativeTo(co, 'cc'); 269 270 if (tinyMCE.isMSIE && !tinyMCE.isOpera) { 271 nl = co.getElementsByTagName('select'); 272 for (i=0; i<nl.length; i++) 273 nl[i].disabled = true; 274 } 275 276 inst.spellcheckerOn = true; 277 tinyMCE.switchClass(editor_id + '_spellchecker', 'mceMenuButtonSelected'); 278 279 self._sendAjax(self.baseURL + "/tinyspell.php", self._ajaxResponse, 'post', args); 280 } else { 281 self._removeWords(inst.getDoc()); 282 inst.spellcheckerOn = false; 283 tinyMCE.switchClass(editor_id + '_spellchecker', 'mceMenuButton'); 284 } 285 286 return true; 287 288 case "mceSpellCheckReplace": 289 if (inst.spellCheckerElm) 290 tinyMCE.setOuterHTML(inst.spellCheckerElm, value); 291 292 self._checkDone(inst); 293 self._contextMenu.hide(); 294 self._menu.hide(); 295 296 return true; 297 298 case "mceSpellCheckIgnore": 299 if (inst.spellCheckerElm) 300 self._removeWord(inst.spellCheckerElm); 301 302 self._checkDone(inst); 303 self._contextMenu.hide(); 304 self._menu.hide(); 305 return true; 306 307 case "mceSpellCheckIgnoreAll": 308 if (inst.spellCheckerElm) 309 self._removeWords(inst.getDoc(), inst.spellCheckerElm.innerHTML); 310 311 self._checkDone(inst); 312 self._contextMenu.hide(); 313 self._menu.hide(); 314 return true; 315 316 case "mceSpellCheckerSetLang": 317 tinyMCE.hideMenus(); 318 inst.spellCheckerLang = value; 319 self._removeWords(inst.getDoc()); 320 inst.spellcheckerOn = false; 321 tinyMCE.switchClass(editor_id + '_spellchecker', 'mceMenuButton'); 322 return true; 323 } 324 325 // Pass to next handler in chain 326 return false; 327 }, 328 329 cleanup : function(type, content, inst) { 330 switch (type) { 331 case "get_from_editor_dom": 332 TinyMCE_SpellCheckerPlugin._removeWords(content); 333 inst.spellcheckerOn = false; 334 break; 335 } 336 337 return content; 338 }, 339 340 // Private plugin specific methods 341 342 _displayUI : function(inst) { 343 var self = TinyMCE_SpellCheckerPlugin; 344 var bb = self._getBlockBoxLayer(inst); 345 var mb = self._getMsgBoxLayer(inst); 346 var nl, i; 347 var co = document.getElementById(inst.editorId + '_parent').firstChild; 348 349 if (tinyMCE.isMSIE && !tinyMCE.isOpera) { 350 nl = co.getElementsByTagName('select'); 351 for (i=0; i<nl.length; i++) 352 nl[i].disabled = false; 353 } 354 355 bb.hide(); 356 mb.hide(); 357 }, 358 359 _ajaxResponse : function(xml) { 360 var el = xml ? xml.documentElement : null; 361 var inst = tinyMCE.selectedInstance, self = TinyMCE_SpellCheckerPlugin; 362 var cmd = el ? el.getAttribute("cmd") : null, err, id = el ? el.getAttribute("id") : null; 363 364 if (id) 365 inst = tinyMCE.getInstanceById(id.substring(0, id.indexOf('|'))); 366 367 self._displayUI(inst); 368 369 // Ignore suggestions for other ajax responses 370 if (cmd == "suggest" && id != inst.editorId + "|" + self._counter) 371 return; 372 373 if (!el) { 374 inst.spellcheckerOn = false; 375 tinyMCE.switchClass(inst.editorId + '_spellchecker', 'mceMenuButton'); 376 alert("Could not execute AJAX call, server didn't return valid a XML."); 377 return; 378 } 379 380 err = el.getAttribute("error"); 381 382 if (err == "true") { 383 inst.spellcheckerOn = false; 384 tinyMCE.switchClass(inst.editorId + '_spellchecker', 'mceMenuButton'); 385 alert(el.getAttribute("msg")); 386 return; 387 } 388 389 switch (cmd) { 390 case "spell": 391 if (xml.documentElement.firstChild) { 392 self._markWords(inst.getDoc(), inst.getBody(), el.firstChild.nodeValue.split(' ')); 393 inst.selection.moveToBookmark(inst.spellCheckerBookmark); 394 } else 395 alert(tinyMCE.getLang('lang_spellchecker_no_mpell', '', true)); 396 397 self._checkDone(inst); 398 399 break; 400 401 case "suggest": 402 self._buildMenu(el.firstChild ? el.firstChild.nodeValue.split(' ') : null, 10); 403 self._contextMenu.show(); 404 break; 405 } 406 }, 407 408 _getWordSeparators : function() { 409 var i, re = '', ch = tinyMCE.getParam('spellchecker_word_separator_chars', '\\s!"#$%&()*+,-./:;<=>?@[\]^_{|}§©«®±¶·¸»¼½¾¿×÷¤\u201d\u201c'); 410 411 for (i=0; i<ch.length; i++) 412 re += '\\' + ch.charAt(i); 413 414 return re; 415 }, 416 417 _getWordList : function(n) { 418 var i, x, s, nv = '', nl = tinyMCE.getNodeTree(n, new Array(), 3), wl = new Array(); 419 var re = TinyMCE_SpellCheckerPlugin._getWordSeparators(); 420 421 for (i=0; i<nl.length; i++) 422 nv += nl[i].nodeValue + " "; 423 424 nv = nv.replace(new RegExp('([0-9]|[' + re + '])', 'g'), ' '); 425 nv = tinyMCE.trim(nv.replace(/(\s+)/g, ' ')); 426 427 nl = nv.split(/\s+/); 428 for (i=0; i<nl.length; i++) { 429 s = false; 430 for (x=0; x<wl.length; x++) { 431 if (wl[x] == nl[i]) { 432 s = true; 433 break; 434 } 435 } 436 437 if (!s) 438 wl[wl.length] = nl[i]; 439 } 440 441 return wl.join(' '); 442 }, 443 444 _removeWords : function(doc, word) { 445 var i, c, nl = doc.getElementsByTagName("span"); 446 var self = TinyMCE_SpellCheckerPlugin; 447 var inst = tinyMCE.selectedInstance, b = inst ? inst.selection.getBookmark() : null; 448 449 word = typeof(word) == 'undefined' ? null : word; 450 451 for (i=nl.length-1; i>=0; i--) { 452 c = tinyMCE.getAttrib(nl[i], 'class'); 453 454 if ((c == 'mceItemHiddenSpellWord' || c == 'mceItemHidden') && (word == null || nl[i].innerHTML == word)) 455 self._removeWord(nl[i]); 456 } 457 458 if (b) 459 inst.selection.moveToBookmark(b); 460 }, 461 462 _checkDone : function(inst) { 463 var i, w = 0, nl = inst.getDoc().getElementsByTagName("span") 464 var self = TinyMCE_SpellCheckerPlugin; 465 466 for (i=nl.length-1; i>=0; i--) { 467 c = tinyMCE.getAttrib(nl[i], 'class'); 468 469 if (c == 'mceItemHiddenSpellWord') 470 w++; 471 } 472 473 if (w == 0) { 474 self._removeWords(inst.getDoc()); 475 inst.spellcheckerOn = false; 476 tinyMCE.switchClass(inst.editorId + '_spellchecker', 'mceMenuButton'); 477 } 478 }, 479 480 _removeWord : function(e) { 481 tinyMCE.setOuterHTML(e, e.innerHTML); 482 }, 483 484 _markWords : function(doc, n, wl) { 485 var i, nv, nn, nl = tinyMCE.getNodeTree(n, new Array(), 3); 486 var r1, r2, r3, r4, r5, w = ''; 487 var re = TinyMCE_SpellCheckerPlugin._getWordSeparators(); 488 489 for (i=0; i<wl.length; i++) 490 w += wl[i] + ((i == wl.length-1) ? '' : '|'); 491 492 r1 = new RegExp('([' + re + '])(' + w + ')([' + re + '])', 'g'); 493 r2 = new RegExp('^(' + w + ')', 'g'); 494 r3 = new RegExp('(' + w + ')([' + re + ']?)$', 'g'); 495 r4 = new RegExp('^(' + w + ')([' + re + ']?)$', 'g'); 496 r5 = new RegExp('(' + w + ')([' + re + '])', 'g'); 497 498 for (i=0; i<nl.length; i++) { 499 nv = nl[i].nodeValue; 500 if (r1.test(nv) || r2.test(nv) || r3.test(nv) || r4.test(nv)) { 501 nv = tinyMCE.xmlEncode(nv); 502 nv = nv.replace(r5, '<span class="mceItemHiddenSpellWord">$1</span>$2'); 503 nv = nv.replace(r3, '<span class="mceItemHiddenSpellWord">$1</span>$2'); 504 505 nn = doc.createElement('span'); 506 nn.className = "mceItemHidden"; 507 nn.innerHTML = nv; 508 509 // Remove old text node 510 nl[i].parentNode.replaceChild(nn, nl[i]); 511 } 512 } 513 }, 514 515 _buildMenu : function(sg, max) { 516 var i, self = TinyMCE_SpellCheckerPlugin, cm = self._contextMenu; 517 518 cm.clear(); 519 520 if (sg != null) { 521 cm.addTitle(tinyMCE.getLang('lang_spellchecker_sug', '', true)); 522 523 for (i=0; i<sg.length && i<max; i++) 524 cm.addItem(sg[i], 'tinyMCE.execCommand("mceSpellCheckReplace",false,"' + sg[i] + '");'); 525 526 cm.addSeparator(); 527 cm.addItem(tinyMCE.getLang('lang_spellchecker_ignore_word', '', true), 'tinyMCE.execCommand(\'mceSpellCheckIgnore\');'); 528 cm.addItem(tinyMCE.getLang('lang_spellchecker_ignore_words', '', true), 'tinyMCE.execCommand(\'mceSpellCheckIgnoreAll\');'); 529 } else 530 cm.addTitle(tinyMCE.getLang('lang_spellchecker_no_sug', '', true)); 531 532 cm.update(); 533 }, 534 535 _getAjaxHTTP : function() { 536 try { 537 return new ActiveXObject('Msxml2.XMLHTTP') 538 } catch (e) { 539 try { 540 return new ActiveXObject('Microsoft.XMLHTTP') 541 } catch (e) { 542 return new XMLHttpRequest(); 543 } 544 } 545 }, 546 547 /** 548 * Perform AJAX call. 549 * 550 * @param {string} u URL of AJAX service. 551 * @param {function} f Function to call when response arrives. 552 * @param {string} m Request method post or get. 553 * @param {Array} a Array with arguments to send. 554 */ 555 _sendAjax : function(u, f, m, a) { 556 var x = TinyMCE_SpellCheckerPlugin._getAjaxHTTP(); 557 558 x.open(m, u, true); 559 560 x.onreadystatechange = function() { 561 if (x.readyState == 4) 562 f(x.responseXML); 563 }; 564 565 if (m == 'post') 566 x.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); 567 568 x.send(a); 569 } 570 }; 571 572 // Register plugin 573 tinyMCE.addPlugin('spellchecker', TinyMCE_SpellCheckerPlugin);
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 30 19:41:27 2007 | par Balluche grâce à PHPXref 0.7 |