root / docs / www / colonyscout / internal / jeditable / js / jquery.charcounter.js @ f59acf11
History | View | Annotate | Download (2.24 KB)
1 |
/**
|
---|---|
2 |
*
|
3 |
* Copyright (c) 2007 Tom Deater (http://www.tomdeater.com)
|
4 |
* Licensed under the MIT License:
|
5 |
* http://www.opensource.org/licenses/mit-license.php
|
6 |
*
|
7 |
*/
|
8 |
|
9 |
(function($) { |
10 |
/**
|
11 |
* attaches a character counter to each textarea element in the jQuery object
|
12 |
* usage: $("#myTextArea").charCounter(max, settings);
|
13 |
*/
|
14 |
|
15 |
$.fn.charCounter = function (max, settings) { |
16 |
max = max || 100;
|
17 |
settings = $.extend({
|
18 |
container: "<span></span>", |
19 |
classname: "charcounter", |
20 |
format: "(%1 characters remaining)", |
21 |
pulse: true, |
22 |
delay: 0 |
23 |
}, settings); |
24 |
var p, timeout;
|
25 |
|
26 |
function count(el, container) { |
27 |
el = $(el);
|
28 |
if (el.val().length > max) {
|
29 |
el.val(el.val().substring(0, max));
|
30 |
if (settings.pulse && !p) {
|
31 |
pulse(container, true);
|
32 |
}; |
33 |
}; |
34 |
if (settings.delay > 0) { |
35 |
if (timeout) {
|
36 |
window.clearTimeout(timeout); |
37 |
} |
38 |
timeout = window.setTimeout(function () {
|
39 |
container.html(settings.format.replace(/%1/, (max - el.val().length)));
|
40 |
}, settings.delay); |
41 |
} else {
|
42 |
container.html(settings.format.replace(/%1/, (max - el.val().length)));
|
43 |
} |
44 |
}; |
45 |
|
46 |
function pulse(el, again) { |
47 |
if (p) {
|
48 |
window.clearTimeout(p); |
49 |
p = null;
|
50 |
}; |
51 |
el.animate({ opacity: 0.1 }, 100, function () { |
52 |
$(this).animate({ opacity: 1.0 }, 100); |
53 |
}); |
54 |
if (again) {
|
55 |
p = window.setTimeout(function () { pulse(el) }, 200); |
56 |
}; |
57 |
}; |
58 |
|
59 |
return this.each(function () { |
60 |
var container = (!settings.container.match(/^<.+>$/)) |
61 |
? $(settings.container)
|
62 |
: $(settings.container)
|
63 |
.insertAfter(this)
|
64 |
.addClass(settings.classname); |
65 |
$(this) |
66 |
.bind("keydown", function () { count(this, container); }) |
67 |
.bind("keypress", function () { count(this, container); }) |
68 |
.bind("keyup", function () { count(this, container); }) |
69 |
.bind("focus", function () { count(this, container); }) |
70 |
.bind("mouseover", function () { count(this, container); }) |
71 |
.bind("mouseout", function () { count(this, container); }) |
72 |
.bind("paste", function () { |
73 |
var me = this; |
74 |
setTimeout(function () { count(me, container); }, 10); |
75 |
}); |
76 |
if (this.addEventListener) { |
77 |
this.addEventListener('input', function () { count(this, container); }, false); |
78 |
}; |
79 |
count(this, container);
|
80 |
}); |
81 |
}; |
82 |
|
83 |
})(jQuery); |