You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
4.6 KiB
JavaScript

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
var $$ = Dom7;
// Detect platform and run platform-specific setup code
// for Cordova, NW.js, or the browser
initPlatform();
Template7.global = {
qrenabled: (platform_type == "cordova")
};
var app = new Framework7({
root: "#app",
name: "HelpingHelena",
id: "com.netsyms.HelpingHelena",
theme: platform_theme,
debug: true,
init: true,
initOnDeviceReady: false,
routes: routes
});
var mainView = app.views.create('.view-main', {
url: "/"
});
var router = mainView.router;
function restartApplication() {
window.location = "index.html";
}
router.on("pageInit", function (pagedata) {
pagedata.$el.find('script').each(function (el) {
if ($$(this).attr('src')) {
var s = document.createElement('script');
s.src = $$(this).attr('src');
$$('head').append(s);
} else {
eval($$(this).text());
}
});
switch (pagedata.name) {
case "settings":
updateSettingsData();
break;
}
});
/**
* Check if there is a valid API key stored locally.
* @param {function} callback A function that will be called with a boolean argument indicating if the key is valid.
* @return {undefined}
*/
function checkKey(callback) {
if (localStorage.getItem("key") === null) {
callback(false);
return;
}
callAPI("checkkey", {
key: localStorage.getItem("key")
}, function (data) {
var currentTimestamp = Math.floor(Date.now() / 1000);
// Check if the key is valid and has at least one day before expiry
// If someone leaves the app open longer than 24 hours and the key is
// not renewed, there might be bugs.
if (data.valid == true && data.expires > (currentTimestamp + (60 * 60 * 24))) {
callback(true);
return;
}
callback(false);
}, function (msg) {
callback(false);
});
}
/**
* Attempt to get a new API key using stored credentials.
* @param {function} callback A function that will be called with a boolean argument indicating if the attempt was successful.
* @return {undefined}
*/
function refreshKey(callback) {
if (localStorage.getItem("username") === null || localStorage.getItem("password") === null) {
callback(false);
return;
}
callAPI("getkey", {
username: localStorage.getItem("username"),
password: localStorage.getItem("password"),
}, function (data) {
localStorage.setItem("key", data.key);
callback(true);
}, function (msg) {
callback(false);
});
}
function setupKeyRefresh() {
// Check and refresh API key as needed
// Should mitigate key expiration if the app is left open for
// a long time when the key is almost expired
setInterval(function () {
checkKey(function (valid) {
if (!valid) {
refreshKey(function (ok) {
if (!ok) {
router.navigate("/setup/0");
}
});
}
});
}, 1000 * 60 * 5);
}
if (localStorage.getItem("configured") == null) {
// Open the setup page
router.navigate("/setup/0");
} else {
checkKey(function (valid) {
if (valid) {
try {
var url = new URL(window.location.href);
if (typeof url.searchParams.get("sendto") == "string") {
router.navigate("/sendmoney/" + url.searchParams.get("sendto"));
} else {
router.navigate("/home");
}
} catch (ex) {
router.navigate("/home");
}
setupKeyRefresh();
} else {
refreshKey(function (ok) {
if (ok) {
try {
var url = new URL(window.location.href);
if (typeof url.searchParams.get("sendto") == "string") {
router.navigate("/sendmoney/" + url.searchParams.get("sendto"));
} else {
router.navigate("/home");
}
} catch (ex) {
router.navigate("/home");
}
setupKeyRefresh();
} else {
//router.navigate("/setup/0");
}
});
}
});
}