iportal
Version:
web-portal
39 lines (37 loc) • 1.27 kB
text/typescript
import { Application } from '../../types'
export default (moduleWindow: Window, application: Application) => {
const realOpen = moduleWindow.open
const blockClick = (event: MouseEvent) => {
const path = event['path'] || event.composedPath?.() || []
const anchor = (() => {
for (const el of path) {
if (el.tagName === 'A') return el
}
})()
if (!anchor) return false
const href = anchor.href || String(anchor)
if (href && anchor.target !== 'self') {
const title = anchor.title || ''
const preset = anchor.getAttribute('preset-effect')
const cloneAs = anchor.getAttribute('clone-as')
event.stopPropagation()
event.preventDefault()
application.pushWindow(href, title, preset, cloneAs, event).catch(() => {
realOpen(href)
})
}
return false
}
moduleWindow.addEventListener('click', blockClick)
moduleWindow.open = (url?: string | URL | undefined, target?: string | undefined, features?: string | undefined) => {
if (typeof url !== 'string' || target || features) {
return realOpen(url, target, features)
}
if (typeof url === 'string') {
application.pushWindow(url, '').catch(() => {
realOpen(url)
})
}
return null
}
}