wp/wp-admin/js/word-count.js
author ymh <ymh.work@gmail.com>
Tue, 09 Jun 2015 11:14:17 +0000
changeset 6 490d5cc509ed
parent 5 5e2f62d02dcd
child 7 cf61fcea0001
permissions -rw-r--r--
update portfolio
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     1
/* global wordCountL10n */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     2
var wpWordCount;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
(function($,undefined) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
	wpWordCount = {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
		settings : {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
			strip : /<[a-zA-Z\/][^<>]*>/g, // strip HTML tags
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
			clean : /[0-9.(),;:!?%#$¿'"_+=\\/-]+/g, // regexp to remove punctuation, etc.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
			w : /\S\s+/g, // word-counting regexp
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
			c : /\S/g // char-counting regexp for asian languages
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
		block : 0,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
		wc : function(tx, type) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
			var t = this, w = $('.word-count'), tc = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
			if ( type === undefined )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
				type = wordCountL10n.type;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
			if ( type !== 'w' && type !== 'c' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
				type = 'w';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
			if ( t.block )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
				return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
			t.block = 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
			setTimeout( function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
				if ( tx ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
					tx = tx.replace( t.settings.strip, ' ' ).replace( /&nbsp;|&#160;/gi, ' ' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
					tx = tx.replace( t.settings.clean, '' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
					tx.replace( t.settings[type], function(){tc++;} );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
				w.html(tc.toString());
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
				setTimeout( function() { t.block = 0; }, 2000 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
			}, 1 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
		}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    39
	};
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
	$(document).bind( 'wpcountwords', function(e, txt) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
		wpWordCount.wc(txt);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
	});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
}(jQuery));