diff --git a/js/utils.js b/js/utils.js index 9a9e973..914f726 100644 --- a/js/utils.js +++ b/js/utils.js @@ -26,11 +26,6 @@ exports.cr = function cr (hdl) { } } -/** Convert any to bool safely */ -exports.bool = function bool (x) { - return (x === 1 || x === '1' || x === true || x === 'true') -} - /** Decode number from 2B encoding */ exports.parse2B = function parse2B (s, i = 0) { return (s.charCodeAt(i++) - 1) + (s.charCodeAt(i) - 1) * 127 diff --git a/js/wifi.js b/js/wifi.js index 9728a6f..16d5e06 100644 --- a/js/wifi.js +++ b/js/wifi.js @@ -1,9 +1,11 @@ const $ = require('./lib/chibi') -const { mk, bool } = require('./utils') +const { mk } = require('./utils') const tr = require('./lang') -;(function (w) { - const authStr = ['Open', 'WEP', 'WPA', 'WPA2', 'WPA/WPA2'] +{ + const w = window.WiFi = {} + + const authTypes = ['Open', 'WEP', 'WPA', 'WPA2', 'WPA/WPA2'] let curSSID // Get XX % for a slider input @@ -20,9 +22,13 @@ const tr = require('./lang') $('#sta-nw-nil').toggleClass('hidden', name.length > 0) $('#sta-nw .essid').html($.htmlEscape(name)) - const nopw = !password || password.length === 0 - $('#sta-nw .passwd').toggleClass('hidden', nopw) - $('#sta-nw .nopasswd').toggleClass('hidden', !nopw) + const hasPassword = !!password + + // (the following is kind of confusing with the double-double negations, + // but it works) + $('#sta-nw .passwd').toggleClass('hidden', !hasPassword) + $('#sta-nw .nopasswd').toggleClass('hidden', hasPassword) + $('#sta-nw .ip').html(ip.length > 0 ? tr('wifi.connected_ip_is') + ip : tr('wifi.not_conn')) } @@ -40,7 +46,7 @@ const tr = require('./lang') if (status !== 200) { // bad response - rescan(5000) // wait 5sm then retry + rescan(5000) // wait 5s then retry return } @@ -52,7 +58,7 @@ const tr = require('./lang') return } - const done = !bool(resp.result.inProgress) && (resp.result.APs.length > 0) + const done = !resp.result.inProgress && resp.result.APs.length > 0 rescan(done ? 15000 : 1000) if (!done) return // no redraw yet @@ -65,9 +71,7 @@ const tr = require('./lang') $('#ap-loader').toggleClass('hidden', done) // scan done - resp.result.APs.sort(function (a, b) { - return b.rssi - a.rssi - }).forEach(function (ap) { + resp.result.APs.sort((a, b) => b.rssi - a.rssi).forEach(function (ap) { ap.enc = parseInt(ap.enc) if (ap.enc > 4) return // hide unsupported auths @@ -90,7 +94,7 @@ const tr = require('./lang') $(inner).addClass('inner') .htmlAppend(`
${ap.rssi_perc}
`) .htmlAppend(`
${escapedSSID}
`) - .htmlAppend(`
${authStr[ap.enc]}
`) + .htmlAppend(`
${authTypes[ap.enc]}
`) $item.on('click', function () { let $th = $(this) @@ -164,4 +168,4 @@ const tr = require('./lang') w.init = wifiInit w.startScanning = startScanning -})(window.WiFi = {}) +}