1 // Word count |
1 (function($,undefined) { |
2 (function($) { |
|
3 wpWordCount = { |
2 wpWordCount = { |
4 |
3 |
5 init : function() { |
4 settings : { |
6 var t = this, last = 0, co = $('#content'); |
5 strip : /<[a-zA-Z\/][^<>]*>/g, // strip HTML tags |
7 |
6 clean : /[0-9.(),;:!?%#$¿'"_+=\\/-]+/g, // regexp to remove punctuation, etc. |
8 $('#wp-word-count').html( wordCountL10n.count.replace( /%d/, '<span id="word-count">0</span>' ) ); |
7 w : /\S\s+/g, // word-counting regexp |
9 t.block = 0; |
8 c : /\S/g // char-counting regexp for asian languages |
10 t.wc(co.val()); |
|
11 co.keyup( function(e) { |
|
12 if ( e.keyCode == last ) return true; |
|
13 if ( 13 == e.keyCode || 8 == last || 46 == last ) t.wc(co.val()); |
|
14 last = e.keyCode; |
|
15 return true; |
|
16 }); |
|
17 }, |
9 }, |
18 |
10 |
19 wc : function(tx) { |
11 block : 0, |
20 var t = this, w = $('#word-count'), tc = 0; |
|
21 |
12 |
22 if ( t.block ) return; |
13 wc : function(tx, type) { |
|
14 var t = this, w = $('.word-count'), tc = 0; |
|
15 |
|
16 if ( type === undefined ) |
|
17 type = wordCountL10n.type; |
|
18 if ( type !== 'w' && type !== 'c' ) |
|
19 type = 'w'; |
|
20 |
|
21 if ( t.block ) |
|
22 return; |
|
23 |
23 t.block = 1; |
24 t.block = 1; |
24 |
25 |
25 setTimeout( function() { |
26 setTimeout( function() { |
26 if ( tx ) { |
27 if ( tx ) { |
27 tx = tx.replace( /<.[^<>]*?>/g, ' ' ).replace( / | /gi, ' ' ); |
28 tx = tx.replace( t.settings.strip, ' ' ).replace( / | /gi, ' ' ); |
28 tx = tx.replace( /[0-9.(),;:!?%#$¿'"_+=\\/-]*/g, '' ); |
29 tx = tx.replace( t.settings.clean, '' ); |
29 tx.replace( /\S\s+/g, function(){tc++;} ); |
30 tx.replace( t.settings[type], function(){tc++;} ); |
30 } |
31 } |
31 w.html(tc.toString()); |
32 w.html(tc.toString()); |
32 |
33 |
33 setTimeout( function() { t.block = 0; }, 2000 ); |
34 setTimeout( function() { t.block = 0; }, 2000 ); |
34 }, 1 ); |
35 }, 1 ); |
35 } |
36 } |
36 } |
37 } |
37 |
38 |
38 $(document).ready( function(){ wpWordCount.init(); } ); |
39 $(document).bind( 'wpcountwords', function(e, txt) { |
|
40 wpWordCount.wc(txt); |
|
41 }); |
39 }(jQuery)); |
42 }(jQuery)); |