diu_ui-ORG.js 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  1. /*
  2. * Copyright (c) 2019 Clementine Computing LLC.
  3. *
  4. * This file is part of PopuFare.
  5. *
  6. * PopuFare is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * PopuFare is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with PopuFare. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. */
  20. var _ADDRESS = "localhost";
  21. var _URL = "http://" + _ADDRESS;
  22. var _PORT = 60535;
  23. var _fqADDRESS = _ADDRESS + ":" + _PORT;
  24. var BG_COLOR = "#f7f7f7";
  25. var TEXT_COLOR = "#444444";
  26. var DIU_UI_VERSION = "0.1.9";
  27. var DIU_UI_VERSION_DATE = "2020-01-30";
  28. var g_snd = {
  29. "reject_fare" : "",
  30. "accept_fare" : "",
  31. "rule_error" : "",
  32. "button_press" : ""
  33. };
  34. function _beep_error() {
  35. g_snd.rule_error.currentTime=0;
  36. g_snd.rule_error.play();
  37. }
  38. function _beep_reject() {
  39. g_snd.reject_fare.currentTime=0;
  40. g_snd.reject_fare.play();
  41. }
  42. function _beep_accept() {
  43. g_snd.accept_fare.currentTime=0;
  44. g_snd.accept_fare.play();
  45. }
  46. function _beep() {
  47. g_snd.button_press.currentTime=0;
  48. g_snd.button_press.play();
  49. }
  50. var g_ctx = {
  51. "driver_login_state" : "driver",
  52. "driver" : "",
  53. "pin" : "",
  54. "paddle" : "",
  55. "eqnum" : "",
  56. "cur_eqnum" : "",
  57. "current_ui" : "ui_login",
  58. "ui_change_timeoutid": -1,
  59. "ui_watchdog_timeoutid": -1,
  60. "bulkfare" : "",
  61. "bulkfare_count" : "",
  62. "status_text": "",
  63. "status_text_w": 32,
  64. "status_text_h": 11,
  65. "status_html_text":"",
  66. "diu_status" : {},
  67. "x": ""
  68. };
  69. function _api_fail() {
  70. console.log(">>api fail");
  71. return true;
  72. }
  73. function _api_tick() { }
  74. function _api_request(action, param, cb_ok, cb_fail, cb_neterr) {
  75. var idx = 0;
  76. var param_name = [], param_val = [];
  77. var req = new XMLHttpRequest();
  78. // Firefox gets testy about parsing assumed XML
  79. //
  80. req.overrideMimeType("text/plain");
  81. var url = "/req";
  82. var varstr = "action=" + action;
  83. for (var x in param) {
  84. param_name.push(x);
  85. param_val.push(param[x]);
  86. }
  87. for (var ii=0; ii<param_name.length; ii++) {
  88. varstr += "&" + param_name[ii] + "=" + param_val[ii];
  89. }
  90. req.onreadystatechange = function() {
  91. if (req.readyState == 4) {
  92. if (req.status == 200) {
  93. var fulltxt = req.responseText;
  94. var resp = req.responseText.split(" ");
  95. if ((resp.length>=2) && (resp[0] == "ok")) {
  96. if (typeof cb_ok !== "undefined") {
  97. cb_ok(fulltxt);
  98. }
  99. return;
  100. }
  101. if (typeof cb_fail !== "undefined") {
  102. cb_fail(fulltxt);
  103. }
  104. }
  105. else if (typeof cb_fail !== "undefined") { cb_fail(); }
  106. }
  107. };
  108. req.addEventListener("error", _api_fail);
  109. req.addEventListener("abort", _api_fail);
  110. //req.addEventListener("load", _api_tick);
  111. //req.addEventListener("progress", _api_tick);
  112. req.onerror = function() { console.log("network error"); };
  113. try {
  114. //DEBUG
  115. console.log("req, varstr:", varstr);
  116. req.open("POST", url, true);
  117. req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  118. req.send(varstr);
  119. } catch (er) {
  120. console.log(">>>>>>ERROR", er);
  121. }
  122. }
  123. function _block_status_window(ui_id) {
  124. var ele = document.getElementById(ui_id + "_status");
  125. ele.ondragstart = function() { return false; }
  126. ele.onselectstart = function() { return false; }
  127. ele.onmousedown = function() { return false; }
  128. ele.ondragstart = function() { return false; }
  129. var rect = ele.getBoundingClientRect();
  130. var block_ele = document.getElementById(ui_id + "_blockwindow");
  131. block_ele.style.width = rect.width;
  132. block_ele.style.height = rect.height;
  133. block_ele.style.bottom = rect.bottom;
  134. block_ele.style.left = rect.left;
  135. block_ele.style.top = rect.top;
  136. block_ele.style.right = rect.right;
  137. block_ele.style.x = rect.x;
  138. block_ele.style.y = rect.y;
  139. }
  140. function _clear_ui_watchdog() {
  141. if (g_ctx.ui_watchdog_timeoutid >= 0) {
  142. window.clearTimeout(g_ctx.ui_watchdog_timeoutid);
  143. }
  144. }
  145. function _set_ui_watchdog(failsafe_ui, _dt) {
  146. // default to 'ui_watchdog' and 5s
  147. //
  148. failsafe_ui = (( typeof failsafe_ui === "undefined") ? "ui_watchdog" : failsafe_ui);
  149. _dt = (( typeof _dt === "undefined") ? 5000 : _dt);
  150. _clear_ui_watchdog();
  151. g_ctx.ui_watchdog_timeoutid = window.setTimeout(
  152. (function(_fsui) {
  153. return function() { console.log("???", _fsui); _switch_ui(_fsui); };
  154. })(failsafe_ui),
  155. _dt);
  156. }
  157. function _switch_ui(to) {
  158. var ele;
  159. if (g_ctx.ui_change_timeoutid > 0) {
  160. window.clearTimeout(g_ctx.ui_change_timeoutid);
  161. g_ctx.ui_change_timeoutid = -1;
  162. }
  163. var from = g_ctx.current_ui;
  164. ele = document.getElementById(from);
  165. ele.style.display = "none";
  166. ele = document.getElementById(to);
  167. ele.style.display = "block";
  168. g_ctx.current_ui = to;
  169. if (to == "ui_main") {
  170. var _x = _calculate_ui_main_status_width_height();
  171. g_ctx.status_text_h = _x.n_height;
  172. g_ctx.status_text_w = _x.n_width;
  173. }
  174. if ((to == "ui_main") ||
  175. (to == "ui_login")) {
  176. _block_status_window(to);
  177. }
  178. }
  179. function _hide_ui(from) {
  180. var ele = document.getElementById(from);
  181. ele.style.display = "none";
  182. }
  183. function _hide_ui_element(from) {
  184. var ele = document.getElementById(from);
  185. ele.style.display = "none";
  186. }
  187. function _show_ui(to) {
  188. var ele = document.getElementById(to);
  189. ele.style.display = "block";
  190. g_ctx.current_ui = to;
  191. // put pane over status window to prevent highlights,
  192. // copy/pasting, etc.
  193. //
  194. if ((to == "ui_main") ||
  195. (to == "ui_login")) {
  196. _block_status_window(to);
  197. }
  198. }
  199. function _show_ui_element(to) {
  200. var ele = document.getElementById(to);
  201. ele.style.display = "block";
  202. }
  203. //------------
  204. function _update_display_time() {
  205. var ele = document.getElementById("ui_main_time");
  206. if (typeof ele === "undefined") { return; }
  207. var _dt = new Date();
  208. var _sep = ":";
  209. if ( (_dt.getSeconds() % 2) == 0) {
  210. _sep = " ";
  211. }
  212. var _hr = _dt.getHours();
  213. var _min = _dt.getMinutes();
  214. var _hr_str = _hr.toString();
  215. var _min_str = _min.toString();
  216. if (_hr < 10) { _hr_str = "0" + _hr_str; }
  217. if (_min < 10) { _min_str = "0" + _min_str; }
  218. var _time_str = _hr_str + _sep + _min_str;
  219. //ele.innerText = _time_str;
  220. ele.innerHTML = "<span style='white-space:nowrap;'>" + _time_str + "</span>";
  221. }
  222. //------------
  223. // volume is from [0,1]
  224. //
  225. function _process_volume(vol) {
  226. var ele = document.getElementById("ui_configuration_displayvolume");
  227. ivol = Math.floor(vol*100.0);
  228. ele.innerHTML = "" + ivol + "%";
  229. }
  230. function _slider_change(name) {
  231. if (name == "configuration.volume") {
  232. var val = document.getElementById("ui_configuration_volume").value;
  233. _process_volume(val);
  234. ivol = Math.floor(val*100.0);
  235. _api_request("volume", {"volume":ivol},
  236. function(inp) { console.log("volume api set ok:", inp); },
  237. function(inp) { console.log("volume api set fail:", inp); });
  238. }
  239. }
  240. function _volume_test() {
  241. _api_request("say", {"message":"testing 1, 2, 3"},
  242. function(inp) { console.log("volume test ok:", inp); },
  243. function(inp) { console.log("volume test fail:", inp); });
  244. }
  245. //------------
  246. function _clear_ui_driver() {
  247. var ele;
  248. var u = ["driver", "pin"];
  249. for (var i=0; i<u.length; i++) {
  250. g_ctx[u[i]] = "";
  251. ele = document.getElementById("ui_driver_" + u[i]);
  252. ele.innerHTML = "";
  253. ele.style.background = "";
  254. }
  255. ele = document.getElementById("ui_driver_pin");
  256. ele.style.display = "none";
  257. ele = document.getElementById("ui_driver_pin_text");
  258. ele.style.display = "none";
  259. }
  260. function _clear_ui_paddle() {
  261. g_ctx.paddle = "";
  262. var ele = document.getElementById("ui_paddle_paddle");
  263. ele.innerHTML = "";
  264. }
  265. function _clear_ui_bulkaccept() {
  266. var ele;
  267. g_ctx.bulkfare = "";
  268. ele = document.getElementById("ui_bulkaccept_faretype");
  269. ele.innerHTML = "";
  270. g_ctx.bulkfare_count = "";
  271. ele = document.getElementById("ui_bulkaccept_count");
  272. ele.innerHTML = "";
  273. }
  274. function _login() {
  275. _switch_ui('ui_driver');
  276. g_ctx.driver_login_state = "driver";
  277. _clear_ui_driver();
  278. _clear_ui_paddle();
  279. }
  280. //------------
  281. function _verify_driver() {
  282. var driver = g_ctx.driver;
  283. var pin = g_ctx.pin;
  284. // have a fail safe for the verification
  285. //
  286. _set_ui_watchdog();
  287. _api_request("driverlogin", {"driver":driver, "pin":pin}, _verify_driver_ok, _verify_driver_fail);
  288. return;
  289. }
  290. function _verify_driver_ok() {
  291. console.log("driver ok");
  292. _clear_ui_watchdog();
  293. _switch_ui("ui_paddle");
  294. }
  295. function _verify_driver_fail() {
  296. console.log("driver fail");
  297. _clear_ui_watchdog();
  298. _switch_ui("ui_driverincorrect");
  299. }
  300. function _driver_incorrect(inp) {
  301. console.log("driver incorrect");
  302. _clear_ui_watchdog();
  303. _clear_ui_driver();
  304. _switch_ui("ui_driver");
  305. }
  306. //------------
  307. function _driver_login(inp) {
  308. var uvar = "";
  309. if (g_ctx.driver_login_state == "driver") { uvar = "driver"; }
  310. else if (g_ctx.driver_login_state == "pin") { uvar = "pin"; }
  311. else {
  312. console.log("_driver_login: ERROR: unknown driver state \"" + g_ctx.driver_login_state + "\"");
  313. return;
  314. }
  315. var n = g_ctx[uvar].length;
  316. //if (inp == 'cancel') { _switch_ui('ui_driver', 'ui_login'); }
  317. if (inp == 'cancel') { _switch_ui('ui_login'); }
  318. else if (inp == 'ok') {
  319. if (uvar == "driver") {
  320. g_ctx.driver_login_state = "pin";
  321. var ele;
  322. ele = document.getElementById("ui_driver_driver");
  323. ele.style.background = "#aaaaaa";
  324. ele = document.getElementById("ui_driver_pin");
  325. ele.style.display = "block";
  326. ele = document.getElementById("ui_driver_pin_text");
  327. ele.style.display = "block";
  328. }
  329. else if (uvar == "pin") {
  330. g_ctx.driver_login_state = "driver";
  331. _verify_driver();
  332. g_ctx.ui_change_timeoutid = window.setTimeout(function() {
  333. _switch_ui("ui_driververify");
  334. }, 150);
  335. return;
  336. }
  337. }
  338. // manage adding/subtracting/clearing relevant field
  339. //
  340. else if (inp == 'bksp') { g_ctx[uvar] = g_ctx[uvar].substr(0, n-1); }
  341. else if (inp == 'clr') { g_ctx[uvar]= ""; }
  342. else { g_ctx[uvar] += inp; }
  343. // Pin should not be displayed on screen.
  344. // Use '*' to hide it.
  345. //
  346. if (uvar == "pin") {
  347. var n = g_ctx[uvar].length;
  348. var x = "";
  349. for (i=0; i<n; i++) { x += "*"; }
  350. var ele = document.getElementById('ui_driver_' + uvar);
  351. ele.innerHTML = x;
  352. }
  353. // Otherwise update the displayed (driver number)
  354. //
  355. else {
  356. var ele = document.getElementById('ui_driver_' + uvar);
  357. ele.innerHTML = g_ctx[uvar];
  358. }
  359. }
  360. //------------
  361. //------------
  362. function _verify_paddle() {
  363. var paddle = g_ctx.paddle;
  364. // have a fail safe for the verification
  365. //
  366. _set_ui_watchdog();
  367. _api_request("paddleinput", {"paddle":paddle}, _verify_paddle_ack, _verify_paddle_fail);
  368. return;
  369. }
  370. function _verify_paddle_ack() {
  371. console.log("got ack for push paddle message\n");
  372. }
  373. function _verify_paddle_ok() {
  374. console.log("verify paddle ok");
  375. _clear_ui_watchdog();
  376. _switch_ui("ui_main");
  377. }
  378. function _verify_paddle_fail() {
  379. console.log("verify paddle fail");
  380. _clear_ui_watchdog();
  381. _switch_ui("ui_paddleunknown");
  382. }
  383. function _paddle_unknown(inp) {
  384. console.log("paddle unknown...");
  385. _clear_ui_watchdog();
  386. _clear_ui_paddle();
  387. _switch_ui("ui_paddle");
  388. }
  389. function _watchdog_tripped(inp) {
  390. console.log("watchdog UI expired!...");
  391. _reset();
  392. }
  393. //------------
  394. function _paddle_input(inp) {
  395. var n = g_ctx.paddle.length;
  396. if (inp == 'cancel') { _switch_ui('ui_login'); }
  397. else if (inp == 'ok') {
  398. _verify_paddle();
  399. g_ctx.ui_change_timeoutid = window.setTimeout(function() {
  400. _switch_ui("ui_paddleverify");
  401. }, 150);
  402. return;
  403. }
  404. else if (inp == 'bksp') { g_ctx.paddle = g_ctx.paddle.substr(0, n-1); }
  405. else if (inp == 'clr') { g_ctx.paddle = ""; }
  406. else { g_ctx.paddle += inp; }
  407. // Otherwise update the displayed (driver number)
  408. //
  409. var ele = document.getElementById('ui_paddle_paddle');
  410. ele.innerHTML = g_ctx.paddle;
  411. }
  412. //-------------
  413. //-------------
  414. //-------------
  415. function _main_prv() {
  416. console.log("prv");
  417. _api_request("prevstop");
  418. }
  419. function _main_nxt() {
  420. console.log("nxt");
  421. _api_request("nextstop");
  422. }
  423. function _main_dim() {
  424. console.log("dim...");
  425. _api_request("dim");
  426. _switch_ui('ui_dim');
  427. }
  428. function _dim_wakeup() {
  429. console.log("waking up from dim");
  430. _switch_ui("ui_main");
  431. }
  432. function _fare_adult() {
  433. console.log("fare adult");
  434. _api_request("fare", {"rule":"CASH-ADULT", "param":"", "fare":"adult","count":1});
  435. }
  436. function _fare_youth() {
  437. console.log("fare youth");
  438. _api_request("fare", {"rule":"CASH-YOUTH", "param":"", "fare":"youth","count":1});
  439. }
  440. function _fare_half() {
  441. console.log("fare half");
  442. _api_request("fare", {"rule":"CASH-HALF", "param":"", "fare":"half","count":1});
  443. }
  444. function _fare_bike() {
  445. console.log("fare bike");
  446. _api_request("fare", {"rule":"MISC-BIKE", "param":"", "fare":"bike","count":1});
  447. }
  448. function _fare_wheelchair() {
  449. console.log("fare wheelchair");
  450. _api_request("fare", {"rule":"MISC-WHEELCHAIR", "param":"", "fare":"wheelchair","count":1});
  451. }
  452. function _fare_transfer() {
  453. console.log("fare transfer");
  454. _api_request("fare", {"rule":"MISC-TRANSFER", "param":"", "fare":"transfer","count":1});
  455. }
  456. //-------------
  457. //-------------
  458. //-------------
  459. function _main_bulk() {
  460. // reset tab to Misc. (first option) by default
  461. //
  462. ele = document.getElementById("ui_bulk_misc");
  463. ele.classList.remove("highlight");
  464. ele.classList.add("highlight");
  465. ele = document.getElementById("ui_bulk_passes");
  466. ele.classList.remove("highlight");
  467. _show_ui_element("ui_bulk_misc_tab");
  468. _hide_ui_element("ui_bulk_passes_tab");
  469. _switch_ui("ui_bulk");
  470. }
  471. function _main_menu() {
  472. // reset tab to Misc. (first option) by default
  473. //
  474. ele = document.getElementById("ui_fareoverride_misc");
  475. ele.classList.remove("highlight");
  476. ele.classList.add("highlight");
  477. ele = document.getElementById("ui_fareoverride_passes");
  478. ele.classList.remove("highlight");
  479. _show_ui_element("ui_fareoverride_misc_tab");
  480. _hide_ui_element("ui_fareoverride_passes_tab");
  481. _switch_ui("ui_fareoverride");
  482. }
  483. //-------------
  484. //-------------
  485. //-------------
  486. function _bulkaccept_ok() {
  487. console.log("bulkfare:", g_ctx.bulkfare, ", count:", g_ctx.bulkfare_count);
  488. _switch_ui("ui_main");
  489. _api_request("fare",{"rule":g_ctx.bulkfare, "param":g_ctx.bulkfare_count.toString(), "fare":g_ctx.bulkfare,"count":g_ctx.bulkfare_count});
  490. }
  491. function _bulkaccept_cancel() {
  492. _switch_ui("ui_main");
  493. }
  494. function _bulkaccept_input(inp) {
  495. var n = g_ctx.bulkfare_count.length;
  496. if (inp == 'cancel') { _switch_ui('ui_main'); }
  497. else if (inp == 'ok') { _bulkaccept_ok(); return; }
  498. else if (inp == 'bksp') { g_ctx.bulkfare_count = g_ctx.bulkfare_count.substr(0, n-1); }
  499. else if (inp == 'clr') { g_ctx.bulkfare_count = ""; }
  500. else { g_ctx.bulkfare_count += inp; }
  501. // Otherwise update the displayed (driver number)
  502. //
  503. var ele = document.getElementById('ui_bulkaccept_count');
  504. ele.innerHTML = g_ctx.bulkfare_count;
  505. }
  506. //-------------
  507. //-------------
  508. //-------------
  509. // space pad
  510. // default 10
  511. //
  512. function _sp(str, align, pad) {
  513. align = ((typeof align=="undefined") ? "left" : align);
  514. pad = ((typeof pad==="undefined") ? 10 : pad);
  515. var n = 0;
  516. n = pad - str.length;
  517. if (align == "left") {
  518. for (var i=0; i<n; i++) { str += " "; }
  519. }
  520. else if (align == "center") {
  521. var rem = n%2;
  522. var n2 = Math.floor(n/2);
  523. var pfx = "", sfx = "";
  524. for (var i=0; i<n2; i++) { pfx += " "; }
  525. for (var i=0; i<(n2+rem); i++) { sfx += " "; }
  526. str = pfx + str + sfx;
  527. }
  528. else if (align == "right") {
  529. var pfx = "";
  530. for (var i=0; i<n; i++) { pfx += " "; }
  531. str = pfx + str;
  532. }
  533. return str;
  534. }
  535. function _cb_status(inp) {
  536. var tok = inp.split(" ");
  537. if (tok.length < 2) { return; }
  538. if (tok[0]!="ok") { return; }
  539. valtok = tok.slice(1).join(" ").split("\n");
  540. if (valtok[0]!="msg=status") { return; }
  541. g_ctx.diu_status = {};
  542. var fields = valtok[1].split("|");
  543. for (var ii=0; ii<fields.length; ii++) {
  544. var kv = fields[ii].split("=");
  545. if (kv.length!=2) { continue; }
  546. g_ctx.diu_status[kv[0].trim()] = kv[1].trim();
  547. }
  548. if ("equipno" in g_ctx.diu_status) {
  549. g_ctx.cur_eqnum = g_ctx.diu_status["equipno"];
  550. }
  551. var m = g_ctx.diu_status;
  552. if (g_ctx.current_ui === "ui_login") {
  553. var has_tunn = ((m["tunnel"] == '1') ? 'YES' : 'NO');
  554. var has_gprs = ((m["gprs"] == '1') ? 'YES' : 'NO');
  555. var has_gps = ((m["gps"] == '1') ? 'YES' : 'NO');
  556. var lat = ((typeof m["gpslat"] !== "undefined") ? m["gpslat"] : "");
  557. var lon = ((typeof m["gpslon"] !== "undefined") ? m["gpslon"] : "");
  558. var gps_str = ( ((lat.length>0) && (lon.length>0)) ? (" (" + lat + "/" + lon + ")") : "" );
  559. var ele = document.getElementById("ui_login_status");
  560. var html_a = [];
  561. html_a.push("Tunnel: " + has_tunn + " GPRS: " + has_gprs + " Eq#: " + m["equipno"] + " #Msg: " + m["nmsg"]);
  562. html_a.push("Last Token Read (" + m["last_token"] + ")");
  563. html_a.push("GPS: " + has_gps + gps_str + "");
  564. html_a.push(" ");
  565. // some site specific fiddling to display package information
  566. //
  567. html_a.push(_sp("Package") + _sp("Version", "left", 18) + _sp("Installed", "right", 12));
  568. html_a.push(_sp("ui") + _sp(DIU_UI_VERSION, "left", 18) + _sp(DIU_UI_VERSION_DATE, "right", 12) + "");
  569. for (var _ii=0; _ii<3; _ii++) {
  570. var key = "pkg" + _ii;
  571. if (key in g_ctx.diu_status) {
  572. var aval = g_ctx.diu_status[key].split(" ");
  573. html_a.push(_sp(aval[0]) + _sp(aval[1], "left", 18) + _sp(aval[2], "right", 12));
  574. }
  575. }
  576. html_a.push(" ");
  577. if ("IMEI" in m) { html_a.push("IMEI = " + m["IMEI"] + ""); }
  578. if ("IMSI" in m) { html_a.push("IMSI = " + m["IMSI"] + ""); }
  579. if ("ETH0" in m) { html_a.push("ETH0 = " + m["ETH0"] + ""); }
  580. html_a.push(" ");
  581. for (var ii=0; ii<html_a.length; ii++) {
  582. html_a[ii] = html_a[ii].replace(/ /g, '&nbsp;');
  583. }
  584. var div_hdr = "<div class='textrow'>";
  585. var str = div_hdr + html_a.join("</div><br>" + div_hdr ) + "</div>";
  586. ele.innerHTML = str;
  587. }
  588. else if (g_ctx.current_ui === "ui_main") {
  589. var ele = document.getElementById("mainstatus");
  590. var str = "";
  591. str += "Rt " + m["route"] + " ";
  592. str += "Trip " + m["trip"] + " ";
  593. str += "Stop " + m["stop"] + " ";
  594. str += "GPS " + m["gps"] + " ";
  595. str += "Tun " + m["tunnel"] + ", ";
  596. str += m["date"];
  597. str += " " + moment().format("HH:mm");
  598. ele.innerHTML = str;
  599. }
  600. }
  601. function _cb_status_err(e) {
  602. console.log("ERROR: _cb_status_err:", e);
  603. }
  604. //-------------
  605. function _main_message_clear() {
  606. g_ctx.status_text = "";
  607. }
  608. function _main_message_replace(msg, bg_color, text_color) {
  609. var ele = document.getElementById("ui_main_status");
  610. bg_color = ((typeof bg_color === "undefined") ? BG_COLOR : bg_color);
  611. text_color = ((typeof text_color === "undefined") ? TEXT_COLOR : text_color);
  612. var lines = g_ctx.status_text.split("\n");
  613. if (lines.length > g_ctx.status_text_h) {
  614. var n = lines.length;
  615. lines = lines.slice(n - g_ctx.status_text_h);
  616. }
  617. for (var i=0; i<lines.length; i++) {
  618. lines[i] = lines[i].slice(0,g_ctx.status_text_w);
  619. }
  620. var idx = lines.length-1;
  621. if (idx<0) { idx = 0; }
  622. lines[idx] = msg;
  623. var html_lines = [];
  624. for (var i=0; i<lines.length; i++) {
  625. html_lines.push(lines[i].slice(0,g_ctx.status_text_w));
  626. if (html_lines[i].match(/ACCEPT/)) {
  627. html_lines[i] = "<div class='textrow accept'>" + html_lines[i] + "</div>";
  628. }
  629. else if (html_lines[i].match(/REJECT/)) {
  630. html_lines[i] = "<div class='textrow reject'>" + html_lines[i] + "</div>";
  631. }
  632. else if (html_lines[i].match(/[Uu]nknown/)) {
  633. html_lines[i] = "<div class='textrow reject'>" + html_lines[i] + "</div>";
  634. }
  635. else if (html_lines[i].match(/[Pp]assback/)) {
  636. html_lines[i] = "<div class='textrow reject'>" + html_lines[i] + "</div>";
  637. }
  638. else if (html_lines[i].match(/Rule execution error/)) {
  639. html_lines[i] = "<div class='textrow error'>" + html_lines[i] + "</div>";
  640. }
  641. else {
  642. html_lines[i] = "<div class='textrow'>" + html_lines[i] + "</div>";
  643. }
  644. }
  645. g_ctx.status_text = lines.join("\n");
  646. g_ctx.status_text = lines.join("\n");
  647. g_ctx.status_html_text = html_lines.join("<br>");
  648. ele.innerHTML = g_ctx.status_html_text;
  649. // sounds
  650. //
  651. if (msg.match(/ACCEPT/)) {
  652. _beep_accept();
  653. }
  654. else if (msg.match(/REJECT/)) {
  655. _beep_reject();
  656. }
  657. else if (msg.match(/[Uu]nknown/)) {
  658. _beep_reject();
  659. }
  660. else if (msg.match(/[Pp]assback/)) {
  661. _beep_reject();
  662. }
  663. else if (msg.match(/Rule execution error/)) {
  664. _beep_error();
  665. }
  666. }
  667. function _main_message_add(msg, bg_color, text_color) {
  668. var ele = document.getElementById("ui_main_status");
  669. bg_color = ((typeof bg_color === "undefined") ? BG_COLOR : bg_color);
  670. text_color = ((typeof text_color === "undefined") ? TEXT_COLOR : text_color);
  671. var txt = g_ctx.status_text + "\n" + msg;
  672. var html_lines = [];
  673. var lines = txt.split("\n");
  674. if (lines.length > g_ctx.status_text_h) {
  675. var n = lines.length;
  676. lines = lines.slice(n - g_ctx.status_text_h);
  677. }
  678. for (var i=0; i<lines.length; i++) {
  679. html_lines.push(lines[i].slice(0,g_ctx.status_text_w-1));
  680. if (html_lines[i].match(/ACCEPT/)) {
  681. html_lines[i] = "<div class='textrow accept'>" + html_lines[i] + "</div>";
  682. }
  683. else if (html_lines[i].match(/REJECT/)) {
  684. html_lines[i] = "<div class='textrow reject'>" + html_lines[i] + "</div>";
  685. }
  686. else if (html_lines[i].match(/[Uu]nknown/)) {
  687. html_lines[i] = "<div class='textrow reject'>" + html_lines[i] + "</div>";
  688. }
  689. else if (html_lines[i].match(/[Pp]assback/)) {
  690. html_lines[i] = "<div class='textrow reject'>" + html_lines[i] + "</div>";
  691. }
  692. else if (html_lines[i].match(/Rule execution error/)) {
  693. html_lines[i] = "<div class='textrow error'>" + html_lines[i] + "</div>";
  694. }
  695. else {
  696. html_lines[i] = "<div class='textrow'>" + html_lines[i] + "</div>";
  697. }
  698. }
  699. g_ctx.status_text = lines.join("\n");
  700. g_ctx.status_html_text = html_lines.join("<br>");
  701. ele.innerHTML = g_ctx.status_html_text;
  702. // sounds
  703. //
  704. if (msg.match(/ACCEPT/)) {
  705. _beep_accept();
  706. }
  707. else if (msg.match(/REJECT/)) {
  708. _beep_reject();
  709. }
  710. else if (msg.match(/[Uu]nknown/)) {
  711. _beep_reject();
  712. }
  713. else if (msg.match(/[Pp]assback/)) {
  714. _beep_reject();
  715. }
  716. else if (msg.match(/Rule execution error/)) {
  717. _beep_error();
  718. }
  719. }
  720. //-------------
  721. //-------------
  722. //-------------
  723. function _ws_process(dat) {
  724. console.log("_ws_process:", dat);
  725. var tok = dat.split(" ");
  726. if (tok.length < 2) { return; }
  727. if (tok[0] == "paddle") {
  728. if (tok[1] == "ok") { _verify_paddle_ok(); }
  729. else if (tok[1] == "fail") { _verify_paddle_fail(); }
  730. else { _verify_paddle_fail(); }
  731. }
  732. else if (tok[0] == "driver_notify") {
  733. if (tok.length < 5) {
  734. console.log("malformed message: ", dat);
  735. }
  736. else if (tok[1] == "replace") {
  737. _main_message_replace(tok.slice(4).join(" "));
  738. }
  739. else if (tok[1] == "ok") {
  740. _main_message_add(tok.slice(4).join(" "));
  741. }
  742. else {
  743. console.log("unknown message: ", dat);
  744. }
  745. }
  746. }
  747. function _init_websocket() {
  748. var sock = new WebSocket("ws://" + _fqADDRESS);
  749. sock.onopen = function(e) { console.log("open", e); }
  750. sock.onmessage = function(e) { _ws_process(e.data); }
  751. sock.onclose = function(e) {
  752. console.log("close", e);
  753. setTimeout( function() { _init_websocket(); }, 1000);
  754. }
  755. sock.onerror = function(e) {
  756. console.log("error", e);
  757. }
  758. }
  759. //-------------
  760. var ui_pattern = {
  761. "state" : "idle",
  762. "lastX" : 0,
  763. "lastY" : 0,
  764. "curX" : 0,
  765. "curY" : 0
  766. };
  767. function ui_pattern_mousedown(ev) {
  768. var ele = document.getElementById("body");
  769. var rect = ele.getBoundingClientRect();
  770. var x = ev["x"];
  771. var y = ev["y"];
  772. var width = rect.width;
  773. var height = rect.height;
  774. if (ui_pattern.state == "idle") {
  775. if (y < (height/4)) {
  776. ui_pattern.state = "square_0";
  777. }
  778. else {
  779. ui_pattern.state = "idle"
  780. }
  781. }
  782. else if (ui_pattern.state == "square_0") {
  783. if (x > (3*width/4)) {
  784. ui_pattern.state = "square_1";
  785. }
  786. else {
  787. ui_pattern.state = "idle"
  788. }
  789. }
  790. else if (ui_pattern.state == "square_1") {
  791. if (y > (3*height/4)) {
  792. ui_pattern.state = "square_2";
  793. }
  794. else {
  795. ui_pattern.state = "idle"
  796. }
  797. }
  798. else if (ui_pattern.state == "square_2") {
  799. if (x < (width/4)) {
  800. ui_pattern.state = "square_3";
  801. }
  802. else {
  803. ui_pattern.state = "idle"
  804. }
  805. }
  806. else if (ui_pattern.state == "square_3") {
  807. if (y < (height/4)) {
  808. ui_pattern.state = "square_4";
  809. }
  810. else {
  811. ui_pattern.state = "idle"
  812. }
  813. }
  814. else if (ui_pattern.state == "square_4") {
  815. if (x > (3*width/4)) {
  816. ui_pattern.state = "square_5";
  817. }
  818. else {
  819. ui_pattern.state = "idle"
  820. }
  821. }
  822. else if (ui_pattern.state == "square_5") {
  823. if (y > (3*height/4)) {
  824. ui_pattern.state = "square_6";
  825. }
  826. else {
  827. ui_pattern.state = "idle"
  828. }
  829. }
  830. else if (ui_pattern.state == "square_6") {
  831. if (x < (width/4)) {
  832. _switch_ui("ui_diagnostic");
  833. }
  834. ui_pattern.state = "idle"
  835. }
  836. return false;
  837. }
  838. function ui_pattern_mouseup(ev) {
  839. return false;
  840. }
  841. //------------
  842. function _mainscreen() {
  843. _switch_ui('ui_login');
  844. g_ctx.driver_login_state = "driver";
  845. _clear_ui_driver();
  846. _clear_ui_paddle();
  847. }
  848. function _configuration() {
  849. _switch_ui('ui_configuration');
  850. g_ctx.driver_login_state = "driver";
  851. _clear_ui_driver();
  852. _clear_ui_paddle();
  853. }
  854. function _eqnum() {
  855. _switch_ui('ui_eqnum');
  856. g_ctx.driver_login_state = "driver";
  857. if (g_ctx.cur_eqnum.length > 0) {
  858. var ele = document.getElementById("ui_eqnum_eqnum");
  859. ele.innerHTML = g_ctx.cur_eqnum;
  860. g_ctx.eqnum = g_ctx.cur_eqnum;
  861. }
  862. _clear_ui_driver();
  863. _clear_ui_paddle();
  864. }
  865. function _reset() {
  866. _switch_ui('ui_login');
  867. g_ctx.driver_login_state = "driver";
  868. _clear_ui_driver();
  869. _clear_ui_paddle();
  870. location.reload(true);
  871. }
  872. function _reboot() {
  873. _switch_ui('ui_login');
  874. g_ctx.driver_login_state = "driver";
  875. _clear_ui_driver();
  876. _clear_ui_paddle();
  877. }
  878. function _custom0() {
  879. _api_request("interfaceupdown", {"interface":"eth0", "state":"up"}, function(inp) { console.log("custom0 ok:", inp); }, function(inp) { console.log("custom0 error:", inp); });
  880. }
  881. function _custom1() {
  882. _api_request("interfaceupdown", {"interface":"eth0", "state":"down"}, function(inp) { console.log("custom1 ok:", inp); }, function(inp) { console.log("custom1 error:", inp); });
  883. }
  884. function _update_diagnostic_window(_msg) {
  885. var ele = document.getElementById("ui_diagnostic_view");
  886. ele.innerHTML = _msg;
  887. }
  888. function _custom2() {
  889. _api_request("interfaceinfo", {"interface":"eth0"}, _update_diagnostic_window, function(inp) { console.log("custom2 error:", inp); });
  890. }
  891. function _custom3() {
  892. _api_request("say", {"message":"speech test"}, function(inp) { console.log("custom3 ok:", inp); }, function(inp) { console.log("custom3 error:", inp); });
  893. }
  894. function _custom4() {
  895. //_switch_ui('ui_login');
  896. //g_ctx.driver_login_state = "driver";
  897. //_clear_ui_driver();
  898. //_clear_ui_paddle();
  899. }
  900. function _custom5() {
  901. //_switch_ui('ui_login');
  902. //g_ctx.driver_login_state = "driver";
  903. //_clear_ui_driver();
  904. //_clear_ui_paddle();
  905. }
  906. //------------
  907. function _set_eqnum() {
  908. if (g_ctx.eqnum.length == 0) { return; }
  909. _api_request("eqnum", {"eqnum":g_ctx.eqnum}, function(inp) { console.log("eqnum ok:", inp); }, function(inp) { console.log("eqnum error:", inp); });
  910. }
  911. function _eqnum_input(inp) {
  912. var n = g_ctx.eqnum.length;
  913. if (inp == 'cancel') { _switch_ui('ui_login'); }
  914. else if (inp == 'ok') {
  915. _set_eqnum();
  916. _switch_ui('ui_login');
  917. return;
  918. }
  919. else if (inp == 'bksp') { g_ctx.eqnum = g_ctx.eqnum.substr(0, n-1); }
  920. else if (inp == 'clr') { g_ctx.eqnum = ""; }
  921. else { g_ctx.eqnum += inp; }
  922. // Otherwise update the displayed equipment number
  923. //
  924. var ele = document.getElementById('ui_eqnum_eqnum');
  925. ele.innerHTML = g_ctx.eqnum;
  926. }
  927. //-------------
  928. function _text_width_height() {
  929. var _t = document.getElementById("ui_test");
  930. console.log(">>>", _t.clientHeight, _t.clientWidth);
  931. }
  932. function _calculate_ui_main_status_width_height() {
  933. var _t = document.getElementById("ui_test");
  934. var ch_w = _t.clientWidth;
  935. var ch_h = _t.clientHeight;
  936. var _w = document.getElementById("ui_main_status");
  937. var win_w = _w.clientWidth;
  938. var win_h = _w.clientHeight;
  939. var n_w = Math.floor(win_w / ch_w);
  940. var n_h = Math.floor(win_h / ch_h);
  941. return {"n_width": n_w, "n_height":n_h};
  942. }
  943. function init() {
  944. _init_websocket();
  945. window.setInterval(_update_display_time, 200);
  946. // watchdog
  947. //
  948. document.getElementById('ui_watchdog_btn').onclick = _watchdog_tripped;
  949. // diagnostics
  950. //
  951. document.getElementById('ui_diagnostic_mainscreen').onclick = _mainscreen;
  952. document.getElementById('ui_diagnostic_reset').onclick = _reset;
  953. document.getElementById('ui_diagnostic_reboot').onclick = _reboot;
  954. document.getElementById('ui_diagnostic_custom0').onclick = _custom0;
  955. document.getElementById('ui_diagnostic_custom1').onclick = _custom1;
  956. document.getElementById('ui_diagnostic_custom2').onclick = _custom2;
  957. document.getElementById('ui_diagnostic_custom3').onclick = _custom3;
  958. document.getElementById('ui_diagnostic_custom4').onclick = _custom4;
  959. document.getElementById('ui_diagnostic_custom5').onclick = _custom5;
  960. // configuration
  961. //
  962. document.getElementById('ui_configuration_back').onclick = _mainscreen;
  963. document.getElementById('ui_configuration_volumetest').onclick = _volume_test;
  964. document.getElementById('ui_configuration_eqnum').onclick = _eqnum;
  965. // equipment number change
  966. //
  967. document.getElementById('ui_eqnum_cancel').onclick = _mainscreen;
  968. document.getElementById('ui_eqnum_ok').onclick = function() { console.log("eqnum change"); }
  969. // login
  970. //
  971. document.getElementById('ui_login_login').onclick = _login;
  972. document.getElementById('ui_login_configuration').onclick = _configuration;
  973. document.getElementById('ui_driverincorrect_btn').onclick = _driver_incorrect;
  974. document.getElementById('ui_paddleunknown_btn').onclick = _paddle_unknown;
  975. // main menu buttons
  976. //
  977. document.getElementById('ui_main_prv').onclick = _main_prv;
  978. document.getElementById('ui_main_nxt').onclick = _main_nxt;
  979. document.getElementById('ui_main_bulk').onclick = _main_bulk;
  980. document.getElementById('ui_main_menu').onclick = _main_menu;
  981. document.getElementById('ui_main_dim').onclick = _main_dim;
  982. // dim
  983. //
  984. document.getElementById('ui_dim_btn').onclick = _dim_wakeup;
  985. var simple_fares = [ "adult", "youth", "half", "bike", "wheelchair", "transfer"];
  986. var simple_fares_f = [ _fare_adult, _fare_youth, _fare_half, _fare_bike, _fare_wheelchair, _fare_transfer];
  987. for (var ii=0; ii<simple_fares.length; ii++) {
  988. var fare = simple_fares[ii];
  989. document.getElementById('ui_main_' + fare).onclick = simple_fares_f[ii];
  990. }
  991. // bulk menu buttons
  992. //
  993. document.getElementById('ui_bulk_passes').onclick =
  994. function() {
  995. var ele;
  996. ele = document.getElementById("ui_bulk_misc");
  997. ele.classList.remove("highlight");
  998. ele = document.getElementById("ui_bulk_passes");
  999. ele.classList.add("highlight");
  1000. _hide_ui_element("ui_bulk_misc_tab");
  1001. _show_ui_element("ui_bulk_passes_tab");
  1002. };
  1003. document.getElementById('ui_bulk_misc').onclick =
  1004. function() {
  1005. var ele;
  1006. ele = document.getElementById("ui_bulk_misc");
  1007. ele.classList.add("highlight");
  1008. ele = document.getElementById("ui_bulk_passes");
  1009. ele.classList.remove("highlight");
  1010. _show_ui_element("ui_bulk_misc_tab");
  1011. _hide_ui_element("ui_bulk_passes_tab");
  1012. };
  1013. document.getElementById('ui_bulk_menu').onclick = function() { _switch_ui("ui_main"); }
  1014. // Setup "Bulk" UI accept buttons to do proper screen switching and
  1015. // other tasks.
  1016. //
  1017. var bulk_pass = ["adult", "youth", "half", "event", "transfer", "interline",
  1018. "schoola", "schoolb", "schoolc", "orga", "orgb", "orgc"];
  1019. var bulk_rule = ["BULK-CASH-ADULT", "BULK-CASH-YOUTH", "BULK-CASH-HALF",
  1020. "BULK-MISC-EVENT", "BULK-MISC-TRANSFER", "BULK-MISC-INTERLINE",
  1021. "BULK-OVR-SCHOOLA", "BULK-OVR-SCHOOLB", "BULK-OVR-SCHOOLC",
  1022. "BULK-OVR-ORGA", "BULK-OVR-ORGB", "BULK-OVR-ORGC"];
  1023. var bulk_pass_name = ["Adult Cash", "Youth Cash", "Half Cash", "Special Event", "Transfer", "Interline",
  1024. "School A", "School B", "School C", "Org A", "Org B", "Org C"];
  1025. // use site specific values if available, otherwise default to the above
  1026. //
  1027. if ((typeof BULK_PASS !== "undefined") && (BULK_PASS.length > 0)) {
  1028. bulk_pass = BULK_PASS;
  1029. }
  1030. if ((typeof BULK_RULE !== "undefined") && (BULK_RULE.length > 0)) {
  1031. bulk_rule= BULK_RULE;
  1032. }
  1033. if ((typeof BULK_PASS_NAME !== "undefined") && (BULK_PASS_NAME.length > 0)) {
  1034. bulk_pass_name = BULK_PASS_NAME;
  1035. }
  1036. for (var i=0; i<bulk_pass.length; i++) {
  1037. var p = bulk_pass[i];
  1038. var u = bulk_pass_name[i];
  1039. var r = bulk_rule[i];
  1040. var ele = document.getElementById('ui_bulk_' + p);
  1041. ele.onclick = (function(x,y) {
  1042. return function() {
  1043. _clear_ui_bulkaccept();
  1044. g_ctx.bulkfare = x;
  1045. _switch_ui("ui_bulkaccept");
  1046. var ee = document.getElementById("ui_bulkaccept_faretype");
  1047. ee.innerHTML = y;
  1048. };
  1049. })(r, u);
  1050. }
  1051. // bulk accept fares
  1052. //
  1053. document.getElementById('ui_bulkaccept_ok').onclick = _bulkaccept_ok;
  1054. document.getElementById('ui_bulkaccept_cancel').onclick = _bulkaccept_cancel;
  1055. // fareoverride menu buttons
  1056. //
  1057. document.getElementById('ui_fareoverride_passes').onclick =
  1058. function() {
  1059. var ele;
  1060. ele = document.getElementById("ui_fareoverride_misc");
  1061. ele.classList.remove("highlight");
  1062. ele = document.getElementById("ui_fareoverride_passes");
  1063. ele.classList.add("highlight");
  1064. _hide_ui_element("ui_fareoverride_misc_tab");
  1065. _show_ui_element("ui_fareoverride_passes_tab");
  1066. };
  1067. document.getElementById('ui_fareoverride_misc').onclick =
  1068. function() {
  1069. var ele;
  1070. ele = document.getElementById("ui_fareoverride_misc");
  1071. ele.classList.add("highlight");
  1072. ele = document.getElementById("ui_fareoverride_passes");
  1073. ele.classList.remove("highlight");
  1074. _show_ui_element("ui_fareoverride_misc_tab");
  1075. _hide_ui_element("ui_fareoverride_passes_tab");
  1076. };
  1077. document.getElementById('ui_fareoverride_menu').onclick = function() { _switch_ui("ui_main"); }
  1078. document.getElementById('ui_fareoverride_logout').onclick =
  1079. function() {
  1080. _main_message_clear();
  1081. _api_request("logout");
  1082. _switch_ui("ui_login");
  1083. }
  1084. // default values for override passes
  1085. //
  1086. var override_pass = [ "adult", "youth", "half",
  1087. "bike", "wheelchair", "transfer",
  1088. "event", "interline",
  1089. "schoola", "schoolb", "schoolc",
  1090. "orga", "orgb", "orgc" ];
  1091. var override_rule = [ "CASH-ADULT", "CASH-YOUTH", "CASH-HALF",
  1092. "MISC-BIKE", "MISC-WHEELCHAIR", "MISC-TRANSFER",
  1093. "MISC-EVENT", "MISC-INTERLINE",
  1094. "OVR-SCHOOLA", "OVR-SCHOOLB", "OVR-SCHOOLC",
  1095. "OVR-ORGA", "OVR-ORGB", "OVR-ORGC" ];
  1096. // use site specific values if available, otherwise default to the above
  1097. //
  1098. if ((typeof OVERRIDE_PASS !== "undefined") && (OVERRIDE_PASS.length > 0)) {
  1099. override_pass = OVERRIDE_PASS;
  1100. }
  1101. if ((typeof OVERRIDE_RULE !== "undefined") && (OVERRIDE_RULE.length > 0)) {
  1102. override_rule= OVERRIDE_RULE;
  1103. }
  1104. for (var i=0; i<override_pass.length; i++) {
  1105. var p = override_pass[i];
  1106. var r = override_rule[i];
  1107. var ele = document.getElementById("ui_fareoverride_" + p);
  1108. ele.onclick = (function(x,y) {
  1109. return function() { _api_request("fare", {"rule":x,"fare":y,"param":"","count":1}); };
  1110. })(r,p);
  1111. }
  1112. var ui_page = [ "driver", "paddle", "bulkaccept", "eqnum" ];
  1113. var ui_func = [ _driver_login, _paddle_input, _bulkaccept_input, _eqnum_input ];
  1114. for (var idx=0; idx<ui_page.length; idx++) {
  1115. var c = ui_page[idx];
  1116. var f = ui_func[idx];
  1117. for (var i=0; i<=9; i++) {
  1118. document.getElementById('ui_' + c + '_' + i).onclick =
  1119. (function(_f, x) { return function() { _f(x); } })(f, i);
  1120. }
  1121. document.getElementById('ui_' + c + '_' + 'bksp').onclick =
  1122. (function(_f,x) { return function() { _f(x); } })(f, 'bksp');
  1123. document.getElementById('ui_' + c + '_' + 'clr').onclick =
  1124. (function(_f,x) { return function() { _f(x); } })(f, 'clr');
  1125. document.getElementById('ui_' + c + '_' + 'ok').onclick =
  1126. (function(_f,x) { return function() { _f(x); } })(f, 'ok');
  1127. document.getElementById('ui_' + c + '_' + 'cancel').onclick =
  1128. (function(_f,x) { return function() { _f(x); } })(f, 'cancel');
  1129. }
  1130. // set interval to check status every 1 second
  1131. //
  1132. var x = window.setInterval(function() { _api_request("status", {}, _cb_status, _cb_status_err); }, 1000);
  1133. // try and reduce textarea highlighting, drag and drop/copy pasting,
  1134. // and other bad behavor.
  1135. //
  1136. var ele;
  1137. var block_ele;
  1138. var rect;
  1139. ele = document.getElementById("ui_login_status");
  1140. ele.ondragstart = function() { return false; }
  1141. ele.onselectstart = function() { return false; }
  1142. ele.onmousedown = function() { return false; }
  1143. ele.ondragstart = function() { return false; }
  1144. rect = ele.getBoundingClientRect();
  1145. block_ele = document.getElementById("ui_login_blockwindow");
  1146. block_ele.style.width = rect.width;
  1147. block_ele.style.height = rect.height;
  1148. block_ele.style.bottom = rect.bottom;
  1149. block_ele.style.left = rect.left;
  1150. block_ele.style.top = rect.top;
  1151. block_ele.style.right = rect.right;
  1152. block_ele.style.x = rect.x;
  1153. block_ele.style.y = rect.y;
  1154. ele = document.getElementById("ui_main_status");
  1155. ele.ondragstart = function() { return false; }
  1156. ele.onselectstart = function() { return false; }
  1157. ele.onmousedown = function() { return false; }
  1158. ele.ondragstart = function() { return false; }
  1159. rect = ele.getBoundingClientRect();
  1160. block_ele = document.getElementById("ui_main_blockwindow");
  1161. block_ele.style.width = rect.width;
  1162. block_ele.style.height = rect.height;
  1163. block_ele.style.bottom = rect.bottom;
  1164. block_ele.style.left = rect.left;
  1165. block_ele.style.top = rect.top;
  1166. block_ele.style.right = rect.right;
  1167. block_ele.style.x = rect.x;
  1168. block_ele.style.y = rect.y;
  1169. ele = document.getElementById("body");
  1170. // We're using jquery so we need to gracefully add callbacks
  1171. // for mouse events.
  1172. //
  1173. ele.addEventListener('mousedown', ui_pattern_mousedown);
  1174. ele.addEventListener('mouseup', ui_pattern_mouseup);
  1175. g_snd.button_press = new Audio("assets/459992_florianreichelt_beep_short.mp3");
  1176. g_snd.accept_fare = new Audio("assets/443026__qubodup__public-domain-beep-sound.mp3");
  1177. g_snd.reject_fare = new Audio("assets/368780__gurie__start-sound-beep.mp3");
  1178. g_snd.rule_error = new Audio("assets/363920__samsterbirdies__8-bit-error.mp3");
  1179. if (typeof PopufareSiteInit !== "undefined") {
  1180. PopufareSiteInit();
  1181. }
  1182. }
  1183. (function($) {
  1184. $(document).ready(function() {
  1185. init();
  1186. });
  1187. })(jQuery);