Refactor wifi.js a tiny bit

box-drawing
cpsdqs 7 years ago
parent f47b7a5c40
commit d85f6c6ea0
  1. 5
      js/utils.js
  2. 30
      js/wifi.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 */ /** Decode number from 2B encoding */
exports.parse2B = function parse2B (s, i = 0) { exports.parse2B = function parse2B (s, i = 0) {
return (s.charCodeAt(i++) - 1) + (s.charCodeAt(i) - 1) * 127 return (s.charCodeAt(i++) - 1) + (s.charCodeAt(i) - 1) * 127

@ -1,9 +1,11 @@
const $ = require('./lib/chibi') const $ = require('./lib/chibi')
const { mk, bool } = require('./utils') const { mk } = require('./utils')
const tr = require('./lang') 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 let curSSID
// Get XX % for a slider input // Get XX % for a slider input
@ -20,9 +22,13 @@ const tr = require('./lang')
$('#sta-nw-nil').toggleClass('hidden', name.length > 0) $('#sta-nw-nil').toggleClass('hidden', name.length > 0)
$('#sta-nw .essid').html($.htmlEscape(name)) $('#sta-nw .essid').html($.htmlEscape(name))
const nopw = !password || password.length === 0 const hasPassword = !!password
$('#sta-nw .passwd').toggleClass('hidden', nopw)
$('#sta-nw .nopasswd').toggleClass('hidden', !nopw) // (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')) $('#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) { if (status !== 200) {
// bad response // bad response
rescan(5000) // wait 5sm then retry rescan(5000) // wait 5s then retry
return return
} }
@ -52,7 +58,7 @@ const tr = require('./lang')
return 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) rescan(done ? 15000 : 1000)
if (!done) return // no redraw yet if (!done) return // no redraw yet
@ -65,9 +71,7 @@ const tr = require('./lang')
$('#ap-loader').toggleClass('hidden', done) $('#ap-loader').toggleClass('hidden', done)
// scan done // scan done
resp.result.APs.sort(function (a, b) { resp.result.APs.sort((a, b) => b.rssi - a.rssi).forEach(function (ap) {
return b.rssi - a.rssi
}).forEach(function (ap) {
ap.enc = parseInt(ap.enc) ap.enc = parseInt(ap.enc)
if (ap.enc > 4) return // hide unsupported auths if (ap.enc > 4) return // hide unsupported auths
@ -90,7 +94,7 @@ const tr = require('./lang')
$(inner).addClass('inner') $(inner).addClass('inner')
.htmlAppend(`<div class="rssi">${ap.rssi_perc}</div>`) .htmlAppend(`<div class="rssi">${ap.rssi_perc}</div>`)
.htmlAppend(`<div class="essid" title="${escapedSSID}">${escapedSSID}</div>`) .htmlAppend(`<div class="essid" title="${escapedSSID}">${escapedSSID}</div>`)
.htmlAppend(`<div class="auth">${authStr[ap.enc]}</div>`) .htmlAppend(`<div class="auth">${authTypes[ap.enc]}</div>`)
$item.on('click', function () { $item.on('click', function () {
let $th = $(this) let $th = $(this)
@ -164,4 +168,4 @@ const tr = require('./lang')
w.init = wifiInit w.init = wifiInit
w.startScanning = startScanning w.startScanning = startScanning
})(window.WiFi = {}) }

Loading…
Cancel
Save