admin.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. document.addEventListener('DOMContentLoaded', function() {
  2. /* add class='elem' elements to array 'el' */
  3. var el = [];
  4. (function() {
  5. x = document.getElementsByClassName('elem');
  6. for (i = 0; i < x.length; i++ ) {
  7. el_id = x[i].id;
  8. el.push(el_id);
  9. el[el_id] = document.getElementById(el_id);
  10. }
  11. })();
  12. el['uploadbtn'].addEventListener('change', function (e) {
  13. el['uploadfile'].value = this.value.replace(/^.*\\/, "");
  14. var len = el['uploadfile'].value.length - 7;
  15. el['uploadfile'].setAttribute('size', len);
  16. var len = el['uploadfile'].offsetWidth;
  17. el['uploadbtn'].style.width = len + "px";
  18. e.stopPropagation();
  19. });
  20. /* all submit buttons */
  21. var submitbtns = document.querySelectorAll('input[type="submit"]');
  22. for (var i=0; i < submitbtns.length; i++){
  23. submitbtns[i].addEventListener('click', function(e) {
  24. if (e.target.hasAttribute('data-wait')) {
  25. e.target.value = e.target.getAttribute('data-wait');
  26. } else e.target.value = 'Working, please wait..';
  27. e.stopPropagation();
  28. }, false);
  29. }
  30. el['wanconf'].addEventListener('change', function(e) {
  31. wanChange(e.target);
  32. e.stopPropagation();
  33. });
  34. el['wanssid'].addEventListener('change', function(e) {
  35. if (enc = document.getElementById(e.target.value).getAttribute('data-enc')) {
  36. el['wanenc'].value = enc;
  37. }
  38. e.stopPropagation();
  39. });
  40. el['wanssid'].addEventListener('focus', function(e) {
  41. if (e.target.length == 1) {
  42. iwScan(e.target);
  43. // e.target.blur();
  44. //getFirstchild(e.target).blur();
  45. //getFirstchild(e.target).style.display = 'none';
  46. } else {
  47. iwScan(e.target);
  48. }
  49. e.stopPropagation();
  50. });
  51. /* update WAN form based on what iface is chosen */
  52. function wanChange(e) {
  53. switch (e[e.selectedIndex].id) {
  54. case 'wlan':
  55. el['wanwifi'].setAttribute('class','show');
  56. iwScan(el['wanssid']);
  57. break;
  58. case 'dhcp':
  59. // wanaddr.setAttribute('class','hide');
  60. for (var i = 0; i < el['wanaddr'].children.length; i++) {
  61. el['wanaddr'].children[i].setAttribute('readonly', true);
  62. }
  63. break;
  64. case 'eth':
  65. el['wanwifi'].setAttribute('class','hide');
  66. break
  67. case 'stat':
  68. // wanaddr.setAttribute('class','show');
  69. for (var i = 0; i < el['wanaddr'].children.length; i++) {
  70. el['wanaddr'].children[i].removeAttribute('readonly');
  71. }
  72. break;
  73. }
  74. }
  75. /* get results from iwlist and show in the UI */
  76. function iwScan(e) {
  77. console.log('scanning wifi..');
  78. if (getFirstchild(e).id != 'scan') {
  79. scan_el = document.createElement('option');
  80. scan_el.selected = true;
  81. scan_el.disabled = true;
  82. scan_el.id = 'scan';
  83. e.insertBefore(scan_el, e.firstChild);
  84. } else scan_el = document.getElementById('scan');
  85. scan_el.textContent = 'scanning for networks..';
  86. scan_el.selected = true;
  87. ajaxReq('POST', '/admin/iwscan', 'null', function(xhr) {
  88. res = JSON.parse(xhr['response']);
  89. aps = res['results'].sort(compSort); /* get APs */
  90. aps_num = Object.keys(aps).length;
  91. function updateAp() {
  92. ap.setAttribute('data-bssid', aps[i]['bssid']);
  93. ap.setAttribute('data-quality', aps[i]['quality']);
  94. if (aps[i]['encryption']['enabled']) {
  95. // WPA[1,2] present
  96. if (aps[i]['encryption']['wpa']) {
  97. // choose last (highest) WPA version
  98. wpa = aps[i]['encryption']['wpa'][aps[i]['encryption']['wpa'].length - 1];
  99. //console.log(ap.id + ':' + 'wpa' + wpa);
  100. ap.setAttribute('data-enc', 'wpa' + wpa);
  101. // assume WEP
  102. } else {
  103. ap.setAttribute('data-enc', 'wep');
  104. }
  105. // Open AP
  106. } else {
  107. ap.setAttribute('data-enc', 'none');
  108. }
  109. }
  110. for (i = 0; i < aps_num; i++) {
  111. // check if AP is already in the DOM
  112. if (document.getElementById(aps[i]['ssid'])) {
  113. //console.log('found ' + aps[i]['ssid'] + ' entry');
  114. /* TODO: remove duplicate code */
  115. ap = document.getElementById(aps[i]['ssid']);
  116. //console.log('update: ');
  117. updateAp();
  118. } else {
  119. ap = document.createElement('option');
  120. ap.id = aps[i]['ssid'];
  121. //console.log('add: ');
  122. updateAp();
  123. ap.textContent = aps[i]['ssid'] ? aps[i]['ssid'] : '[hidden network]';
  124. e.appendChild(ap);
  125. }
  126. }
  127. scan_el.textContent = 'select a network..';
  128. if (! aps_num) {
  129. scan_el.textContent = 'no scan results..';
  130. console.log('no scan results');
  131. }
  132. });
  133. // e.blur();
  134. // just focusing someother element
  135. // document.getElementById('wansubmit').focus();
  136. return false;
  137. }
  138. /* v simple XHR */
  139. function ajaxReq(method, url, data, callback) {
  140. var xhr = new XMLHttpRequest();
  141. xhr.open(method, url, true);
  142. xhr.onerror = function() { clearInterval(uptime); console.log('network error, dying'); return; }
  143. xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  144. xhr.onreadystatechange = function() {
  145. if (xhr.readyState == 4 && xhr.status == 200) {
  146. callback(xhr);
  147. }
  148. }
  149. xhr.send(data);
  150. }
  151. /*
  152. update uptime output
  153. we just run ahead
  154. */
  155. var uptime = (function uptimeUpdate() {
  156. function run() {
  157. ajaxReq('POST', '/admin/uptime', 'null', function(xhr) {
  158. if (xhr['response'] != '') {
  159. document.getElementById('uptime').textContent = xhr['response'];
  160. }
  161. });
  162. }
  163. return setInterval(run, 5000);
  164. })();
  165. /* sort by comparing a and b */
  166. function compSort(a,b) {
  167. if (a.quality < b.quality)
  168. return 1;
  169. if (a.quality > b.quality)
  170. return -1;
  171. return 0;
  172. }
  173. /* because firstChild returns whitespaces too */
  174. function getFirstchild(n) {
  175. x = n.firstChild;
  176. while (x.nodeType != 1) {
  177. x = x.nextSibling;
  178. }
  179. return x;
  180. }
  181. });