-
Notifications
You must be signed in to change notification settings - Fork 0
/
be-clonable.js
61 lines (61 loc) · 2.03 KB
/
be-clonable.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
import { config as beCnfg } from 'be-enhanced/config.js';
import { BE } from 'be-enhanced/BE.js';
export class BeClonable extends BE {
static config = {
propDefaults: {
byob: true,
triggerInsertPosition: 'beforeend',
cloneInsertPosition: 'afterend',
buttonContent: '❏'
},
propInfo: {
...(beCnfg.propInfo),
trigger: {
ro: true,
}
},
actions: {
addCloneBtn: {
ifAllOf: ['triggerInsertPosition'],
},
setBtnContent: {
ifAllOf: ['buttonContent', 'trigger'],
ifNoneOf: ['byob'],
}
},
handlers: {
trigger_to_beCloned_on: 'click'
}
};
async addCloneBtn(self) {
const { triggerInsertPosition, enhancedElement, buttonContent } = self;
const { findAdjacentElement } = await import('trans-render/lib/findAdjacentElement.js');
let trigger = findAdjacentElement(triggerInsertPosition, enhancedElement, 'button.be-clonable-trigger');
let byob = true;
if (trigger === null) {
byob = false;
trigger = document.createElement('button');
trigger.type = 'button';
trigger.classList.add('be-clonable-trigger');
trigger.ariaLabel = 'Clone this.';
trigger.title = 'Clone this.';
enhancedElement.insertAdjacentElement(triggerInsertPosition, trigger);
}
return {
trigger: new WeakRef(trigger),
resolved: true,
byob
};
}
setBtnContent({ buttonContent, trigger }) {
const btn = trigger?.deref();
if (btn === undefined)
return;
btn.innerHTML = buttonContent;
}
beCloned(self) {
const { enhancedElement, cloneInsertPosition } = self;
const clone = enhancedElement.cloneNode(true);
enhancedElement.insertAdjacentElement(cloneInsertPosition, clone);
}
}