Initial revision with basic functionability
[bcms.git] / media / tinymce / plugins / spellchecker / editor_plugin_src.js
blobc913c4603e482a19460362762d3e6c569d9e4ad3
1 /**\r
2  * $Id: editor_plugin_src.js 425 2007-11-21 15:17:39Z spocke $\r
3  *\r
4  * @author Moxiecode\r
5  * @copyright Copyright © 2004-2008, Moxiecode Systems AB, All rights reserved.\r
6  */\r
7 \r
8 (function() {\r
9         var JSONRequest = tinymce.util.JSONRequest, each = tinymce.each, DOM = tinymce.DOM;\r
11         tinymce.create('tinymce.plugins.SpellcheckerPlugin', {\r
12                 getInfo : function() {\r
13                         return {\r
14                                 longname : 'Spellchecker',\r
15                                 author : 'Moxiecode Systems AB',\r
16                                 authorurl : 'http://tinymce.moxiecode.com',\r
17                                 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/spellchecker',\r
18                                 version : tinymce.majorVersion + "." + tinymce.minorVersion\r
19                         };\r
20                 },\r
22                 init : function(ed, url) {\r
23                         var t = this, cm;\r
25                         t.url = url;\r
26                         t.editor = ed;\r
28                         // Register commands\r
29                         ed.addCommand('mceSpellCheck', function() {\r
30                                 if (!t.active) {\r
31                                         ed.setProgressState(1);\r
32                                         t._sendRPC('checkWords', [t.selectedLang, t._getWords()], function(r) {\r
33                                                 if (r.length > 0) {\r
34                                                         t.active = 1;\r
35                                                         t._markWords(r);\r
36                                                         ed.setProgressState(0);\r
37                                                         ed.nodeChanged();\r
38                                                 } else {\r
39                                                         ed.setProgressState(0);\r
40                                                         ed.windowManager.alert('spellchecker.no_mpell');\r
41                                                 }\r
42                                         });\r
43                                 } else\r
44                                         t._done();\r
45                         });\r
47                         ed.onInit.add(function() {\r
48                                 if (ed.settings.content_css !== false)\r
49                                         ed.dom.loadCSS(url + '/css/content.css');\r
50                         });\r
52                         ed.onClick.add(t._showMenu, t);\r
53                         ed.onContextMenu.add(t._showMenu, t);\r
54                         ed.onBeforeGetContent.add(function() {\r
55                                 if (t.active)\r
56                                         t._removeWords();\r
57                         });\r
59                         ed.onNodeChange.add(function(ed, cm) {\r
60                                 cm.setActive('spellchecker', t.active);\r
61                         });\r
63                         ed.onSetContent.add(function() {\r
64                                 t._done();\r
65                         });\r
67                         ed.onBeforeGetContent.add(function() {\r
68                                 t._done();\r
69                         });\r
71                         ed.onBeforeExecCommand.add(function(ed, cmd) {\r
72                                 if (cmd == 'mceFullScreen')\r
73                                         t._done();\r
74                         });\r
76                         // Find selected language\r
77                         t.languages = {};\r
78                         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
79                                 if (k.indexOf('+') === 0) {\r
80                                         k = k.substring(1);\r
81                                         t.selectedLang = v;\r
82                                 }\r
84                                 t.languages[k] = v;\r
85                         });\r
86                 },\r
88                 createControl : function(n, cm) {\r
89                         var t = this, c, ed = t.editor;\r
91                         if (n == 'spellchecker') {\r
92                                 c = cm.createSplitButton(n, {title : 'spellchecker.desc', cmd : 'mceSpellCheck', scope : t});\r
94                                 c.onRenderMenu.add(function(c, m) {\r
95                                         m.add({title : 'spellchecker.langs', 'class' : 'mceMenuItemTitle'}).setDisabled(1);\r
96                                         each(t.languages, function(v, k) {\r
97                                                 var o = {icon : 1}, mi;\r
99                                                 o.onclick = function() {\r
100                                                         mi.setSelected(1);\r
101                                                         t.selectedItem.setSelected(0);\r
102                                                         t.selectedItem = mi;\r
103                                                         t.selectedLang = v;\r
104                                                 };\r
106                                                 o.title = k;\r
107                                                 mi = m.add(o);\r
108                                                 mi.setSelected(v == t.selectedLang);\r
110                                                 if (v == t.selectedLang)\r
111                                                         t.selectedItem = mi;\r
112                                         })\r
113                                 });\r
115                                 return c;\r
116                         }\r
117                 },\r
119                 // Internal functions\r
121                 _walk : function(n, f) {\r
122                         var d = this.editor.getDoc(), w;\r
124                         if (d.createTreeWalker) {\r
125                                 w = d.createTreeWalker(n, NodeFilter.SHOW_TEXT, null, false);\r
127                                 while ((n = w.nextNode()) != null)\r
128                                         f.call(this, n);\r
129                         } else\r
130                                 tinymce.walk(n, f, 'childNodes');\r
131                 },\r
133                 _getSeparators : function() {\r
134                         var re = '', i, str = this.editor.getParam('spellchecker_word_separator_chars', '\\s!"#$%&()*+,-./:;<=>?@[\]^_{|}§©«®±¶·¸»¼½¾¿×÷¤\u201d\u201c');\r
136                         // Build word separator regexp\r
137                         for (i=0; i<str.length; i++)\r
138                                 re += '\\' + str.charAt(i);\r
140                         return re;\r
141                 },\r
143                 _getWords : function() {\r
144                         var ed = this.editor, wl = [], tx = '', lo = {};\r
146                         // Get area text\r
147                         this._walk(ed.getBody(), function(n) {\r
148                                 if (n.nodeType == 3)\r
149                                         tx += n.nodeValue + ' ';\r
150                         });\r
152                         // Split words by separator\r
153                         tx = tx.replace(new RegExp('([0-9]|[' + this._getSeparators() + '])', 'g'), ' ');\r
154                         tx = tinymce.trim(tx.replace(/(\s+)/g, ' '));\r
156                         // Build word array and remove duplicates\r
157                         each(tx.split(' '), function(v) {\r
158                                 if (!lo[v]) {\r
159                                         wl.push(v);\r
160                                         lo[v] = 1;\r
161                                 }\r
162                         });\r
164                         return wl;\r
165                 },\r
167                 _removeWords : function(w) {\r
168                         var ed = this.editor, dom = ed.dom, se = ed.selection, b = se.getBookmark();\r
170                         each(dom.select('span').reverse(), function(n) {\r
171                                 if (n && (dom.hasClass(n, 'mceItemHiddenSpellWord') || dom.hasClass(n, 'mceItemHidden'))) {\r
172                                         if (!w || dom.decode(n.innerHTML) == w)\r
173                                                 dom.remove(n, 1);\r
174                                 }\r
175                         });\r
177                         se.moveToBookmark(b);\r
178                 },\r
180                 _markWords : function(wl) {\r
181                         var r1, r2, r3, r4, r5, w = '', ed = this.editor, re = this._getSeparators(), dom = ed.dom, nl = [];\r
182                         var se = ed.selection, b = se.getBookmark();\r
184                         each(wl, function(v) {\r
185                                 w += (w ? '|' : '') + v;\r
186                         });\r
188                         r1 = new RegExp('([' + re + '])(' + w + ')([' + re + '])', 'g');\r
189                         r2 = new RegExp('^(' + w + ')', 'g');\r
190                         r3 = new RegExp('(' + w + ')([' + re + ']?)$', 'g');\r
191                         r4 = new RegExp('^(' + w + ')([' + re + ']?)$', 'g');\r
192                         r5 = new RegExp('(' + w + ')([' + re + '])', 'g');\r
194                         // Collect all text nodes\r
195                         this._walk(this.editor.getBody(), function(n) {\r
196                                 if (n.nodeType == 3) {\r
197                                         nl.push(n);\r
198                                 }\r
199                         });\r
201                         // Wrap incorrect words in spans\r
202                         each(nl, function(n) {\r
203                                 var v;\r
205                                 if (n.nodeType == 3) {\r
206                                         v = n.nodeValue;\r
208                                         if (r1.test(v) || r2.test(v) || r3.test(v) || r4.test(v)) {\r
209                                                 v = dom.encode(v);\r
210                                                 v = v.replace(r5, '<span class="mceItemHiddenSpellWord">$1</span>$2');\r
211                                                 v = v.replace(r3, '<span class="mceItemHiddenSpellWord">$1</span>$2');\r
213                                                 dom.replace(dom.create('span', {'class' : 'mceItemHidden'}, v), n);\r
214                                         }\r
215                                 }\r
216                         });\r
218                         se.moveToBookmark(b);\r
219                 },\r
221                 _showMenu : function(ed, e) {\r
222                         var t = this, ed = t.editor, m = t._menu, p1, dom = ed.dom, vp = dom.getViewPort(ed.getWin());\r
224                         if (!m) {\r
225                                 p1 = DOM.getPos(ed.getContentAreaContainer());\r
226                                 //p2 = DOM.getPos(ed.getContainer());\r
228                                 m = ed.controlManager.createDropMenu('spellcheckermenu', {\r
229                                         offset_x : p1.x,\r
230                                         offset_y : p1.y,\r
231                                         'class' : 'mceNoIcons'\r
232                                 });\r
234                                 t._menu = m;\r
235                         }\r
237                         if (dom.hasClass(e.target, 'mceItemHiddenSpellWord')) {\r
238                                 m.removeAll();\r
239                                 m.add({title : 'spellchecker.wait', 'class' : 'mceMenuItemTitle'}).setDisabled(1);\r
241                                 t._sendRPC('getSuggestions', [t.selectedLang, dom.decode(e.target.innerHTML)], function(r) {\r
242                                         m.removeAll();\r
244                                         if (r.length > 0) {\r
245                                                 m.add({title : 'spellchecker.sug', 'class' : 'mceMenuItemTitle'}).setDisabled(1);\r
246                                                 each(r, function(v) {\r
247                                                         m.add({title : v, onclick : function() {\r
248                                                                 dom.replace(ed.getDoc().createTextNode(v), e.target);\r
249                                                                 t._checkDone();\r
250                                                         }});\r
251                                                 });\r
253                                                 m.addSeparator();\r
254                                         } else\r
255                                                 m.add({title : 'spellchecker.no_sug', 'class' : 'mceMenuItemTitle'}).setDisabled(1);\r
257                                         m.add({\r
258                                                 title : 'spellchecker.ignore_word',\r
259                                                 onclick : function() {\r
260                                                         dom.remove(e.target, 1);\r
261                                                         t._checkDone();\r
262                                                 }\r
263                                         });\r
265                                         m.add({\r
266                                                 title : 'spellchecker.ignore_words',\r
267                                                 onclick : function() {\r
268                                                         t._removeWords(dom.decode(e.target.innerHTML));\r
269                                                         t._checkDone();\r
270                                                 }\r
271                                         });\r
273                                         m.update();\r
274                                 });\r
276                                 ed.selection.select(e.target);\r
277                                 p1 = dom.getPos(e.target);\r
278                                 m.showMenu(p1.x, p1.y + e.target.offsetHeight - vp.y);\r
280                                 return tinymce.dom.Event.cancel(e);\r
281                         } else\r
282                                 m.hideMenu();\r
283                 },\r
285                 _checkDone : function() {\r
286                         var t = this, ed = t.editor, dom = ed.dom, o;\r
288                         each(dom.select('span'), function(n) {\r
289                                 if (n && dom.hasClass(n, 'mceItemHiddenSpellWord')) {\r
290                                         o = true;\r
291                                         return false;\r
292                                 }\r
293                         });\r
295                         if (!o)\r
296                                 t._done();\r
297                 },\r
299                 _done : function() {\r
300                         var t = this, la = t.active;\r
302                         if (t.active) {\r
303                                 t.active = 0;\r
304                                 t._removeWords();\r
306                                 if (t._menu)\r
307                                         t._menu.hideMenu();\r
309                                 if (la)\r
310                                         t.editor.nodeChanged();\r
311                         }\r
312                 },\r
314                 _sendRPC : function(m, p, cb) {\r
315                         var t = this, url = t.editor.getParam("spellchecker_rpc_url", "{backend}");\r
317                         if (url == '{backend}') {\r
318                                 t.editor.setProgressState(0);\r
319                                 alert('Please specify: spellchecker_rpc_url');\r
320                                 return;\r
321                         }\r
323                         JSONRequest.sendRPC({\r
324                                 url : url,\r
325                                 method : m,\r
326                                 params : p,\r
327                                 success : cb,\r
328                                 error : function(e, x) {\r
329                                         t.editor.setProgressState(0);\r
330                                         t.editor.windowManager.alert(e.errstr || ('Error response: ' + x.responseText));\r
331                                 }\r
332                         });\r
333                 }\r
334         });\r
336         // Register plugin\r
337         tinymce.PluginManager.add('spellchecker', tinymce.plugins.SpellcheckerPlugin);\r
338 })();