function bool(x) { return (x === 1 || x === '1' || x === true || x === 'true'); } /** html entities */ function e(x) { return String(x) .replace(/&/g, '&') .replace(/"/g, '"') .replace(/'/g, ''') .replace(//g, '>'); } /** Returns true if argument is array [] */ function isArray(obj) { return Object.prototype.toString.call(obj) === '[object Array]'; } /** Returns true if argument is object {} */ function isObject(obj) { return Object.prototype.toString.call(obj) === '[object Object]'; } /** escape a string to have no special meaning in regex */ function regexEscape(s) { return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); } /** Convert RSSI 0-255 to % */ function rssiPerc(rssi) { var r = parseInt(rssi); if (r > -50) return 100; // 100% if (r < -100) return 0; // 0% return Math.round(2 * (r + 100)); // approximation } /** Perform a substitution in the given string. * * Arguments - array or list of replacements. * Arguments numeric keys will replace {0}, {1} etc. * Named keys also work, ie. {foo: "bar"} -> replaces {foo} with bar. * * Braces are added to keys if missing. * * @returns {String} result */ String.prototype.format = function () { var out = this; var repl = arguments; if (arguments.length == 1 && (isArray(arguments[0]) || isObject(arguments[0]))) { repl = arguments[0]; } for (var ph in repl) { if (repl.hasOwnProperty(ph)) { var ph_orig = ph; if (!ph.match(/^\{.*\}$/)) { ph = '{' + ph + '}'; } // replace all occurrences var pattern = new RegExp(regexEscape(ph), "g"); out = out.replace(pattern, repl[ph_orig]); } } return out; }; /** Module for toggling a modal overlay */ var modal = (function () { var modal = {}; modal.show = function (sel) { var $m = $(sel); $m.removeClass('hidden visible'); setTimeout(function () { $m.addClass('visible'); }, 1); }; modal.hide = function (sel) { var $m = $(sel); $m.removeClass('visible'); setTimeout(function () { $m.addClass('hidden'); }, 500); // transition time }; modal.init = function () { // close modal by click outside the dialog $('.Modal').on('click', function () { modal.hide(this); }); $('.Dialog').on('click', function (e) { e.stopImmediatePropagation(); }); // Hide all modals on esc $(window).on('keydown', function (e) { if (e.which == 27) { modal.hide('.Modal'); } }); }; return modal; })(); /** Wifi page */ var wifi = (function () { var wifi = {}; var authStr = ['Open', 'WEP', 'WPA', 'WPA2', 'WPA/WPA2']; /** Update display for received response */ function onScan(resp, status) { if (status != 200) { // bad response rescan(5000); // wait 5sm then retry return; } resp = JSON.parse(resp); var done = !bool(resp.result.inProgress) && (resp.result.APs.length > 0); rescan(done ? 15000 : 1000); if (!done) return; // no redraw yet // clear the AP list var $list = $('#ap-list'); // remove old APs $('.AP').remove(); $list.toggle(done); $('#ap-loader').toggle(!done); // scan done resp.result.APs .sort(function (a, b) { return b.rssi - a.rssi }) .forEach(function (ap) { ap.enc = parseInt(ap.enc); if (ap.enc > 4) return; // hide unsupported auths var item = document.createElement('div'); var $item = $(item) .data('ssid', ap.essid) .data('pwd', ap.enc != 0) .addClass('AP'); // mark current SSID if (ap.essid == wifi.current) { $item.addClass('selected'); } var inner = document.createElement('div'); var $inner = $(inner).addClass('inner') .htmlAppend('