/*
* Copyright (c) 2019 Clementine Computing LLC.
*
* This file is part of PopuFare.
*
* PopuFare is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PopuFare is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with PopuFare. If not, see .
*
*/
var _ADDRESS = "localhost";
var _URL = "http://" + _ADDRESS;
var _PORT = 60535;
var _fqADDRESS = _ADDRESS + ":" + _PORT;
var BG_COLOR = "#f7f7f7";
var TEXT_COLOR = "#444444";
var DIU_UI_VERSION = "0.1.0";
var DIU_UI_VERSION_DATE = "2019-09-04 15:24:11";
var g_ctx = {
"driver_login_state" : "driver",
"driver" : "",
"pin" : "",
"paddle" : "",
"current_ui" : "ui_login",
"ui_change_timeoutid": -1,
"bulkfare" : "",
"bulkfare_count" : "",
"status_text": "",
"status_text_w": 0,
"status_text_h": 0,
"diu_status" : {},
"x": ""
};
function _api_fail() {
console.log(">>api fail");
return true;
}
function _api_tick() { }
function _api_request(action, param, cb_ok, cb_fail, cb_neterr) {
var idx = 0;
var param_name = [], param_val = [];
var req = new XMLHttpRequest();
//var url = _URL + ":" + _PORT + "/req";
var url = "/req";
var varstr = "action=" + action;
for (var x in param) {
param_name.push(x);
param_val.push(param[x]);
}
for (var ii=0; ii=2) && (resp[0] == "ok")) {
if (typeof cb_ok !== "undefined") {
cb_ok(fulltxt);
}
return;
}
if (typeof cb_fail !== "undefined") {
cb_fail(fulltxt);
}
}
else if (typeof cb_fail !== "undefined") { cb_fail(); }
}
};
req.addEventListener("error", _api_fail);
req.addEventListener("abort", _api_fail);
//req.addEventListener("load", _api_tick);
//req.addEventListener("progress", _api_tick);
req.onerror = function() { console.log("network error"); };
try {
//DEBUG
console.log("req, varstr:", varstr);
req.open("POST", url, true);
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
req.send(varstr);
} catch (er) {
console.log(">>>>>>ERROR", er);
}
}
function _block_status_window(ui_id) {
var ele = document.getElementById(ui_id + "_status");
ele.ondragstart = function() { return false; }
ele.onselectstart = function() { return false; }
ele.onmousedown = function() { return false; }
ele.ondragstart = function() { return false; }
var rect = ele.getBoundingClientRect();
var block_ele = document.getElementById(ui_id + "_blockwindow");
block_ele.style.width = rect.width;
block_ele.style.height = rect.height;
block_ele.style.bottom = rect.bottom;
block_ele.style.left = rect.left;
block_ele.style.top = rect.top;
block_ele.style.right = rect.right;
block_ele.style.x = rect.x;
block_ele.style.y = rect.y;
}
function _switch_ui(to) {
var ele;
console.log(">>>to", to);
if (g_ctx.ui_change_timeoutid > 0) {
window.clearTimeout(g_ctx.ui_change_timeoutid);
g_ctx.ui_change_timeoutid = -1;
}
var from = g_ctx.current_ui;
ele = document.getElementById(from);
ele.style.display = "none";
ele = document.getElementById(to);
ele.style.display = "block";
g_ctx.current_ui = to;
if ((to == "ui_main") ||
(to == "ui_login")) {
_block_status_window(to);
}
}
function _hide_ui(from) {
var ele = document.getElementById(from);
ele.style.display = "none";
}
function _hide_ui_element(from) {
var ele = document.getElementById(from);
ele.style.display = "none";
}
function _show_ui(to) {
var ele = document.getElementById(to);
ele.style.display = "block";
g_ctx.current_ui = to;
// put pane over status window to prevent highlights,
// copy/pasting, etc.
//
if ((to == "ui_main") ||
(to == "ui_login")) {
_block_status_window(to);
}
}
function _show_ui_element(to) {
var ele = document.getElementById(to);
ele.style.display = "block";
}
//------------
function _clear_ui_driver() {
var ele;
var u = ["driver", "pin"];
for (var i=0; i g_ctx.status_text_h) {
var n = lines.length;
lines = lines.slice(n - g_ctx.status_text_h);
}
for (var i=0; i>replace:", msg);
}
function _main_message_add(msg, bg_color, text_color) {
var ele = document.getElementById("ui_main_status");
g_ctx.status_text_w = ele.cols;
g_ctx.status_text_h = ele.rows;
bg_color = ((typeof bg_color === "undeinfed") ? BG_COLOR : bg_color);
text_color = ((typeof text_color === "undefined") ? TEXT_COLOR : text_color);
var txt = g_ctx.status_text + "\n" + msg;
var lines = txt.split("\n");
if (lines.length > g_ctx.status_text_h) {
var n = lines.length;
lines = lines.slice(n - g_ctx.status_text_h);
}
for (var i=0; i>add:", msg);
}
//-------------
//-------------
//-------------
function _ws_process(dat) {
var tok = dat.split(" ");
if (tok.length < 2) { return; }
if (tok[0] == "paddle") {
if (tok[1] == "ok") { _verify_paddle_ok(); }
else if (tok[1] == "fail") { _verify_paddle_fail(); }
else { _verify_paddle_fail(); }
}
else if (tok[0] == "driver_notify") {
if (tok.length < 5) {
console.log("malformed message: ", dat);
}
else if (tok[1] == "replace") {
_main_message_replace(tok.slice(4).join(" "));
}
else if (tok[1] == "ok") {
_main_message_add(tok.slice(4).join(" "));
}
else {
console.log("unknown message: ", dat);
}
}
}
function _init_websocket() {
var sock = new WebSocket("ws://" + _fqADDRESS);
sock.onopen = function(e) { console.log("open", e); }
sock.onmessage = function(e) { _ws_process(e.data); }
sock.onclose = function(e) { console.log("close", e); }
sock.onerror = function(e) { console.log("error", e); }
}
//-------------
function init() {
_init_websocket();
// login
//
document.getElementById('ui_login_login').onclick = _login;
document.getElementById('ui_driverincorrect_btn').onclick = _driver_incorrect;
document.getElementById('ui_paddleunknown_btn').onclick = _paddle_unknown;;
// main menu buttons
//
document.getElementById('ui_main_prv').onclick = _main_prv;
document.getElementById('ui_main_nxt').onclick = _main_nxt;
document.getElementById('ui_main_bulk').onclick = _main_bulk;
document.getElementById('ui_main_menu').onclick = _main_menu;
var simple_fares = [ "adult", "youth", "half", "bike", "wheelchair", "transfer"];
var simple_fares_f = [ _fare_adult, _fare_youth, _fare_half, _fare_bike, _fare_wheelchair, _fare_transfer];
for (var ii=0; ii>>", x);
// try and reduce textarea highlighting, drag and drop/copy pasting,
// and other bad behavor.
//
var ele;
var block_ele;
var rect;
ele = document.getElementById("ui_login_status");
ele.ondragstart = function() { return false; }
ele.onselectstart = function() { return false; }
ele.onmousedown = function() { return false; }
ele.ondragstart = function() { return false; }
rect = ele.getBoundingClientRect();
block_ele = document.getElementById("ui_login_blockwindow");
block_ele.style.width = rect.width;
block_ele.style.height = rect.height;
block_ele.style.bottom = rect.bottom;
block_ele.style.left = rect.left;
block_ele.style.top = rect.top;
block_ele.style.right = rect.right;
block_ele.style.x = rect.x;
block_ele.style.y = rect.y;
ele = document.getElementById("ui_main_status");
ele.ondragstart = function() { return false; }
ele.onselectstart = function() { return false; }
ele.onmousedown = function() { return false; }
ele.ondragstart = function() { return false; }
rect = ele.getBoundingClientRect();
block_ele = document.getElementById("ui_main_blockwindow");
block_ele.style.width = rect.width;
block_ele.style.height = rect.height;
block_ele.style.bottom = rect.bottom;
block_ele.style.left = rect.left;
block_ele.style.top = rect.top;
block_ele.style.right = rect.right;
block_ele.style.x = rect.x;
block_ele.style.y = rect.y;
}
init();