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.
HelenaExpressApp/www/assets/js/main.js

273 lines
7.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();
var app = new Framework7({
root: "#app",
name: "Helena Express",
id: "com.netsyms.helenaexpress.app",
theme: "auto",
card: {
swipeToClose: false,
hideNavbarOnOpen: false,
},
popup: {
backdrop: true
},
popover: {
backdrop: true
},
sheet: {
backdrop: false
},
touch: {
tapHold: true
},
statusbar: {
enabled: false
},
init: false,
initOnDeviceReady: false,
routes: routes
});
var mainView = app.views.create('#view-main', {
url: "/",
animate: true,
loadInitialPage: false
});
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());
}
});
// Stop text selection from popping a system toolbar even after changing pages
if (window.getSelection) {
window.getSelection().removeAllRanges();
} else if (document.selection) {
document.selection.empty();
}
});
/**
* Perform back button behavior.
* Call this function whenever the equivalent to the Android back button is pressed.
* @returns {undefined}
*/
function handleBackButton() {
// Close sheet if it's open
if ($(".sheet-modal").hasClass("modal-in")) {
app.sheet.close();
} else if ($(".popup").hasClass("modal-in")) {
app.popup.close();
} else if ($(".searchbar-enabled")[0]) {
app.searchbar.disable();
} else if (scanningBarcode) {
return;
} else {
router.back({force: true, ignoreCache: true});
}
// Stop text selection from popping a system toolbar even after changing pages
if (window.getSelection) {
window.getSelection().removeAllRanges();
} else if (document.selection) {
document.selection.empty();
}
}
$(document).on("mousedown", ".hapticbtn,.input-clear-button,.button,.link", function () {
doHapticFeedback();
});
$(document).keyup(function (e) {
if (e.key === "Escape" || e.keyCode == 27) {
handleBackButton();
}
});
router.on("routeChange", function (newRoute) {
console.log("Info", "Navigating to ", newRoute.path);
trackPageView(newRoute.path, $("#view-main .page-current .navbar .navbar-inner .title").first().text());
});
function setAppTheme(theme) {
if (theme == "light") {
$("body").removeClass("dark");
if (platform_type == "cordova" && typeof StatusBar !== 'undefined') {
StatusBar.backgroundColorByHexString("#e8f9fe");
StatusBar.styleDefault();
}
} else if (theme == "dark") {
$("body").addClass("dark");
if (platform_type == "cordova" && typeof StatusBar !== 'undefined') {
StatusBar.styleLightContent();
if (cordova.platformId == "ios") {
StatusBar.backgroundColorByHexString("#121212");
} else {
StatusBar.backgroundColorByName("black");
}
}
}
}
function applyColorTheme() {
if (getStorage("apptheme") == "dark") {
setAppTheme("dark");
} else if (getStorage("apptheme") == "light") {
setAppTheme("light");
} else {
setAppTheme(appTheme);
}
}
/**
* Turn animations on or off.
* @param {boolean} on true for on, false for off.
* @returns {undefined}
*/
function toggleAnimations(on) {
if (on) {
$("#app").removeClass("no-animation");
} else {
$("#app").addClass("no-animation");
}
mainView.params.animate = on;
}
/**
* Turn animations on or off while considering user preferences.
* @param boolean enabled true to enable, false to disable, undefined to use animation=on/off setting.
* @returns {undefined}
*/
function setAnimations(enabled) {
if (getStorage("animation") == null) {
setStorage("animation", "auto");
}
if (typeof enabled !== "undefined") {
toggleAnimations(enabled == true);
return;
}
if (getStorage("animation") == "off") {
toggleAnimations(false);
} else if (getStorage("animation") == "on") {
toggleAnimations(true);
}
}
applyColorTheme();
setAnimations();
// Migrate from old to new account number storage key
if (inStorage("phonenumber") && !inStorage("accountnumber")) {
setStorage("accountnumber", getStorage("phonenumber"));
removeFromStorage("phonenumber");
}
var setup = (inStorage("accountnumber") && inStorage("accountkey")) || inStorage("setupskipped");
app.init();
if (getStorage("analytics") !== "false") {
try {
var _paq = window._paq = window._paq || [];
_paq.push(['enableLinkTracking']);
(function () {
var u = "https://analytics.netsyms.net/";
_paq.push(['setTrackerUrl', u + 'matomo.php']);
_paq.push(['setSiteId', '40']);
var d = document, g = d.createElement('script'), s = d.getElementsByTagName('script')[0];
g.async = true;
g.src = u + 'matomo.js';
s.parentNode.insertBefore(g, s);
})();
} catch (ex) {
// skip
}
} else {
console.log("Analytics code not loaded at user request.");
}
document.title = SETTINGS.branding.apptitle;
/**
* Open the page set in the startpage URL parameter.
* @returns {Boolean} true if navigated to a set page, false if app should open something else.
*/
function openStartpageURLArg() {
try {
var urlParams = new URLSearchParams(window.location.search);
if (urlParams.has("startpage")) {
var startpage = urlParams.get("startpage");
if (startpage == "") {
return false;
}
router.navigate("/" + startpage);
return true;
}
} catch (ex) {
}
return false;
}
/**
* Get the cached dynamic page content if available, else return null (and try to fetch it for next time)
* @param {string} page the page ID
* @returns {string|null}
*/
function getDynamicPageContent(page) {
if (inStorage("dynamiccontent_" + page)) {
return getStorage("dynamiccontent_" + page);
}
apirequest(SETTINGS.apis.dynamicappcontent, {page: page}, function (resp) {
if (resp.status == "OK") {
setStorage("dynamiccontent_" + resp.page, resp.content);
}
});
return null;
}
// update dynamic page text cache
setTimeout(function () {
dynamicPages.forEach(function (pageid) {
// Only update if it's been over eight hours since last update
if (!inStorage("dynamiccontent-lastupdated_" + pageid) || getStorage("dynamiccontent-lastupdated_" + pageid) < time() - 60 * 60 * 8) {
apirequest(SETTINGS.apis.dynamicappcontent, {page: pageid}, function (resp) {
if (resp.status == "OK") {
setStorage("dynamiccontent_" + resp.page, resp.content);
setStorage("dynamiccontent-lastupdated_" + resp.page, time());
}
});
}
});
}, 500);
if (setup) {
router.navigate("/home");
} else {
router.navigate("/welcome", {
history: false,
pushState: false
});
}
openStartpageURLArg();