-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
init-component-by-launcher.js
78 lines (72 loc) · 3.16 KB
/
init-component-by-launcher.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
/**
* Copyright IBM Corp. 2016, 2018
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import eventMatches from '../misc/event-matches';
import on from '../misc/on';
export default function(ToMix) {
/**
* Mix-in class to instantiate components events on launcher button.
* @class InitComponentByLauncher
*/
class InitComponentByLauncher extends ToMix {
/**
* `true` suggests that this component is lazily initialized upon an action/event, etc.
* @type {boolean}
*/
static forLazyInit /* #__PURE_CLASS_PROPERTY__ */ = true;
/**
* Instantiates this component in the given element.
* If the given element indicates that it's an component of this class, instantiates it.
* Otherwise, instantiates this component by clicking on launcher buttons
* (buttons with attribute that `options.attribInitTarget` points to) of this component in the given node.
* @param {Node} target The DOM node to instantiate this component in. Should be a document or an element.
* @param {Object} [options] The component options.
* @param {string} [options.selectorInit] The CSS selector to find this component.
* @param {string} [options.attribInitTarget] The attribute name in the launcher buttons to find target component.
* @returns {Handle} The handle to remove the event listener to handle clicking.
*/
static init(target = document, options = {}) {
const effectiveOptions = Object.assign(Object.create(this.options), options);
if (!target || (target.nodeType !== Node.ELEMENT_NODE && target.nodeType !== Node.DOCUMENT_NODE)) {
throw new TypeError('DOM document or DOM element should be given to search for and initialize this widget.');
}
if (target.nodeType === Node.ELEMENT_NODE && target.matches(effectiveOptions.selectorInit)) {
this.create(target, options);
} else {
const handles = effectiveOptions.initEventNames.map(name =>
on(target, name, event => {
const launcher = eventMatches(event, `[${effectiveOptions.attribInitTarget}]`);
if (launcher) {
event.delegateTarget = launcher; // eslint-disable-line no-param-reassign
const elements = launcher.ownerDocument.querySelectorAll(launcher.getAttribute(effectiveOptions.attribInitTarget));
if (elements.length > 1) {
throw new Error('Target widget must be unique.');
}
if (elements.length === 1) {
if (launcher.tagName === 'A') {
event.preventDefault();
}
const component = this.create(elements[0], options);
if (typeof component.createdByLauncher === 'function') {
component.createdByLauncher(event);
}
}
}
})
);
return {
release() {
for (let handle = handles.pop(); handle; handle = handles.pop()) {
handle.release();
}
},
};
}
return '';
}
}
return InitComponentByLauncher;
}