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