Kagera Web Interface
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kagera.js 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. function getRequest() {
  2. var req;
  3. if(window.XMLHttpRequest) {
  4. req = new XMLHttpRequest();
  5. } else {
  6. req = new ActiveXObject("Microsoft.XMLHTTP");
  7. }
  8. return req;
  9. }
  10. function initMenu() {
  11. this.menu = document.getElementById("mainmenu");
  12. this.submenus = this.menu.getElementsByTagName("div");
  13. this.oneSmOnly = 3;
  14. this.collapseMenu = function(submenu) {
  15. var minHeight = submenu.getElementsByTagName("span")[0].offsetHeight;
  16. var moveBy = Math.round(4 * submenu.getElementsByTagName("a").length);
  17. var mainInstance = this;
  18. var intId = setInterval(function() {
  19. var curHeight = submenu.offsetHeight;
  20. var newHeight = curHeight - moveBy;
  21. if(newHeight > minHeight) {
  22. submenu.style.height = newHeight + "px";
  23. } else {
  24. clearInterval(intId);
  25. submenu.style.height = "";
  26. submenu.className = "collapsed";
  27. mainInstance.memorize();
  28. }
  29. }, 30);
  30. };
  31. this.collapseOthers = function(submenu) {
  32. if(!multimenu) {
  33. for(var i = 0; i < this.submenus.length; i++) {
  34. if(this.submenus[i] != submenu && this.submenus[i].className != "collapsed") {
  35. this.collapseMenu(this.submenus[i]);
  36. }
  37. }
  38. }
  39. };
  40. this.expandMenu = function(submenu) {
  41. var fullHeight = submenu.getElementsByTagName("span")[0].offsetHeight;
  42. var links = submenu.getElementsByTagName("a");
  43. for(var i = 0; i < links.length; i++) {
  44. fullHeight += links[i].offsetHeight;
  45. }
  46. var moveBy = Math.round(4 * links.length);
  47. var mainInstance = this;
  48. var intId = setInterval(function() {
  49. var curHeight = submenu.offsetHeight;
  50. var newHeight = curHeight + moveBy;
  51. if(newHeight < fullHeight) {
  52. submenu.style.height = newHeight + "px";
  53. } else {
  54. clearInterval(intId);
  55. submenu.style.height= "";
  56. submenu.className = "navbox";
  57. mainInstance.memorize();
  58. }
  59. }, 30);
  60. this.collapseOthers(submenu);
  61. };
  62. this.init = function() {
  63. var mainInstance = this;
  64. for(var i = 0; i < this.submenus.length; i++) {
  65. this.submenus[i].getElementsByTagName("span")[0].onclick = function() {
  66. if(this.parentNode.className == "collapsed") {
  67. mainInstance.expandMenu(this.parentNode);
  68. } else {
  69. mainInstance.collapseMenu(this.parentNode);
  70. }
  71. };
  72. }
  73. var links = this.menu.getElementsByTagName("a");
  74. links[0].className = "active";
  75. for(var i = 0; i < links.length; i++) {
  76. if(links[i].href == document.location.href) {
  77. links[0].className = "";
  78. links[i].className = "active";
  79. break;
  80. }
  81. }
  82. var regex = new RegExp("kagera_" + encodeURIComponent("mainmenu") + "=([01]+)");
  83. var match = regex.exec(document.cookie);
  84. if(match) {
  85. var states = match[1].split("");
  86. for(var i = 0; i < states.length; i++) {
  87. this.submenus[i].className = (states[i] == 0 ? "collapsed" : "navbox");
  88. }
  89. } else if(!multimenu) {
  90. this.collapseOthers(this.submenus[0]);
  91. }
  92. };
  93. this.memorize = function() {
  94. var states = new Array();
  95. for(var i = 0; i < this.submenus.length; i++) {
  96. states.push(this.submenus[i].className == "collapsed" ? 0 : 1);
  97. var d = new Date();
  98. d.setTime(d.getTime() + (30 * 24 * 60 * 60 * 1000));
  99. document.cookie = "kagera_" + encodeURIComponent("mainmenu") + "=" + states.join("") + "; expires=" + d.toGMTString() + "; path=/";
  100. }
  101. };
  102. }
  103. function initTips() {
  104. var help = document.getElementById("help")
  105. if(help) {
  106. var abbrs = document.getElementsByTagName("abbr");
  107. var tips = help.getElementsByTagName("div");
  108. var mouseX, mouseY;
  109. document.onmousemove = function(e) {
  110. mouseX = (window.Event) ? e.pageX : event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
  111. mouseY = (window.Event) ? e.pageY : event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
  112. };
  113. for(var i = 0; i < abbrs.length; i++) {
  114. abbrs[i].onclick = function() {
  115. for(var a = 0; a < tips.length; a++) {
  116. if("s" + tips[a].id == this.id) {
  117. tips[a].style.left = mouseX + "px";
  118. tips[a].style.top = (mouseY - (tips[a].offsetHeight + 3)) + "px";
  119. tips[a].style.visibility = "visible";
  120. break;
  121. }
  122. }
  123. };
  124. abbrs[i].onmouseout = function() {
  125. for(var a = 0; a < tips.length; a++) {
  126. if("s" + tips[a].id == this.id) {
  127. tips[a].style.visibility = "hidden";
  128. break;
  129. }
  130. }
  131. };
  132. }
  133. }
  134. }
  135. function logout() {
  136. window.location.href="index.sh?option=logout";
  137. }
  138. function reboot(ask) {
  139. var boot = true;
  140. if(ask) {
  141. var answer = confirm(sysmsg[6]);
  142. if(!answer) {
  143. boot = false;
  144. }
  145. }
  146. if(boot) {
  147. toggleMsg(sysmsg[5]);
  148. statefunc = function() {
  149. window.location.href="index.sh?option=overview";
  150. }
  151. testcon = function() {
  152. runRequest("GET", document.location.href, "&refresh=1", statefunc, false);
  153. }
  154. runCommand("reboot", statefunc, true);
  155. setInterval("testcon()", 5000);
  156. }
  157. }
  158. function refreshData(func) {
  159. statefunc = function(req) {
  160. document.getElementById("content").innerHTML = req.responseText;
  161. var target = document.getElementById("content");
  162. var scripts = target.getElementsByTagName("script");
  163. for (var i = 0; i < scripts.length; i++) {
  164. eval(scripts[i].innerHTML);
  165. }
  166. if(typeof func == "function") {
  167. func();
  168. }
  169. }
  170. runRequest("GET", document.location.href, "&refresh=1", statefunc, false);
  171. }
  172. function runCommand(params, statefunc, noreload) {
  173. runRequest("POST", document.location.href + "&refresh=1&runcommand=1", "commands=" + params, statefunc, noreload);
  174. }
  175. function runRequest(method, url, params, statefunc, noreload) {
  176. var req = getRequest();
  177. if(req) {
  178. req.onreadystatechange = function() {
  179. if(req.readyState == 4 && req.status == 200) {
  180. if(req.responseText != "") {
  181. statefunc(req);
  182. } else if(!noreload) {
  183. location.reload(true);
  184. }
  185. }
  186. }
  187. if(method == "POST") {
  188. req.open("POST", url, true);
  189. req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  190. req.send(params);
  191. } else {
  192. req.open("GET", url + params, true);
  193. req.send();
  194. }
  195. }
  196. return req;
  197. }
  198. function switchView(outid, swval) {
  199. sw = document.getElementById(outid);
  200. for(var i = 0; i < sw.children.length; i++) {
  201. if(sw.children[i].id == swval) {
  202. sw.children[i].style.display = "block";
  203. } else {
  204. sw.children[i].style.display = "none";
  205. }
  206. }
  207. }
  208. function toggleMsg(msg) {
  209. dimbox = document.getElementById("dim");
  210. msgbox = document.getElementById("msg");
  211. if(msg) {
  212. document.body.style.overflow = "hidden";
  213. msgbox.innerHTML = msgtxt + "&nbsp;&nbsp;" + msg;
  214. dimbox.style.display = "inline";
  215. msgbox.style.display = "inline";
  216. } else {
  217. msgbox.style.display = "none";
  218. dimbox.style.display = "none";
  219. document.body.style.overflow = "auto";
  220. }
  221. }
  222. window.onload = function() {
  223. msgtxt = document.getElementById("msg").innerHTML;
  224. mainmenu = new initMenu();
  225. mainmenu.init();
  226. initTips();
  227. if(typeof initModule == "function") {
  228. initModule();
  229. }
  230. }