2 * $Id: editor_plugin_src.js 28 2006-08-01 16:02:56Z spocke $
\r
5 * @copyright Copyright © 2004-2006, Moxiecode Systems AB, All rights reserved.
\r
8 tinyMCE.importPluginLanguagePack('spellchecker', 'en,fr,sv,nn,nb');
\r
10 // Plucin static class
\r
11 var TinyMCE_SpellCheckerPlugin = {
\r
12 _contextMenu : new TinyMCE_Menu(),
\r
13 _menu : new TinyMCE_Menu(),
\r
15 _ajaxPage : '/tinyspell.php',
\r
17 getInfo : function() {
\r
19 longname : 'Spellchecker',
\r
20 author : 'Moxiecode Systems AB',
\r
21 authorurl : 'http://tinymce.moxiecode.com',
\r
22 infourl : 'http://tinymce.moxiecode.com/tinymce/docs/plugin_spellchecker.html',
\r
23 version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion
\r
27 handleEvent : function(e) {
\r
28 var elm = tinyMCE.isMSIE ? e.srcElement : e.target;
\r
29 var inst = tinyMCE.selectedInstance, args = '';
\r
30 var self = TinyMCE_SpellCheckerPlugin;
\r
31 var cm = self._contextMenu;
\r
32 var p, p2, x, y, sx, sy, h, elm;
\r
34 // Handle click on word
\r
35 if ((e.type == "click" || e.type == "contextmenu") && elm) {
\r
37 if (tinyMCE.getAttrib(elm, 'class') == "mceItemHiddenSpellWord") {
\r
38 inst.spellCheckerElm = elm;
\r
41 args += 'id=' + inst.editorId + "|" + (++self._counter);
\r
42 args += '&cmd=suggest&check=' + encodeURIComponent(elm.innerHTML);
\r
43 args += '&lang=' + escape(inst.spellCheckerLang);
\r
45 elm = inst.spellCheckerElm;
\r
46 p = tinyMCE.getAbsPosition(inst.iframeElement);
\r
47 p2 = tinyMCE.getAbsPosition(elm);
\r
48 h = parseInt(elm.offsetHeight);
\r
49 sx = inst.getBody().scrollLeft;
\r
50 sy = inst.getBody().scrollTop;
\r
51 x = p.absLeft + p2.absLeft - sx;
\r
52 y = p.absTop + p2.absTop - sy + h;
\r
55 cm.addTitle(tinyMCE.getLang('lang_spellchecker_wait', '', true));
\r
59 inst.selection.selectNode(elm, false, false);
\r
61 self._sendAjax(self.baseURL + self._ajaxPage, self._ajaxResponse, 'post', args);
\r
63 tinyMCE.cancelEvent(e);
\r
66 } while ((elm = elm.parentNode));
\r
72 initInstance : function(inst) {
\r
73 var self = TinyMCE_SpellCheckerPlugin, m = self._menu, cm = self._contextMenu, e;
\r
75 tinyMCE.importCSS(inst.getDoc(), tinyMCE.baseURL + "/plugins/spellchecker/css/content.css");
\r
77 if (!tinyMCE.hasMenu('spellcheckercontextmenu')) {
\r
78 tinyMCE.importCSS(document, tinyMCE.baseURL + "/plugins/spellchecker/css/spellchecker.css");
\r
80 cm.init({drop_menu : false});
\r
81 tinyMCE.addMenu('spellcheckercontextmenu', cm);
\r
84 if (!tinyMCE.hasMenu('spellcheckermenu')) {
\r
86 tinyMCE.addMenu('spellcheckermenu', m);
\r
89 inst.spellCheckerLang = 'en';
\r
90 self._buildSettingsMenu(inst, null);
\r
92 e = self._getBlockBoxLayer(inst).create('div', 'mceBlockBox', document.getElementById(inst.editorId + '_parent'));
\r
93 self._getMsgBoxLayer(inst).create('div', 'mceMsgBox', document.getElementById(inst.editorId + '_parent'));
\r
96 _getMsgBoxLayer : function(inst) {
\r
97 if (!inst.spellCheckerMsgBoxL)
\r
98 inst.spellCheckerMsgBoxL = new TinyMCE_Layer(inst.editorId + '_spellcheckerMsgBox', false);
\r
100 return inst.spellCheckerMsgBoxL;
\r
103 _getBlockBoxLayer : function(inst) {
\r
104 if (!inst.spellCheckerBoxL)
\r
105 inst.spellCheckerBoxL = new TinyMCE_Layer(inst.editorId + '_spellcheckerBlockBox', false);
\r
107 return inst.spellCheckerBoxL;
\r
110 _buildSettingsMenu : function(inst, lang) {
\r
111 var i, ar = tinyMCE.getParam('spellchecker_languages', '+English=en').split(','), p;
\r
112 var self = TinyMCE_SpellCheckerPlugin, m = self._menu, c;
\r
115 m.addTitle(tinyMCE.getLang('lang_spellchecker_langs', '', true));
\r
117 for (i=0; i<ar.length; i++) {
\r
119 p = ar[i].split('=');
\r
120 c = 'mceMenuCheckItem';
\r
122 if (p[0].charAt(0) == '+') {
\r
123 p[0] = p[0].substring(1);
\r
125 if (lang == null) {
\r
126 c = 'mceMenuSelectedItem';
\r
127 inst.spellCheckerLang = p[1];
\r
132 c = 'mceMenuSelectedItem';
\r
134 m.add({text : p[0], js : "tinyMCE.execInstanceCommand('" + inst.editorId + "','mceSpellCheckerSetLang',false,'" + p[1] + "');", class_name : c});
\r
139 setupContent : function(editor_id, body, doc) {
\r
140 TinyMCE_SpellCheckerPlugin._removeWords(doc);
\r
143 getControlHTML : function(cn) {
\r
145 case "spellchecker":
\r
146 return TinyMCE_SpellCheckerPlugin._getMenuButtonHTML(cn, 'lang_spellchecker_desc', '{$pluginurl}/images/spellchecker.gif', 'lang_spellchecker_desc', 'mceSpellCheckerMenu', 'mceSpellCheck');
\r
153 * Returns the HTML code for a normal button control.
\r
155 * @param {string} id Button control id, this will be the suffix for the element id, the prefix is the editor id.
\r
156 * @param {string} lang Language variable key name to insert as the title/alt of the button image.
\r
157 * @param {string} img Image URL to insert, {$themeurl} and {$pluginurl} will be replaced.
\r
158 * @param {string} mlang Language variable key name to insert as the title/alt of the menu button image.
\r
159 * @param {string} mid Menu by id to display when the menu button is pressed.
\r
160 * @param {string} cmd Command to execute when the user clicks the button.
\r
161 * @param {string} ui Optional user interface boolean for command.
\r
162 * @param {string} val Optional value for command.
\r
163 * @return HTML code for a normal button based in input information.
\r
166 _getMenuButtonHTML : function(id, lang, img, mlang, mid, cmd, ui, val) {
\r
169 cmd = 'tinyMCE.hideMenus();tinyMCE.execInstanceCommand(\'{$editor_id}\',\'' + cmd + '\'';
\r
171 if (typeof(ui) != "undefined" && ui != null)
\r
174 if (typeof(val) != "undefined" && val != null)
\r
175 cmd += ",'" + val + "'";
\r
179 // Use tilemaps when enabled and found and never in MSIE since it loads the tile each time from cache if cahce is disabled
\r
180 if (tinyMCE.getParam('button_tile_map') && (!tinyMCE.isMSIE || tinyMCE.isOpera) && (m = tinyMCE.buttonMap[id]) != null && (tinyMCE.getParam("language") == "en" || img.indexOf('$lang') == -1)) {
\r
182 x = 0 - (m * 20) == 0 ? '0' : 0 - (m * 20);
\r
183 h += '<a id="{$editor_id}_' + id + '" href="javascript:' + cmd + '" onclick="' + cmd + 'return false;" onmousedown="return false;" class="mceTiledButton mceButtonNormal" target="_self">';
\r
184 h += '<img src="{$themeurl}/images/spacer.gif" style="background-position: ' + x + 'px 0" title="{$' + lang + '}" />';
\r
185 h += '<img src="{$themeurl}/images/button_menu.gif" title="{$' + lang + '}" class="mceMenuButton" onclick="' + mcmd + 'return false;" />';
\r
188 if (tinyMCE.isMSIE && !tinyMCE.isOpera)
\r
189 h += '<span id="{$editor_id}_' + id + '" class="mceMenuButton" onmouseover="tinyMCE.plugins.spellchecker._menuButtonEvent(\'over\',this);" onmouseout="tinyMCE.plugins.spellchecker._menuButtonEvent(\'out\',this);">';
\r
191 h += '<span id="{$editor_id}_' + id + '" class="mceMenuButton">';
\r
193 h += '<a href="javascript:' + cmd + '" onclick="' + cmd + 'return false;" onmousedown="return false;" class="mceMenuButtonNormal" target="_self">';
\r
194 h += '<img src="' + img + '" title="{$' + lang + '}" /></a>';
\r
195 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" />';
\r
196 h += '</a></span>';
\r
202 _menuButtonEvent : function(e, o) {
\r
203 if (o.className == 'mceMenuButtonFocus')
\r
207 o.className = o.className + ' mceMenuHover';
\r
209 o.className = o.className.replace(/\s.*$/, '');
\r
212 _toggleMenu : function(editor_id, id) {
\r
213 var self = TinyMCE_SpellCheckerPlugin;
\r
214 var e = document.getElementById(editor_id + '_spellchecker');
\r
215 var inst = tinyMCE.getInstanceById(editor_id);
\r
217 if (self._menu.isVisible()) {
\r
218 tinyMCE.hideMenus();
\r
222 tinyMCE.lastMenuBtnClass = e.className.replace(/\s.*$/, '');
\r
223 tinyMCE.switchClass(editor_id + '_spellchecker', 'mceMenuButtonFocus');
\r
225 self._menu.moveRelativeTo(e, 'bl');
\r
226 self._menu.moveBy(tinyMCE.isMSIE && !tinyMCE.isOpera ? 0 : 1, -1);
\r
228 if (tinyMCE.isOpera)
\r
229 self._menu.moveBy(0, -2);
\r
231 self._onMenuEvent(inst, self._menu, 'show');
\r
235 tinyMCE.lastSelectedMenuBtn = editor_id + '_spellchecker';
\r
238 _onMenuEvent : function(inst, m, n) {
\r
239 TinyMCE_SpellCheckerPlugin._buildSettingsMenu(inst, inst.spellCheckerLang);
\r
242 execCommand : function(editor_id, element, command, user_interface, value) {
\r
243 var inst = tinyMCE.getInstanceById(editor_id), self = TinyMCE_SpellCheckerPlugin, args = '', co, bb, mb, nl, i, e;
\r
247 case "mceSpellCheck":
\r
248 if (!inst.spellcheckerOn) {
\r
249 inst.spellCheckerBookmark = inst.selection.getBookmark();
\r
252 args += 'id=' + inst.editorId + "|" + (++self._counter);
\r
253 args += '&cmd=spell&check=' + encodeURIComponent(self._getWordList(inst.getBody())).replace( /\'/g, '%27' );
\r
254 args += '&lang=' + escape(inst.spellCheckerLang);
\r
256 co = document.getElementById(inst.editorId + '_parent').firstChild;
\r
257 bb = self._getBlockBoxLayer(inst);
\r
258 bb.moveRelativeTo(co, 'tl');
\r
259 bb.resizeTo(co.offsetWidth, co.offsetHeight);
\r
262 // Setup message box
\r
263 mb = self._getMsgBoxLayer(inst);
\r
264 e = mb.getElement();
\r
265 e.innerHTML = '<span>' + tinyMCE.getLang('lang_spellchecker_swait', '', true) + '</span>';
\r
267 mb.moveRelativeTo(co, 'cc');
\r
269 if (tinyMCE.isMSIE && !tinyMCE.isOpera) {
\r
270 nl = co.getElementsByTagName('select');
\r
271 for (i=0; i<nl.length; i++)
\r
272 nl[i].disabled = true;
\r
275 inst.spellcheckerOn = true;
\r
276 tinyMCE.switchClass(editor_id + '_spellchecker', 'mceMenuButtonSelected');
\r
278 self._sendAjax(self.baseURL + self._ajaxPage, self._ajaxResponse, 'post', args);
\r
280 self._removeWords(inst.getDoc());
\r
281 inst.spellcheckerOn = false;
\r
282 tinyMCE.switchClass(editor_id + '_spellchecker', 'mceMenuButton');
\r
287 case "mceSpellCheckReplace":
\r
288 if (inst.spellCheckerElm)
\r
289 tinyMCE.setOuterHTML(inst.spellCheckerElm, value);
\r
291 self._checkDone(inst);
\r
292 self._contextMenu.hide();
\r
297 case "mceSpellCheckIgnore":
\r
298 if (inst.spellCheckerElm)
\r
299 self._removeWord(inst.spellCheckerElm);
\r
301 self._checkDone(inst);
\r
302 self._contextMenu.hide();
\r
306 case "mceSpellCheckIgnoreAll":
\r
307 if (inst.spellCheckerElm)
\r
308 self._removeWords(inst.getDoc(), inst.spellCheckerElm.innerHTML);
\r
310 self._checkDone(inst);
\r
311 self._contextMenu.hide();
\r
315 case "mceSpellCheckerSetLang":
\r
316 tinyMCE.hideMenus();
\r
317 inst.spellCheckerLang = value;
\r
318 self._removeWords(inst.getDoc());
\r
319 inst.spellcheckerOn = false;
\r
320 tinyMCE.switchClass(editor_id + '_spellchecker', 'mceMenuButton');
\r
324 // Pass to next handler in chain
\r
328 cleanup : function(type, content, inst) {
\r
330 case "get_from_editor_dom":
\r
331 TinyMCE_SpellCheckerPlugin._removeWords(content);
\r
332 inst.spellcheckerOn = false;
\r
339 // Private plugin specific methods
\r
341 _displayUI : function(inst) {
\r
342 var self = TinyMCE_SpellCheckerPlugin;
\r
343 var bb = self._getBlockBoxLayer(inst);
\r
344 var mb = self._getMsgBoxLayer(inst);
\r
346 var co = document.getElementById(inst.editorId + '_parent').firstChild;
\r
348 if (tinyMCE.isMSIE && !tinyMCE.isOpera) {
\r
349 nl = co.getElementsByTagName('select');
\r
350 for (i=0; i<nl.length; i++)
\r
351 nl[i].disabled = false;
\r
358 _ajaxResponse : function(xml) {
\r
359 var el = xml ? xml.documentElement : null;
\r
360 var inst = tinyMCE.selectedInstance, self = TinyMCE_SpellCheckerPlugin;
\r
361 var cmd = el ? el.getAttribute("cmd") : null, err, id = el ? el.getAttribute("id") : null;
\r
364 inst = tinyMCE.getInstanceById(id.substring(0, id.indexOf('|')));
\r
366 self._displayUI(inst);
\r
368 // Ignore suggestions for other ajax responses
\r
369 if (cmd == "suggest" && id != inst.editorId + "|" + self._counter)
\r
373 inst.spellcheckerOn = false;
\r
374 tinyMCE.switchClass(inst.editorId + '_spellchecker', 'mceMenuButton');
\r
375 alert("Could not execute AJAX call, server didn't return valid a XML.");
\r
379 err = el.getAttribute("error");
\r
381 if (err == "true") {
\r
382 inst.spellcheckerOn = false;
\r
383 tinyMCE.switchClass(inst.editorId + '_spellchecker', 'mceMenuButton');
\r
384 alert(el.getAttribute("msg"));
\r
390 if (xml.documentElement.firstChild) {
\r
391 self._markWords(inst.getDoc(), inst.getBody(), decodeURIComponent(el.firstChild.nodeValue).split('+'));
\r
392 inst.selection.moveToBookmark(inst.spellCheckerBookmark);
\r
394 if(tinyMCE.getParam('spellchecker_report_mispellings', false))
\r
395 alert(tinyMCE.getLang('lang_spellchecker_mpell_found', '', true, {words : self._countWords(inst)}));
\r
397 alert(tinyMCE.getLang('lang_spellchecker_no_mpell', '', true));
\r
399 self._checkDone(inst);
\r
404 self._buildMenu(el.firstChild ? decodeURIComponent(el.firstChild.nodeValue).split('+') : null, 10);
\r
405 self._contextMenu.show();
\r
410 _getWordSeparators : function() {
\r
411 var i, re = '', ch = tinyMCE.getParam('spellchecker_word_separator_chars', '\\s!"#$%&()*+,-./:;<=>?@[\]^_{|}§©«®±¶·¸»¼½¾¿×÷¤\u201d\u201c');
\r
413 for (i=0; i<ch.length; i++)
\r
414 re += '\\' + ch.charAt(i);
\r
419 _getWordList : function(n) {
\r
420 var i, x, s, nv = '', nl = tinyMCE.getNodeTree(n, new Array(), 3), wl = new Array();
\r
421 var re = TinyMCE_SpellCheckerPlugin._getWordSeparators();
\r
423 for (i=0; i<nl.length; i++) {
\r
424 if (!new RegExp('/SCRIPT|STYLE/').test(nl[i].parentNode.nodeName))
\r
425 nv += nl[i].nodeValue + " ";
\r
428 nv = nv.replace(new RegExp('([0-9]|[' + re + '])', 'g'), ' ');
\r
429 nv = tinyMCE.trim(nv.replace(/(\s+)/g, ' '));
\r
431 nl = nv.split(/\s+/);
\r
432 for (i=0; i<nl.length; i++) {
\r
434 for (x=0; x<wl.length; x++) {
\r
435 if (wl[x] == nl[i]) {
\r
441 if (!s && nl[i].length > 0)
\r
442 wl[wl.length] = nl[i];
\r
445 return wl.join(' ');
\r
448 _removeWords : function(doc, word) {
\r
449 var i, c, nl = doc.getElementsByTagName("span");
\r
450 var self = TinyMCE_SpellCheckerPlugin;
\r
451 var inst = tinyMCE.selectedInstance, b = inst ? inst.selection.getBookmark() : null;
\r
453 word = typeof(word) == 'undefined' ? null : word;
\r
455 for (i=nl.length-1; i>=0; i--) {
\r
456 c = tinyMCE.getAttrib(nl[i], 'class');
\r
458 if ((c == 'mceItemHiddenSpellWord' || c == 'mceItemHidden') && (word == null || nl[i].innerHTML == word))
\r
459 self._removeWord(nl[i]);
\r
463 inst.selection.moveToBookmark(b);
\r
466 _checkDone : function(inst) {
\r
467 var self = TinyMCE_SpellCheckerPlugin;
\r
468 var w = self._countWords(inst);
\r
471 self._removeWords(inst.getDoc());
\r
472 inst.spellcheckerOn = false;
\r
473 tinyMCE.switchClass(inst.editorId + '_spellchecker', 'mceMenuButton');
\r
477 _countWords : function(inst) {
\r
478 var i, w = 0, nl = inst.getDoc().getElementsByTagName("span"), c;
\r
479 var self = TinyMCE_SpellCheckerPlugin;
\r
481 for (i=nl.length-1; i>=0; i--) {
\r
482 c = tinyMCE.getAttrib(nl[i], 'class');
\r
484 if (c == 'mceItemHiddenSpellWord')
\r
491 _removeWord : function(e) {
\r
493 tinyMCE.setOuterHTML(e, e.innerHTML);
\r
496 _markWords : function(doc, n, wl) {
\r
497 var i, nv, nn, nl = tinyMCE.getNodeTree(n, new Array(), 3);
\r
498 var r1, r2, r3, r4, r5, w = '';
\r
499 var re = TinyMCE_SpellCheckerPlugin._getWordSeparators();
\r
501 for (i=0; i<wl.length; i++) {
\r
502 if (wl[i].length > 0)
\r
503 w += wl[i] + ((i == wl.length-1) ? '' : '|');
\r
506 for (i=0; i<nl.length; i++) {
\r
507 nv = nl[i].nodeValue;
\r
508 r1 = new RegExp('([' + re + '])(' + w + ')([' + re + '])', 'g');
\r
509 r2 = new RegExp('^(' + w + ')', 'g');
\r
510 r3 = new RegExp('(' + w + ')([' + re + ']?)$', 'g');
\r
511 r4 = new RegExp('^(' + w + ')([' + re + ']?)$', 'g');
\r
512 r5 = new RegExp('(' + w + ')([' + re + '])', 'g');
\r
514 if (r1.test(nv) || r2.test(nv) || r3.test(nv) || r4.test(nv)) {
\r
515 nv = tinyMCE.xmlEncode(nv);
\r
516 nv = nv.replace(r5, '<span class="mceItemHiddenSpellWord">$1</span>$2');
\r
517 nv = nv.replace(r3, '<span class="mceItemHiddenSpellWord">$1</span>$2');
\r
519 nn = doc.createElement('span');
\r
520 nn.className = "mceItemHidden";
\r
523 // Remove old text node
\r
524 nl[i].parentNode.replaceChild(nn, nl[i]);
\r
529 _buildMenu : function(sg, max) {
\r
530 var i, self = TinyMCE_SpellCheckerPlugin, cm = self._contextMenu;
\r
535 cm.addTitle(tinyMCE.getLang('lang_spellchecker_sug', '', true));
\r
537 for (i=0; i<sg.length && i<max; i++)
\r
538 cm.addItem(sg[i], 'tinyMCE.execCommand("mceSpellCheckReplace",false,"' + sg[i] + '");');
\r
542 cm.addTitle(tinyMCE.getLang('lang_spellchecker_no_sug', '', true));
\r
544 cm.addItem(tinyMCE.getLang('lang_spellchecker_ignore_word', '', true), 'tinyMCE.execCommand(\'mceSpellCheckIgnore\');');
\r
545 cm.addItem(tinyMCE.getLang('lang_spellchecker_ignore_words', '', true), 'tinyMCE.execCommand(\'mceSpellCheckIgnoreAll\');');
\r
550 _getAjaxHTTP : function() {
\r
552 return new ActiveXObject('Msxml2.XMLHTTP')
\r
555 return new ActiveXObject('Microsoft.XMLHTTP')
\r
557 return new XMLHttpRequest();
\r
563 * Perform AJAX call.
\r
565 * @param {string} u URL of AJAX service.
\r
566 * @param {function} f Function to call when response arrives.
\r
567 * @param {string} m Request method post or get.
\r
568 * @param {Array} a Array with arguments to send.
\r
570 _sendAjax : function(u, f, m, a) {
\r
571 var x = TinyMCE_SpellCheckerPlugin._getAjaxHTTP();
\r
573 x.open(m, u, true);
\r
575 x.onreadystatechange = function() {
\r
576 if (x.readyState == 4)
\r
581 x.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
\r
588 tinyMCE.addPlugin('spellchecker', TinyMCE_SpellCheckerPlugin);
\r