~canonical-sysadmins/wordpress/4.7.2

« back to all changes in this revision

Viewing changes to wp-admin/js/word-count.js

  • Committer: Jacek Nykis
  • Date: 2015-01-05 16:17:05 UTC
  • Revision ID: jacek.nykis@canonical.com-20150105161705-w544l1h5mcg7u4w9
Initial commit

Show diffs side-by-side

added added

removed removed

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