murph-skeleton/assets/js/admin/modules/push-state.js

45 lines
1 KiB
JavaScript
Raw Normal View History

2021-06-15 14:16:07 +02:00
const $ = require('jquery')
2021-03-24 12:27:07 +01:00
2021-06-15 14:16:07 +02:00
module.exports = function () {
$('*[data-pushstate]').click((e) => {
const url = $(e.target).attr('data-pushstate')
2021-03-24 12:27:07 +01:00
2021-06-15 14:16:07 +02:00
history.pushState({ url: url }, null, url)
history.replaceState({ url: url }, null, url)
})
2021-03-24 12:27:07 +01:00
2021-06-15 14:16:07 +02:00
const forms = $('form[data-formpushstate]')
2021-03-24 12:27:07 +01:00
2021-06-15 14:16:07 +02:00
const checkAndUsePushState = () => {
const state = [window.location.pathname, window.location.search].join('')
2021-03-24 12:27:07 +01:00
2021-06-15 14:16:07 +02:00
$('*[data-pushstate]').each((i, v) => {
let method = 'compare'
2021-03-24 12:27:07 +01:00
2021-06-15 14:16:07 +02:00
if ($(v).is('[data-pushstate-method]')) {
method = $(v).attr('data-pushstate-method')
}
2021-03-24 12:27:07 +01:00
2021-06-15 14:16:07 +02:00
let isThisOne = false
2021-03-24 12:27:07 +01:00
2021-06-15 14:16:07 +02:00
if (method === 'compare' && $(v).attr('data-pushstate') === state) {
isThisOne = true
}
2021-03-24 12:27:07 +01:00
2021-06-15 14:16:07 +02:00
if (method === 'indexOf' && state.indexOf($(v).attr('data-pushstate')) !== -1) {
isThisOne = true
}
2021-03-24 12:27:07 +01:00
2021-06-15 14:16:07 +02:00
if (isThisOne) {
$(v).click()
2021-03-24 12:27:07 +01:00
2021-06-15 14:16:07 +02:00
forms.attr('action', state)
}
})
}
2021-03-24 12:27:07 +01:00
2021-06-15 14:16:07 +02:00
checkAndUsePushState()
2021-03-24 12:27:07 +01:00
2021-06-15 14:16:07 +02:00
$(window).on('statechange', checkAndUsePushState, false)
2021-03-24 12:27:07 +01:00
}