admin.js 5.7 KB

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