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