-
-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathavo.base.js
116 lines (96 loc) · 3.39 KB
/
avo.base.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// eslint-disable-next-line import/no-extraneous-dependencies
import 'core-js/stable'
// eslint-disable-next-line import/no-extraneous-dependencies
import 'chartkick/chart.js/chart.esm'
import 'mapkick/bundle'
import 'regenerator-runtime/runtime'
import * as ActiveStorage from '@rails/activestorage'
import * as Mousetrap from 'mousetrap'
import { Turbo } from '@hotwired/turbo-rails'
import tippy from 'tippy.js'
import { LocalStorageService } from './js/local-storage-service'
import './js/active-storage'
import './js/controllers'
import './js/custom-stream-actions'
window.Avo.localStorage = new LocalStorageService()
window.Turbolinks = Turbo
let scrollTop = null
Mousetrap.bind('r r r', () => {
// Cpture scroll position
scrollTop = document.scrollingElement.scrollTop
Turbo.visit(window.location.href, { action: 'replace' })
})
function isMac() {
const isMac = window.navigator.userAgent.indexOf('Mac OS X')
if (isMac >= 0) {
document.body.classList.add('os-mac')
document.body.classList.remove('os-pc')
} else {
document.body.classList.add('os-pc')
document.body.classList.remove('os-mac')
}
}
function initTippy() {
tippy('[data-tippy="tooltip"]', {
theme: 'light',
content(reference) {
const title = reference.getAttribute('title')
reference.removeAttribute('title')
reference.removeAttribute('data-tippy')
return title
},
})
}
window.initTippy = initTippy
ActiveStorage.start()
document.addEventListener('turbo:load', () => {
initTippy()
isMac()
// Restore scroll position after r r r turbo reload
if (scrollTop) {
setTimeout(() => {
document.scrollingElement.scrollTo(0, scrollTop)
scrollTop = 0
}, 50)
}
setTimeout(() => {
document.body.classList.remove('turbo-loading')
}, 1)
})
document.addEventListener('turbo:frame-load', () => {
initTippy()
})
document.addEventListener('turbo:before-fetch-response', async (e) => {
if (e.detail.fetchResponse.response.status === 500) {
const { id, src } = e.target
// Don't try to redirect to failed to load if this is alread a redirection to failed to load and crashed somewhere.
// You'll end up with a request loop.
if (!e.detail.fetchResponse?.response?.url?.includes('/failed_to_load')) {
e.target.src = `${window.Avo.configuration.root_path}/failed_to_load?turbo_frame=${id}&src=${src}`
}
}
})
document.addEventListener('turbo:visit', () => {
document.body.classList.add('turbo-loading')
})
document.addEventListener('turbo:submit-start', () => document.body.classList.add('turbo-loading'))
document.addEventListener('turbo:submit-end', () => document.body.classList.remove('turbo-loading'))
document.addEventListener('turbo:before-cache', () => {
document.querySelectorAll('[data-turbo-remove-before-cache]').forEach((element) => element.remove())
})
// Watch for live changes when the user has "auto" as the default setting.
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (event) => {
const method = event.matches ? 'add' : 'remove'
document.documentElement.classList[method]('dark')
})
window.Avo = window.Avo || { configuration: {} }
window.Avo.menus = {
resetCollapsedState() {
Array.from(document.querySelectorAll('[data-menu-key-param]'))
.map((i) => i.getAttribute('data-menu-key-param'))
.filter(Boolean)
.forEach((key) => {
window.localStorage.removeItem(key)
})
},
}