|
| 1 | +/** |
| 2 | + * Hook used for LiveView dashboard. Currently its main purpose |
| 3 | + * is facilitiating migration from React to LiveView. |
| 4 | + */ |
| 5 | + |
| 6 | +const WIDGETS = { |
| 7 | + 'dashboard-root': { |
| 8 | + initialize: function () { |
| 9 | + this.url = window.location.href |
| 10 | + |
| 11 | + addListener.bind(this)('phx:update_local_storage', window, (e) => { |
| 12 | + localStorage.setItem(e.detail.key, e.detail.value) |
| 13 | + }) |
| 14 | + |
| 15 | + addListener.bind(this)('phx:update_local_storage', this.el, (e) => { |
| 16 | + const type = e.target.dataset.type || null |
| 17 | + |
| 18 | + if (type && type == 'dashboard-link') { |
| 19 | + this.url = e.target.href |
| 20 | + const uri = new URL(this.url) |
| 21 | + const path = '/' + uri.pathname.split('/').slice(2).join('/') |
| 22 | + this.el.dispatchEvent( |
| 23 | + new CustomEvent('dashboard:live-navigate', { |
| 24 | + bubbles: true, |
| 25 | + detail: { path: path, search: uri.search } |
| 26 | + }) |
| 27 | + ) |
| 28 | + |
| 29 | + this.pushEvent('handle_dashboard_params', { url: this.url }) |
| 30 | + |
| 31 | + e.preventDefault() |
| 32 | + } |
| 33 | + }) |
| 34 | + |
| 35 | + addListener.bind(this)('popstate', window, () => { |
| 36 | + if (this.url !== window.location.href) { |
| 37 | + this.pushEvent('handle_dashboard_params', { |
| 38 | + url: window.location.href |
| 39 | + }) |
| 40 | + } |
| 41 | + }) |
| 42 | + |
| 43 | + addListener.bind(this)('dashboard:live-navigate-back', window, (e) => { |
| 44 | + if ( |
| 45 | + typeof e.detail.search === 'string' && |
| 46 | + this.url !== window.location.href |
| 47 | + ) { |
| 48 | + this.pushEvent('handle_dashboard_params', { |
| 49 | + url: window.location.href |
| 50 | + }) |
| 51 | + } |
| 52 | + }) |
| 53 | + }, |
| 54 | + cleanup: function () { |
| 55 | + removeListeners.bind(this)() |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +function addListener(eventName, listener, callback) { |
| 61 | + this.listeners = this.listeners || [] |
| 62 | + |
| 63 | + listener.addEventListener(eventName, callback) |
| 64 | + |
| 65 | + this.listeners.push({ |
| 66 | + element: listener, |
| 67 | + event: eventName, |
| 68 | + callback: callback |
| 69 | + }) |
| 70 | +} |
| 71 | + |
| 72 | +function removeListeners() { |
| 73 | + if (this.listeners) { |
| 74 | + this.listeners.forEach((l) => { |
| 75 | + l.element.removeEventListener(l.event, l.callback) |
| 76 | + }) |
| 77 | + |
| 78 | + this.listeners = null |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +export default { |
| 83 | + mounted() { |
| 84 | + this.widget = this.el.getAttribute('data-widget') |
| 85 | + |
| 86 | + this.initialize() |
| 87 | + }, |
| 88 | + |
| 89 | + updated() { |
| 90 | + this.initialize() |
| 91 | + }, |
| 92 | + |
| 93 | + reconnected() { |
| 94 | + this.initialize() |
| 95 | + }, |
| 96 | + |
| 97 | + destroyed() { |
| 98 | + this.cleanup() |
| 99 | + }, |
| 100 | + |
| 101 | + initialize() { |
| 102 | + this.cleanup() |
| 103 | + WIDGETS[this.widget].initialize.bind(this)() |
| 104 | + }, |
| 105 | + |
| 106 | + cleanup() { |
| 107 | + WIDGETS[this.widget].cleanup.bind(this)() |
| 108 | + } |
| 109 | +} |
0 commit comments