Updated tiny-mce to most recent 3.4 version
[citadel.git] / webcit / 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, VK = tinymce.VK;\r
20 \r
21                         t.countre = ed.getParam('wordcount_countregex', /[\w\u2019\'-]+/g); // u2019 == ’\r
22                         t.cleanre = ed.getParam('wordcount_cleanregex', /[0-9.(),;:!?%#$?\'\"_+=\\\/-]*/g);\r
23                         t.update_rate = ed.getParam('wordcount_update_rate', 2000);\r
24                         t.update_on_delete = ed.getParam('wordcount_update_on_delete', false);\r
25                         t.id = ed.id + '-word-count';\r
26 \r
27                         ed.onPostRender.add(function(ed, cm) {\r
28                                 var row, id;\r
29 \r
30                                 // Add it to the specified id or the theme advanced path\r
31                                 id = ed.getParam('wordcount_target_id');\r
32                                 if (!id) {\r
33                                         row = tinymce.DOM.get(ed.id + '_path_row');\r
34 \r
35                                         if (row)\r
36                                                 tinymce.DOM.add(row.parentNode, 'div', {'style': 'float: right'}, ed.getLang('wordcount.words', 'Words: ') + '<span id="' + t.id + '">0</span>');\r
37                                 } else {\r
38                                         tinymce.DOM.add(id, 'span', {}, '<span id="' + t.id + '">0</span>');\r
39                                 }\r
40                         });\r
41 \r
42                         ed.onInit.add(function(ed) {\r
43                                 ed.selection.onSetContent.add(function() {\r
44                                         t._count(ed);\r
45                                 });\r
46 \r
47                                 t._count(ed);\r
48                         });\r
49 \r
50                         ed.onSetContent.add(function(ed) {\r
51                                 t._count(ed);\r
52                         });\r
53 \r
54                         function checkKeys(key) {\r
55                                 return key !== last && (key === VK.ENTER || last === VK.SPACEBAR || checkDelOrBksp(last));\r
56                         }\r
57 \r
58                         function checkDelOrBksp(key) {\r
59                                 return key === VK.DELETE || key === VK.BACKSPACE;\r
60                         }\r
61 \r
62                         ed.onKeyUp.add(function(ed, e) {\r
63                                 if (checkKeys(e.keyCode) || t.update_on_delete && checkDelOrBksp(e.keyCode)) {\r
64                                         t._count(ed);\r
65                                 }\r
66 \r
67                                 last = e.keyCode;\r
68                         });\r
69                 },\r
70 \r
71                 _getCount : function(ed) {\r
72                         var tc = 0;\r
73                         var tx = ed.getContent({ format: 'raw' });\r
74 \r
75                         if (tx) {\r
76                                         tx = tx.replace(/\.\.\./g, ' '); // convert ellipses to spaces\r
77                                         tx = tx.replace(/<.[^<>]*?>/g, ' ').replace(/&nbsp;|&#160;/gi, ' '); // remove html tags and space chars\r
78 \r
79                                         // deal with html entities\r
80                                         tx = tx.replace(/(\w+)(&.+?;)+(\w+)/, "$1$3").replace(/&.+?;/g, ' ');\r
81                                         tx = tx.replace(this.cleanre, ''); // remove numbers and punctuation\r
82 \r
83                                         var wordArray = tx.match(this.countre);\r
84                                         if (wordArray) {\r
85                                                         tc = wordArray.length;\r
86                                         }\r
87                         }\r
88 \r
89                         return tc;\r
90                 },\r
91 \r
92                 _count : function(ed) {\r
93                         var t = this;\r
94 \r
95                         // Keep multiple calls from happening at the same time\r
96                         if (t.block)\r
97                                 return;\r
98 \r
99                         t.block = 1;\r
100 \r
101                         setTimeout(function() {\r
102                                 if (!ed.destroyed) {\r
103                                         var tc = t._getCount(ed);\r
104                                         tinymce.DOM.setHTML(t.id, tc.toString());\r
105                                         setTimeout(function() {t.block = 0;}, t.update_rate);\r
106                                 }\r
107                         }, 1);\r
108                 },\r
109 \r
110                 getInfo: function() {\r
111                         return {\r
112                                 longname : 'Word Count plugin',\r
113                                 author : 'Moxiecode Systems AB',\r
114                                 authorurl : 'http://tinymce.moxiecode.com',\r
115                                 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/wordcount',\r
116                                 version : tinymce.majorVersion + "." + tinymce.minorVersion\r
117                         };\r
118                 }\r
119         });\r
120 \r
121         tinymce.PluginManager.add('wordcount', tinymce.plugins.WordCount);\r
122 })();\r