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