-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathview.svelte
More file actions
222 lines (201 loc) · 6.39 KB
/
view.svelte
File metadata and controls
222 lines (201 loc) · 6.39 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
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
<script>
import { onMount, onDestroy, tick } from 'svelte';
import { app, f7ready } from '../shared/f7.js';
import { colorClasses } from '../shared/mixins.js';
import { classNames, noUndefinedProps, getRouterId } from '../shared/utils.js';
import { getRouterInitialComponent } from '../shared/get-router-initial-component.js';
import { useTab } from '../shared/use-tab.js';
import RouterContextProvider from './router-context-provider.svelte';
let {
class: className,
id = undefined,
style = undefined,
init = true,
url = undefined,
children,
...restProps
} = $props();
const {
main,
tab,
tabActive,
browserHistoryInitialMatch = true,
initRouterOnTabShow,
} = restProps;
const shouldInitRouter = $derived(!(initRouterOnTabShow && tab && !tabActive));
let initialPage = $state(null);
let initialRoute = $state(null);
let el = $state(null);
let routerData = $state(null);
let f7View = $state(null);
export function instance() {
return f7View;
}
function onViewInit(view) {
restProps.onViewInit?.(view);
restProps.onviewinit?.(view);
if (!init) {
f7View = view;
routerData.instance = view;
}
}
if (app.f7 && !f7View && init) {
const routerId = getRouterId();
f7View = app.f7.views.create(el, {
routerId,
init: false,
...noUndefinedProps({ url, ...restProps }),
browserHistoryInitialMatch,
on: {
init: onViewInit,
},
});
routerData = {
routerId,
instance: f7View,
};
app.f7routers.views.push(routerData);
if (shouldInitRouter && f7View && f7View.router && (url || main)) {
const initialData = getRouterInitialComponent(f7View.router);
initialPage = initialData.initialPage;
initialRoute = initialData.initialRoute;
if (initialRoute && initialRoute.route && initialRoute.route.masterRoute) {
initialPage = undefined;
initialRoute = undefined;
}
}
}
let pages = $state(initialPage ? [initialPage] : []);
function onResize(view, width) {
restProps.onViewResize?.(width);
restProps.onviewresize?.(width);
}
function onSwipeBackMove(data) {
restProps.onSwipeBackMove?.(data);
restProps.onswipebackmove?.(data);
}
function onSwipeBackBeforeChange(data) {
restProps.onSwipeBackBeforeChange?.(data);
restProps.onswipebackbeforechange?.(data);
}
function onSwipeBackAfterChange(data) {
restProps.onSwipeBackAfterChange?.(data);
restProps.onswipebackafterchange?.(data);
}
function onSwipeBackBeforeReset(data) {
restProps.onSwipeBackBeforeReset?.(data);
restProps.onswipebackbeforereset?.(data);
}
function onSwipeBackAfterReset(data) {
restProps.onSwipeBackAfterReset?.(data);
restProps.onswipebackafterreset?.(data);
}
const classes = $derived(
classNames(
className,
'view',
{
'view-main': main,
'tab-active': tabActive,
tab,
},
colorClasses(restProps),
),
);
useTab(() => el, restProps);
onMount(() => {
f7ready(() => {
if (f7View) {
routerData.el = el;
routerData.pages = pages;
routerData.setPages = (newPages) => {
tick().then(() => {
pages = [...newPages];
});
};
if (initialPage && initialPage.isAsync && !initialPage.initialComponent) {
initialPage.component().then(() => {
setTimeout(() => {
f7View.init(el);
if (initialPage) {
initialPage.el = f7View.router.currentPageEl;
const routeId = pages?.[0]?.props?.routeId;
if (routeId) initialPage.el.routeId = routeId;
if (initialRoute && initialRoute.route && initialRoute.route.keepAlive) {
initialRoute.route.keepAliveData = { pageEl: initialPage.el };
}
}
}, 100);
});
} else {
f7View.init(el);
if (initialPage) {
initialPage.el = f7View.router.currentPageEl;
const routeId = pages?.[0]?.props?.routeId;
if (routeId) initialPage.el.routeId = routeId;
if (initialRoute && initialRoute.route && initialRoute.route.keepAlive) {
initialRoute.route.keepAliveData = { pageEl: initialPage.el };
}
}
}
} else {
const routerId = getRouterId();
routerData = {
el,
routerId,
pages,
instance: f7View,
setPages(newPages) {
tick().then(() => {
pages = [...newPages];
});
},
};
app.f7routers.views.push(routerData);
routerData.instance = app.f7.views.create(el, {
routerId,
...noUndefinedProps(restProps),
browserHistoryInitialMatch,
on: {
init: onViewInit,
},
});
f7View = routerData.instance;
}
if (!init) return;
f7View.on('resize', onResize);
f7View.on('swipebackMove', onSwipeBackMove);
f7View.on('swipebackBeforeChange', onSwipeBackBeforeChange);
f7View.on('swipebackAfterChange', onSwipeBackAfterChange);
f7View.on('swipebackBeforeReset', onSwipeBackBeforeReset);
f7View.on('swipebackAfterReset', onSwipeBackAfterReset);
});
});
const watchPagesAndRouterData = (pages, routerData) => {
if (!routerData || !pages) return;
app.f7events.emit('viewRouterDidUpdate', routerData);
};
$effect(() => watchPagesAndRouterData(pages, routerData));
onDestroy(() => {
if (f7View) {
f7View.off('resize', onResize);
f7View.off('swipebackMove', onSwipeBackMove);
f7View.off('swipebackBeforeChange', onSwipeBackBeforeChange);
f7View.off('swipebackAfterChange', onSwipeBackAfterChange);
f7View.off('swipebackBeforeReset', onSwipeBackBeforeReset);
f7View.off('swipebackAfterReset', onSwipeBackAfterReset);
if (f7View.destroy) f7View.destroy();
f7View = null;
}
app.f7routers.views.splice(app.f7routers.views.indexOf(routerData), 1);
routerData = null;
});
</script>
<div class={classes} {style} {id} bind:this={el}>
{@render children?.(f7View)}
{#each pages as page (page.id)}
<RouterContextProvider route={page.props.f7route} router={page.props.f7router}>
<page.component {...page.props} />
</RouterContextProvider>
{/each}
</div>