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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import qs from 'qs';
import { AppServiceBase, Util } from 'ibiz-core';
import { AppContextStore } from './app-context-store';
import { UIStateService } from './ui-state-service';
import { AppEvent } from '../../events/app-event';
/**
* 历史记录项
*
* @export
* @interface HistoryItem
*/
export interface HistoryItem {
/**
* 路由信息
*
* @type {*}
* @memberof HistoryItem
*/
to?: any;
/**
* 参数信息
*
* @type {*}
* @memberof HistoryItem
*/
meta?: any;
/**
* 视图标识
*
* @type {string}
* @memberof HistoryItem
*/
tag?: string;
/**
* 上下文
*
* @type {*}
* @memberof HistoryItem
*/
context?: any;
/**
* 标题
*
* @type {string}
* @memberof HistoryItem
*/
caption?: string;
/**
* 子标题
*
* @type {string}
* @memberof HistoryItem
*/
info?: string;
}
/**
* 应用导航记录基类
*
* @export
* @class AppNavHistory
*/
export class AppNavHistory {
/**
* 应用事件
*
* @memberof AppNavHistory
*/
public readonly appEvent = AppEvent.getInstance();
/**
* 应用上下文仓库
*
* @type {AppContextStore}
* @memberof AppNavHistory
*/
public readonly contextStore: AppContextStore = new AppContextStore();
/**
* 界面UI状态服务
*
* @type {UIStateService}
* @memberof AppNavHistory
*/
public readonly uiStateService: UIStateService = new UIStateService();
/**
* 国际化
*
* @memberof AppNavHistory
*/
public i18n: any = AppServiceBase.getInstance().getI18n();
/**
* 路由记录缓存
*
* @type {HistoryItem[]}
* @memberof AppNavHistory
*/
public readonly historyList: HistoryItem[] = [];
/**
* 导航缓存,忽略判断的导航参数正则
*
* @type {RegExp}
* @memberof AppNavHistory
*/
public readonly navIgnoreParameters: RegExp = new RegExp(/(srftabactivate|srftreeexpactivate)/);
/**
* 首页mate信息
*
* @type {*}
* @memberof AppNavHistory
*/
public indexMeta: any = null;
/**
* 分页切换历史记录排序值(最大值表示离当前最近的路由)
*
* @type {*}
* @memberof AppNavHistory
*/
public static sortIndex: number = 0;
/**
* Creates an instance of AppNavHistory.
*
* @memberof AppNavHistory
*/
constructor() {
if (this.uiStateService.layoutState.styleMode === 'STYLE2') {
addEventListener('hashchange', ({ oldURL, newURL }) => {
if (this.historyList.length > 0) {
const param = this.calcRouteParam(oldURL);
const param2 = this.calcRouteParam(newURL);
const lastHistory = this.historyList[this.historyList.length - 1];
if (this.isRouteSame(param, lastHistory.to)) {
this.pop();
} else if (this.isRouteSame(param2, lastHistory.to) && this.historyList.length > 1) {
const item = this.historyList[this.historyList.length - 2];
if (this.isRouteSame(param, item.to)) {
this.historyList.splice(this.historyList.length - 2, 1);
}
}
}
});
}
}
/**
* 根据url计算路由参数
*
* @protected
* @param {string} url
* @returns {*}
* @memberof AppNavHistory
*/
protected calcRouteParam(url: string): any {
const hash = url.substring(url.indexOf('#') + 1);
const queryIndex = hash.indexOf('?');
const path = queryIndex === -1 ? hash : hash.substring(0, queryIndex);
const queryStr = queryIndex === -1 ? '' : hash.substring(queryIndex + 1);
return { path, query: !Util.isEmpty(queryStr) ? qs.parse(queryStr) : {} };
}
/**
* 根据视图标识查找记录
*
* @param {string} tag
* @returns {*}
* @memberof AppNavHistory
*/
public findHistoryByTag(tag: string): any {
return this.historyList.find((item) => Util.isExistAndNotEmpty(item.tag) && item.tag === tag);
}
/**
* 查找路由缓存
*
* @param {*} page
* @param {any[]} [list=this.historyList]
* @returns {number}
* @memberof AppNavHistory
*/
public findHistoryIndex(page: any, list: any[] = this.historyList): number {
if (!Util.isExist(page)) {
return -1;
}
return list.findIndex((item: any) => {
return this.isRouteSame(page, item.to);
});
}
/**
* 新旧路由是否相同
*
* @param {*} newRoute
* @param {*} oldRoute
* @returns {boolean}
* @memberof AppNavHistory
*/
public isRouteSame(newRoute: any, oldRoute: any): boolean {
if (newRoute && oldRoute && Object.is(newRoute.path, oldRoute.path)) {
return this.isQuerySame(newRoute.query, oldRoute.query);
}
return false;
}
/**
* 判断查询参数是否相同,会排除预定义的忽略参数
*
* @param {*} newQuery 新查询参数
* @param {*} oldQuery 旧查询参数
* @returns {boolean}
* @memberof AppNavHistory
*/
public isQuerySame(newQuery: any, oldQuery: any): boolean {
if (Object.keys(newQuery).length !== Object.keys(oldQuery).length) {
return false;
}
for (const key in newQuery) {
// 忽略的参数略过
if (this.navIgnoreParameters.test(`|${key}|`)) {
continue;
}
if (!Util.isExist(oldQuery) || !Object.is(oldQuery[key], newQuery[key])) {
return false;
}
}
return true;
}
/**
* 添加视图缓存
*
* @param {*} to 当前路由信息
* @memberof AppNavHistory
*/
public add(to: any): void {
if (this.findHistoryIndex(to) === -1) {
if (this.uiStateService.layoutState.styleMode === 'DEFAULT' && to?.matched?.length === 1) {
return;
}
const item: any = {
to,
meta: JSON.parse(JSON.stringify(to.meta)),
tag: '',
context: {},
sortIndex: AppNavHistory.sortIndex++
};
const { caption, info } = item.meta;
item.caption = caption;
item.info = '';
this.historyList.push(item);
}
}
/**
* 修改当前项排序值(记录上一分页项)
*
* @param item
*/
public updateSortIndex(item: any) {
if(item) {
item.sortIndex = ++AppNavHistory.sortIndex;
}
}
/**
* 删除视图缓存
*
* @param {HistoryItem} item
* @memberof AppNavHistory
*/
public remove(item: HistoryItem): void {
const i = this.findHistoryIndex(item.to);
if (i !== -1) {
this.historyList.splice(i, 1);
}
}
/**
* 重置路由缓存
*
* @param {number} [num=0]
* @memberof AppNavHistory
*/
public reset(num: number = 0): void {
this.historyList.splice(num, this.historyList.length);
}
/**
* 设置指定缓存视图标题
*
* @param {({ tag: string, caption: string | null, info: string | null })} { tag, caption, info }
* @returns {boolean}
* @memberof AppNavHistory
*/
public setCaption({ tag, caption, info }: { tag: string; caption?: string; info?: string }): boolean {
const item: HistoryItem = this.findHistoryByTag(tag);
if (item) {
const meta = item.meta;
if (caption) {
meta.caption = caption;
}
if (Util.isExistAndNotEmpty(info)) {
meta.info = info;
item.info = info;
}
this.appEvent.emit('navHistoryItemChange', item);
}
return true;
}
/**
* 设置路由视图标识
*
* @param {string} tag
* @param {*} route
* @returns {boolean}
* @memberof AppNavHistory
*/
public setViewTag(tag: string, route: any): boolean {
const i = this.findHistoryIndex(route);
if (i === -1) {
return false;
}
const item = this.historyList[i];
if (Util.isExistAndNotEmpty(item.tag)) {
return false;
}
item.tag = tag;
return true;
}
/**
* 设置路由视图上下文
*
* @param {*} context
* @param {*} tag
* @returns {boolean}
* @memberof AppNavHistory
*/
public setViewContext(context: any, tag: any): boolean {
const item = this.findHistoryByTag(tag);
if (item) {
item.context = context;
return true;
}
return false;
}
/**
* 删除其他缓存
*
* @param {HistoryItem} item
* @memberof AppNavHistory
*/
public removeOther(item: HistoryItem): void {
const i = this.findHistoryIndex(item.to);
if (i !== -1) {
const page = this.historyList[i];
this.historyList.splice(0, this.historyList.length);
this.historyList.push(page);
}
}
/**
* 缓存后退
*
* @memberof AppNavHistory
*/
public pop(): void {
this.historyList.pop();
}
}