Explorar o código

small fixes in admin UI

Danja Vasiliev %!s(int64=9) %!d(string=hai) anos
pai
achega
a730a2dfe3

+ 3 - 3
openwrt/common/opt/lib/cgi/admin2.cgi

@@ -586,7 +586,7 @@ wanuptime=${wan[4]}
 wanssid=$(doUci get wanssid)
 wankey=$(doUci get wankey)
 
-#logThis $stor
+logThis $wanifname
 %>
 
 <body>
@@ -607,11 +607,11 @@ wankey=$(doUci get wankey)
   <option value='eth0' id='eth' <% ( [[ $wanifname =~ ('eth') ]] && _echo 'selected' ) %> >Wired (WAN port)</option>
   <option value='wlan1' id='wlan' <% ( [[ $wanifname =~ ('wlan') ]] && _echo 'selected' ) %> >Wireless (WiFi)</option>
   </select>
-  <fieldset id='wanwifi' class='elem' <% ( [[ $wanifname =~ ('wlan') ]] && _echo "class='show'" || _echo "class='hide'" ) %>>
+  <fieldset id='wanwifi' <% ( [[ $wanifname =~ ('wlan') ]] && _echo "class='show elem'" || _echo "class='hide elem'" ) %>>
     
   <select name='wanssid' id='wanssid' class='elem' style='display:block'>
   <% if [[ -z $wanssid ]]; then
-    _echo '<option disabled>choose network..</option>'
+    _echo "<option id='scan' disabled>select a network..</option>"
   else
     _echo "<option id=$wanssid selected>$wanssid</option>"
   fi %>

+ 58 - 30
openwrt/common/opt/lib/resources/admin/admin.js

@@ -1,20 +1,31 @@
 document.addEventListener('DOMContentLoaded', function() {
 
-/* make wanted 'elem' class elements to vars */
+/* make wanted 'elem' class elements to array or vars */
+var el = [];
 (function() {
   x = document.getElementsByClassName('elem');
   for (i = 0; i < x.length; i++ ) {
-    y = x[i].id;
-    y = document.getElementById(x[i].id);
-}
+    el_id = x[i].id;
+    el.push(el_id);
+    el[el_id] = document.getElementById(el_id);
+  }
 })();
 
-uploadbtn.addEventListener('change', function () {
-  uploadfile.value = this.value.replace(/^.*\\/, "");
-  var len = uploadfile.value.length - 7;
-  uploadfile.setAttribute('size', len);
-  var len = uploadfile.offsetWidth;
-  uploadbtn.style.width = len + "px";
+/*
+var uploadbtn = document.getElementById('uploadbtn');
+var uploadfile = document.getElementById('uploadfile');
+var wanconf = document.getElementById('wanconf');
+var wanssid = document.getElementById('wanssid');
+var wanwifi = document.getElementById('wanwifi');
+var wanaddr = document.getElementById('wanaddr');
+*/
+
+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();
 });
 
@@ -29,13 +40,20 @@ for (var i=0; i < submitbtns.length; i++){
   }, false);
 }
 
-wanconf.addEventListener('change', function(e) { 
+el['wanconf'].addEventListener('change', function(e) { 
   wanChange(e.target)
   e.stopPropagation();
 });
 
-wanssid.addEventListener('focus', function(e) {
-  iwScan(e.target);
+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();
 });
 
@@ -43,22 +61,22 @@ wanssid.addEventListener('focus', function(e) {
 function wanChange(e) {
   switch (e[e.selectedIndex].id) {
     case 'wlan':
-      wanwifi.setAttribute('class','show');
-      iwScan(wanssid);
+      el['wanwifi'].setAttribute('class','show');
+      //iwScan(el['wanssid']);
       break;
     case 'dhcp':
     //  wanaddr.setAttribute('class','hide');
-      for (var i = 0; i < wanaddr.children.length; i++) {
-        wanaddr.children[i].setAttribute('readonly', true);
+      for (var i = 0; i < el['wanaddr'].children.length; i++) {
+        el['wanaddr'].children[i].setAttribute('readonly', true);
       }
       break;
     case 'eth':
-      wanwifi.setAttribute('class','hide');
+      el['wanwifi'].setAttribute('class','hide');
       break
     case 'stat':
     //  wanaddr.setAttribute('class','show');
-      for (var i = 0; i < wanaddr.children.length; i++) {
-        wanaddr.children[i].removeAttribute('readonly');
+      for (var i = 0; i < el['wanaddr'].children.length; i++) {
+        el['wanaddr'].children[i].removeAttribute('readonly');
       }
     break;
   }
@@ -67,34 +85,33 @@ function wanChange(e) {
 /* get results from iwlist and show in the UI */
 function iwScan(e) {
   console.log('scanning wifi..');
-  var scan_el;
-  if (e.firstChild != document.getElementById('scan')) {
+  if (getFirstchild(e).id != 'scan') {
     scan_el = document.createElement('option');
     scan_el.selected = true;
     scan_el.disabled = true;
-    //scan_el.style.visibility = 'hidden';
     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 = 1;
   ajaxReq('POST', '/admin/iwscan', 'null', function(xhr) {
-    var res = JSON.parse(xhr['response']);
-    var aps = res['results'].sort(compSort); /* get APs */
+    res = JSON.parse(xhr['response']);
+    aps = res['results'].sort(compSort); /* get APs */
     aps_num = Object.keys(aps).length;
     for (i = 0; i < aps_num; i++) {
-      if (document.getElementById(aps[i]['ssid'])) {
+      // check if AP is already in the DOM
+      if (document.getElementById(aps[i]['bssid'])) {
         //console.log('found ' + aps[i]['ssid'] + ' entry');
         /* TODO: update existing records */
       } else {
         ap = document.createElement('option');
-        ap.id = aps[i]['ssid'];
+        ap.id = aps[i]['bssid'];
         ap.setAttribute('data-quality',  aps[i]['quality']);
         if (aps[i]['encryption']['enabled']) {
           ap.setAttribute('data-enc', 'wpa2');
         } else {
           ap.setAttribute('data-enc', 'false');
         }
-        ap.innerHTML = aps[i]['ssid'];
+        ap.textContent = aps[i]['ssid'] ? aps[i]['ssid'] : '[hidden network]';
         e.appendChild(ap);
       }
     }
@@ -104,7 +121,9 @@ function iwScan(e) {
       console.log('no scan results');
     }
   });
-  scan_el.selected = 1;
+  e.blur();
+  // just focusing someother element 
+  document.getElementById('wansubmit').focus();
   return
 }
 
@@ -146,4 +165,13 @@ function compSort(a,b) {
   return 0;
 }
 
+/* because firstChild returns whitespaces too */
+function getFirstchild(n) {
+  x = n.firstChild;
+  while (x.nodeType != 1) {
+    x = x.nextSibling;
+  }
+  return x;
+}
+
 });