diu_ui-ORG.js 42 KB

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