|
1 | 1 | import { Component } from "./component";
|
2 |
| -import $ from "cash-dom"; |
3 | 2 |
|
4 |
| - let _defaults = {}; |
5 |
| - |
6 |
| - /** |
7 |
| - * @class |
8 |
| - * |
9 |
| - */ |
10 |
| - export class CharacterCounter extends Component { |
11 |
| - isInvalid: boolean; |
12 |
| - isValidLength: boolean; |
13 |
| - private _handleUpdateCounterBound: any; |
14 |
| - counterEl: HTMLSpanElement; |
15 |
| - /** |
16 |
| - * Construct CharacterCounter instance |
17 |
| - * @constructor |
18 |
| - * @param {Element} el |
19 |
| - * @param {Object} options |
20 |
| - */ |
21 |
| - constructor(el, options) { |
22 |
| - super(CharacterCounter, el, options); |
23 |
| - |
24 |
| - (this.el as any).M_CharacterCounter = this; |
25 |
| - |
26 |
| - /** |
27 |
| - * Options for the character counter |
28 |
| - */ |
29 |
| - this.options = $.extend({}, CharacterCounter.defaults, options); |
30 |
| - |
31 |
| - this.isInvalid = false; |
32 |
| - this.isValidLength = false; |
33 |
| - this._setupCounter(); |
34 |
| - this._setupEventHandlers(); |
35 |
| - } |
| 3 | +let _defaults = {}; |
| 4 | + |
| 5 | +export class CharacterCounter extends Component { |
| 6 | + isInvalid: boolean; |
| 7 | + isValidLength: boolean; |
| 8 | + private _handleUpdateCounterBound: any; |
| 9 | + counterEl: HTMLSpanElement; |
| 10 | + |
| 11 | + constructor(el: Element, options: Object) { |
| 12 | + super(CharacterCounter, el, options); |
| 13 | + (this.el as any).M_CharacterCounter = this; |
| 14 | + this.options = {...CharacterCounter.defaults, ...options}; |
| 15 | + this.isInvalid = false; |
| 16 | + this.isValidLength = false; |
| 17 | + this._setupCounter(); |
| 18 | + this._setupEventHandlers(); |
| 19 | + } |
36 | 20 |
|
37 |
| - static get defaults() { |
38 |
| - return _defaults; |
39 |
| - } |
| 21 | + static get defaults() { |
| 22 | + return _defaults; |
| 23 | + } |
40 | 24 |
|
41 |
| - static init(els, options) { |
42 |
| - return super.init(this, els, options); |
43 |
| - } |
| 25 | + static init(els, options) { |
| 26 | + return super.init(this, els, options); |
| 27 | + } |
44 | 28 |
|
45 |
| - /** |
46 |
| - * Get Instance |
47 |
| - */ |
48 |
| - static getInstance(el) { |
49 |
| - let domElem = !!el.jquery ? el[0] : el; |
50 |
| - return domElem.M_CharacterCounter; |
51 |
| - } |
| 29 | + static getInstance(el) { |
| 30 | + let domElem = !!el.jquery ? el[0] : el; |
| 31 | + return domElem.M_CharacterCounter; |
| 32 | + } |
52 | 33 |
|
53 |
| - /** |
54 |
| - * Teardown component |
55 |
| - */ |
56 |
| - destroy() { |
57 |
| - this._removeEventHandlers(); |
58 |
| - (this.el as any).CharacterCounter = undefined; |
59 |
| - this._removeCounter(); |
60 |
| - } |
| 34 | + destroy() { |
| 35 | + this._removeEventHandlers(); |
| 36 | + (this.el as any).CharacterCounter = undefined; |
| 37 | + this._removeCounter(); |
| 38 | + } |
61 | 39 |
|
62 |
| - /** |
63 |
| - * Setup Event Handlers |
64 |
| - */ |
65 |
| - _setupEventHandlers() { |
66 |
| - this._handleUpdateCounterBound = this.updateCounter.bind(this); |
| 40 | + _setupEventHandlers() { |
| 41 | + this._handleUpdateCounterBound = this.updateCounter.bind(this); |
| 42 | + this.el.addEventListener('focus', this._handleUpdateCounterBound, true); |
| 43 | + this.el.addEventListener('input', this._handleUpdateCounterBound, true); |
| 44 | + } |
67 | 45 |
|
68 |
| - this.el.addEventListener('focus', this._handleUpdateCounterBound, true); |
69 |
| - this.el.addEventListener('input', this._handleUpdateCounterBound, true); |
70 |
| - } |
| 46 | + _removeEventHandlers() { |
| 47 | + this.el.removeEventListener('focus', this._handleUpdateCounterBound, true); |
| 48 | + this.el.removeEventListener('input', this._handleUpdateCounterBound, true); |
| 49 | + } |
71 | 50 |
|
72 |
| - /** |
73 |
| - * Remove Event Handlers |
74 |
| - */ |
75 |
| - _removeEventHandlers() { |
76 |
| - this.el.removeEventListener('focus', this._handleUpdateCounterBound, true); |
77 |
| - this.el.removeEventListener('input', this._handleUpdateCounterBound, true); |
78 |
| - } |
| 51 | + _setupCounter() { |
| 52 | + this.counterEl = document.createElement('span'); |
| 53 | + this.counterEl.classList.add('character-counter'); |
| 54 | + this.counterEl.style.float = 'right'; |
| 55 | + this.counterEl.style.fontSize = '12px'; |
| 56 | + this.counterEl.style.height = '1'; |
| 57 | + this.el.parentElement.appendChild(this.counterEl); |
| 58 | + } |
79 | 59 |
|
80 |
| - /** |
81 |
| - * Setup counter element |
82 |
| - */ |
83 |
| - _setupCounter() { |
84 |
| - this.counterEl = document.createElement('span'); |
85 |
| - $(this.counterEl) |
86 |
| - .addClass('character-counter') |
87 |
| - .css({ |
88 |
| - float: 'right', |
89 |
| - 'font-size': '12px', |
90 |
| - height: 1 |
91 |
| - }); |
| 60 | + _removeCounter() { |
| 61 | + this.counterEl.remove(); |
| 62 | + } |
92 | 63 |
|
93 |
| - this.$el.parent().append(this.counterEl); |
94 |
| - } |
| 64 | + updateCounter() { |
| 65 | + let maxLength = parseInt(this.el.getAttribute('data-length')), |
| 66 | + actualLength = (this.el as HTMLInputElement).value.length; |
95 | 67 |
|
96 |
| - /** |
97 |
| - * Remove counter element |
98 |
| - */ |
99 |
| - _removeCounter() { |
100 |
| - $(this.counterEl).remove(); |
| 68 | + this.isValidLength = actualLength <= maxLength; |
| 69 | + let counterString = actualLength.toString(); |
| 70 | + if (maxLength) { |
| 71 | + counterString += '/' + maxLength; |
| 72 | + this._validateInput(); |
101 | 73 | }
|
| 74 | + this.counterEl.innerHTML = counterString; |
| 75 | + } |
102 | 76 |
|
103 |
| - /** |
104 |
| - * Update counter |
105 |
| - */ |
106 |
| - updateCounter() { |
107 |
| - let maxLength = +this.$el.attr('data-length'), |
108 |
| - actualLength = (this.el as HTMLInputElement).value.length; |
109 |
| - this.isValidLength = actualLength <= maxLength; |
110 |
| - let counterString = actualLength.toString(); |
111 |
| - |
112 |
| - if (maxLength) { |
113 |
| - counterString += '/' + maxLength; |
114 |
| - this._validateInput(); |
115 |
| - } |
116 |
| - |
117 |
| - $(this.counterEl).html(counterString); |
| 77 | + _validateInput() { |
| 78 | + if (this.isValidLength && this.isInvalid) { |
| 79 | + this.isInvalid = false; |
| 80 | + this.el.classList.remove('invalid'); |
118 | 81 | }
|
119 |
| - |
120 |
| - /** |
121 |
| - * Add validation classes |
122 |
| - */ |
123 |
| - _validateInput() { |
124 |
| - if (this.isValidLength && this.isInvalid) { |
125 |
| - this.isInvalid = false; |
126 |
| - this.$el.removeClass('invalid'); |
127 |
| - } else if (!this.isValidLength && !this.isInvalid) { |
128 |
| - this.isInvalid = true; |
129 |
| - this.$el.removeClass('valid'); |
130 |
| - this.$el.addClass('invalid'); |
131 |
| - } |
| 82 | + else if (!this.isValidLength && !this.isInvalid) { |
| 83 | + this.isInvalid = true; |
| 84 | + this.el.classList.remove('valid'); |
| 85 | + this.el.classList.add('invalid'); |
132 | 86 | }
|
133 | 87 | }
|
| 88 | +} |
0 commit comments