-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathcharacterCounter.js
150 lines (126 loc) · 3.23 KB
/
characterCounter.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
(function ($) {
'use strict';
let _defaults = {
};
/**
* @class
*
*/
class CharacterCounter {
/**
* Construct CharacterCounter instance
* @constructor
* @param {Element} el
* @param {Object} options
*/
constructor(el, options) {
// If exists, destroy and reinitialize
if (!!el.M_CharacterCounter) {
el.M_CharacterCounter.destroy();
}
this.el = el;
this.$el = $(el);
this.el.M_CharacterCounter = this;
/**
* Options for the character counter
*/
this.options = $.extend({}, CharacterCounter.defaults, options);
this.isInvalid = false;
this.isValidLength = false;
this._setupCounter();
this._setupEventHandlers();
}
static get defaults() {
return _defaults;
}
static init($els, options) {
let arr = [];
$els.each(function() {
arr.push(new CharacterCounter(this, options));
});
return arr;
}
/**
* Get Instance
*/
static getInstance(el) {
let domElem = !!el.jquery ? el[0] : el;
return domElem.M_CharacterCounter;
}
/**
* Teardown component
*/
destroy() {
this._removeEventHandlers();
this.el.CharacterCounter = undefined;
this._removeCounter();
}
/**
* Setup Event Handlers
*/
_setupEventHandlers() {
this._handleUpdateCounterBound = this.updateCounter.bind(this);
this.el.addEventListener('focus', this._handleUpdateCounterBound, true);
this.el.addEventListener('input', this._handleUpdateCounterBound, true);
}
/**
* Remove Event Handlers
*/
_removeEventHandlers() {
this.el.removeEventListener('focus', this._handleUpdateCounterBound, true);
this.el.removeEventListener('input', this._handleUpdateCounterBound, true);
}
/**
* Setup counter element
*/
_setupCounter() {
this.counterEl = document.createElement('span');
$(this.counterEl)
.addClass('character-counter')
.css({
float: 'right',
'font-size': '12px',
height: 1
});
this.$el.parent().append(this.counterEl);
}
/**
* Remove counter element
*/
_removeCounter() {
$(this.counterEl).remove();
}
/**
* Update counter
*/
updateCounter() {
let maxLength = +this.$el.attr('data-length'),
actualLength = this.el.value.length;
this.isValidLength = actualLength <= maxLength;
let counterString = actualLength;
if (maxLength) {
counterString += '/' + maxLength;
this._validateInput();
}
$(this.counterEl).html(counterString);
}
/**
* Add validation classes
*/
_validateInput() {
if (this.isValidLength && this.isInvalid) {
this.isInvalid = false;
this.$el.removeClass('invalid');
}
else if(!this.isValidLength && !this.isInvalid){
this.isInvalid = true;
this.$el.removeClass('valid');
this.$el.addClass('invalid');
}
}
}
M.CharacterCounter = CharacterCounter;
if (M.jQueryLoaded) {
M.initializeJqueryWrapper(CharacterCounter, 'characterCounter', 'M_CharacterCounter');
}
}( cash ));