MobileApp/www/js/app-inject.js

127 lines
4.4 KiB
JavaScript
Raw Normal View History

2017-12-16 13:36:56 -07:00
/* 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/. */
2017-06-29 04:01:49 -06:00
/*
* sidemenu.js
*/
// This code from
// https://stackoverflow.com/a/6177502
$.cssHooks.backgroundColor = {
2017-12-21 00:34:05 -07:00
get: function (elem) {
if (elem.currentStyle)
var bg = elem.currentStyle["backgroundColor"];
else if (window.getComputedStyle)
var bg = document.defaultView.getComputedStyle(elem,
2017-12-21 00:34:05 -07:00
null).getPropertyValue("background-color");
if (bg.search("rgb") == -1)
return bg;
else {
bg = bg.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
function hex(x) {
return ("0" + parseInt(x).toString(16)).slice(-2);
}
return "#" + hex(bg[1]) + hex(bg[2]) + hex(bg[3]);
}
}
}
2017-06-29 04:01:49 -06:00
function openmenu() {
if ($('#swipe-nav').css('display') == 'none') {
$('#swipe-shader').show("fade", {}, 300);
$('#swipe-nav').show("slide", {direction: "left"}, 300);
}
}
function closemenu() {
if ($('#swipe-nav').css('display') != 'none') {
$('#swipe-shader').hide("fade", {}, 300);
$('#swipe-nav').hide("slide", {direction: "left"}, 300);
}
}
function togglemenu() {
if ($('#swipe-nav').css('display') != 'none') {
closemenu();
} else {
openmenu();
}
}
2017-06-29 04:01:49 -06:00
$(document).ready(function () {
2018-01-27 22:39:25 -07:00
if (typeof navbar_breakpoint != "undefined") {
var nav_breakpoint = navbar_breakpoint;
} else {
var nav_breakpoint = "sm";
}
2017-06-29 04:01:49 -06:00
var pages = $('#navbar-collapse .navbar-nav').html();
2017-12-23 01:18:11 -07:00
var body = $('body');
var username = "__USERNAME__";
var menucolor = $('.navbar').css('backgroundColor');
2018-01-27 22:39:25 -07:00
var textcolor = $('.navbar .navbar-brand').css('color');
var logo = "__LOGO__";
2018-01-27 22:39:25 -07:00
parent.postMessage("setcolor " + menucolor, "*");
parent.postMessage("load_css " + nav_breakpoint, "*");
2018-01-27 22:39:25 -07:00
pages = pages.replace(/\s?py-[a-z]{2}-0/g, ""); // Issue #12
$('#navbar-right').html("<span class='nav-item py-" + nav_breakpoint + "-0'><a class='nav-link py-" + nav_breakpoint + "-0' onclick='quitapp()'><i class='fas fa-sign-out-alt fa-fw'></i> Back to Menu</a></span>");
body.append("<div id='swipe-nav'><div id='swipe-header' style='background-color: " + menucolor + "; color: " + textcolor + "'><a href='./app.php'><img id='swipe-appicon' src='" + logo + "' /></a> <div id='swipe-username'><i class='fa fa-user fa-fw'></i> " + username + "</div></div>\n<div id='swipe-pages' class='swipe-list'>" + pages + "</div><div class='swipe-list'><span class='nav-item'><a class='nav-link' onclick='quitapp()'><i class='fas fa-sign-out-alt fa-fw'></i> Back to Menu</a></span></div></div>");
2017-12-23 01:18:11 -07:00
body.append("<div id='swipe-shader'></div>");
2017-12-21 00:34:05 -07:00
$(".navbar-brand").attr("href", "#");
2017-12-21 00:34:05 -07:00
2017-06-29 04:01:49 -06:00
$('button.navbar-toggle[data-toggle="collapse"]').click(togglemenu);
2018-01-27 22:39:25 -07:00
$('button.navbar-toggler[data-toggle="collapse"]').click(togglemenu);
2017-06-29 04:01:49 -06:00
$('#swipe-shader').click(togglemenu);
Hammer(document.body).on("swiperight", function (e) {
var endPoint = e.pointers[0].pageX;
var distance = e.distance;
var origin = endPoint - distance;
if (origin <= 25) {
openmenu();
}
});
2017-12-21 00:34:05 -07:00
2017-06-29 04:01:49 -06:00
Hammer(document.body).on("swipeleft", function (e) {
closemenu();
});
2017-12-21 00:34:05 -07:00
window.addEventListener('message', function (event) {
if (event.data.startsWith("coderesult~|~")) {
var data = event.data.split("~|~");
var ref = data[1];
var code = data[2];
$(ref).val(code);
$(ref).trigger("input");
$(ref).trigger("change");
console.log("app: received " + event.data);
} else if (event.data.startsWith("goback")) {
2017-12-21 00:34:05 -07:00
console.log("app: received " + event.data);
window.history.back();
parent.postMessage('goneback', '*');
}
});
2017-12-21 00:34:05 -07:00
2017-11-19 01:52:35 -07:00
setInterval(function () {
$.getJSON("mobile/index.php", {action: "ping"}, function (d) {
console.log("app: keepalive ping " + d.status);
});
}, 1000 * 60);
parent.postMessage('done_loading', '*');
2017-06-30 00:56:47 -06:00
});
function quitapp() {
2017-12-21 00:34:05 -07:00
parent.postMessage('quit', '*');
}
function scancode(refstring) {
console.log("app: sent scancode " + refstring);
parent.postMessage('scancode ' + refstring, "*");
2017-06-30 00:56:47 -06:00
}