Skip to content

Commit c89ff02

Browse files
authored
Merge pull request #41 from ractoon/t3mujin-master
Merge #38
2 parents 1316b99 + cdea17e commit c89ff02

File tree

3 files changed

+37
-24
lines changed

3 files changed

+37
-24
lines changed

README.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ Fires when counter is under min limit.
111111
```javascript
112112
type : "character", // "character" or "word"
113113
min : 0, // minimum number of characters/words
114-
max : 200, // maximum number of characters/words, -1 for unlimited, 'auto' to use maxlength attribute
114+
max : 200, // maximum number of characters/words, -1 for unlimited, 'auto' to use maxlength attribute, , 'autocustom' to use a custom attribute for the length (must set "autoCustomAttr")
115+
autoCustomAttr : "counterlimit", // custom attribute name with the counter limit if the max is 'autocustom'
115116
countContainerElement : "div", // HTML element to wrap the text count in
116117
countContainerClass : "text-count-wrapper", // class applied to the countContainerElement
117118
textCountMessageClass : "text-count-message", // class applied to the counter message
@@ -136,8 +137,8 @@ minDisplayCutoff : -1, // maximum number
136137
maxDisplayCutoff : -1, // maximum number of characters/words below the maximum to display a count
137138

138139
// Callback API
139-
maxunder : function(el){}, // Callback: function(element) - Fires when counter under max limit
140-
minunder : function(el){}, // Callback: function(element) - Fires when counter under min limit
140+
maxunder : function(el){}, // Callback: function(element) - Fires when counter is under max limit
141+
minunder : function(el){}, // Callback: function(element) - Fires when counter is under min limit
141142
maxcount : function(el){}, // Callback: function(element) - Fires when the counter hits the maximum word/character count
142143
mincount : function(el){}, // Callback: function(element) - Fires when the counter hits the minimum word/character count
143144
init : function(el){} // Callback: function(element) - Fires after the counter is initially setup
@@ -166,4 +167,5 @@ init : function(el){} // Callback: func
166167
- [dtipson](https://github.com/dtipson) - multiple classes error fix
167168
- [jmichalicek](https://github.com/jmichalicek) - count carriage returns/newlines as 2 characters
168169
- [diptopol](https://github.com/diptopol) - `stopInputAtMaximum` with `twoCharCarriageReturn` count fix, trimmed newline calculation fix, maximum text reached condition fix, text count overflow notification
169-
- [trevorloflin](https://github.com/trevorloflin) - `minDisplayCutoff` and `maxDisplayCutoff` options
170+
- [trevorloflin](https://github.com/trevorloflin) - `minDisplayCutoff` and `maxDisplayCutoff` options
171+
- [t3mujin](https://github.com/t3mujin) - autocustom support (maxlength workaround), text fixes

textcounter.js

+23-12
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@
6868
base.$container.text('error: [maxlength] attribute not set');
6969
}
7070
}
71+
else if (base.options.max == 'autocustom') {
72+
var max = base.$el.attr(base.options.autoCustomAttr);
73+
74+
if (typeof max !== 'undefined' && max !== false) {
75+
base.options.max = max;
76+
}
77+
else {
78+
base.$container.text('error: [' + base.options.autoCustomAttr + '] attribute not set');
79+
}
80+
}
7181

7282
// if this is a countdown counter deduct from the max characters/words
7383
textTotalCount = base.options.countDown ? base.options.max - textCount : textCount;
@@ -151,17 +161,17 @@
151161
base.clearErrors('max');
152162
}
153163
}
154-
155-
// hide the counter if it doesn't meet either the minimum or maximum display cutoff
156-
if (base.options.minDisplayCutoff == -1 && base.options.maxDisplayCutoff == -1) {
157-
base.$container.show();
158-
} else if (textCount <= base.options.min + base.options.minDisplayCutoff) {
159-
base.$container.show();
160-
} else if (base.options.max !== -1 && textCount >= base.options.max - base.options.maxDisplayCutoff) {
161-
base.$container.show();
162-
} else {
163-
base.$container.hide();
164-
}
164+
165+
// hide the counter if it doesn't meet either the minimum or maximum display cutoff
166+
if (base.options.minDisplayCutoff == -1 && base.options.maxDisplayCutoff == -1) {
167+
base.$container.show();
168+
} else if (textCount <= base.options.min + base.options.minDisplayCutoff) {
169+
base.$container.show();
170+
} else if (base.options.max !== -1 && textCount >= base.options.max - base.options.maxDisplayCutoff) {
171+
base.$container.show();
172+
} else {
173+
base.$container.hide();
174+
}
165175
};
166176

167177
base.textCount = function(text) {
@@ -305,7 +315,8 @@
305315
$.textcounter.defaultOptions = {
306316
'type' : "character", // "character" or "word"
307317
'min' : 0, // minimum number of characters/words
308-
'max' : 200, // maximum number of characters/words, -1 for unlimited, 'auto' to use maxlength attribute
318+
'max' : 200, // maximum number of characters/words, -1 for unlimited, 'auto' to use maxlength attribute, 'autocustom' to use a custom attribute for the length (must set "autoCustomAttr")
319+
'autoCustomAttr' : "counterlimit", // custom attribute name with the counter limit if the max is 'autocustom'
309320
'countContainerElement' : "div", // HTML element to wrap the text count in
310321
'countContainerClass' : "text-count-wrapper", // class applied to the countContainerElement
311322
'textCountMessageClass' : "text-count-message", // class applied to the counter message

textcounter.min.js

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)