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