-
Notifications
You must be signed in to change notification settings - Fork 1
/
depage-text-limiter.js
122 lines (107 loc) · 2.94 KB
/
depage-text-limiter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* @require framework/shared/jquery-1.8.3.js
*
* @file depage-text-limiter
*
* Depage Text Limiter Plugin.
*
* Limit the max length of textbox and text area input fields.
*
* copyright (c) 2006-2012 Frank Hellenkamp [jonas@depage.net]
*
* @author Ben Wallis
*/
(function($){
if(!$.depage){
$.depage = {};
};
/**
* tabs
*
* @param el - file input
* @param index
* @param options
*/
$.depage.textlimiter = function(el, index, options){
// To avoid scope issues, use 'base' instead of 'this' to reference this class from internal events and functions.
var base = this;
// Access to jQuery and DOM versions of element
base.$el = $(el);
base.el = el;
// Add a reverse reference to the DOM object
base.$el.data("depage.textlimiter", base);
// add a reference to an optional selector to store the number of remaining chars available.
var $remaining = null;
// {{{ init
/**
* Init
*
* Get the plugin options.
*
* @return void
*/
base.init = function(){
base.options = $.extend({}, $.depage.textlimiter.defaultOptions, options);
if (!base.options.max) {
base.options.max = base.$el.attr("maxlength");
}
if (base.options.remaining) {
$remaining = $(base.options.remaining);
}
base.limiter();
};
// }}}
// {{{ limiter()
/**
* Text Limiter
*
* @return void
*/
base.limiter = function(){
base.filter(null);
if (base.options.max) {
base.$el.bind('keyup keypress paste', function(e) {
base.filter(e);
});
}
};
// }}}
// {{{ filter()
/**
* Filter
*
* Filter the imput set the remaining chars
*
* @return void
*/
base.filter = function(e) {
var val = base.$el.val();
if (val.length >= base.options.max) {
val = val.substr(0, base.options.max);
base.$el.val(val);
}
if ($remaining) {
$remaining.html(base.options.max - val.length);
}
};
// }}}
base.init();
};
/**
* Options
*
* @param max - max chars to display otherwise taken from maxlength attribute
* @param showChars - display the remaining characters in the field
*
*/
$.depage.textlimiter.defaultOptions = {
max : 0,
remaining : false
};
$.fn.depageTextLimiter = function(options){
return this.each(function(index){
(new $.depage.textlimiter(this, index, options));
});
};
})(jQuery);
/* vim:set ft=javascript sw=4 sts=4 fdm=marker : */