moved code up one level to eliminate the docs subdirectory
[acontent.git] / include / jscripts / tiny_mce / plugins / wordcount / 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     tinymce.create('tinymce.plugins.WordCount', {\r
13                 block : 0,\r
14                 id : null,\r
15                 countre : null,\r
16                 cleanre : null,\r
17 \r
18                 init : function(ed, url) {\r
19                         var t = this, last = 0;\r
20 \r
21                         t.countre = ed.getParam('wordcount_countregex', /\S\s+/g);\r
22                         t.cleanre = ed.getParam('wordcount_cleanregex', /[0-9.(),;:!?%#$¿'"_+=\\/-]*/g);\r
23                         t.id = ed.id + '-word-count';\r
24 \r
25                         ed.onPostRender.add(function(ed, cm) {\r
26                                 var row, id;\r
27 \r
28                                 // Add it to the specified id or the theme advanced path\r
29                                 id = ed.getParam('wordcount_target_id');\r
30                                 if (!id) {\r
31                                         row = tinymce.DOM.get(ed.id + '_path_row');\r
32 \r
33                                         if (row)\r
34                                                 tinymce.DOM.add(row.parentNode, 'div', {'style': 'float: right'}, ed.getLang('wordcount.words', 'Words: ') + '<span id="' + t.id + '">0</span>');\r
35                                 } else\r
36                                         tinymce.DOM.add(id, 'span', {}, '<span id="' + t.id + '">0</span>');\r
37                         });\r
38 \r
39             ed.onInit.add(function(ed) {\r
40                                 ed.selection.onSetContent.add(function() {\r
41                                         t._count(ed);\r
42                                 });\r
43 \r
44                                 t._count(ed);\r
45                         });\r
46 \r
47                         ed.onSetContent.add(function(ed) {\r
48                                 t._count(ed);\r
49                         });\r
50 \r
51                         ed.onKeyUp.add(function(ed, e) {\r
52                                 if (e.keyCode == last)\r
53                                         return;\r
54 \r
55                                 if (13 == e.keyCode || 8 == last || 46 == last)\r
56                                         t._count(ed);\r
57 \r
58                                 last = e.keyCode;\r
59                         });\r
60                 },\r
61 \r
62                 _count : function(ed) {\r
63                         var t = this, tc = 0;\r
64 \r
65                         // Keep multiple calls from happening at the same time\r
66                         if (t.block)\r
67                                 return;\r
68 \r
69                         t.block = 1;\r
70 \r
71                         setTimeout(function() {\r
72                                 var tx = ed.getContent({format : 'raw'});\r
73 \r
74                                 if (tx) {\r
75                                         tx = tx.replace(/<.[^<>]*?>/g, ' ').replace(/&nbsp;|&#160;/gi, ' '); // remove html tags and space chars\r
76                                         tx = tx.replace(t.cleanre, ''); // remove numbers and punctuation\r
77                                         tx.replace(t.countre, function() {tc++;}); // count the words\r
78                                 }\r
79 \r
80                                 tinymce.DOM.setHTML(t.id, tc.toString());\r
81 \r
82                                 setTimeout(function() {t.block = 0;}, 2000);\r
83                         }, 1);\r
84                 },\r
85 \r
86         getInfo: function() {\r
87                         return {\r
88                                 longname : 'Word Count plugin',\r
89                                 author : 'Moxiecode Systems AB',\r
90                                 authorurl : 'http://tinymce.moxiecode.com',\r
91                                 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/wordcount',\r
92                                 version : tinymce.majorVersion + "." + tinymce.minorVersion\r
93                         };\r
94         }\r
95     });\r
96 \r
97     tinymce.PluginManager.add('wordcount', tinymce.plugins.WordCount);\r
98 })();\r