62 lines
1.7 KiB
JavaScript
62 lines
1.7 KiB
JavaScript
// variables
|
|
let deferredPrompt
|
|
const events = ['click', 'touchend', 'ontouchend']
|
|
const customDialog = document.querySelector('.pwa_dialog')
|
|
const installBtn = document.querySelector('#installPWA')
|
|
const showPwaDialogBtn = document.querySelector('#showPwaDialog')
|
|
const closePwaDialogBtn = document.querySelector('#closePwaDialog')
|
|
|
|
// functions
|
|
function showCustomInstallPromotion() {
|
|
setTimeout(function() {
|
|
customDialog?.classList.add('active')
|
|
}, 500)
|
|
}
|
|
function hideCustomInstallPromotion() {
|
|
setTimeout(function() {
|
|
customDialog?.classList.remove('active')
|
|
}, 500)
|
|
}
|
|
|
|
// events
|
|
window.addEventListener('beforeinstallprompt', e => {
|
|
// Prevent the mini-infobar from appearing on mobile
|
|
e.preventDefault()
|
|
// Stash the event so it can be triggered later.
|
|
deferredPrompt = e
|
|
// Update UI notify the user they can install the PWA
|
|
// if (location.pathname !== '/') showCustomInstallPromotion()
|
|
showCustomInstallPromotion()
|
|
})
|
|
|
|
events.forEach(evt => {
|
|
closePwaDialogBtn?.addEventListener(evt, e => {
|
|
hideCustomInstallPromotion()
|
|
})
|
|
|
|
installBtn?.addEventListener(evt, e => {
|
|
// Hide the app provided install promotion
|
|
hideCustomInstallPromotion()
|
|
// Show the install prompt
|
|
deferredPrompt.prompt()
|
|
// Wait for the user to respond to the prompt
|
|
deferredPrompt.userChoice.then(choiceResult => {
|
|
if (choiceResult.outcome === 'accepted') {
|
|
console.log('User accepted the install prompt')
|
|
} else {
|
|
console.log('User dismissed the install prompt')
|
|
}
|
|
})
|
|
})
|
|
|
|
try {
|
|
showPwaDialogBtn?.addEventListener(evt, e => {
|
|
if (deferredPrompt) {
|
|
showCustomInstallPromotion()
|
|
} else {
|
|
alert('شما قبلا برنامه را نصب کرده اید')
|
|
}
|
|
})
|
|
} catch (e) {}
|
|
})
|