nouveau style menu_top
[mtweb] / mw / libs / tiny_mce / plugins / spellchecker / editor_plugin_src.js
1 /**\r
2  * editor_plugin_src.js\r
3  *\r
4  * Copyright 2009, Moxiecode Systems AB\r
5  * Released under LGPL License.\r
6  *\r
7  * License: http://tinymce.moxiecode.com/license\r
8  * Contributing: http://tinymce.moxiecode.com/contributing\r
9  */\r
10 \r
11 (function() {\r
12         var JSONRequest = tinymce.util.JSONRequest, each = tinymce.each, DOM = tinymce.DOM;\r
13 \r
14         tinymce.create('tinymce.plugins.SpellcheckerPlugin', {\r
15                 getInfo : function() {\r
16                         return {\r
17                                 longname : 'Spellchecker',\r
18                                 author : 'Moxiecode Systems AB',\r
19                                 authorurl : 'http://tinymce.moxiecode.com',\r
20                                 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/spellchecker',\r
21                                 version : tinymce.majorVersion + "." + tinymce.minorVersion\r
22                         };\r
23                 },\r
24 \r
25                 init : function(ed, url) {\r
26                         var t = this, cm;\r
27 \r
28                         t.url = url;\r
29                         t.editor = ed;\r
30                         t.rpcUrl = ed.getParam("spellchecker_rpc_url", "{backend}");\r
31 \r
32                         if (t.rpcUrl == '{backend}') {\r
33                                 // Sniff if the browser supports native spellchecking (Don't know of a better way)\r
34                                 if (tinymce.isIE)\r
35                                         return;\r
36 \r
37                                 t.hasSupport = true;\r
38 \r
39                                 // Disable the context menu when spellchecking is active\r
40                                 ed.onContextMenu.addToTop(function(ed, e) {\r
41                                         if (t.active)\r
42                                                 return false;\r
43                                 });\r
44                         }\r
45 \r
46                         // Register commands\r
47                         ed.addCommand('mceSpellCheck', function() {\r
48                                 if (t.rpcUrl == '{backend}') {\r
49                                         // Enable/disable native spellchecker\r
50                                         t.editor.getBody().spellcheck = t.active = !t.active;\r
51                                         return;\r
52                                 }\r
53 \r
54                                 if (!t.active) {\r
55                                         ed.setProgressState(1);\r
56                                         t._sendRPC('checkWords', [t.selectedLang, t._getWords()], function(r) {\r
57                                                 if (r.length > 0) {\r
58                                                         t.active = 1;\r
59                                                         t._markWords(r);\r
60                                                         ed.setProgressState(0);\r
61                                                         ed.nodeChanged();\r
62                                                 } else {\r
63                                                         ed.setProgressState(0);\r
64 \r
65                                                         if (ed.getParam('spellchecker_report_no_misspellings', true))\r
66                                                                 ed.windowManager.alert('spellchecker.no_mpell');\r
67                                                 }\r
68                                         });\r
69                                 } else\r
70                                         t._done();\r
71                         });\r
72 \r
73                         ed.onInit.add(function() {\r
74                                 if (ed.settings.content_css !== false)\r
75                                         ed.dom.loadCSS(url + '/css/content.css');\r
76                         });\r
77 \r
78                         ed.onClick.add(t._showMenu, t);\r
79                         ed.onContextMenu.add(t._showMenu, t);\r
80                         ed.onBeforeGetContent.add(function() {\r
81                                 if (t.active)\r
82                                         t._removeWords();\r
83                         });\r
84 \r
85                         ed.onNodeChange.add(function(ed, cm) {\r
86                                 cm.setActive('spellchecker', t.active);\r
87                         });\r
88 \r
89                         ed.onSetContent.add(function() {\r
90                                 t._done();\r
91                         });\r
92 \r
93                         ed.onBeforeGetContent.add(function() {\r
94                                 t._done();\r
95                         });\r
96 \r
97                         ed.onBeforeExecCommand.add(function(ed, cmd) {\r
98                                 if (cmd == 'mceFullScreen')\r
99                                         t._done();\r
100                         });\r
101 \r
102                         // Find selected language\r
103                         t.languages = {};\r
104                         each(ed.getParam('spellchecker_languages', '+English=en,Danish=da,Dutch=nl,Finnish=fi,French=fr,German=de,Italian=it,Polish=pl,Portuguese=pt,Spanish=es,Swedish=sv', 'hash'), function(v, k) {\r
105                                 if (k.indexOf('+') === 0) {\r
106                                         k = k.substring(1);\r
107                                         t.selectedLang = v;\r
108                                 }\r
109 \r
110                                 t.languages[k] = v;\r
111                         });\r
112                 },\r
113 \r
114                 createControl : function(n, cm) {\r
115                         var t = this, c, ed = t.editor;\r
116 \r
117                         if (n == 'spellchecker') {\r
118                                 // Use basic button if we use the native spellchecker\r
119                                 if (t.rpcUrl == '{backend}') {\r
120                                         // Create simple toggle button if we have native support\r
121                                         if (t.hasSupport)\r
122                                                 c = cm.createButton(n, {title : 'spellchecker.desc', cmd : 'mceSpellCheck', scope : t});\r
123 \r
124                                         return c;\r
125                                 }\r
126 \r
127                                 c = cm.createSplitButton(n, {title : 'spellchecker.desc', cmd : 'mceSpellCheck', scope : t});\r
128 \r
129                                 c.onRenderMenu.add(function(c, m) {\r
130                                         m.add({title : 'spellchecker.langs', 'class' : 'mceMenuItemTitle'}).setDisabled(1);\r
131                                         each(t.languages, function(v, k) {\r
132                                                 var o = {icon : 1}, mi;\r
133 \r
134                                                 o.onclick = function() {\r
135                                                         mi.setSelected(1);\r
136                                                         t.selectedItem.setSelected(0);\r
137                                                         t.selectedItem = mi;\r
138                                                         t.selectedLang = v;\r
139                                                 };\r
140 \r
141                                                 o.title = k;\r
142                                                 mi = m.add(o);\r
143                                                 mi.setSelected(v == t.selectedLang);\r
144 \r
145                                                 if (v == t.selectedLang)\r
146                                                         t.selectedItem = mi;\r
147                                         })\r
148                                 });\r
149 \r
150                                 return c;\r
151                         }\r
152                 },\r
153 \r
154                 // Internal functions\r
155 \r
156                 _walk : function(n, f) {\r
157                         var d = this.editor.getDoc(), w;\r
158 \r
159                         if (d.createTreeWalker) {\r
160                                 w = d.createTreeWalker(n, NodeFilter.SHOW_TEXT, null, false);\r
161 \r
162                                 while ((n = w.nextNode()) != null)\r
163                                         f.call(this, n);\r
164                         } else\r
165                                 tinymce.walk(n, f, 'childNodes');\r
166                 },\r
167 \r
168                 _getSeparators : function() {\r
169                         var re = '', i, str = this.editor.getParam('spellchecker_word_separator_chars', '\\s!"#$%&()*+,-./:;<=>?@[\]^_{|}§©«®±¶·¸»¼½¾¿×÷¤\u201d\u201c');\r
170 \r
171                         // Build word separator regexp\r
172                         for (i=0; i<str.length; i++)\r
173                                 re += '\\' + str.charAt(i);\r
174 \r
175                         return re;\r
176                 },\r
177 \r
178                 _getWords : function() {\r
179                         var ed = this.editor, wl = [], tx = '', lo = {}, rawWords = [];\r
180 \r
181                         // Get area text\r
182                         this._walk(ed.getBody(), function(n) {\r
183                                 if (n.nodeType == 3)\r
184                                         tx += n.nodeValue + ' ';\r
185                         });\r
186 \r
187                         // split the text up into individual words\r
188                         if (ed.getParam('spellchecker_word_pattern')) {\r
189                                 // look for words that match the pattern\r
190                                 rawWords = tx.match('(' + ed.getParam('spellchecker_word_pattern') + ')', 'gi');\r
191                         } else {\r
192                                 // Split words by separator\r
193                                 tx = tx.replace(new RegExp('([0-9]|[' + this._getSeparators() + '])', 'g'), ' ');\r
194                                 tx = tinymce.trim(tx.replace(/(\s+)/g, ' '));\r
195                                 rawWords = tx.split(' ');\r
196                         }\r
197 \r
198                         // Build word array and remove duplicates\r
199                         each(rawWords, function(v) {\r
200                                 if (!lo[v]) {\r
201                                         wl.push(v);\r
202                                         lo[v] = 1;\r
203                                 }\r
204                         });\r
205 \r
206                         return wl;\r
207                 },\r
208 \r
209                 _removeWords : function(w) {\r
210                         var ed = this.editor, dom = ed.dom, se = ed.selection, b = se.getBookmark();\r
211 \r
212                         each(dom.select('span').reverse(), function(n) {\r
213                                 if (n && (dom.hasClass(n, 'mceItemHiddenSpellWord') || dom.hasClass(n, 'mceItemHidden'))) {\r
214                                         if (!w || dom.decode(n.innerHTML) == w)\r
215                                                 dom.remove(n, 1);\r
216                                 }\r
217                         });\r
218 \r
219                         se.moveToBookmark(b);\r
220                 },\r
221 \r
222                 _markWords : function(wl) {\r
223                         var r1, r2, r3, r4, r5, w = '', ed = this.editor, re = this._getSeparators(), dom = ed.dom, nl = [];\r
224                         var se = ed.selection, b = se.getBookmark();\r
225 \r
226                         each(wl, function(v) {\r
227                                 w += (w ? '|' : '') + v;\r
228                         });\r
229 \r
230                         r1 = new RegExp('([' + re + '])(' + w + ')([' + re + '])', 'g');\r
231                         r2 = new RegExp('^(' + w + ')', 'g');\r
232                         r3 = new RegExp('(' + w + ')([' + re + ']?)$', 'g');\r
233                         r4 = new RegExp('^(' + w + ')([' + re + ']?)$', 'g');\r
234                         r5 = new RegExp('(' + w + ')([' + re + '])', 'g');\r
235 \r
236                         // Collect all text nodes\r
237                         this._walk(this.editor.getBody(), function(n) {\r
238                                 if (n.nodeType == 3) {\r
239                                         nl.push(n);\r
240                                 }\r
241                         });\r
242 \r
243                         // Wrap incorrect words in spans\r
244                         each(nl, function(n) {\r
245                                 var v;\r
246 \r
247                                 if (n.nodeType == 3) {\r
248                                         v = n.nodeValue;\r
249 \r
250                                         if (r1.test(v) || r2.test(v) || r3.test(v) || r4.test(v)) {\r
251                                                 v = dom.encode(v);\r
252                                                 v = v.replace(r5, '<span class="mceItemHiddenSpellWord">$1</span>$2');\r
253                                                 v = v.replace(r3, '<span class="mceItemHiddenSpellWord">$1</span>$2');\r
254 \r
255                                                 dom.replace(dom.create('span', {'class' : 'mceItemHidden'}, v), n);\r
256                                         }\r
257                                 }\r
258                         });\r
259 \r
260                         se.moveToBookmark(b);\r
261                 },\r
262 \r
263                 _showMenu : function(ed, e) {\r
264                         var t = this, ed = t.editor, m = t._menu, p1, dom = ed.dom, vp = dom.getViewPort(ed.getWin()), wordSpan = e.target;\r
265 \r
266                         e = 0; // Fixes IE memory leak\r
267 \r
268                         if (!m) {\r
269                                 p1 = DOM.getPos(ed.getContentAreaContainer());\r
270                                 //p2 = DOM.getPos(ed.getContainer());\r
271 \r
272                                 m = ed.controlManager.createDropMenu('spellcheckermenu', {\r
273                                         offset_x : p1.x,\r
274                                         offset_y : p1.y,\r
275                                         'class' : 'mceNoIcons'\r
276                                 });\r
277 \r
278                                 t._menu = m;\r
279                         }\r
280 \r
281                         if (dom.hasClass(wordSpan, 'mceItemHiddenSpellWord')) {\r
282                                 m.removeAll();\r
283                                 m.add({title : 'spellchecker.wait', 'class' : 'mceMenuItemTitle'}).setDisabled(1);\r
284 \r
285                                 t._sendRPC('getSuggestions', [t.selectedLang, dom.decode(wordSpan.innerHTML)], function(r) {\r
286                                         var ignoreRpc;\r
287 \r
288                                         m.removeAll();\r
289 \r
290                                         if (r.length > 0) {\r
291                                                 m.add({title : 'spellchecker.sug', 'class' : 'mceMenuItemTitle'}).setDisabled(1);\r
292                                                 each(r, function(v) {\r
293                                                         m.add({title : v, onclick : function() {\r
294                                                                 dom.replace(ed.getDoc().createTextNode(v), wordSpan);\r
295                                                                 t._checkDone();\r
296                                                         }});\r
297                                                 });\r
298 \r
299                                                 m.addSeparator();\r
300                                         } else\r
301                                                 m.add({title : 'spellchecker.no_sug', 'class' : 'mceMenuItemTitle'}).setDisabled(1);\r
302 \r
303                                         ignoreRpc = t.editor.getParam("spellchecker_enable_ignore_rpc", '');\r
304                                         m.add({\r
305                                                 title : 'spellchecker.ignore_word',\r
306                                                 onclick : function() {\r
307                                                         var word = wordSpan.innerHTML;\r
308 \r
309                                                         dom.remove(wordSpan, 1);\r
310                                                         t._checkDone();\r
311 \r
312                                                         // tell the server if we need to\r
313                                                         if (ignoreRpc) {\r
314                                                                 ed.setProgressState(1);\r
315                                                                 t._sendRPC('ignoreWord', [t.selectedLang, word], function(r) {\r
316                                                                         ed.setProgressState(0);\r
317                                                                 });\r
318                                                         }\r
319                                                 }\r
320                                         });\r
321 \r
322                                         m.add({\r
323                                                 title : 'spellchecker.ignore_words',\r
324                                                 onclick : function() {\r
325                                                         var word = wordSpan.innerHTML;\r
326 \r
327                                                         t._removeWords(dom.decode(word));\r
328                                                         t._checkDone();\r
329 \r
330                                                         // tell the server if we need to\r
331                                                         if (ignoreRpc) {\r
332                                                                 ed.setProgressState(1);\r
333                                                                 t._sendRPC('ignoreWords', [t.selectedLang, word], function(r) {\r
334                                                                         ed.setProgressState(0);\r
335                                                                 });\r
336                                                         }\r
337                                                 }\r
338                                         });\r
339 \r
340 \r
341                                         if (t.editor.getParam("spellchecker_enable_learn_rpc")) {\r
342                                                 m.add({\r
343                                                         title : 'spellchecker.learn_word',\r
344                                                         onclick : function() {\r
345                                                                 var word = wordSpan.innerHTML;\r
346 \r
347                                                                 dom.remove(wordSpan, 1);\r
348                                                                 t._checkDone();\r
349 \r
350                                                                 ed.setProgressState(1);\r
351                                                                 t._sendRPC('learnWord', [t.selectedLang, word], function(r) {\r
352                                                                         ed.setProgressState(0);\r
353                                                                 });\r
354                                                         }\r
355                                                 });\r
356                                         }\r
357 \r
358                                         m.update();\r
359                                 });\r
360 \r
361                                 ed.selection.select(wordSpan);\r
362                                 p1 = dom.getPos(wordSpan);\r
363                                 m.showMenu(p1.x, p1.y + wordSpan.offsetHeight - vp.y);\r
364 \r
365                                 return tinymce.dom.Event.cancel(e);\r
366                         } else\r
367                                 m.hideMenu();\r
368                 },\r
369 \r
370                 _checkDone : function() {\r
371                         var t = this, ed = t.editor, dom = ed.dom, o;\r
372 \r
373                         each(dom.select('span'), function(n) {\r
374                                 if (n && dom.hasClass(n, 'mceItemHiddenSpellWord')) {\r
375                                         o = true;\r
376                                         return false;\r
377                                 }\r
378                         });\r
379 \r
380                         if (!o)\r
381                                 t._done();\r
382                 },\r
383 \r
384                 _done : function() {\r
385                         var t = this, la = t.active;\r
386 \r
387                         if (t.active) {\r
388                                 t.active = 0;\r
389                                 t._removeWords();\r
390 \r
391                                 if (t._menu)\r
392                                         t._menu.hideMenu();\r
393 \r
394                                 if (la)\r
395                                         t.editor.nodeChanged();\r
396                         }\r
397                 },\r
398 \r
399                 _sendRPC : function(m, p, cb) {\r
400                         var t = this;\r
401 \r
402                         JSONRequest.sendRPC({\r
403                                 url : t.rpcUrl,\r
404                                 method : m,\r
405                                 params : p,\r
406                                 success : cb,\r
407                                 error : function(e, x) {\r
408                                         t.editor.setProgressState(0);\r
409                                         t.editor.windowManager.alert(e.errstr || ('Error response: ' + x.responseText));\r
410                                 }\r
411                         });\r
412                 }\r
413         });\r
414 \r
415         // Register plugin\r
416         tinymce.PluginManager.add('spellchecker', tinymce.plugins.SpellcheckerPlugin);\r
417 })();\r