cross-magic
Version:
跨平台公共模块
161 lines (136 loc) • 4.11 kB
text/typescript
import FastClick from 'fastclick'
import ApplicationConfig, { VueParams } from './applicationConfig'
// import { UiMgr, EnvMgr, NativeMgr, NetworkMgr, StorageMgr } from '@/main'
import { RunTime } from '../runTime/runTime'
import { createRuntime } from '../runTime'
import createUiMgr from '../mgrs/ui'
import UiMgr from '../mgrs/ui/uiMgr'
import EnvMgr from '../mgrs/env/envMgr'
import NativeMgr from '../mgrs/native/nativeMgr'
import NetworkMgr from '../mgrs/network/networkMgr'
import StorageMgr from '../mgrs/storage/storageMgr'
import Vue from 'vue/types'
/**
* App类,简化app构建流程
*/
export default class Application {
config!: ApplicationConfig
private _runtime!: RunTime
private _vue!: Vue
private _uiMgr!: UiMgr
private _envMgr!: EnvMgr
private _nativeMgr!: NativeMgr
private _networkMgr!: NetworkMgr
private _storageMgr!: StorageMgr
constructor(config: ApplicationConfig) {
this.config = config
this.init()
}
get runtime(): RunTime {
return this._runtime
}
get uiMgr(): UiMgr {
return this._uiMgr
}
get envMgr(): EnvMgr {
return this._envMgr
}
get nativeMgr(): NativeMgr {
return this._nativeMgr
}
get networkMgr(): NetworkMgr {
return this._networkMgr
}
get storageMgr(): StorageMgr {
return this._storageMgr
}
private async init() {
console.log('cross-maigc app init')
const beforeStartFn: Function | undefined = this.config.hooks && this.config.hooks.beforeStart
if (beforeStartFn) {
// 执行app启动前钩子
await beforeStartFn()
}
this.createMgrs() // 创建mgrs
this.remAdaptive() // rem适配
this.handleClick() // 处理ios快速点击问题
this.createVue()
const startedFn: Function | undefined = this.config.hooks && this.config.hooks.started
if (startedFn) {
// 执行app启动完成钩子
await startedFn()
}
}
private createVue() {
if (!this.config.vueParams) {
return
}
const vueParams: VueParams = this.config.vueParams
this._vue = new this.config.vueParams.vue.vueClass({
router: vueParams.router,
store: vueParams.store,
render: h => h(vueParams.vue.rootVue)
}).$mount(vueParams.vue.rootDom)
}
/**
* 创建mgrs
*/
private createMgrs() {
this._runtime = createRuntime(this.config.runTimeData)
if (this.config.uiConfig) {
this._uiMgr = createUiMgr(this.config.uiConfig)
}
// this._uiMgr = new UiMgr(this.config.uiConfig || {})
this._envMgr = new EnvMgr({}) // todo
this._nativeMgr = new NativeMgr({}) // todo
this._networkMgr = new NetworkMgr({}) // todo
this._storageMgr = new StorageMgr({}) // todo
}
/**
* rem适配
*/
private remAdaptive() {
if (!this.config.remAdaptive) {
return
}
function updateRemRatio(remNum: number, ratio: number) {
if (!document) {
console.error('updateRemRatioError:document is empty')
return
}
const html = document.getElementsByTagName('html')[0]
const docEleWidth = document.documentElement && document.documentElement.clientWidth
const oWidth = docEleWidth || document.body.clientWidth
html.style.fontSize = `${(oWidth / remNum) * ratio}px`
}
window.onload = () => {
if (this.config.remAdaptive) {
updateRemRatio(this.config.remAdaptive.designWidth, this.config.remAdaptive.ratio)
}
}
window.onresize = () => {
if (this.config.remAdaptive) {
updateRemRatio(this.config.remAdaptive.designWidth, this.config.remAdaptive.ratio)
}
}
}
/**
* 处理ios快速点击问题
*/
private handleClick() {
const document = window.document
if ('addEventListener' in document) {
document.addEventListener(
'DOMContentLoaded',
() => {
;(FastClick as any).attach(document.body)
// 处理点击input反映不灵敏的问题
FastClick.prototype.focus = function(targetElement: any) {
targetElement.focus()
}
},
false
)
}
}
}