import i18n from '../lang' import { checkIsFormTouched } from '../utils/form-touched' // some weird behaviour will trigger twice which i have no idea why // so use a boolean to prevent it let preventDoublePrompt = true export const showPrompt = (to: any, from: any, next: any) => { if (preventDoublePrompt) { preventDoublePrompt = false let prompt = true prompt = checkIsFormTouched() try { if ( (from.query.back && from.query.back === '1') || (to.query.back && to.query.back === '1') ) { prompt = false } else if (from.query.edit) { if (from.query.edit === '1') { prompt = true } else { prompt = false } } else if (from.params.type && from.params.type === 'view') { prompt = false if (from.query.prompt && from.query.prompt === '1') { prompt = true } } else if (!from.params.type) { prompt = false } } catch (err) {} if (!prompt) { next() } else { const message: any = i18n.t('general.leavePagePrompt') const answer = window.confirm(message) if (answer) { next() } else { next(false) } } setTimeout(() => { preventDoublePrompt = true }) } } export const leaveEvent = (e: any) => { let prompt = true const path = e.target.URL || e.target.baseURI prompt = checkIsFormTouched() if (path) { if (path.includes('/view?') || path.includes('back=1')) { prompt = false } } if (prompt) { e.preventDefault() // Chrome requires returnValue to be set e.returnValue = '' } }