|
1 | | -'use strict' |
2 | | - |
3 | | -const UpdateTriggerDisplay = (_txt, _target, _trigger, _class ) => { |
4 | | - if (_txt){ |
5 | | - if(_target) _target.innerText = _txt; |
6 | | - else _trigger.innerHTML = _txt; |
7 | | - } |
8 | | - if (_class) _trigger.classList.toggle(_class); |
9 | | -} |
10 | | - |
11 | | -const SetCopyToClipboard = () => { |
12 | | - const copyTriggers = document.querySelectorAll('[wt-copycb-element^="trigger-"], [wt-copycb-element="trigger"]'); |
| 1 | +class CopyToClipboard { |
| 2 | + constructor(_ctcContainer) { |
| 3 | + this.ctcContainer = _ctcContainer; |
| 4 | + this.ctcTrigger = this.ctcContainer.querySelector(`[wt-copycb-element="trigger"]`) || null; |
| 5 | + this.ctcTarget = this.ctcContainer.querySelector(`[wt-copycb-element="target"]`) || null; |
| 6 | + this.textTarget = this.ctcTrigger.querySelector(`[wt-copycb-element="texttarget"]`) || null; |
| 7 | + this.ctcDefaultTxt = this.ctcTrigger.innerText; |
| 8 | + this.textToCopy = this.ctcTarget.innerText; |
| 9 | + this.copiedTxt = this.ctcTrigger.getAttribute("wt-copycb-message") || null; |
| 10 | + this.activeClass = this.ctcTrigger.getAttribute('wt-copycb-active') || 'is-copy'; |
| 11 | + this.timeOut = this.ctcTrigger.getAttribute('wt-copycb-timeout') || 2000; |
| 12 | + this.initialize(); |
| 13 | + } |
13 | 14 |
|
14 | | - if(!copyTriggers || copyTriggers.length === 0) return; |
| 15 | + initialize() { |
| 16 | + if(!this.ctcTrigger || !this.ctcTarget) return; |
| 17 | + this.ctcTrigger.addEventListener('click', () => this.copyTextToClipboard()); |
| 18 | + } |
15 | 19 |
|
16 | | - for(let _trigger of copyTriggers) { |
17 | | - let _triggerAttr = _trigger.getAttribute(`wt-copycb-element`); |
18 | | - let index = _triggerAttr.replace('trigger',''); |
19 | | - let _target = document.querySelector(`[wt-copycb-element="target${index}"]`); |
| 20 | + copyTextToClipboard() { |
| 21 | + this.updateTriggerDisplay(); |
| 22 | + navigator.clipboard.writeText(this.textToCopy); |
| 23 | + setTimeout(() => this.resetTriggerDisplay(), this.timeOut); |
| 24 | + } |
20 | 25 |
|
21 | | - if(_target) { |
22 | | - _trigger.addEventListener('click', () => { |
23 | | - let textToCopy = _target.innerText; |
24 | | - let copiedTxt = _trigger.getAttribute("wt-copycb-message"); |
25 | | - let activeClass = _trigger.getAttribute('wt-copycb-active'); |
26 | | - let timeOut = _trigger.getAttribute('wt-copycb-timeout') || 2000; |
27 | | - let _defaultTxt = _trigger.innerText; |
28 | | - let textTarget = document.querySelector(`[wt-copycb-element="text-target${index}"]`); |
| 26 | + updateTriggerDisplay() { |
| 27 | + if (this.copiedTxt) { |
| 28 | + if(this.textTarget)this.textTarget.innerText = this.copiedTxt; |
| 29 | + else this.ctcTrigger.innerText = this.copiedTxt; |
| 30 | + } |
| 31 | + if (this.activeClass) this.ctcTrigger.classList.toggle(this.activeClass); |
| 32 | + } |
29 | 33 |
|
30 | | - UpdateTriggerDisplay(copiedTxt, textTarget, _trigger, activeClass); |
31 | | - setTimeout(() => { |
32 | | - UpdateTriggerDisplay(_defaultTxt, textTarget, _trigger, activeClass); |
33 | | - }, timeOut); |
34 | | - navigator.clipboard.writeText(textToCopy); |
35 | | - }); |
| 34 | + resetTriggerDisplay() { |
| 35 | + if (this.copiedTxt) { |
| 36 | + if(this.textTarget)this.textTarget.innerText = this.ctcDefaultTxt; |
| 37 | + else this.ctcTrigger.innerText = this.ctcDefaultTxt; |
36 | 38 | } |
| 39 | + if (this.activeClass) this.ctcTrigger.classList.toggle(this.activeClass); |
37 | 40 | } |
38 | 41 | } |
39 | 42 |
|
40 | | -window.addEventListener('DOMContentLoaded', SetCopyToClipboard()); |
| 43 | +const initializeCopyToClipboard = () => { |
| 44 | + window.trickeries = window.trickeries || []; |
| 45 | + const triggers = document.querySelectorAll('[wt-copycb-element="container"]'); |
| 46 | + triggers.forEach(trigger => { |
| 47 | + let instance = new CopyToClipboard(trigger); |
| 48 | + window.trickeries.push({'CopyToClipboard': instance}); |
| 49 | + }); |
| 50 | +} |
| 51 | + |
| 52 | +if (/complete|interactive|loaded/.test(document.readyState)) { |
| 53 | + initializeCopyToClipboard(); |
| 54 | +} else { |
| 55 | + window.addEventListener('DOMContentLoaded', initializeCopyToClipboard); |
| 56 | +} |
0 commit comments