|
1 | | -export function init(id) { |
2 | | - const list = document.getElementById(id); |
3 | | - console.log(list); |
| 1 | +import Data from '../../../_content/BootstrapBlazor/modules/data.js'; |
| 2 | +import EventHandler from '../../../_content/BootstrapBlazor/modules/event-handler.js'; |
| 3 | +import { copy } from '../../../_content/BootstrapBlazor/modules/utility.js'; |
| 4 | +export function init(id) { |
| 5 | + const el = document.getElementById(id); |
| 6 | + const tooltips = []; |
| 7 | + [...el.querySelectorAll('svg')].forEach(s => { |
| 8 | + const use = s.querySelector('use'); |
| 9 | + const url = use.getAttribute('href'); |
| 10 | + const segs = url.split('#'); |
| 11 | + if (segs.length == 2) { |
| 12 | + const title = segs[1]; |
| 13 | + s.parentElement.setAttribute('data-bs-original-title', title); |
| 14 | + tooltips.push(new bootstrap.Tooltip(s.parentElement)); |
| 15 | + } |
| 16 | + }); |
| 17 | + EventHandler.on(el, 'click', 'div', e => { |
| 18 | + const div = e.delegateTarget; |
| 19 | + const href = div.querySelector('use').getAttribute('href'); |
| 20 | + const text = `<svg xmlns="http://www.w3.org/2000/svg"><use href="${href}"></use></svg>`; |
| 21 | + copy(text); |
| 22 | + const tooltip = bootstrap.Tooltip.getInstance(div); |
| 23 | + tooltip.setContent({ '.tooltip-inner': 'Copy' }); |
| 24 | + const handler = setTimeout(() => { |
| 25 | + clearTimeout(handler); |
| 26 | + tooltip.setContent({ '.tooltip-inner': div.getAttribute('data-bs-original-title') }); |
| 27 | + }, 1000); |
| 28 | + }); |
| 29 | + Data.set(id, { |
| 30 | + el, |
| 31 | + tooltips |
| 32 | + }) |
4 | 33 | } |
5 | 34 |
|
6 | 35 | export function dispose(id) { |
| 36 | + const data = Data.get(id); |
| 37 | + Data.remove(id); |
7 | 38 |
|
| 39 | + const { el, tooltips } = data; |
| 40 | + EventHandler.off(el, 'click'); |
| 41 | + tooltips.forEach(t => t.dispose()); |
8 | 42 | } |
0 commit comments