-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathconstructor-methods.js
32 lines (31 loc) · 1022 Bytes
/
constructor-methods.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
import $ from './dom7.js';
export default function ConstructorMethods(parameters = {}) {
const { defaultSelector, constructor: Constructor, domProp, app, addMethods } = parameters;
const methods = {
create(...args) {
if (app) return new Constructor(app, ...args);
return new Constructor(...args);
},
get(el = defaultSelector) {
if (el instanceof Constructor) return el;
const $el = $(el);
if ($el.length === 0) return undefined;
return $el[0][domProp];
},
destroy(el) {
const instance = methods.get(el);
if (instance && instance.destroy) return instance.destroy();
return undefined;
},
};
if (addMethods && Array.isArray(addMethods)) {
addMethods.forEach((methodName) => {
methods[methodName] = (el = defaultSelector, ...args) => {
const instance = methods.get(el);
if (instance && instance[methodName]) return instance[methodName](...args);
return undefined;
};
});
}
return methods;
}