admin.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. } else scan_el = document.getElementById('scan');
  86. scan_el.textContent = 'scanning for networks..';
  87. scan_el.selected = 1;
  88. ajaxReq('POST', '/admin/iwscan', 'null', function(xhr) {
  89. res = JSON.parse(xhr['response']);
  90. aps = res['results'].sort(compSort); /* get APs */
  91. aps_num = Object.keys(aps).length;
  92. for (i = 0; i < aps_num; i++) {
  93. // check if AP is already in the DOM
  94. if (document.getElementById(aps[i]['bssid'])) {
  95. //console.log('found ' + aps[i]['ssid'] + ' entry');
  96. /* TODO: update existing records */
  97. } else {
  98. ap = document.createElement('option');
  99. ap.id = aps[i]['bssid'];
  100. ap.setAttribute('data-quality', aps[i]['quality']);
  101. if (aps[i]['encryption']['enabled']) {
  102. ap.setAttribute('data-enc', 'wpa2');
  103. } else {
  104. ap.setAttribute('data-enc', 'false');
  105. }
  106. ap.textContent = aps[i]['ssid'] ? aps[i]['ssid'] : '[hidden network]';
  107. e.appendChild(ap);
  108. }
  109. }
  110. scan_el.textContent = 'select a network..';
  111. if (! aps_num) {
  112. scan_el.textContent = 'no scan results..';
  113. console.log('no scan results');
  114. }
  115. });
  116. e.blur();
  117. // just focusing someother element
  118. document.getElementById('wansubmit').focus();
  119. return
  120. }
  121. /* v simple XHR */
  122. function ajaxReq(method, url, data, callback) {
  123. var xhr = new XMLHttpRequest();
  124. xhr.open(method, url, true);
  125. xhr.onerror = function() { clearInterval(uptime); console.log('network error, dying'); return; }
  126. xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  127. xhr.onreadystatechange = function() {
  128. if (xhr.readyState == 4 && xhr.status == 200) {
  129. callback(xhr);
  130. }
  131. }
  132. xhr.send(data);
  133. }
  134. /*
  135. update uptime output
  136. we just run ahead
  137. */
  138. var uptime = (function uptimeUpdate() {
  139. function run() {
  140. ajaxReq('POST', '/admin/uptime', 'null', function(xhr) {
  141. if (xhr['response'] != '') {
  142. document.getElementById('uptime').textContent = xhr['response'];
  143. }
  144. });
  145. }
  146. return setInterval(run, 5000);
  147. })();
  148. /* sort by comparing a and b */
  149. function compSort(a,b) {
  150. if (a.quality < b.quality)
  151. return 1;
  152. if (a.quality > b.quality)
  153. return -1;
  154. return 0;
  155. }
  156. /* because firstChild returns whitespaces too */
  157. function getFirstchild(n) {
  158. x = n.firstChild;
  159. while (x.nodeType != 1) {
  160. x = x.nextSibling;
  161. }
  162. return x;
  163. }
  164. });