/
inline-loading-demo-button.js
executable file
·73 lines (68 loc) · 2.41 KB
/
inline-loading-demo-button.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
import mixin from 'carbon-components/es/globals/js/misc/mixin';
import createComponent from 'carbon-components/es/globals/js/mixins/create-component';
import initComponentBySearch from 'carbon-components/es/globals/js/mixins/init-component-by-search';
import handles from 'carbon-components/es/globals/js/mixins/handles';
import on from 'carbon-components/es/globals/js/misc/on';
import InlineLoading from 'carbon-components/es/components/inline-loading/inline-loading';
class InlineLoadingDemoButton extends mixin(
createComponent,
initComponentBySearch,
handles
) {
/**
* The button for inline loading demo.
* @extends CreateComponent
* @extends InitComponentBySearch
* @extends Handles
* @param {HTMLElement} element The element working as the button for inline loading demo.
*/
constructor(element) {
super(element);
this.manage(
on(element, 'click', event => {
this.toggle(event);
})
);
const targetElem = element.ownerDocument.querySelector(
this.options.selectorTarget
);
if (targetElem) {
this.state = InlineLoading.states.ACTIVE;
this.target = InlineLoading.create(targetElem).setState(this.state);
}
}
/**
* Toggles the state of inline loading component.
*/
toggle() {
if (this.target) {
this.state =
this.state === InlineLoading.states.ACTIVE
? InlineLoading.states.FINISHED
: InlineLoading.states.ACTIVE;
this.target.setState(this.state);
}
}
/**
* The map associating DOM element and the instance.
* @member InlineLoadingDemoButton.components
* @type {WeakMap}
*/
static components = new WeakMap();
/**
* The component options.
* If `options` is specified in the constructor, {@linkcode InlineLoadingDemoButton.create .create()},
* or {@linkcode InlineLoadingDemoButton.init .init()},
* properties in this object are overriden for the instance being create
* and how {@linkcode InlineLoadingDemoButton.init .init()} works.
* @member InlineLoadingDemoButton.options
* @type {Object}
* @property {string} selectorInit The CSS selector to find the button for inline loading demo.
* @property {string} selectorTarget The CSS selector to find the target `InlineLoading` component.
*/
static options = {
selectorInit: '[data-inline-loading-demo-button]',
selectorTarget: '[data-inline-loading]',
};
}
export default InlineLoadingDemoButton;