includes/clientside/tinymce/plugins/wordcount/editor_plugin_src.js
changeset 1344 dc96d6c5cd1e
parent 1343 2a31905a567d
child 1345 1de01205143b
equal deleted inserted replaced
1343:2a31905a567d 1344:dc96d6c5cd1e
     1 /**
       
     2  * $Id: editor_plugin_src.js 201 2007-02-12 15:56:56Z spocke $
       
     3  *
       
     4  * @author Moxiecode
       
     5  * @copyright Copyright © 2004-2008, Moxiecode Systems AB, All rights reserved.
       
     6  */
       
     7 
       
     8 (function() {
       
     9     tinymce.create('tinymce.plugins.WordCount', {
       
    10 		block : 0,
       
    11 		id : null,
       
    12 		countre : null,
       
    13 		cleanre : null,
       
    14 
       
    15 		init : function(ed, url) {
       
    16 			var t = this, last = 0;
       
    17 
       
    18 			t.countre = ed.getParam('wordcount_countregex', /\S\s+/g);
       
    19 			t.cleanre = ed.getParam('wordcount_cleanregex', /[0-9.(),;:!?%#$¿'"_+=\\/-]*/g);
       
    20 			t.id = ed.id + '-word-count';
       
    21 
       
    22 			ed.onPostRender.add(function(ed, cm) {
       
    23 				var row, id;
       
    24 
       
    25 				// Add it to the specified id or the theme advanced path
       
    26 				id = ed.getParam('wordcount_target_id');
       
    27 				if (!id) {
       
    28 					row = tinymce.DOM.get(ed.id + '_path_row');
       
    29 
       
    30 					if (row)
       
    31 						tinymce.DOM.add(row.parentNode, 'div', {'style': 'float: right'}, ed.getLang('wordcount.words', 'Words: ') + '<span id="' + t.id + '">0</span>');
       
    32 				} else
       
    33 					tinymce.DOM.add(id, 'span', {}, '<span id="' + t.id + '">0</span>');
       
    34 			});
       
    35 
       
    36             ed.onInit.add(function(ed) {
       
    37 				ed.selection.onSetContent.add(function() {
       
    38 					t._count(ed);
       
    39 				});
       
    40 
       
    41 				t._count(ed);
       
    42 			});
       
    43 
       
    44 			ed.onSetContent.add(function(ed) {
       
    45 				t._count(ed);
       
    46 			});
       
    47 
       
    48 			ed.onKeyUp.add(function(ed, e) {
       
    49 				if (e.keyCode == last)
       
    50 					return;
       
    51 
       
    52 				if (13 == e.keyCode || 8 == last || 46 == last)
       
    53 					t._count(ed);
       
    54 
       
    55 				last = e.keyCode;
       
    56 			});
       
    57 		},
       
    58 
       
    59 		_count : function(ed) {
       
    60 			var t = this, tc = 0;
       
    61 
       
    62 			// Keep multiple calls from happening at the same time
       
    63 			if (t.block)
       
    64 				return;
       
    65 
       
    66 			t.block = 1;
       
    67 
       
    68 			setTimeout(function() {
       
    69 				var tx = ed.getContent({format : 'raw'});
       
    70 
       
    71 				if (tx) {
       
    72 					tx = tx.replace(/<.[^<>]*?>/g, ' ').replace(/&nbsp;|&#160;/gi, ' '); // remove html tags and space chars
       
    73 					tx = tx.replace(t.cleanre, ''); // remove numbers and punctuation
       
    74 					tx.replace(t.countre, function() {tc++;}); // count the words
       
    75 				}
       
    76 
       
    77 				tinymce.DOM.setHTML(t.id, tc.toString());
       
    78 
       
    79 				setTimeout(function() {t.block = 0;}, 2000);
       
    80 			}, 1);
       
    81 		},
       
    82 
       
    83         getInfo: function() {
       
    84 			return {
       
    85 				longname : 'Word Count plugin',
       
    86 				author : 'Moxiecode Systems AB',
       
    87 				authorurl : 'http://tinymce.moxiecode.com',
       
    88 				infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/wordcount',
       
    89 				version : tinymce.majorVersion + "." + tinymce.minorVersion
       
    90 			};
       
    91         }
       
    92     });
       
    93 
       
    94     tinymce.PluginManager.add('wordcount', tinymce.plugins.WordCount);
       
    95 })();