-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.js
executable file
·191 lines (170 loc) · 5.88 KB
/
service.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
/**
* 通用 service
*
* @file service.js
* @author harttle<harttle@harttle.com>
*/
define(function (require) {
var rt = require('ralltiir');
var Promise = rt.promise;
var View = require('./view/view');
var Performance = require('./utils/performance');
var UA = require('./utils/ua');
var _ = require('@searchfe/underscore');
var logger = rt.logger;
var config = require('./config');
// eslint-disable-next-line
function Service(url, options) {
this.options = normalize(options);
this.scope = {
name: this.options.name,
performance: new Performance(),
options: this.options
};
this.name = this.options.name;
}
Service.prototype.hasValidView = function () {
return this.view && this.view.valid;
};
Service.prototype.shouldUseCache = function (current, useAnimation) {
if (config.cacheDisabled) {
return false;
}
if (this.options.isolateCSS && useAnimation) {
return false;
}
var reason = _.get(current, 'options.src');
return reason === 'back' || reason === 'history';
};
Service.prototype.beforeAttach = function (current, prev) {
this.scope.performance.startNavigation();
_.assign(this.options, current.options);
var useAnimation = this.shouldEnterAnimate(current);
logger.debug('service.beforeAttach', this.beforeAttach);
if (this.shouldUseCache(current, useAnimation) && this.hasValidView()) {
logger.info('using cached dom for', current.url);
this.view.reuse();
}
else if (isServerRendered(current)) {
var rootEl = document.querySelector('#sfr-app .rt-view');
this.view = new View(this.scope, rootEl);
}
else {
this.view = new View(this.scope);
this.view.fetchUrl({
current: current,
prev: prev,
url: current.url
});
}
return this.view.enter(useAnimation);
};
Service.prototype.shouldEnterAnimate = function (state) {
var useAnimate = _.get(state, 'options.view.useAnimate');
if (useAnimate === false) {
return false;
}
if (isServerRendered(state)) {
return false;
}
var reason = _.get(state, 'options.src');
return !reason || reason === 'hijack';
};
Service.prototype.shouldExitAnimate = function (current, prev) {
if (this.options.isolateCSS) {
if (!this.name) {
return false;
}
if (this.name !== _.get(current, 'service.name')) {
return false;
}
}
var reason = _.get(current, 'options.src');
return reason === 'back';
};
Service.prototype.attach = function (current) {
if (UA.isWKWebView && _.get(current, 'options.src') === 'history') {
// WKWebview 在部分回退场景下不引起重绘
window.scroll(0, 1);
}
var view = this.view;
view.setActive();
return Promise.resolve()
.then(function () {
return view.populated ? '' : view.render();
})
.then(function () {
return view.setAttached();
})
.catch(function (err) {
var code = err.code || 999;
var msg = err.message || 'unkown';
var query = location.search + (location.search ? '&' : '?');
// eslint-disable-next-line
console.error(err);
if (_.get(current, 'options.src') !== 'sync' && query.indexOf('rt-err=') === -1) {
query += 'rt-err=' + code;
query += '&rt-msg=' + encodeURIComponent(msg);
var url = location.protocol + '//' + location.host
+ location.pathname + query + location.hash;
location.replace(url);
}
});
};
Service.prototype.beforeDetach = function (current, prev) {
return this.view.prepareExit(this.shouldExitAnimate(current, prev));
};
Service.prototype.detach = function (current, prev, extra) {
var view = this.view;
return view
.exit(this.shouldExitAnimate(current, prev))
.then(function () {
view.setDetached();
});
};
Service.prototype.destroy = function () {
return this.view.destroy();
};
Service.prototype.onMessage = function (msg) {
this.view.trigger('rt.message', {data: msg});
};
Service.setBackHtml = function (html) {
View.backHtml = html;
};
function isServerRendered(state) {
return _.get(state, 'options.src') === 'sync';
}
function normalize(options) {
if (!options) {
return {baseUrl: ''};
}
options = _.cloneDeep(options);
if (options.view || options.head) {
// eslint-disable-next-line
console.warn(
'[DEPRECATED] options.head, options.view will not be supported in future version',
'checkout: https://ralltiir.github.io/ralltiir/get-started/view-options.html'
);
}
options = _.assign({}, options, options.view, options.head);
if (_.isString(options.title)) {
options.title = {html: options.title};
}
if (_.isString(options.subtitle)) {
options.subtitle = {html: options.subtitle};
}
if (_.isString(options.back)) {
options.back = {html: options.back};
}
_.forEach(options.actions, function (action, i) {
if (_.isString(action)) {
options.actions[i] = {html: action};
}
});
if (options.baseUrl === undefined) {
options.baseUrl = '';
}
return options;
}
return Service;
});