admin.js 5.2 KB

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