123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- document.addEventListener('DOMContentLoaded', function() {
- /* add class='elem' elements to array 'el' */
- var el = [];
- (function() {
- x = document.getElementsByClassName('elem');
- for (i = 0; i < x.length; i++ ) {
- el_id = x[i].id;
- el.push(el_id);
- el[el_id] = document.getElementById(el_id);
- }
- })();
- el['uploadbtn'].addEventListener('change', function (e) {
- el['uploadfile'].value = this.value.replace(/^.*\\/, "");
- var len = el['uploadfile'].value.length - 7;
- el['uploadfile'].setAttribute('size', len);
- var len = el['uploadfile'].offsetWidth;
- el['uploadbtn'].style.width = len + "px";
- e.stopPropagation();
- });
- /* all submit buttons */
- var submitbtns = document.querySelectorAll('input[type="submit"]');
- for (var i=0; i < submitbtns.length; i++){
- submitbtns[i].addEventListener('click', function(e) {
- if (e.target.hasAttribute('data-wait')) {
- e.target.value = e.target.getAttribute('data-wait');
- } else e.target.value = 'Working, please wait..';
- /* e.target.form.submit(function () {
- e.target.disabled = true;
- }); */
- e.stopPropagation();
- });
- }
- el['wanconf'].addEventListener('change', function(e) {
- wanChange(e.target);
- e.stopPropagation();
- });
- el['wanssid'].addEventListener('change', function(e) {
- if (enc = document.getElementById(e.target.value).getAttribute('data-enc')) {
- el['wanenc'].value = enc;
- }
- e.stopPropagation();
- });
- el['wanssid'].addEventListener('focus', function(e) {
- if (e.target.length == 1) {
- iwScan(e.target);
- // e.target.blur();
- //getFirstchild(e.target).blur();
- //getFirstchild(e.target).style.display = 'none';
- } else {
- iwScan(e.target);
- }
- e.stopPropagation();
- });
- /* update WAN form based on what iface is chosen */
- function wanChange(e) {
- switch (e[e.selectedIndex].id) {
- case 'wlan':
- el['wanwifi'].setAttribute('class','show');
- iwScan(el['wanssid']);
- break;
- case 'dhcp':
- // wanaddr.setAttribute('class','hide');
- for (var i = 0; i < el['wanaddr'].children.length; i++) {
- el['wanaddr'].children[i].setAttribute('readonly', true);
- }
- break;
- case 'eth':
- el['wanwifi'].setAttribute('class','hide');
- break
- case 'stat':
- // wanaddr.setAttribute('class','show');
- for (var i = 0; i < el['wanaddr'].children.length; i++) {
- el['wanaddr'].children[i].removeAttribute('readonly');
- }
- break;
- }
- }
- /* get results from iwlist and show in the UI */
- function iwScan(e) {
- console.log('scanning wifi..');
- if (getFirstchild(e).id != 'scan') {
- scan_el = document.createElement('option');
- scan_el.selected = true;
- scan_el.disabled = true;
- scan_el.id = 'scan';
- e.insertBefore(scan_el, e.firstChild);
- } else scan_el = document.getElementById('scan');
- scan_el.textContent = 'scanning for networks..';
- scan_el.selected = true;
- ajaxReq('POST', '/admin/iwscan', 'null', function(xhr) {
- res = JSON.parse(xhr['response']);
- aps = res['results'].sort(compSort); /* get APs */
- aps_num = Object.keys(aps).length;
- function updateAp() {
- ap.setAttribute('data-bssid', aps[i]['bssid']);
- ap.setAttribute('data-quality', aps[i]['quality']);
- if (aps[i]['encryption']['enabled']) {
- // WPA[1,2] present
- if (aps[i]['encryption']['wpa']) {
- // choose last (highest) WPA version
- wpa = aps[i]['encryption']['wpa'][aps[i]['encryption']['wpa'].length - 1];
- //console.log(ap.id + ':' + 'wpa' + wpa);
- ap.setAttribute('data-enc', 'wpa' + wpa);
- // assume WEP
- } else {
- ap.setAttribute('data-enc', 'wep');
- }
- // Open AP
- } else {
- ap.setAttribute('data-enc', 'none');
- }
- }
- for (i = 0; i < aps_num; i++) {
- // check if AP is already in the DOM
- if (document.getElementById(aps[i]['ssid'])) {
- //console.log('found ' + aps[i]['ssid'] + ' entry');
- /* TODO: remove duplicate code */
- ap = document.getElementById(aps[i]['ssid']);
- //console.log('update: ');
- updateAp();
- } else {
- ap = document.createElement('option');
- ap.id = aps[i]['ssid'];
- //console.log('add: ');
- updateAp();
- ap.textContent = aps[i]['ssid'] ? aps[i]['ssid'] : '[hidden network]';
- e.appendChild(ap);
- }
- }
- scan_el.textContent = 'select a network..';
- if (! aps_num) {
- scan_el.textContent = 'no scan results..';
- console.log('no scan results');
- }
- });
- // e.blur();
- // just focusing someother element
- // document.getElementById('wansubmit').focus();
- return false;
- }
- /* v simple XHR */
- function ajaxReq(method, url, data, callback) {
- var xhr = new XMLHttpRequest();
- xhr.open(method, url, true);
- xhr.onerror = function() { clearInterval(uptime); console.log('network error, dying'); return; }
- xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
- xhr.onreadystatechange = function() {
- if (xhr.readyState == 4 && xhr.status == 200) {
- callback(xhr);
- }
- }
- xhr.send(data);
- }
-
- /*
- update uptime output
- we just run ahead
- */
- var uptime = (function uptimeUpdate() {
- function run() {
- ajaxReq('POST', '/admin/uptime', 'null', function(xhr) {
- if (xhr['response'] != '') {
- document.getElementById('uptime').textContent = xhr['response'];
- }
- });
- }
- return setInterval(run, 5000);
- })();
-
- /* sort by comparing a and b */
- function compSort(a,b) {
- if (a.quality < b.quality)
- return 1;
- if (a.quality > b.quality)
- return -1;
- return 0;
- }
- /* because firstChild returns whitespaces too */
- function getFirstchild(n) {
- x = n.firstChild;
- while (x.nodeType != 1) {
- x = x.nextSibling;
- }
- return x;
- }
- });
|