-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathform-element.js
195 lines (176 loc) · 5.65 KB
/
form-element.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
'use strict';
// we need Map (which requires Symbol) here
import 'core-js/es6/symbol';
import Map from 'core-js/es6/map';
/**
* Bubbles up each parent node of the element, triggering the callback on each element until traversal
* either runs out of parent nodes, reaches the document element, or if callback returns a falsy value
* @param {HTMLElement} [startEl] - The element where traversal will begin (including the passed element), defaults to current el
* @param {Function} callback - A callback that fires which gets passed the current element
*/
let traverseEachParent = function (startEl, callback) {
var parentNode = startEl.parentNode || startEl,
predicate;
// check if the node has classname property, if not, we know we're at the #document element
while (parentNode && typeof parentNode.className === 'string') {
predicate = callback(parentNode);
if (predicate !== undefined && !predicate) {
break;
}
parentNode = parentNode.parentNode;
}
};
/**
* @class FormElement
* @description An extendable base class that provides common functionality among all form elements.
*/
class FormElement {
/**
* Sets up stuff.
* @abstract
* @param {Object} options - Instantiation options
*/
constructor (options) {
this.options = options || {};
this._eventListeners = new Map();
}
/**
* Gets the form element.
* @returns {HTMLElement} Returns the form element
* @abstract
*/
getFormElement () {
return this.options.el;
}
/**
* Gets the ui version of the form element.
* @returns {HTMLElement} Returns the ui-version of the element.
* @abstract
*/
getUIElement () {
return this.getFormElement();
}
/**
* Gets the form elements.
* @returns {Array} Returns the array of form elements
* @abstract
*/
getFormElements () {
return [this.getFormElement()];
}
/**
* Gets the current value of the element.
* @returns {string}
* @abstract
*/
getValue () {
return this.getFormElement().value;
}
/**
* Sets the value of the form element.
* @param {string} value - The new value
* @abstract
*/
setValue (value) {
var el = this.getFormElements()[0];
if (el) {
el.value = value;
}
}
/**
* Clears the element.
* @abstract
*/
clear () {}
/**
* Gets the ui versions of the form elements.
* @returns {Array} Returns the array of ui-versions of the element.
* @abstract
*/
getUIElements () {
return [this.getUIElement()];
}
/**
* Enables the form element.
* @abstract
*/
enable () {
this.getFormElement().disabled = false;
}
/**
* Disables the form element.
* @abstract
*/
disable () {
this.getFormElement().disabled = true;
}
/**
* Gets the element's identifier (preferably unique from all other elements that extend this class).
* @returns {string} Return the unique key
* @abstract
*/
static getElementKey () {
return 'element';
}
/**
* Adds an event listener to an element.
* @param {HTMLElement} element - The element to add the listener to
* @param {String} eventName - The name of the event
* @param {String} method - The name of the method to call when event fires
* @param {Object} [context] - The context in which to call the method parameter
* @param {Boolean} [useCapture] - Whether to use capture
*/
addEventListener (element, eventName, method, context, useCapture) {
context = context || this;
let listener = context[method].bind(context);
element.addEventListener(eventName, listener, useCapture);
this._eventListeners.set(listener, {
name: eventName,
el: element,
method: method,
context: context
});
}
/**
* Gets the closest ancestor element that has a css class.
* @param {HTMLElement} el - The element of which to get the closest ancestor
* @param {string} className - The class name that the ancestor must have to match
*/
getClosestAncestorElementByClassName (el, className) {
let result = null;
traverseEachParent(el, function (parent) {
if (parent.classList.contains(className)) {
result = parent;
return false;
}
});
return result;
}
/**
* Removes an event listener from an element.
* @param {HTMLElement} element - The element to add the listener to
* @param {String} eventName - The name of the event
* @param {String} method - The name of the method to call when event fires
* @param {Object} [context] - The context in which to call the method parameter
* @param {Boolean} [useCapture] - Whether to use capture
*/
removeEventListener (element, eventName, method, context, useCapture) {
for (let [listener, obj] of this._eventListeners) {
if (obj.el === element && obj.name === eventName && obj.context === context && obj.method === method) {
element.removeEventListener(eventName, listener, useCapture);
this._eventListeners.delete(listener);
break;
}
}
}
/**
* Removes all event listeners.
*/
destroy () {
this._eventListeners.forEach((obj) => {
this.removeEventListener(obj.el, obj.name, obj.method, obj.context);
});
this._eventListeners.clear();
}
}
module.exports = FormElement;