webpack-bootstrap-ui-kit/src/js/ajax/links.js

222 lines
6.5 KiB
JavaScript
Raw Normal View History

2021-08-09 18:04:09 +02:00
// browser tab visibility state detection
2022-05-03 20:50:57 +02:00
import Events from '../_events'
import Consts from '../_consts'
import Page from './models/page.jsx'
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
import { getParents } from '../main/funcs'
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
import { Collapse } from 'bootstrap'
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
import SpinnerUI from '../main/loading-spinner'
2021-08-09 18:04:09 +02:00
const MainUILinks = ((W) => {
2022-05-03 20:50:57 +02:00
const NAME = 'main.links'
const D = document
const BODY = D.body
2021-08-09 18:04:09 +02:00
class MainUILinks {
2022-05-03 20:50:57 +02:00
window
static init () {
const ui = this
ui.GraphPage = null
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
console.log(`${NAME}: init`)
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
ui.loaded()
2021-08-09 18:04:09 +02:00
2021-08-18 20:51:15 +02:00
// history state switch
2022-05-03 20:50:57 +02:00
W.addEventListener('popstate', (e) => {
ui.popState(e)
})
2021-08-18 20:51:15 +02:00
}
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
static loaded () {
const ui = this
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
D.querySelectorAll('.graphql-page').forEach((el, i) => {
const el_id = el.getAttribute('href')
el.setAttribute(`data-${ui.name}-id`, el_id)
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
el.addEventListener('click', ui.loadClick)
})
2021-08-18 20:51:15 +02:00
}
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
static setActiveLinks (link) {
const ui = this
2021-08-18 20:51:15 +02:00
D.querySelectorAll(`[data-${ui.name}-id="${link}"]`).forEach((el) => {
2022-05-03 20:50:57 +02:00
el.classList.add('active')
})
2021-08-18 20:51:15 +02:00
}
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
static reset () {
2021-08-18 20:51:15 +02:00
// reset focus
2022-05-03 20:50:57 +02:00
D.activeElement.blur()
2021-08-09 18:04:09 +02:00
2021-08-18 20:51:15 +02:00
// remove active and loading classes
2022-05-03 20:50:57 +02:00
D.querySelectorAll('.graphql-page,.nav-item').forEach((el2) => {
el2.classList.remove('active', 'loading')
})
2021-08-18 20:51:15 +02:00
}
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
static popState (e) {
const ui = this
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
SpinnerUI.show()
2021-08-09 18:04:09 +02:00
2021-08-18 20:51:15 +02:00
if (e.state && e.state.page) {
2022-05-03 20:50:57 +02:00
console.log(`${NAME}: [popstate] load`)
const state = JSON.parse(e.state.page)
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
state.current = null
state.popstate = true
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
ui.reset()
ui.setActiveLinks(e.state.link)
2021-08-09 18:04:09 +02:00
2021-08-18 20:51:15 +02:00
if (!ui.GraphPage) {
console.log(
`${NAME}: [popstate] GraphPage is missing. Have to render it first`
2022-05-03 20:50:57 +02:00
)
2021-08-09 18:04:09 +02:00
2021-08-18 20:51:15 +02:00
ui.GraphPage = ReactDOM.render(
<Page />,
2022-05-03 20:50:57 +02:00
document.getElementById('MainContent')
)
2021-08-09 18:04:09 +02:00
}
2022-05-03 20:50:57 +02:00
ui.GraphPage.setState(state)
SpinnerUI.hide()
2021-08-18 20:51:15 +02:00
2022-05-03 20:50:57 +02:00
window.dispatchEvent(new Event(Events.AJAX))
2021-08-18 20:51:15 +02:00
} else if (e.state && e.state.landing) {
2022-05-03 20:50:57 +02:00
console.log(`${NAME}: [popstate] go to landing`)
W.location.href = e.state.landing
2021-08-18 20:51:15 +02:00
} else {
2022-05-03 20:50:57 +02:00
console.warn(`${NAME}: [popstate] state is missing`)
console.log(e)
SpinnerUI.hide()
2021-08-18 20:51:15 +02:00
}
}
// link specific event {this} = current event, not MainUILinks
2022-05-03 20:50:57 +02:00
static loadClick (e) {
console.groupCollapsed(`${NAME}: load on click`)
e.preventDefault()
2021-08-18 20:51:15 +02:00
2022-05-03 20:50:57 +02:00
const ui = MainUILinks
const el = e.currentTarget
2021-08-18 20:51:15 +02:00
2022-05-03 20:50:57 +02:00
SpinnerUI.show()
2021-08-18 20:51:15 +02:00
2022-05-03 20:50:57 +02:00
ui.reset()
el.classList.add('loading')
el.classList.remove('response-404', 'response-500', 'response-523')
BODY.classList.add('ajax-loading')
2021-08-18 20:51:15 +02:00
// hide parent mobile nav
2022-05-03 20:50:57 +02:00
const navs = getParents(el, '.collapse')
2021-08-18 20:51:15 +02:00
if (navs.length) {
navs.forEach((nav) => {
2022-05-03 20:50:57 +02:00
const collapseInst = Collapse.getInstance(nav)
2021-08-18 20:51:15 +02:00
if (collapseInst) {
2022-05-03 20:50:57 +02:00
collapseInst.hide()
2021-08-09 18:04:09 +02:00
}
2022-05-03 20:50:57 +02:00
})
2021-08-18 20:51:15 +02:00
}
2021-08-09 18:04:09 +02:00
2021-08-18 20:51:15 +02:00
// hide parent dropdown
2022-05-03 20:50:57 +02:00
/* const dropdowns = getParents(el, '.dropdown-menu');
2021-08-09 18:04:09 +02:00
if (dropdowns.length) {
const DropdownInst = Dropdown.getInstance(dropdowns[0]);
DropdownInst.hide();
2022-05-03 20:50:57 +02:00
} */
2021-08-09 18:04:09 +02:00
2021-08-18 20:51:15 +02:00
if (!ui.GraphPage) {
ui.GraphPage = ReactDOM.render(
<Page />,
2022-05-03 20:50:57 +02:00
document.getElementById('MainContent')
)
2021-08-18 20:51:15 +02:00
}
2022-05-03 20:50:57 +02:00
const link = el.getAttribute('href') || el.getAttribute('data-href')
2021-08-18 20:51:15 +02:00
2022-05-03 20:50:57 +02:00
ui.GraphPage.state.current = el
2021-08-18 20:51:15 +02:00
ui.GraphPage.load(link)
.then((response) => {
2022-05-03 20:50:57 +02:00
BODY.classList.remove('ajax-loading')
el.classList.remove('loading')
el.classList.add('active')
2021-08-18 20:51:15 +02:00
2022-05-03 20:50:57 +02:00
D.loading_apollo_link = null
2021-08-18 20:51:15 +02:00
if (ui.GraphPage.state.Link) {
window.history.pushState(
{
page: JSON.stringify(ui.GraphPage.state),
link: el.getAttribute(`data-${ui.name}-id`),
},
ui.GraphPage.state.Title,
ui.GraphPage.state.Link
2022-05-03 20:50:57 +02:00
)
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
ui.setActiveLinks(ui.GraphPage.state.Link)
2021-08-18 20:51:15 +02:00
}
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
SpinnerUI.hide()
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
window.dispatchEvent(new Event(Events.AJAX))
console.groupEnd(`${NAME}: load on click`)
2021-08-18 20:51:15 +02:00
})
.catch((e) => {
2022-05-03 20:50:57 +02:00
console.error(`${NAME}: loading error`)
console.log(e)
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
/* BODY.classList.remove('ajax-loading');
el.classList.remove('loading'); */
el.classList.add('error', `response-${e.status}`)
/* switch (e.status) {
2021-08-09 18:04:09 +02:00
case 500:
break;
case 404:
el.classList.add('not-found');
break;
case 523:
el.classList.add('unreachable');
break;
2022-05-03 20:50:57 +02:00
} */
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
// SpinnerUI.hide();
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
// window.dispatchEvent(new Event(Events.AJAX));
console.groupEnd(`${NAME}: load on click`)
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
console.log(`${NAME}: reloading page ${link}`)
2021-08-09 18:04:09 +02:00
2021-08-18 20:51:15 +02:00
// fallback loading
2022-05-03 20:50:57 +02:00
W.location.href = link
})
2021-08-18 20:51:15 +02:00
}
2021-08-09 18:04:09 +02:00
}
W.addEventListener(`${Events.LOADED}`, () => {
2022-05-03 20:50:57 +02:00
MainUILinks.init()
})
2021-08-09 18:04:09 +02:00
W.addEventListener(`${Events.AJAX}`, () => {
2022-05-03 20:50:57 +02:00
MainUILinks.loaded()
})
2021-08-09 18:04:09 +02:00
// fallback
2022-05-03 20:50:57 +02:00
/* W.addEventListener(`${Events.APOLLO_ERROR}`, (e) => {
2021-08-09 18:04:09 +02:00
console.error(`${NAME}: [APOLLO_ERROR] loading failure, reloading the page`);
//W.dispatchEvent(new Event(Events.OFFLINE));
if (D.loading_apollo_link) {
W.location.href = D.loading_apollo_link;
}
2022-05-03 20:50:57 +02:00
}); */
})(window)
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
export default MainUILinks