forked from tvfe/wxpage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.js
221 lines (210 loc) · 5.45 KB
/
page.js
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
'use strict'
var fns = require('./lib/fns.js')
var message = require('./lib/message')
var redirector = require('./lib/redirector')
var cache = require('./lib/cache')
var C = require('./lib/component')
var bridge = require('./lib/bridge')
var _conf = require('./lib/conf')
var dispatcher = new message()
var hasPageLoaded = 0
var isAppLaunched = 0
var isAppShowed = 0
var hideTime = 0
var modules = {
fns, redirector, cache, message, dispatcher,
channel: bridge.channel
}
bridge.ref(C.getRef)
bridge.dispatcher(dispatcher)
C.dispatcher(dispatcher)
function WXPage(name, option) {
if (fns.type(name) == 'object') {
option = name
name = option.name || '_unknow'
}
// page internal message
var emitter = new message()
// extend page config
var extendPageBefore = _conf.get('extendPageBefore')
extendPageBefore && extendPageBefore(name, option, modules)
// mixin component defs
// C.use(option, option.comps, `Page[${name}]`, emitter)
if (option.onNavigate){
let onNavigateHandler = function (url, query) {
option.onNavigate({url, query})
}
console.log(`Page[${name}] define "onNavigate".`)
dispatcher.on('navigateTo:'+name, onNavigateHandler)
dispatcher.on('redirectTo:'+name, onNavigateHandler)
dispatcher.on('switchTab:'+name, onNavigateHandler)
dispatcher.on('reLaunch:'+name, onNavigateHandler)
}
/**
* Preload lifecycle method
*/
if (option.onPreload){
console.log(`Page[${name}] define "onPreload".`)
dispatcher.on('preload:'+name, function (url, query) {
option.onPreload({url, query})
})
}
/**
* Instance props
*/
option.$state = {
// 是否小程序被打开首页启动页面
firstOpen: false
}
option.$emitter = emitter
bridge.methods(option)
/**
* Cross pages message methods
*/
option.$on = function () {
return dispatcher.on.apply(dispatcher, arguments)
}
option.$emit = function () {
return dispatcher.emit.apply(dispatcher, arguments)
}
option.$off = function () {
return dispatcher.off.apply(dispatcher, arguments)
}
/**
* 父子通信枢纽模块
*/
option.$ = bridge.mount
/**
* setData wrapper, for component setData with prefix
* @param {String} prefix prefix of component's data
* @param {Object} data
*/
option.$setData = function (prefix, data) {
if (fns.type(prefix) == 'string') {
var props = {}
fns.objEach(data, function (k,v) {
props[prefix + '.' + k] = v
})
return this.setData(props)
} else if (fns.type(prefix) == 'object') {
return this.setData(prefix)
}
}
/**
* AOP life-cycle methods hook
*/
option.onLoad = fns.wrapFun(option.onLoad, function() {
// After onLoad, onAwake is valid if defined
option.onAwake && message.on('app:sleep', (t) => {
option.onAwake.call(this, t)
})
if (!hasPageLoaded) {
hasPageLoaded = true
let $state = this.$state
$state.firstOpen = true
}
})
option.onReady = fns.wrapFun(option.onReady, function () {
redirector.emit('page:ready')
})
// call on launch
if (option.onPageLaunch) {
option.onPageLaunch()
}
if (option.onAppLaunch) {
isAppLaunched ? option.onAppLaunch.apply(option, isAppLaunched) : dispatcher.on('app:launch', function (args) {
option.onAppLaunch.apply(option, args)
})
}
if (option.onAppShow) {
isAppLaunched ? option.onAppShow.apply(option, isAppLaunched) : dispatcher.on('app:show', function (args) {
option.onAppShow.apply(option, args)
})
}
// extend page config
var extendPageAfter = _conf.get('extendPageAfter')
extendPageAfter && extendPageAfter(name, option, modules)
// register page
Page(option)
return option;
}
/**
* 由重定向模块转发到页面内派发器
*/
bridge.redirectDelegate(redirector, dispatcher)
/**
* Application wrapper
*/
function Application (option) {
if (!option.config || !option.config.route || !option.config.route.length) {
throw new Error('config.route is necessary !')
}
if (option.config) {
WXPage.config(option.config)
}
var ctx = option
/**
* APP sleep logical
*/
option.onShow = option.onShow ? fns.wrapFun(option.onShow, appShowHandler) : appShowHandler
option.onHide = option.onHide ? fns.wrapFun(option.onHide, appHideHandler) : appHideHandler
option.onLaunch = option.onLaunch ? fns.wrapFun(option.onLaunch, appLaunchHandler) : appLaunchHandler
option.onLaunch = fns.wrapFun(option.onLaunch, function () {
ctx = this
})
if (option.onAwake) {
message.on('app:sleep', function(t){
option.onAwake.call(ctx, t)
})
}
/**
* Use app config
*/
App(option)
}
function appLaunchHandler() {
isAppLaunched = [].slice.call(arguments)
message.emit('app:launch', isAppLaunched)
}
function appShowHandler () {
try {
if (!isAppShowed) {
// call onAppShow only once
isAppShowed = [].slice.call(arguments)
message.emit('app:show', isAppShowed)
}
} finally {
if (!hideTime) return
var t = hideTime
hideTime = 0
message.emit('app:sleep', new Date() - t)
}
}
function appHideHandler() {
hideTime = new Date()
}
Page.P = WXPage
Page.C = Component.C = WXPage.C = WXPage.Comp = WXPage.Component = C
Page.A = App.A = WXPage.A = WXPage.App = WXPage.Application = Application
WXPage.redirector = redirector
WXPage.message = message
WXPage.cache = cache
WXPage.fns = fns
WXPage.getPageName = bridge.getPageName
/**
* Config handler
*/
WXPage.config = function (key, value) {
if (fns.type(key) == 'object') {
fns.objEach(key, function (k, v) {
_conf.set(k, v)
})
} else {
_conf.set(key, value)
}
return this
}
message.assign(WXPage)
message.assign(C)
message.assign(Application)
module.exports = WXPage