|
| 1 | +/** |
| 2 | + * Hook used by LiveView dashboard. |
| 3 | + * |
| 4 | + * Defines various widgets to use by various dashboard specific components. |
| 5 | + */ |
| 6 | + |
| 7 | +const WIDGETS = { |
| 8 | + // Hook widget delegating navigation events to and from React. |
| 9 | + // Necessary to emulate navigation events in LiveView with pushState |
| 10 | + // manipulation disabled. |
| 11 | + 'dashboard-root': { |
| 12 | + initialize: function () { |
| 13 | + this.url = window.location.href |
| 14 | + |
| 15 | + addListener.bind(this)('click', document.body, (e) => { |
| 16 | + const type = e.target.dataset.type || null |
| 17 | + |
| 18 | + if (type === 'dashboard-link') { |
| 19 | + this.url = e.target.href |
| 20 | + const uri = new URL(this.url) |
| 21 | + // Domain is dropped from URL prefix, because that's what react-dom-router |
| 22 | + // expects. |
| 23 | + const path = '/' + uri.pathname.split('/').slice(2).join('/') |
| 24 | + this.el.dispatchEvent( |
| 25 | + new CustomEvent('dashboard:live-navigate', { |
| 26 | + bubbles: true, |
| 27 | + detail: { path: path, search: uri.search } |
| 28 | + }) |
| 29 | + ) |
| 30 | + |
| 31 | + this.pushEvent('handle_dashboard_params', { url: this.url }) |
| 32 | + |
| 33 | + e.preventDefault() |
| 34 | + } |
| 35 | + }) |
| 36 | + |
| 37 | + // Browser back and forward navigation triggers that event. |
| 38 | + addListener.bind(this)('popstate', window, () => { |
| 39 | + if (this.url !== window.location.href) { |
| 40 | + this.pushEvent('handle_dashboard_params', { |
| 41 | + url: window.location.href |
| 42 | + }) |
| 43 | + } |
| 44 | + }) |
| 45 | + |
| 46 | + // Navigation events triggered from liveview are propagated via this |
| 47 | + // handler. |
| 48 | + addListener.bind(this)('dashboard:live-navigate-back', window, (e) => { |
| 49 | + if ( |
| 50 | + typeof e.detail.search === 'string' && |
| 51 | + this.url !== window.location.href |
| 52 | + ) { |
| 53 | + this.pushEvent('handle_dashboard_params', { |
| 54 | + url: window.location.href |
| 55 | + }) |
| 56 | + } |
| 57 | + }) |
| 58 | + }, |
| 59 | + cleanup: function () { |
| 60 | + removeListeners.bind(this)() |
| 61 | + } |
| 62 | + }, |
| 63 | + // Hook widget for optimistic loading of tabs and |
| 64 | + // client-side persistence of selection using localStorage. |
| 65 | + tabs: { |
| 66 | + initialize: function () { |
| 67 | + const domain = getDomain(window.location.href) |
| 68 | + |
| 69 | + addListener.bind(this)('click', this.el, (e) => { |
| 70 | + const button = e.target.closest('button') |
| 71 | + const tab = button && button.dataset.tab |
| 72 | + |
| 73 | + if (tab) { |
| 74 | + const label = button.dataset.label |
| 75 | + const storageKey = button.dataset.storageKey |
| 76 | + const activeClasses = button.dataset.activeClasses |
| 77 | + const inactiveClasses = button.dataset.inactiveClasses |
| 78 | + const title = this.el |
| 79 | + .closest('[data-tile]') |
| 80 | + .querySelector('[data-title]') |
| 81 | + |
| 82 | + title.innerText = label |
| 83 | + |
| 84 | + this.el.querySelectorAll(`button[data-tab] span`).forEach((s) => { |
| 85 | + s.className = inactiveClasses |
| 86 | + }) |
| 87 | + |
| 88 | + button.querySelector('span').className = activeClasses |
| 89 | + |
| 90 | + if (storageKey) { |
| 91 | + localStorage.setItem(`${storageKey}__${domain}`, tab) |
| 92 | + } |
| 93 | + } |
| 94 | + }) |
| 95 | + }, |
| 96 | + cleanup: function () { |
| 97 | + removeListeners.bind(this)() |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +function getDomain(url) { |
| 103 | + const uri = typeof url === 'object' ? url : new URL(url) |
| 104 | + return uri.pathname.split('/')[1] |
| 105 | +} |
| 106 | + |
| 107 | +function addListener(eventName, listener, callback) { |
| 108 | + this.listeners = this.listeners || [] |
| 109 | + |
| 110 | + listener.addEventListener(eventName, callback) |
| 111 | + |
| 112 | + this.listeners.push({ |
| 113 | + element: listener, |
| 114 | + event: eventName, |
| 115 | + callback: callback |
| 116 | + }) |
| 117 | +} |
| 118 | + |
| 119 | +function removeListeners() { |
| 120 | + if (this.listeners) { |
| 121 | + this.listeners.forEach((l) => { |
| 122 | + l.element.removeEventListener(l.event, l.callback) |
| 123 | + }) |
| 124 | + |
| 125 | + this.listeners = null |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +export default { |
| 130 | + mounted() { |
| 131 | + this.widget = this.el.getAttribute('data-widget') |
| 132 | + |
| 133 | + this.initialize() |
| 134 | + }, |
| 135 | + |
| 136 | + updated() { |
| 137 | + this.initialize() |
| 138 | + }, |
| 139 | + |
| 140 | + reconnected() { |
| 141 | + this.initialize() |
| 142 | + }, |
| 143 | + |
| 144 | + destroyed() { |
| 145 | + this.cleanup() |
| 146 | + }, |
| 147 | + |
| 148 | + initialize() { |
| 149 | + this.cleanup() |
| 150 | + WIDGETS[this.widget].initialize.bind(this)() |
| 151 | + }, |
| 152 | + |
| 153 | + cleanup() { |
| 154 | + WIDGETS[this.widget].cleanup.bind(this)() |
| 155 | + } |
| 156 | +} |
0 commit comments