h5-patch.js
2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// #ifdef H5
import {
DOM
} from '../component/h5-dom.js';
import {init} from '../vueRouter/init.js'
// #endif
import {warn} from '../helpers/warn.js'
class Patch {
constructor(H5) {
this.H5 = H5;
this.isLoading = true;
this.loadingCount = 0; //在APP.vue中进行跳转时,DOMContentLoaded过慢。使用索引来判断
}
on(fun, args, callback) {
if (this.H5) {
return this[fun](args);
}
callback && callback();
}
/**
* 把vueRouter的生命周期代理过来
* @param {Object} Router
* @param {Object} vueRouter
* @param {VueComponent} vueVim
*/
registerHook(Router, vueRouter,vueVim) {
init(Router,vueRouter,vueVim);
}
/**
* H5 专属 history.back API
* @param {Number} backLayer 需要返回的层级必须是正整数
* 2020年1月14日14:39:38 修复 https://github.com/SilurianYang/uni-simple-router/issues/73
*/
historyBack({backLayer,delta={from: 'navigateBack'}}={}) {
const pages=getCurrentPages();
const page=pages[pages.length-1]
const onBackPress=page.$options.onBackPress;
if(onBackPress!=null&&onBackPress.constructor===Array){
const callFun=onBackPress[onBackPress.length-1];
const isNext=callFun.call(page,delta);
if(isNext){
return true;
}
}
history.go(-backLayer)
}
/**
* 把加载动画添加到dom下面,为什么一定要先添加,后移除。保证动画的连续性
*/
appendHTML({
style,
html,
script
}) {
window.addEventListener('DOMContentLoaded', () => {
const body = document.querySelector('body');
body.appendChild(style);
body.appendChild(html);
body.appendChild(script);
this.toogle('startLodding', true);
})
}
/**
* 页面是否加载完毕触发对应事件
*/
toogle(toogle, DOMContentLoaded = false) {
if (DOMContentLoaded && this.loadingCount !== 0) {
this.loadingCount++;
return false;
}
try {
this.loadingCount++;
if (this.isLoading) {
window[toogle]();
}
} catch (error) {
warn(`你使用了 addRoutes API 提现进行了生命周期 并触发了startLodding`)
}
}
async setLoadingStatus({
loading,
replaceStyle,
resetStyle
}) {
this.isLoading = loading;
if (loading) { //确认需要加载样式 开始插入节点
const userStyle=resetStyle();
for(let key in userStyle){
let html =userStyle[key];
if(key==='style'&&!replaceStyle){ //开发者设置为追加style
html=DOM[key].innerHTML+html;
}
DOM[key].innerHTML=html;
}
this.appendHTML(DOM);
}
}
}
export default Patch;