@saltcorn/mobile-app 0.9.3 → 0.9.4-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/www/index.html +1 -0
- package/www/js/routes/page.js +54 -9
- package/www/js/utils/global_utils.js +3 -0
package/package.json
CHANGED
package/www/index.html
CHANGED
package/www/js/routes/page.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*global MobileRequest, parseQuery, MobileResponse, wrapContents, saltcorn, loadFileAsText*/
|
|
1
|
+
/*global window, MobileRequest, parseQuery, MobileResponse, wrapContents, saltcorn, loadFileAsText*/
|
|
2
2
|
|
|
3
3
|
// post/page/:pagename/action/:rndid
|
|
4
4
|
const postPageAction = async (context) => {
|
|
@@ -24,19 +24,64 @@ const postPageAction = async (context) => {
|
|
|
24
24
|
return result || {};
|
|
25
25
|
};
|
|
26
26
|
|
|
27
|
+
const findPageOrGroup = (pagename) => {
|
|
28
|
+
const page = saltcorn.data.models.Page.findOne({ name: pagename });
|
|
29
|
+
if (page) return { page, pageGroup: null };
|
|
30
|
+
else {
|
|
31
|
+
const pageGroup = saltcorn.data.models.PageGroup.findOne({
|
|
32
|
+
name: pagename,
|
|
33
|
+
});
|
|
34
|
+
if (pageGroup) return { page: null, pageGroup };
|
|
35
|
+
else return { page: null, pageGroup: null };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const runPage = async (page, state, context, { req, res }) => {
|
|
40
|
+
if (state.mobileConfig.role_id > page.min_role)
|
|
41
|
+
throw new saltcorn.data.utils.NotAuthorized(req.__("Not authorized"));
|
|
42
|
+
const query = parseQuery(context.query);
|
|
43
|
+
return await page.run(query, { req, res });
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const getEligiblePage = async (pageGroup, req) => {
|
|
47
|
+
const screenInfos = {
|
|
48
|
+
width: window.screen.width,
|
|
49
|
+
height: window.screen.height,
|
|
50
|
+
innerWidth: window.innerWidth,
|
|
51
|
+
innerHeight: window.innerHeight,
|
|
52
|
+
device: "mobile", // TODO UAParser knows tablet and mobile
|
|
53
|
+
};
|
|
54
|
+
if (pageGroup.members.length === 0)
|
|
55
|
+
return req.__("Pagegroup %s has no members", pageGroup.name);
|
|
56
|
+
return await pageGroup.getEligiblePage(
|
|
57
|
+
screenInfos,
|
|
58
|
+
req.user,
|
|
59
|
+
req.getLocale()
|
|
60
|
+
);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const runPageGroup = async (pageGroup, state, context, { req, res }) => {
|
|
64
|
+
if (state.mobileConfig.role_id > pageGroup.min_role)
|
|
65
|
+
throw new saltcorn.data.utils.NotAuthorized(req.__("Not authorized"));
|
|
66
|
+
const page = await getEligiblePage(pageGroup, req);
|
|
67
|
+
if (!page)
|
|
68
|
+
throw new Error(req.__(`Pagegroup ${pageGroup.name} has no eligible page`));
|
|
69
|
+
else if (typeof page === "string") throw new Error(page);
|
|
70
|
+
return await runPage(page, state, context, { req, res });
|
|
71
|
+
};
|
|
72
|
+
|
|
27
73
|
// get/page/pagename
|
|
28
74
|
const getPage = async (context) => {
|
|
29
75
|
const state = saltcorn.data.state.getState();
|
|
30
76
|
const req = new MobileRequest({ xhr: context.xhr });
|
|
31
|
-
const { page_name } = context.params;
|
|
32
|
-
const page = await saltcorn.data.models.Page.findOne({ name: page_name });
|
|
33
|
-
if (!page) throw new Error(req.__("Page %s not found", page_name));
|
|
34
|
-
if (state.mobileConfig.role_id > page.min_role) {
|
|
35
|
-
throw new saltcorn.data.utils.NotAuthorized(req.__("Not authorized"));
|
|
36
|
-
}
|
|
37
|
-
const query = parseQuery(context.query);
|
|
38
77
|
const res = new MobileResponse();
|
|
39
|
-
const
|
|
78
|
+
const { page_name } = context.params;
|
|
79
|
+
const { page, pageGroup } = findPageOrGroup(page_name);
|
|
80
|
+
let contents = null;
|
|
81
|
+
if (page) contents = await runPage(page, state, context, { req, res });
|
|
82
|
+
else if (pageGroup)
|
|
83
|
+
contents = await runPageGroup(pageGroup, state, context, { req, res });
|
|
84
|
+
else throw new Error(req.__("Page %s not found", page_name));
|
|
40
85
|
if (contents.html_file) {
|
|
41
86
|
if (state.mobileConfig?.isOfflineMode)
|
|
42
87
|
throw new Error(req.__("Offline mode: cannot load file"));
|
|
@@ -292,6 +292,7 @@ function isHtmlFile() {
|
|
|
292
292
|
|
|
293
293
|
async function handleRoute(route, query, files, data) {
|
|
294
294
|
const mobileConfig = saltcorn.data.state.getState().mobileConfig;
|
|
295
|
+
let routeAdded = false;
|
|
295
296
|
try {
|
|
296
297
|
if (
|
|
297
298
|
mobileConfig.networkState === "none" &&
|
|
@@ -306,6 +307,7 @@ async function handleRoute(route, query, files, data) {
|
|
|
306
307
|
return await gotoEntryView();
|
|
307
308
|
const safeRoute = route ? route : currentLocation();
|
|
308
309
|
addRoute({ route: safeRoute, query });
|
|
310
|
+
routeAdded = true;
|
|
309
311
|
const page = await router.resolve({
|
|
310
312
|
pathname: safeRoute,
|
|
311
313
|
query: query,
|
|
@@ -348,6 +350,7 @@ async function handleRoute(route, query, files, data) {
|
|
|
348
350
|
}
|
|
349
351
|
}
|
|
350
352
|
} catch (error) {
|
|
353
|
+
if (routeAdded) popRoute();
|
|
351
354
|
showAlerts([
|
|
352
355
|
{
|
|
353
356
|
type: "error",
|