-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathapp-class.js
More file actions
397 lines (343 loc) · 9.94 KB
/
app-class.js
File metadata and controls
397 lines (343 loc) · 9.94 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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* eslint-disable no-underscore-dangle */
import { getWindow, getDocument } from 'ssr-window';
import { extend, nextFrame } from '../../shared/utils.js';
import { getDevice } from '../../shared/get-device.js';
import { getSupport } from '../../shared/get-support.js';
import Framework7Class from '../../shared/class.js';
import EventsClass from '../../shared/events-class.js';
import ConstructorMethods from '../../shared/constructor-methods.js';
import ModalMethods from '../../shared/modal-methods.js';
import $ from '../../shared/dom7.js';
import loadModule from './load-module.js';
import $jsx from '../../shared/$jsx.js';
class Framework7 extends Framework7Class {
constructor(params = {}) {
const routes = params.routes;
if (routes) {
params.routes = routes.map(route => {
const newRoute = { ...route };
const options = newRoute.options ||= {};
const props = options.props ||= {};
Object.defineProperty(props, 'routeId', {
get: () => `${Date.now()}_${Math.random().toString(36).slice(2)}`,
enumerable: true,
configurable: true
});
return newRoute;
});
}
super(params);
// eslint-disable-next-line
if (Framework7.instance && typeof window !== 'undefined') {
throw new Error("Framework7 is already initialized and can't be initialized more than once");
}
const device = getDevice({ userAgent: params.userAgent || undefined });
const support = getSupport();
const passedParams = extend({}, params);
// App Instance
const app = this;
app.device = device;
app.support = support;
const w = getWindow();
const d = getDocument();
Framework7.instance = app;
// Default
const defaults = {
el: 'body',
theme: 'auto',
routes: [],
name: 'Framework7',
lazyModulesPath: null,
initOnDeviceReady: true,
init: true,
darkMode: undefined,
iosTranslucentBars: true,
iosTranslucentModals: true,
component: undefined,
componentUrl: undefined,
userAgent: null,
url: null,
mdColorScheme: 'default',
colors: {
primary: '#007aff',
red: '#ff3b30',
green: '#4cd964',
blue: '#2196f3',
pink: '#ff2d55',
yellow: '#ffcc00',
orange: '#ff9500',
purple: '#9c27b0',
deeppurple: '#673ab7',
lightblue: '#5ac8fa',
teal: '#009688',
lime: '#cddc39',
deeporange: '#ff6b22',
white: '#ffffff',
black: '#000000',
},
};
// Extend defaults with modules params
app.useModulesParams(defaults);
// Extend defaults with passed params
app.params = extend(defaults, params);
extend(app, {
// App Name
name: app.params.name,
// Routes
routes: app.params.routes,
// Theme
theme: (function getTheme() {
if (app.params.theme === 'auto') {
if (device.ios) return 'ios';
return 'md';
}
return app.params.theme;
})(),
// Initially passed parameters
passedParams,
online: w.navigator.onLine,
colors: app.params.colors,
mdColorScheme: app.params.mdColorScheme || 'default',
darkMode: app.params.darkMode,
});
if (params.store) app.params.store = params.store;
// Save Root
if (app.$el && app.$el[0]) {
app.$el[0].f7 = app;
}
// Install Modules
app.useModules();
// Init Store
app.initStore();
// Init
if (app.params.init) {
if (device.cordova && app.params.initOnDeviceReady) {
$(d).on('deviceready', () => {
app.init();
});
} else {
app.init();
}
}
// Return app instance
return app;
}
setColorTheme(color, mdColorScheme) {
if (!color) return;
const app = this;
app.colors.primary = color;
app.mdColorScheme = mdColorScheme || app.mdColorScheme;
app.setColors();
}
setMdColorScheme(mdColorScheme) {
const app = this;
app.mdColorScheme = mdColorScheme || app.mdColorScheme;
app.setColors();
}
setColors() {
const app = this;
const document = getDocument();
if (!app.colorsStyleEl) {
app.colorsStyleEl = document.createElement('style');
document.head.prepend(app.colorsStyleEl);
}
app.colorsStyleEl.textContent = app.utils.colorThemeCSSStyles(app.colors, app.mdColorScheme);
}
mount(rootEl) {
const app = this;
const window = getWindow();
const document = getDocument();
const $rootEl = $(rootEl || app.params.el).eq(0);
app.$el = $rootEl;
if (app.$el && app.$el[0]) {
app.el = app.$el[0];
app.el.f7 = app;
app.rtl = $rootEl.css('direction') === 'rtl';
}
// Auto Dark Mode
const DARK = '(prefers-color-scheme: dark)';
const LIGHT = '(prefers-color-scheme: light)';
app.mq = {};
if (window.matchMedia) {
app.mq.dark = window.matchMedia(DARK);
app.mq.light = window.matchMedia(LIGHT);
}
app.colorSchemeListener = function colorSchemeListener({ matches, media }) {
if (!matches) {
return;
}
const html = document.querySelector('html');
if (media === DARK) {
html.classList.add('dark');
app.darkMode = true;
app.emit('darkModeChange', true);
} else if (media === LIGHT) {
html.classList.remove('dark');
app.darkMode = false;
app.emit('darkModeChange', false);
}
};
app.emit('mount');
}
initStore() {
const app = this;
if (typeof app.params.store !== 'undefined' && app.params.store.__store) {
app.store = app.params.store;
} else {
app.store = app.createStore(app.params.store);
}
}
enableAutoDarkMode() {
const window = getWindow();
const document = getDocument();
if (!window.matchMedia) return;
const app = this;
const html = document.querySelector('html');
if (app.mq.dark && app.mq.light) {
app.mq.dark.addEventListener('change', app.colorSchemeListener);
app.mq.light.addEventListener('change', app.colorSchemeListener);
}
if (app.mq.dark && app.mq.dark.matches) {
html.classList.add('dark');
app.darkMode = true;
app.emit('darkModeChange', true);
} else if (app.mq.light && app.mq.light.matches) {
html.classList.remove('dark');
app.darkMode = false;
app.emit('darkModeChange', false);
}
}
disableAutoDarkMode() {
const window = getWindow();
if (!window.matchMedia) return;
const app = this;
if (app.mq.dark) app.mq.dark.removeEventListener('change', app.colorSchemeListener);
if (app.mq.light) app.mq.light.removeEventListener('change', app.colorSchemeListener);
}
setDarkMode(mode) {
const app = this;
if (mode === 'auto') {
app.enableAutoDarkMode();
} else {
app.disableAutoDarkMode();
$('html')[mode ? 'addClass' : 'removeClass']('dark');
app.darkMode = mode;
}
}
initAppComponent(callback) {
const app = this;
app.router.componentLoader(
app.params.component,
app.params.componentUrl,
{ componentOptions: { el: app.$el[0] } },
(el) => {
app.$el = $(el);
app.$el[0].f7 = app;
app.$elComponent = el.f7Component;
app.el = app.$el[0];
if (callback) callback();
},
() => {},
);
}
init(rootEl) {
const app = this;
app.setColors();
app.mount(rootEl);
const init = () => {
if (app.initialized) return;
app.$el.addClass('framework7-initializing');
// RTL attr
if (app.rtl) {
$('html').attr('dir', 'rtl');
}
// Auto Dark Mode
if (typeof app.params.darkMode === 'undefined') {
app.darkMode = $('html').hasClass('dark');
} else {
app.setDarkMode(app.params.darkMode);
}
// Watch for online/offline state
const window = getWindow();
window.addEventListener('offline', () => {
app.online = false;
app.emit('offline');
app.emit('connection', false);
});
window.addEventListener('online', () => {
app.online = true;
app.emit('online');
app.emit('connection', true);
});
// Root class
app.$el.addClass('framework7-root');
// Theme class
$('html').removeClass('ios md').addClass(app.theme);
// iOS Translucent
if (app.params.iosTranslucentBars && app.theme === 'ios') {
$('html').addClass('ios-translucent-bars');
}
if (app.params.iosTranslucentModals && app.theme === 'ios') {
$('html').addClass('ios-translucent-modals');
}
// Init class
nextFrame(() => {
app.$el.removeClass('framework7-initializing');
});
// Emit, init other modules
app.initialized = true;
app.emit('init');
};
if (app.params.component || app.params.componentUrl) {
app.initAppComponent(() => {
init();
});
} else {
init();
}
return app;
}
// eslint-disable-next-line
loadModule(...args) {
return Framework7.loadModule(...args);
}
// eslint-disable-next-line
loadModules(...args) {
return Framework7.loadModules(...args);
}
getVnodeHooks(hook, id) {
const app = this;
if (!app.vnodeHooks || !app.vnodeHooks[hook]) return [];
return app.vnodeHooks[hook][id] || [];
}
// eslint-disable-next-line
get $() {
return $;
}
static get Dom7() {
return $;
}
static get $() {
return $;
}
static get device() {
return getDevice();
}
static get support() {
return getSupport();
}
static get Class() {
return Framework7Class;
}
static get Events() {
return EventsClass;
}
}
Framework7.$jsx = $jsx;
Framework7.ModalMethods = ModalMethods;
Framework7.ConstructorMethods = ConstructorMethods;
Framework7.loadModule = loadModule;
Framework7.loadModules = function loadModules(modules) {
return Promise.all(modules.map((module) => Framework7.loadModule(module)));
};
export default Framework7;