@saltcorn/mobile-app 1.1.0-beta.11 → 1.1.0-beta.13
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/.babelrc +3 -0
- package/build_scripts/modify_android_manifest.js +47 -0
- package/build_scripts/modify_gradle_cfg.js +34 -0
- package/package.json +20 -11
- package/src/.eslintrc +21 -0
- package/src/helpers/api.js +41 -0
- package/src/helpers/auth.js +191 -0
- package/src/helpers/common.js +175 -0
- package/{www/js/utils/table_utils.js → src/helpers/db_schema.js} +18 -40
- package/src/helpers/file_system.js +102 -0
- package/{www/js/utils/global_utils.js → src/helpers/navigation.js} +169 -335
- package/src/helpers/offline_mode.js +645 -0
- package/src/index.js +20 -0
- package/src/init.js +424 -0
- package/src/routing/index.js +98 -0
- package/{www/js → src/routing}/mocks/request.js +5 -5
- package/{www/js → src/routing}/mocks/response.js +1 -1
- package/{www/js → src/routing}/routes/api.js +10 -15
- package/{www/js → src/routing}/routes/auth.js +12 -6
- package/{www/js → src/routing}/routes/delete.js +9 -6
- package/{www/js → src/routing}/routes/edit.js +9 -6
- package/src/routing/routes/error.js +6 -0
- package/{www/js → src/routing}/routes/fields.js +7 -2
- package/{www/js → src/routing}/routes/page.js +14 -9
- package/{www/js → src/routing}/routes/sync.js +9 -5
- package/{www/js → src/routing}/routes/view.js +16 -11
- package/{www/js/routes/common.js → src/routing/utils.js} +15 -13
- package/webpack.config.js +31 -0
- package/www/data/encoded_site_logo.js +1 -0
- package/www/index.html +23 -491
- package/www/js/{utils/iframe_view_utils.js → iframe_view_utils.js} +137 -269
- package/config.xml +0 -27
- package/res/icon/android/icon.png +0 -0
- package/res/screen/android/splash-icon.png +0 -0
- package/res/screen/ios/Default@2x~universal~anyany.png +0 -0
- package/www/js/routes/error.js +0 -5
- package/www/js/routes/init.js +0 -76
- package/www/js/utils/file_helpers.js +0 -108
- package/www/js/utils/offline_mode_helper.js +0 -625
package/.babelrc
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
const { parseStringPromise, Builder } = require("xml2js");
|
|
2
|
+
const { join } = require("path");
|
|
3
|
+
const { readFileSync, writeFileSync } = require("fs");
|
|
4
|
+
|
|
5
|
+
(async () => {
|
|
6
|
+
try {
|
|
7
|
+
const androidManifest = join(
|
|
8
|
+
"android",
|
|
9
|
+
"app",
|
|
10
|
+
"src",
|
|
11
|
+
"main",
|
|
12
|
+
"AndroidManifest.xml"
|
|
13
|
+
);
|
|
14
|
+
const content = readFileSync(androidManifest);
|
|
15
|
+
const parsed = await parseStringPromise(content);
|
|
16
|
+
|
|
17
|
+
parsed.manifest["uses-permission"] = [
|
|
18
|
+
{ $: { "android:name": "android.permission.READ_EXTERNAL_STORAGE" } },
|
|
19
|
+
{ $: { "android:name": "android.permission.WRITE_EXTERNAL_STORAGE" } },
|
|
20
|
+
{ $: { "android:name": "android.permission.INTERNET" } },
|
|
21
|
+
{ $: { "android:name": "android.permission.CAMERA" } },
|
|
22
|
+
{ $: { "android:name": "android.permission.ACCESS_COARSE_LOCATION" } },
|
|
23
|
+
{ $: { "android:name": "android.permission.ACCESS_FINE_LOCATION" } },
|
|
24
|
+
];
|
|
25
|
+
parsed.manifest["uses-feature"] = [
|
|
26
|
+
{ $: { "android:name": "android.hardware.location.gps" } },
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
parsed.manifest.application[0].$ = {
|
|
30
|
+
...parsed.manifest.application[0].$,
|
|
31
|
+
"android:allowBackup": "false",
|
|
32
|
+
"android:fullBackupContent": "false",
|
|
33
|
+
"android:dataExtractionRules": "@xml/data_extraction_rules",
|
|
34
|
+
"android:networkSecurityConfig": "@xml/network_security_config",
|
|
35
|
+
"android:usesCleartextTraffic": "true",
|
|
36
|
+
};
|
|
37
|
+
const xmlBuilder = new Builder();
|
|
38
|
+
const newCfg = xmlBuilder.buildObject(parsed);
|
|
39
|
+
writeFileSync(androidManifest, newCfg);
|
|
40
|
+
} catch (error) {
|
|
41
|
+
console.log(
|
|
42
|
+
`Unable to modify the AndroidManifest.xml: ${
|
|
43
|
+
error.message ? error.message : "Unknown error"
|
|
44
|
+
}`
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
})();
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
const { join } = require("path");
|
|
2
|
+
const { readFileSync, writeFileSync } = require("fs");
|
|
3
|
+
|
|
4
|
+
console.log("Writing gradle config");
|
|
5
|
+
console.log("args", process.argv);
|
|
6
|
+
|
|
7
|
+
const args = process.argv.slice(2);
|
|
8
|
+
const keyStoreFile = args[0].split("=")[1];
|
|
9
|
+
const keyStoreAlias = args[1].split("=")[1];
|
|
10
|
+
const keyStorePassword = args[2].split("=")[1];
|
|
11
|
+
|
|
12
|
+
const gradleFile = join(__dirname, "..", "android", "app", "build.gradle");
|
|
13
|
+
const gradleContent = readFileSync(gradleFile, "utf8");
|
|
14
|
+
const newGradleContent = gradleContent
|
|
15
|
+
.replace(
|
|
16
|
+
/release\s*{/,
|
|
17
|
+
`release {
|
|
18
|
+
signingConfig signingConfigs.release`
|
|
19
|
+
)
|
|
20
|
+
.replace(
|
|
21
|
+
/buildTypes\s*{/,
|
|
22
|
+
`
|
|
23
|
+
signingConfigs {
|
|
24
|
+
release {
|
|
25
|
+
keyAlias '${keyStoreAlias}'
|
|
26
|
+
keyPassword '${keyStorePassword}'
|
|
27
|
+
storeFile file('${keyStoreFile}')
|
|
28
|
+
storePassword '${keyStorePassword}'
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
buildTypes {`
|
|
32
|
+
);
|
|
33
|
+
console.log("newGradleContent", newGradleContent);
|
|
34
|
+
writeFileSync(gradleFile, newGradleContent, "utf8");
|
package/package.json
CHANGED
|
@@ -1,25 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@saltcorn/mobile-app",
|
|
3
3
|
"displayName": "Saltcorn mobile app",
|
|
4
|
-
"version": "1.1.0-beta.
|
|
4
|
+
"version": "1.1.0-beta.13",
|
|
5
5
|
"description": "Apache Cordova application with @saltcorn/markup",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"
|
|
9
|
-
"add-
|
|
10
|
-
"
|
|
8
|
+
"build": "webpack",
|
|
9
|
+
"add-platform": "npx cap add",
|
|
10
|
+
"modify-gradle-cfg": "node build_scripts/modify_gradle_cfg.js",
|
|
11
|
+
"modify-android-manifest": "node build_scripts/modify_android_manifest.js",
|
|
11
12
|
"test": "echo NO TESTS"
|
|
12
13
|
},
|
|
13
14
|
"author": "Christian Hugo",
|
|
14
15
|
"license": "MIT",
|
|
15
|
-
"cordova": {
|
|
16
|
-
"platforms": [
|
|
17
|
-
"android"
|
|
18
|
-
]
|
|
19
|
-
},
|
|
20
16
|
"overrides": {
|
|
21
17
|
"ansi-regex": "4.1.1"
|
|
22
18
|
},
|
|
23
|
-
"dependencies": {
|
|
24
|
-
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"universal-router": "9.2.0",
|
|
21
|
+
"axios": "^1.7.7",
|
|
22
|
+
"jwt-decode": "4.0.0",
|
|
23
|
+
"i18next": "23.16.4",
|
|
24
|
+
"i18next-sprintf-postprocessor": "0.2.2"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"xml2js": "0.6.0",
|
|
28
|
+
"@babel/core": "^7.26.0",
|
|
29
|
+
"@babel/preset-env": "^7.26.0",
|
|
30
|
+
"babel-loader": "^9.2.1",
|
|
31
|
+
"webpack": "^5.96.1",
|
|
32
|
+
"webpack-cli": "^5.1.4"
|
|
33
|
+
}
|
|
25
34
|
}
|
package/src/.eslintrc
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "eslint:recommended",
|
|
3
|
+
|
|
4
|
+
"parserOptions": {
|
|
5
|
+
"ecmaVersion": 2021,
|
|
6
|
+
"sourceType": "module",
|
|
7
|
+
|
|
8
|
+
// typescript-eslint specific options
|
|
9
|
+
"warnOnUnsupportedTypeScriptVersion": true
|
|
10
|
+
},
|
|
11
|
+
"env": {
|
|
12
|
+
"browser": true
|
|
13
|
+
},
|
|
14
|
+
"ignorePatterns": ["dist/**"],
|
|
15
|
+
"rules": {
|
|
16
|
+
"no-unused-vars": "warn",
|
|
17
|
+
"no-case-declarations": "off",
|
|
18
|
+
"no-empty": "warn",
|
|
19
|
+
"no-fallthrough": "warn"
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/*global saltcorn, splashConfig*/
|
|
2
|
+
|
|
3
|
+
import axios from "axios";
|
|
4
|
+
|
|
5
|
+
export async function apiCall({
|
|
6
|
+
method,
|
|
7
|
+
path,
|
|
8
|
+
params,
|
|
9
|
+
body,
|
|
10
|
+
responseType,
|
|
11
|
+
timeout,
|
|
12
|
+
}) {
|
|
13
|
+
const config =
|
|
14
|
+
typeof saltcorn !== "undefined"
|
|
15
|
+
? saltcorn.data.state.getState().mobileConfig
|
|
16
|
+
: splashConfig;
|
|
17
|
+
const serverPath = config.server_path;
|
|
18
|
+
const url = `${serverPath}${path}`;
|
|
19
|
+
const headers = {
|
|
20
|
+
"X-Requested-With": "XMLHttpRequest",
|
|
21
|
+
"X-Saltcorn-Client": "mobile-app",
|
|
22
|
+
};
|
|
23
|
+
if (config.tenantAppName) headers["X-Saltcorn-App"] = config.tenantAppName;
|
|
24
|
+
const token = config.jwt;
|
|
25
|
+
if (token) headers.Authorization = `jwt ${token}`;
|
|
26
|
+
try {
|
|
27
|
+
const result = await axios({
|
|
28
|
+
url: url,
|
|
29
|
+
method,
|
|
30
|
+
params,
|
|
31
|
+
headers,
|
|
32
|
+
responseType: responseType ? responseType : "json",
|
|
33
|
+
data: body,
|
|
34
|
+
timeout: timeout ? timeout : 0,
|
|
35
|
+
});
|
|
36
|
+
return result;
|
|
37
|
+
} catch (error) {
|
|
38
|
+
error.message = `Unable to call ${method} ${url}:\n${error.message}`;
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/*global saltcorn*/
|
|
2
|
+
|
|
3
|
+
import { jwtDecode } from "jwt-decode";
|
|
4
|
+
import i18next from "i18next";
|
|
5
|
+
import { apiCall } from "./api";
|
|
6
|
+
import { router } from "../routing/index";
|
|
7
|
+
import { getLastOfflineSession, deleteOfflineData, sync } from "./offline_mode";
|
|
8
|
+
import { addRoute, replaceIframe } from "../helpers/navigation";
|
|
9
|
+
import { showAlerts } from "./common";
|
|
10
|
+
|
|
11
|
+
async function loginRequest({ email, password, isSignup, isPublic }) {
|
|
12
|
+
const opts = isPublic
|
|
13
|
+
? {
|
|
14
|
+
method: "GET",
|
|
15
|
+
path: "/auth/login-with/jwt",
|
|
16
|
+
}
|
|
17
|
+
: isSignup
|
|
18
|
+
? {
|
|
19
|
+
method: "POST",
|
|
20
|
+
path: "/auth/signup",
|
|
21
|
+
body: {
|
|
22
|
+
email,
|
|
23
|
+
password,
|
|
24
|
+
},
|
|
25
|
+
}
|
|
26
|
+
: {
|
|
27
|
+
method: "GET",
|
|
28
|
+
path: "/auth/login-with/jwt",
|
|
29
|
+
params: {
|
|
30
|
+
email,
|
|
31
|
+
password,
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
const response = await apiCall(opts);
|
|
35
|
+
return response.data;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function login({ email, password, entryPoint, isSignup }) {
|
|
39
|
+
const loginResult = await loginRequest({
|
|
40
|
+
email,
|
|
41
|
+
password,
|
|
42
|
+
isSignup,
|
|
43
|
+
});
|
|
44
|
+
if (typeof loginResult === "string") {
|
|
45
|
+
// use it as a token
|
|
46
|
+
const decodedJwt = jwtDecode(loginResult);
|
|
47
|
+
const config = saltcorn.data.state.getState().mobileConfig;
|
|
48
|
+
config.user = decodedJwt.user;
|
|
49
|
+
config.isPublicUser = false;
|
|
50
|
+
config.isOfflineMode = false;
|
|
51
|
+
await insertUser(config.user);
|
|
52
|
+
await setJwt(loginResult);
|
|
53
|
+
config.jwt = loginResult;
|
|
54
|
+
i18next.changeLanguage(config.user.language);
|
|
55
|
+
const alerts = [];
|
|
56
|
+
if (config.allowOfflineMode) {
|
|
57
|
+
const { offlineUser, hasOfflineData } =
|
|
58
|
+
(await getLastOfflineSession()) || {};
|
|
59
|
+
if (!offlineUser || offlineUser === config.user.email) {
|
|
60
|
+
await sync();
|
|
61
|
+
} else {
|
|
62
|
+
if (hasOfflineData)
|
|
63
|
+
alerts.push({
|
|
64
|
+
type: "warning",
|
|
65
|
+
msg: `'${offlineUser}' has not yet uploaded offline data.`,
|
|
66
|
+
});
|
|
67
|
+
else {
|
|
68
|
+
await deleteOfflineData(true);
|
|
69
|
+
await sync();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
alerts.push({
|
|
74
|
+
type: "success",
|
|
75
|
+
msg: i18next.t("Welcome, %s!", {
|
|
76
|
+
postProcess: "sprintf",
|
|
77
|
+
sprintf: [config.user.email],
|
|
78
|
+
}),
|
|
79
|
+
});
|
|
80
|
+
addRoute({ route: entryPoint, query: undefined });
|
|
81
|
+
const page = await router.resolve({
|
|
82
|
+
pathname: entryPoint,
|
|
83
|
+
fullWrap: true,
|
|
84
|
+
alerts,
|
|
85
|
+
});
|
|
86
|
+
if (page.content) await replaceIframe(page.content, page.isFile);
|
|
87
|
+
} else if (loginResult?.alerts) {
|
|
88
|
+
showAlerts(loginResult?.alerts);
|
|
89
|
+
} else {
|
|
90
|
+
throw new Error("The login failed.");
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export async function publicLogin(entryPoint) {
|
|
95
|
+
try {
|
|
96
|
+
const loginResult = await loginRequest({ isPublic: true });
|
|
97
|
+
if (typeof loginResult === "string") {
|
|
98
|
+
const config = saltcorn.data.state.getState().mobileConfig;
|
|
99
|
+
config.user = {
|
|
100
|
+
role_id: 100,
|
|
101
|
+
email: "public",
|
|
102
|
+
language: "en",
|
|
103
|
+
};
|
|
104
|
+
config.isPublicUser = true;
|
|
105
|
+
await setJwt(loginResult);
|
|
106
|
+
config.jwt = loginResult;
|
|
107
|
+
i18next.changeLanguage(config.user.language);
|
|
108
|
+
addRoute({
|
|
109
|
+
route: entryPoint,
|
|
110
|
+
query: undefined,
|
|
111
|
+
});
|
|
112
|
+
const page = await router.resolve({
|
|
113
|
+
pathname: entryPoint,
|
|
114
|
+
fullWrap: true,
|
|
115
|
+
alerts: [
|
|
116
|
+
{
|
|
117
|
+
type: "success",
|
|
118
|
+
msg: i18next.t("Welcome to %s!", {
|
|
119
|
+
postProcess: "sprintf",
|
|
120
|
+
sprintf: [
|
|
121
|
+
saltcorn.data.state.getState().getConfig("site_name") ||
|
|
122
|
+
"Saltcorn",
|
|
123
|
+
],
|
|
124
|
+
}),
|
|
125
|
+
},
|
|
126
|
+
],
|
|
127
|
+
});
|
|
128
|
+
if (page.content) await replaceIframe(page.content, page.isFile);
|
|
129
|
+
} else if (loginResult?.alerts) {
|
|
130
|
+
showAlerts(loginResult?.alerts);
|
|
131
|
+
} else {
|
|
132
|
+
throw new Error("The login failed.");
|
|
133
|
+
}
|
|
134
|
+
} catch (error) {
|
|
135
|
+
console.error(error);
|
|
136
|
+
showAlerts([
|
|
137
|
+
{
|
|
138
|
+
type: "error",
|
|
139
|
+
msg: error.message ? error.message : "An error occured.",
|
|
140
|
+
},
|
|
141
|
+
]);
|
|
142
|
+
throw error;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export async function logout() {
|
|
147
|
+
try {
|
|
148
|
+
const config = saltcorn.data.state.getState().mobileConfig;
|
|
149
|
+
const page = await router.resolve({
|
|
150
|
+
pathname: "get/auth/logout",
|
|
151
|
+
entryView: config.entry_point,
|
|
152
|
+
versionTag: config.version_tag,
|
|
153
|
+
});
|
|
154
|
+
await replaceIframe(page.content);
|
|
155
|
+
} catch (error) {
|
|
156
|
+
showAlerts([
|
|
157
|
+
{
|
|
158
|
+
type: "error",
|
|
159
|
+
msg: error.message ? error.message : "An error occured.",
|
|
160
|
+
},
|
|
161
|
+
]);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export async function insertUser({ id, email, role_id, language }) {
|
|
166
|
+
await saltcorn.data.db.insert(
|
|
167
|
+
"users",
|
|
168
|
+
{ id, email, role_id, language },
|
|
169
|
+
{ ignoreExisting: true }
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export async function removeJwt() {
|
|
174
|
+
await saltcorn.data.db.deleteWhere("jwt_table");
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export async function setJwt(jwt) {
|
|
178
|
+
await removeJwt();
|
|
179
|
+
await saltcorn.data.db.insert("jwt_table", { jwt: jwt });
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export async function checkJWT(jwt) {
|
|
183
|
+
if (jwt && jwt !== "undefined") {
|
|
184
|
+
const response = await apiCall({
|
|
185
|
+
method: "GET",
|
|
186
|
+
path: "/auth/authenticated",
|
|
187
|
+
timeout: 10000,
|
|
188
|
+
});
|
|
189
|
+
return response.data.authenticated;
|
|
190
|
+
} else return false;
|
|
191
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/*global saltcorn, $*/
|
|
2
|
+
|
|
3
|
+
import { apiCall } from "./api";
|
|
4
|
+
import { Camera, CameraResultType } from "@capacitor/camera";
|
|
5
|
+
import { Geolocation } from "@capacitor/geolocation";
|
|
6
|
+
|
|
7
|
+
export function clearAlerts() {
|
|
8
|
+
const iframe = document.getElementById("content-iframe");
|
|
9
|
+
const alertsArea =
|
|
10
|
+
iframe.contentWindow.document.getElementById("toasts-area");
|
|
11
|
+
alertsArea.innerHTML = "";
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function showAlerts(alerts, toast = true) {
|
|
15
|
+
if (typeof saltcorn === "undefined") {
|
|
16
|
+
console.log("Not yet initalized.");
|
|
17
|
+
console.log(alerts);
|
|
18
|
+
} else {
|
|
19
|
+
const iframe = document.getElementById("content-iframe");
|
|
20
|
+
let area = iframe.contentWindow.document.getElementById(
|
|
21
|
+
toast ? "toasts-area" : "top-alert"
|
|
22
|
+
);
|
|
23
|
+
if (!area) {
|
|
24
|
+
const areaHtml = `<div class="container">
|
|
25
|
+
<div
|
|
26
|
+
id="toasts-area"
|
|
27
|
+
class="toast-container position-fixed bottom-0 start-50 p-0"
|
|
28
|
+
style="z-index: 9999;"
|
|
29
|
+
aria-live="polite"
|
|
30
|
+
aria-atomic="true">
|
|
31
|
+
</div>
|
|
32
|
+
</div>`;
|
|
33
|
+
iframe.contentWindow.document
|
|
34
|
+
.getElementById("page-inner-content")
|
|
35
|
+
.insertAdjacentHTML("beforeend", areaHtml);
|
|
36
|
+
area = iframe.contentWindow.document.getElementById(
|
|
37
|
+
toast ? "toasts-area" : "top-alert"
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
const successIds = [];
|
|
41
|
+
area.innerHTML = "";
|
|
42
|
+
for (const { type, msg } of alerts) {
|
|
43
|
+
if (toast) {
|
|
44
|
+
const rndid = `tab${Math.floor(Math.random() * 16777215).toString(16)}`;
|
|
45
|
+
area.innerHTML += saltcorn.markup.toast(type, msg, rndid);
|
|
46
|
+
if (type === "success") successIds.push(rndid);
|
|
47
|
+
} else area.innerHTML += saltcorn.markup.alert(type, msg);
|
|
48
|
+
}
|
|
49
|
+
if (successIds.length > 0) {
|
|
50
|
+
setTimeout(() => {
|
|
51
|
+
for (const id of successIds) {
|
|
52
|
+
const toastEl = iframe.contentWindow.document.getElementById(id);
|
|
53
|
+
if (toastEl) $(toastEl).removeClass("show");
|
|
54
|
+
}
|
|
55
|
+
}, 5000);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function showLoadSpinner() {
|
|
62
|
+
const iframe = document.getElementById("content-iframe");
|
|
63
|
+
if (iframe) iframe.contentWindow.showLoadSpinner();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function removeLoadSpinner() {
|
|
67
|
+
const iframe = document.getElementById("content-iframe");
|
|
68
|
+
if (iframe) iframe.contentWindow.removeLoadSpinner();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function clearTopAlerts() {
|
|
72
|
+
const iframe = document.getElementById("content-iframe");
|
|
73
|
+
const area = iframe.contentWindow.document.getElementById("alerts-area");
|
|
74
|
+
if (area) area.innerHTML = "";
|
|
75
|
+
const topAlert = iframe.contentWindow.document.getElementById("top-alert");
|
|
76
|
+
if (topAlert) topAlert.innerHTML = "";
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function errorAlert(error) {
|
|
80
|
+
showAlerts([
|
|
81
|
+
{
|
|
82
|
+
type: "error",
|
|
83
|
+
msg: error.message ? error.message : "An error occured.",
|
|
84
|
+
},
|
|
85
|
+
]);
|
|
86
|
+
console.error(error);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// TODO combine with loadEncodedFile
|
|
90
|
+
export async function loadFileAsText(fileId) {
|
|
91
|
+
try {
|
|
92
|
+
const response = await apiCall({
|
|
93
|
+
method: "GET",
|
|
94
|
+
path: `/files/download/${fileId}`,
|
|
95
|
+
responseType: "blob",
|
|
96
|
+
});
|
|
97
|
+
return new Promise((resolve, reject) => {
|
|
98
|
+
const reader = new FileReader();
|
|
99
|
+
reader.onloadend = () => {
|
|
100
|
+
return resolve(reader.result);
|
|
101
|
+
};
|
|
102
|
+
reader.onerror = (error) => {
|
|
103
|
+
return reject(error);
|
|
104
|
+
};
|
|
105
|
+
reader.readAsText(response.data);
|
|
106
|
+
});
|
|
107
|
+
} catch (error) {
|
|
108
|
+
if (
|
|
109
|
+
!showAlerts([
|
|
110
|
+
{
|
|
111
|
+
type: "error",
|
|
112
|
+
msg: error.message ? error.message : "An error occured.",
|
|
113
|
+
},
|
|
114
|
+
])
|
|
115
|
+
);
|
|
116
|
+
throw error;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export async function loadEncodedFile(fileId) {
|
|
121
|
+
try {
|
|
122
|
+
const response = await apiCall({
|
|
123
|
+
method: "GET",
|
|
124
|
+
path: `/files/download/${fileId}`,
|
|
125
|
+
responseType: "blob",
|
|
126
|
+
});
|
|
127
|
+
return new Promise((resolve, reject) => {
|
|
128
|
+
const reader = new FileReader();
|
|
129
|
+
reader.onloadend = () => {
|
|
130
|
+
return resolve(reader.result);
|
|
131
|
+
};
|
|
132
|
+
reader.onerror = (error) => {
|
|
133
|
+
return reject(error);
|
|
134
|
+
};
|
|
135
|
+
reader.readAsDataURL(response.data);
|
|
136
|
+
});
|
|
137
|
+
} catch (error) {
|
|
138
|
+
showAlerts([
|
|
139
|
+
{
|
|
140
|
+
type: "error",
|
|
141
|
+
msg: error.message ? error.message : "An error occured.",
|
|
142
|
+
},
|
|
143
|
+
]);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export async function takePhoto() {
|
|
148
|
+
try {
|
|
149
|
+
const image = await Camera.getPhoto({
|
|
150
|
+
quality: 90,
|
|
151
|
+
allowEditing: false,
|
|
152
|
+
resultType: CameraResultType.Uri,
|
|
153
|
+
});
|
|
154
|
+
return image.path;
|
|
155
|
+
} catch (error) {
|
|
156
|
+
showAlerts([
|
|
157
|
+
{
|
|
158
|
+
type: "error",
|
|
159
|
+
msg: error.message ? error.message : "An error occured.",
|
|
160
|
+
},
|
|
161
|
+
]);
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export async function getGeolocation(successCb, errorCb) {
|
|
167
|
+
try {
|
|
168
|
+
const coordinates = await Geolocation.getCurrentPosition();
|
|
169
|
+
if (successCb) successCb(coordinates);
|
|
170
|
+
return coordinates;
|
|
171
|
+
} catch (error) {
|
|
172
|
+
if (errorCb) errorCb(error);
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
/*global
|
|
1
|
+
/*global saltcorn */
|
|
2
2
|
|
|
3
3
|
const historyFile = "update_history";
|
|
4
4
|
const jwtTableName = "jwt_table";
|
|
5
5
|
|
|
6
|
+
import { readFile, fileExists, writeJSON } from "./file_system";
|
|
7
|
+
import { Directory } from "@capacitor/filesystem";
|
|
8
|
+
|
|
6
9
|
/**
|
|
7
10
|
* drop tables that are no longer in the 'tables.json' file
|
|
8
11
|
* the server db uses a serial (with postgres), so checking ids should suffice
|
|
9
12
|
*/
|
|
10
|
-
async function dropDeletedTables(incomingTables) {
|
|
13
|
+
export async function dropDeletedTables(incomingTables) {
|
|
11
14
|
const existingTables = await saltcorn.data.models.Table.find();
|
|
12
15
|
for (const table of existingTables) {
|
|
13
16
|
if (
|
|
@@ -27,7 +30,7 @@ async function dropDeletedTables(incomingTables) {
|
|
|
27
30
|
* @param {*} rows
|
|
28
31
|
* @returns
|
|
29
32
|
*/
|
|
30
|
-
async function safeRows(table, rows) {
|
|
33
|
+
export async function safeRows(table, rows) {
|
|
31
34
|
const existingFields = (
|
|
32
35
|
await saltcorn.data.db.query(
|
|
33
36
|
`PRAGMA table_info('${saltcorn.data.db.sqlsanitize(table)}')`
|
|
@@ -45,7 +48,7 @@ async function safeRows(table, rows) {
|
|
|
45
48
|
});
|
|
46
49
|
}
|
|
47
50
|
|
|
48
|
-
async function updateScTables(tablesJSON, skipScPlugins = true) {
|
|
51
|
+
export async function updateScTables(tablesJSON, skipScPlugins = true) {
|
|
49
52
|
await saltcorn.data.db.query("PRAGMA foreign_keys = OFF;");
|
|
50
53
|
for (const { table, rows } of tablesJSON.sc_tables) {
|
|
51
54
|
if (skipScPlugins && table === "_sc_plugins") continue;
|
|
@@ -56,7 +59,7 @@ async function updateScTables(tablesJSON, skipScPlugins = true) {
|
|
|
56
59
|
await saltcorn.data.db.query("PRAGMA foreign_keys = ON;");
|
|
57
60
|
}
|
|
58
61
|
|
|
59
|
-
async function updateScPlugins(tablesJSON) {
|
|
62
|
+
export async function updateScPlugins(tablesJSON) {
|
|
60
63
|
const { table, rows } = tablesJSON.sc_tables.find(
|
|
61
64
|
({ table }) => table === "_sc_plugins"
|
|
62
65
|
);
|
|
@@ -66,7 +69,7 @@ async function updateScPlugins(tablesJSON) {
|
|
|
66
69
|
}
|
|
67
70
|
}
|
|
68
71
|
|
|
69
|
-
async function updateUserDefinedTables() {
|
|
72
|
+
export async function updateUserDefinedTables() {
|
|
70
73
|
const existingTables = await saltcorn.data.db.listUserDefinedTables();
|
|
71
74
|
const tables = await saltcorn.data.models.Table.find();
|
|
72
75
|
for (const table of tables) {
|
|
@@ -92,7 +95,7 @@ async function updateUserDefinedTables() {
|
|
|
92
95
|
}
|
|
93
96
|
}
|
|
94
97
|
|
|
95
|
-
async function createSyncInfoTables(synchTbls) {
|
|
98
|
+
export async function createSyncInfoTables(synchTbls) {
|
|
96
99
|
const infoTbls = (await saltcorn.data.db.listTables()).filter(({ name }) => {
|
|
97
100
|
name.endsWith("_sync_info");
|
|
98
101
|
});
|
|
@@ -139,11 +142,8 @@ async function createSyncInfoTables(synchTbls) {
|
|
|
139
142
|
}
|
|
140
143
|
}
|
|
141
144
|
|
|
142
|
-
async function tablesUptodate(createdAt, historyFile) {
|
|
143
|
-
const { updated_at } = await
|
|
144
|
-
historyFile,
|
|
145
|
-
cordova.file.dataDirectory
|
|
146
|
-
);
|
|
145
|
+
export async function tablesUptodate(createdAt, historyFile) {
|
|
146
|
+
const { updated_at } = await readFile(historyFile, Directory.Data);
|
|
147
147
|
if (!updated_at) {
|
|
148
148
|
console.log("No updated_at in history file");
|
|
149
149
|
return false;
|
|
@@ -155,52 +155,30 @@ async function tablesUptodate(createdAt, historyFile) {
|
|
|
155
155
|
* Do a table update when the history file doesn't exist or is older than createdAt
|
|
156
156
|
* @param {number} createdAt UTC Date number when the tables.json file was created on the server
|
|
157
157
|
*/
|
|
158
|
-
async function dbUpdateNeeded(createdAt) {
|
|
158
|
+
export async function dbUpdateNeeded(createdAt) {
|
|
159
159
|
return (
|
|
160
|
-
!(await fileExists(
|
|
160
|
+
!(await fileExists(historyFile, Directory.Data)) ||
|
|
161
161
|
!(await tablesUptodate(createdAt, historyFile))
|
|
162
162
|
);
|
|
163
163
|
}
|
|
164
164
|
|
|
165
|
-
async function updateDb(tablesJSON) {
|
|
165
|
+
export async function updateDb(tablesJSON) {
|
|
166
166
|
await updateScTables(tablesJSON);
|
|
167
167
|
await saltcorn.data.state.getState().refresh_tables();
|
|
168
168
|
await updateUserDefinedTables();
|
|
169
|
-
await writeJSON(historyFile,
|
|
169
|
+
await writeJSON(historyFile, Directory.Data, {
|
|
170
170
|
updated_at: new Date().valueOf(),
|
|
171
171
|
});
|
|
172
172
|
}
|
|
173
173
|
|
|
174
|
-
async function getTableIds(tableNames) {
|
|
174
|
+
export async function getTableIds(tableNames) {
|
|
175
175
|
return (await saltcorn.data.models.Table.find())
|
|
176
176
|
.filter((table) => tableNames.indexOf(table.name) > -1)
|
|
177
177
|
.map((table) => table.id);
|
|
178
178
|
}
|
|
179
179
|
|
|
180
|
-
async function createJwtTable() {
|
|
180
|
+
export async function createJwtTable() {
|
|
181
181
|
await saltcorn.data.db.query(`CREATE TABLE IF NOT EXISTS ${jwtTableName} (
|
|
182
182
|
jwt VARCHAR(500)
|
|
183
183
|
)`);
|
|
184
184
|
}
|
|
185
|
-
|
|
186
|
-
async function getJwt() {
|
|
187
|
-
const rows = await saltcorn.data.db.select(jwtTableName);
|
|
188
|
-
return rows?.length > 0 ? rows[0].jwt : null;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
async function removeJwt() {
|
|
192
|
-
await saltcorn.data.db.deleteWhere(jwtTableName);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
async function setJwt(jwt) {
|
|
196
|
-
await removeJwt();
|
|
197
|
-
await saltcorn.data.db.insert(jwtTableName, { jwt: jwt });
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
async function insertUser({ id, email, role_id, language }) {
|
|
201
|
-
await saltcorn.data.db.insert(
|
|
202
|
-
"users",
|
|
203
|
-
{ id, email, role_id, language },
|
|
204
|
-
{ ignoreExisting: true }
|
|
205
|
-
);
|
|
206
|
-
}
|