console.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //===== Fetching console text
  2. function fetchText(delay, repeat) {
  3. var el = $("#console");
  4. if (el.textEnd == undefined) {
  5. el.textEnd = 0;
  6. el.innerHTML = "";
  7. }
  8. window.setTimeout(function() {
  9. ajaxJson('GET', console_url + "?start=" + el.textEnd,
  10. function(resp) {
  11. var dly = updateText(resp);
  12. if (repeat) fetchText(dly, repeat);
  13. },
  14. function() { retryLoad(repeat); });
  15. }, delay);
  16. }
  17. function updateText(resp) {
  18. var el = $("#console");
  19. var delay = 3000;
  20. if (resp != null && resp.len > 0) {
  21. // console.log("updateText got", resp.len, "chars at", resp.start);
  22. var isScrolledToBottom = el.scrollHeight - el.clientHeight <= el.scrollTop + 1;
  23. //console.log("isScrolledToBottom="+isScrolledToBottom, "scrollHeight="+el.scrollHeight,
  24. // "clientHeight="+el.clientHeight, "scrollTop="+el.scrollTop,
  25. // "" + (el.scrollHeight - el.clientHeight) + "<=" + (el.scrollTop + 1));
  26. // append the text
  27. if (resp.start > el.textEnd) {
  28. el.innerHTML = el.innerHTML.concat("\r\n<missing lines\r\n");
  29. }
  30. el.innerHTML = el.innerHTML.concat(resp.text
  31. .replace(/&/g, '&amp;')
  32. .replace(/</g, '&lt;')
  33. .replace(/>/g, '&gt;')
  34. .replace(/"/g, '&quot;'));
  35. el.textEnd = resp.start + resp.len;
  36. delay = 500;
  37. // scroll to bottom
  38. if(isScrolledToBottom) el.scrollTop = el.scrollHeight - el.clientHeight;
  39. }
  40. return delay;
  41. }
  42. function retryLoad(repeat) {
  43. fetchText(1000, repeat);
  44. }
  45. //===== Text entry
  46. function consoleSendInit() {
  47. var sendHistory = $("#send-history");
  48. var inputText = $("#input-text");
  49. var inputAddCr = $("#input-add-cr");
  50. var inputAddLf = $("#input-add-lf");
  51. function findHistory(text) {
  52. for (var i = 0; i < sendHistory.children.length; i++) {
  53. if (text == sendHistory.children[i].value) {
  54. return i;
  55. }
  56. }
  57. return null;
  58. }
  59. function loadHistory(idx) {
  60. sendHistory.value = sendHistory.children[idx].value;
  61. inputText.value = sendHistory.children[idx].value;
  62. }
  63. function navHistory(rel) {
  64. var idx = findHistory(sendHistory.value) + rel;
  65. if (idx < 0) {
  66. idx = sendHistory.children.length - 1;
  67. }
  68. if (idx >= sendHistory.children.length) {
  69. idx = 0;
  70. }
  71. loadHistory(idx);
  72. }
  73. sendHistory.addEventListener("change", function(e) {
  74. inputText.value = sendHistory.value;
  75. });
  76. function pushHistory(text) {
  77. var idx = findHistory(text);
  78. if (idx !== null) {
  79. loadHistory(idx);
  80. return false;
  81. }
  82. var newOption = m('<option>'+
  83. (text
  84. .replace(/&/g, '&amp;')
  85. .replace(/</g, '&lt;')
  86. .replace(/>/g, '&gt;')
  87. .replace(/"/g, '&quot;'))
  88. +'</option>');
  89. newOption.value = text;
  90. sendHistory.appendChild(newOption);
  91. sendHistory.value = text;
  92. for (; sendHistory.children.length > 15; ) {
  93. sendHistory.removeChild(sendHistory.children[0]);
  94. }
  95. return true;
  96. }
  97. inputText.addEventListener("keydown", function(e) {
  98. switch (e.keyCode) {
  99. case 38: /* the up arrow key pressed */
  100. e.preventDefault();
  101. navHistory(-1);
  102. break;
  103. case 40: /* the down arrow key pressed */
  104. e.preventDefault();
  105. navHistory(+1);
  106. break;
  107. case 27: /* the escape key pressed */
  108. e.preventDefault();
  109. inputText.value = "";
  110. sendHistory.value = "";
  111. break;
  112. case 13: /* the enter key pressed */
  113. e.preventDefault();
  114. var text = inputText.value;
  115. if (inputAddCr.checked) text += '\r';
  116. if (inputAddLf.checked) text += '\n';
  117. pushHistory(inputText.value);
  118. inputText.value = "";
  119. ajaxSpin('POST', "/console/send?text=" + encodeURIComponent(text),
  120. function(resp) { showNotification("Text sent"); },
  121. function(s, st) { showWarning("Error sending text"); }
  122. );
  123. break;
  124. }
  125. });
  126. }
  127. //===== Log page
  128. function showDbgMode(mode) {
  129. var btns = $('.dbg-btn');
  130. for (var i=0; i < btns.length; i++) {
  131. if (btns[i].id === "dbg-"+mode)
  132. addClass(btns[i], "button-selected");
  133. else
  134. removeClass(btns[i], "button-selected");
  135. }
  136. }