iportal
Version:
web-portal
73 lines (62 loc) • 2.12 kB
text/typescript
import { ModuleProptey } from './proptey'
import { ModuleManifest, ModuleStatus, TransformActionOrigin, Application } from '../types'
class ModuleState extends ModuleProptey {
private viewTypeCache: 'iframe' | 'portal' | 'shadow' | null = null
public visibility: boolean = false
public status: ModuleStatus = {
init: false,
preload: false,
prefetch: false,
prerender: false
}
public actionOriginMap?: TransformActionOrigin
public nowViewIndex = this.config.level ?? 0
constructor (id: string, model: ModuleManifest, application: Application) {
super(id, model, application)
}
get sameOrigin (): boolean {
if (!this.uri) {
if (this.config.sandbox === undefined) return true
if (this.config.sandbox.includes('allow-same-origin')) return true
return false
}
const link = new URL(
this.uri,
window.location.toString()
);
const isSameOrigin = link.host === location.host
return isSameOrigin
}
get level (): number {
return this.config.level ?? 0
}
get viewIndex (): number {
const level = (this.application.preActiveModule?.nowViewIndex || 1) + (this.config.level ?? 1)
this.nowViewIndex = level
const baseLevel = this.config.free ? 20000 : 10000
return baseLevel + level
}
get rel (): 'system' | 'frameworks' | 'module' {
if (this.id === 'system') return 'system'
if (this.id === 'frameworks') return 'frameworks'
return this.config.rel || 'module'
}
get uri (): string {
return this.config?.source?.src || ''
}
get viewType (): 'portal' | 'iframe' | 'shadow' {
if (this.viewTypeCache) return this.viewTypeCache
if (this.rel !== 'module') return this.viewTypeCache = 'shadow'
if (!this.config?.portal || !this.uri) return this.viewTypeCache = 'iframe'
return this.viewTypeCache = ('HTMLPortalElement' in window) && this.sameOrigin ? 'portal' : 'iframe'
}
public setActionOrigin (origin: TransformActionOrigin) {
this.actionOriginMap = origin
}
public getActionOrigin () {
return this.actionOriginMap
}
}
export {
ModuleState
}