Upgrade TinyMCE to v3.4.5
[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;\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.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 \r
40                         ed.onInit.add(function(ed) {\r
41                                 ed.selection.onSetContent.add(function() {\r
42                                         t._count(ed);\r
43                                 });\r
44 \r
45                                 t._count(ed);\r
46                         });\r
47 \r
48                         ed.onSetContent.add(function(ed) {\r
49                                 t._count(ed);\r
50                         });\r
51 \r
52                         ed.onKeyUp.add(function(ed, e) {\r
53                                 if (e.keyCode == last)\r
54                                         return;\r
55 \r
56                                 if (13 == e.keyCode || 8 == last || 46 == last)\r
57                                         t._count(ed);\r
58 \r
59                                 last = e.keyCode;\r
60                         });\r
61                 },\r
62 \r
63                 _getCount : function(ed) {\r
64                         var tc = 0;\r
65                         var tx = ed.getContent({ format: 'raw' });\r
66 \r
67                         if (tx) {\r
68                                         tx = tx.replace(/\.\.\./g, ' '); // convert ellipses to spaces\r
69                                         tx = tx.replace(/<.[^<>]*?>/g, ' ').replace(/&nbsp;|&#160;/gi, ' '); // remove html tags and space chars\r
70 \r
71                                         // deal with html entities\r
72                                         tx = tx.replace(/(\w+)(&.+?;)+(\w+)/, "$1$3").replace(/&.+?;/g, ' ');\r
73                                         tx = tx.replace(this.cleanre, ''); // remove numbers and punctuation\r
74 \r
75                                         var wordArray = tx.match(this.countre);\r
76                                         if (wordArray) {\r
77                                                         tc = wordArray.length;\r
78                                         }\r
79                         }\r
80 \r
81                         return tc;\r
82                 },\r
83 \r
84                 _count : function(ed) {\r
85                         var t = this;\r
86 \r
87                         // Keep multiple calls from happening at the same time\r
88                         if (t.block)\r
89                                 return;\r
90 \r
91                         t.block = 1;\r
92 \r
93                         setTimeout(function() {\r
94                                 if (!ed.destroyed) {\r
95                                         var tc = t._getCount(ed);\r
96                                         tinymce.DOM.setHTML(t.id, tc.toString());\r
97                                         setTimeout(function() {t.block = 0;}, 2000);\r
98                                 }\r
99                         }, 1);\r
100                 },\r
101 \r
102                 getInfo: function() {\r
103                         return {\r
104                                 longname : 'Word Count plugin',\r
105                                 author : 'Moxiecode Systems AB',\r
106                                 authorurl : 'http://tinymce.moxiecode.com',\r
107                                 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/wordcount',\r
108                                 version : tinymce.majorVersion + "." + tinymce.minorVersion\r
109                         };\r
110                 }\r
111         });\r
112 \r
113         tinymce.PluginManager.add('wordcount', tinymce.plugins.WordCount);\r
114 })();\r