webpack-bootstrap-ui-kit/src/js/main/main.js

111 lines
2.4 KiB
JavaScript
Raw Normal View History

2022-05-03 20:50:57 +02:00
import Events from '../_events'
import Consts from '../_consts'
import SpinnerUI from './loading-spinner'
2021-08-09 18:04:09 +02:00
2021-11-12 07:27:48 +01:00
const MainUI = ((window) => {
2022-05-03 20:50:57 +02:00
const NAME = '_main'
const BODY = document.body
2021-08-09 18:04:09 +02:00
console.info(
`%cUI Kit ${UINAME} ${UIVERSION}`,
2021-11-12 07:27:48 +01:00
'color:yellow;font-size:14px'
2022-05-03 20:50:57 +02:00
)
2021-08-09 18:04:09 +02:00
console.info(
`%c${UIMetaNAME} ${UIMetaVersion}`,
2021-11-12 07:27:48 +01:00
'color:yellow;font-size:12px'
2022-05-03 20:50:57 +02:00
)
2021-08-09 18:04:09 +02:00
console.info(
`%chttps://github.com/a2nt/webpack-bootstrap-ui-kit by ${UIAUTHOR}`,
2021-11-12 07:27:48 +01:00
'color:yellow;font-size:10px'
2022-05-03 20:50:57 +02:00
)
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
console.info(`%cENV: ${process.env.NODE_ENV}`, 'color:green;font-size:10px')
console.groupCollapsed('Events')
2021-08-09 18:04:09 +02:00
Object.keys(Events).forEach((k) => {
2022-05-03 20:50:57 +02:00
console.info(`${k}: ${Events[k]}`)
})
console.groupEnd('Events')
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
console.groupCollapsed('Consts')
2021-08-09 18:04:09 +02:00
Object.keys(Consts).forEach((k) => {
2022-05-03 20:50:57 +02:00
console.info(`${k}: ${Consts[k]}`)
})
console.groupEnd('Events')
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
console.groupCollapsed('Init')
console.time('init')
2021-08-09 18:04:09 +02:00
class MainUI {
// first time the website initialization
static init() {
2022-05-03 20:50:57 +02:00
const ui = this
2021-08-09 18:04:09 +02:00
// store landing page state
2021-11-12 07:27:48 +01:00
window.history.replaceState(
2021-08-09 18:04:09 +02:00
{
2021-11-12 07:27:48 +01:00
landing: window.location.href,
2021-08-09 18:04:09 +02:00
},
2021-11-12 07:27:48 +01:00
document.title,
window.location.href
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.loaded()
2021-08-09 18:04:09 +02:00
}
// init AJAX components
static loaded() {
2022-05-03 21:44:33 +02:00
// const ui = this
2022-05-03 20:50:57 +02:00
console.log(`${NAME}: loaded`)
2021-08-09 18:04:09 +02:00
}
2023-08-04 00:02:42 +02:00
static unloaded() {
2023-08-04 00:02:42 +02:00
console.log(`${NAME}: unloaded`)
SpinnerUI.show()
BODY.classList.remove('loaded')
}
2021-08-09 18:04:09 +02:00
}
2021-11-12 07:27:48 +01:00
const documentInit = () => {
2022-05-03 20:50:57 +02:00
MainUI.init()
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
console.groupEnd('init')
console.timeEnd('init')
2021-08-09 18:04:09 +02:00
2021-11-12 07:34:34 +01:00
window.addEventListener(`${Events.LOADED}`, (event) => {
2022-05-03 20:50:57 +02:00
window.dispatchEvent(new Event(Events.LODEDANDREADY))
})
}
if (document.readyState === 'loading') { // Loading hasn't finished yet
document.addEventListener(`${Events.DOMLOADED}`, documentInit)
} else {
documentInit()
2021-11-12 07:27:48 +01:00
}
2021-08-09 18:04:09 +02:00
window.addEventListener(`${Events.LODEDANDREADY}`, () => {
BODY.classList.add('loaded')
if (typeof window.app === 'undefined' || typeof window.app.Router === 'undefined') {
console.warn('Spinner is hidden')
SpinnerUI.hide()
}
});
2021-11-12 07:27:48 +01:00
window.addEventListener(`${Events.AJAX}`, () => {
2022-05-03 20:50:57 +02:00
MainUI.loaded()
})
2021-08-09 18:04:09 +02:00
2023-08-04 00:56:00 +02:00
window.addEventListener('beforeunload', () => {
MainUI.unloaded()
})
2023-08-04 00:02:42 +02:00
2022-05-03 20:50:57 +02:00
window.MainUI = MainUI
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
return MainUI
})(window)
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
export default MainUI