Skip to content

Commit 550e201

Browse files
committed
refactor: removed comments
1 parent 7ac621b commit 550e201

File tree

5 files changed

+563
-789
lines changed

5 files changed

+563
-789
lines changed

docs/js/materialize.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/characterCounter.ts

Lines changed: 71 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,88 @@
11
import { Component } from "./component";
2-
import $ from "cash-dom";
32

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+
}
3620

37-
static get defaults() {
38-
return _defaults;
39-
}
21+
static get defaults() {
22+
return _defaults;
23+
}
4024

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+
}
4428

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+
}
5233

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+
}
6139

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+
}
6745

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+
}
7150

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+
}
7959

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+
}
9263

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;
9567

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();
10173
}
74+
this.counterEl.innerHTML = counterString;
75+
}
10276

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');
11881
}
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');
13286
}
13387
}
88+
}

0 commit comments

Comments
 (0)