domConsole.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. import { LocalizedStrings } from "./../services/localizedStringsService";
  2. import * as Utils from "./../util/utils";
  3. import { Config } from "./../util/config";
  4. export class DOMConsole {
  5. static get BASH_TEMPLATE () {
  6. return `
  7. <div class="bash-title">
  8. <i id="ivprog-console-clearbtn" class="icon eraser" style="float:left;padding-left: 5px"></i>
  9. <span>Terminal</span>
  10. <i id="ivprog-console-showbtn" class="icon window maximize outline" style="float:right"></i>
  11. <i id="ivprog-console-hidebtn" class="icon window minimize outline" style="float:right"></i>
  12. </div>
  13. <div id='ivprog-term' class="bash-body"></div>`;
  14. }
  15. static get INPUT_CARET_TEMPLATE () {
  16. return `
  17. <div id="cmd">
  18. <span></span>
  19. <div id="cursor"></div>
  20. </div>`;
  21. }
  22. static get USER () {
  23. return 0;
  24. }
  25. static get INFO () {
  26. return 1;
  27. }
  28. static get ERR () {
  29. return 2;
  30. }
  31. static get INPUT () {
  32. return 3;
  33. }
  34. constructor (elementID, disableMarginTop = false) {
  35. this.disableMarginTop = disableMarginTop;
  36. this.input = null;
  37. this.cursorInterval = null;
  38. this.idleInterval = null;
  39. this.inputDiv = null;
  40. this.inputCMD = null;
  41. this.inputSpan = null;
  42. this.cursorRef = null;
  43. this.needInput = false;
  44. this.clearBtn = null;
  45. this.hideBtn = null;
  46. this.showBtn = null;
  47. this.termDiv = null;
  48. this.anyKey = false;
  49. let actualID = elementID;
  50. if (elementID[0] === "#") {
  51. actualID = elementID.substring(1);
  52. }
  53. this.parent = document.getElementById(actualID);
  54. this.setup();
  55. this.inputListeners = [];
  56. this.hideInput();
  57. this.pending_writes = [];
  58. this.last_clear = -1;
  59. }
  60. setup () {
  61. this._setupDom();
  62. this._setupEvents();
  63. }
  64. _setupEvents () {
  65. this.input.addEventListener("keydown", this.registerInput.bind(this));
  66. this.clearBtn.addEventListener("click", this.clearBtnClick.bind(this));
  67. this.hideBtn.addEventListener("click", this.hideBtnClick.bind(this));
  68. this.showBtn.addEventListener("click", this.showBtnClick.bind(this));
  69. }
  70. registerInput (event) {
  71. if (!this.needInput) {
  72. return;
  73. }
  74. const keyCode = event.which;
  75. if (keyCode === 13 || this.anyKey) {
  76. if (this.idleInterval != null) {
  77. clearInterval(this.idleInterval);
  78. this.idleInterval = null;
  79. }
  80. let text = this.input.value;
  81. text = text.replace("[\n\r]+", "");
  82. this.notifyListeners(text);
  83. this._appendUserInput(text);
  84. this.input.value = "";
  85. this.inputSpan.innerHTML = "";
  86. this.currentLine = null;
  87. }
  88. }
  89. _setupDom () {
  90. const bashNode = document.createElement("div");
  91. bashNode.classList.add("bash");
  92. bashNode.innerHTML = DOMConsole.BASH_TEMPLATE;
  93. this.termDiv = bashNode.querySelector("#ivprog-term");
  94. this.termDiv.classList.add("ivprog-term-div");
  95. this.inputDiv = document.createElement("div");
  96. this.inputDiv.id = "ivprog-terminal-inputdiv";
  97. this.inputDiv.innerHTML = DOMConsole.INPUT_CARET_TEMPLATE;
  98. this.input = document.createElement("input");
  99. this.input.setAttribute("name", "command");
  100. this.input.setAttribute("value", "");
  101. this.input.setAttribute("type", "text");
  102. this.inputDiv.append(this.input);
  103. this.termDiv.append(this.inputDiv);
  104. bashNode.append(this.termDiv);
  105. this.parent.append(bashNode);
  106. this.inputCMD = this.inputDiv.querySelector("#cmd");
  107. this.cursorRef = this.inputCMD.querySelector("#cursor");
  108. this.inputSpan = this.inputCMD.querySelector("span");
  109. this.clearBtn = bashNode.querySelector("#ivprog-console-clearbtn");
  110. this.hideBtn = bashNode.querySelector("#ivprog-console-hidebtn");
  111. this.showBtn = bashNode.querySelector("#ivprog-console-showbtn");
  112. this._setupCursor();
  113. //Jquery tooltips....
  114. window
  115. .$(this.clearBtn)
  116. .popup({ content: LocalizedStrings.getUI("tooltip_terminal_clear") });
  117. window
  118. .$(this.showBtn)
  119. .popup({ content: LocalizedStrings.getUI("tooltip_terminal_show") });
  120. window
  121. .$(this.hideBtn)
  122. .popup({ content: LocalizedStrings.getUI("tooltip_terminal_hide") });
  123. }
  124. _setupCursor () {
  125. this.inputCMD.addEventListener("click", this.blinkCaretAndFocus.bind(this));
  126. //this.inputCMD.click();
  127. this.input.addEventListener("keyup", this.updateSpanText.bind(this));
  128. this.input.addEventListener("blur", this.stopBlinkCaret.bind(this));
  129. }
  130. blinkCaretAndFocus () {
  131. if (this.cursorInterval != null) {
  132. return;
  133. }
  134. this.input.focus();
  135. this.cursorInterval = window.setInterval(() => {
  136. if (this.cursorRef.style.visibility === "visible") {
  137. this.cursorRef.style.visibility = "hidden";
  138. } else {
  139. this.cursorRef.style.visibility = "visible";
  140. }
  141. }, 500);
  142. }
  143. updateSpanText () {
  144. this.inputSpan.innerHTML = this.input.value;
  145. if (this.idleInterval != null) window.clearInterval(this.idleInterval);
  146. this.scheduleNotify();
  147. }
  148. stopBlinkCaret () {
  149. clearInterval(this.cursorInterval);
  150. this.cursorInterval = null;
  151. this.cursorRef.style.visibility = "visible";
  152. }
  153. notifyListeners (text) {
  154. this.inputListeners.forEach((resolve) => resolve(text));
  155. this.inputListeners.splice(0, this.inputListeners.length);
  156. this.hideInput();
  157. this.anyKey = false;
  158. }
  159. writeRawHTML (text, channel) {
  160. this._appendTextLn(text, channel, false);
  161. }
  162. write (text, newLine = false) {
  163. this._appendText(text, DOMConsole.USER, newLine);
  164. }
  165. info (text) {
  166. this._appendTextLn(text, DOMConsole.INFO);
  167. }
  168. err (text) {
  169. this._appendTextLn(text, DOMConsole.ERR);
  170. }
  171. async _appendText (text, type, newLine = false) {
  172. console.debug("Caling appendText");
  173. const write_time = Date.now();
  174. this.pending_writes.push(0);
  175. await Utils.sleep(5);
  176. this.pending_writes.pop();
  177. if (this.last_clear >= write_time) {
  178. return;
  179. }
  180. if (this.currentLine == null) {
  181. const divClass = this.getClassForType(type);
  182. const textDiv = document.createElement("div");
  183. textDiv.classList.add(divClass);
  184. this.termDiv.insertBefore(textDiv, this.inputDiv);
  185. this.currentLine = textDiv;
  186. }
  187. this.currentLine.innerHTML += this.getOutputText(text);
  188. if (newLine) {
  189. console.debug("append newline");
  190. this.currentLine = null;
  191. }
  192. this.scrollTerm();
  193. }
  194. async _appendTextLn (text, type, filter = true) {
  195. const write_time = Date.now();
  196. this.pending_writes.push(0);
  197. await Utils.sleep(5);
  198. this.pending_writes.pop();
  199. if (this.last_clear >= write_time) {
  200. return;
  201. }
  202. const divClass = this.getClassForType(type);
  203. const textDiv = document.createElement("div");
  204. textDiv.classList.add(divClass);
  205. if (filter) textDiv.innerHTML = this.getOutputText(text);
  206. else textDiv.innerHTML = `<span>${text}</span>`;
  207. this.termDiv.insertBefore(textDiv, this.inputDiv);
  208. this.currentLine = null;
  209. this.scrollTerm();
  210. }
  211. async _appendUserInput (text) {
  212. const write_time = Date.now();
  213. this.pending_writes.push(0);
  214. await Utils.sleep(5);
  215. this.pending_writes.pop();
  216. if (this.last_clear >= write_time) {
  217. return;
  218. }
  219. const divClass = this.getClassForType(DOMConsole.INPUT);
  220. const textDiv = document.createElement("div");
  221. textDiv.innerHTML = this.getUserInputText(text);
  222. textDiv.classList.add(divClass);
  223. this.termDiv.insertBefore(textDiv, this.inputDiv);
  224. this.currentLine = null;
  225. this.scrollTerm();
  226. }
  227. getOutputText (text) {
  228. text = text.replace(/\s/g, "&#160;");
  229. return `<span>${text}</span>`;
  230. }
  231. getUserInputText (text) {
  232. if (text.trim().length == 0) {
  233. text = "&nbsp;";
  234. }
  235. return `<i class="icon keyboard outline" style="float:left"></i><span>${text}</span>`;
  236. }
  237. scrollTerm () {
  238. //scrollIt(this.inputDiv.previousSibling,200);
  239. this.termDiv.scrollTop = this.termDiv.scrollHeight;
  240. }
  241. focus () {
  242. this.termDiv.style.display = "block";
  243. // Is in draggable mode?
  244. if (!this.disableMarginTop && this.parent.style.top.length == 0) {
  245. this.parent.style.marginTop = "-160px";
  246. }
  247. if (this.needInput) {
  248. this.showInput();
  249. this.scheduleNotify();
  250. }
  251. if (!Utils.isElementInViewport(this.termDiv))
  252. this.termDiv.scrollIntoView(false);
  253. this.scrollTerm();
  254. }
  255. hide () {
  256. if (this.needInput) {
  257. clearInterval(this.idleInterval);
  258. this.hideInput();
  259. this.needInput = true;
  260. }
  261. // Is in draggable mode?
  262. if (!this.disableMarginTop && this.parent.style.top.length == 0) {
  263. this.parent.style.marginTop = "0";
  264. }
  265. this.termDiv.style.display = "none";
  266. }
  267. getClassForType (type) {
  268. switch (type) {
  269. case DOMConsole.INPUT:
  270. return "ivprog-term-userInput";
  271. case DOMConsole.USER:
  272. return "ivprog-term-userText";
  273. case DOMConsole.INFO:
  274. return "ivprog-term-info";
  275. case DOMConsole.ERR:
  276. return "ivprog-term-error";
  277. }
  278. }
  279. dispose () {
  280. this.input.removeEventListener("keyup", this.updateSpanText.bind(this));
  281. this.input.removeEventListener("blur", this.stopBlinkCaret.bind(this));
  282. this.input.removeEventListener("keydown", this.registerInput.bind(this));
  283. this.inputCMD.removeEventListener(
  284. "click",
  285. this.blinkCaretAndFocus.bind(this)
  286. );
  287. this.clearBtn.removeEventListener("click", this.clearBtnClick.bind(this));
  288. this.hideBtn.removeEventListener("click", this.hideBtnClick.bind(this));
  289. this.showBtn.removeEventListener("click", this.showBtnClick.bind(this));
  290. this.input = null;
  291. this.inputCMD = null;
  292. this.inputDiv = null;
  293. this.termDiv = null;
  294. this.inputSpan = null;
  295. this.cursorRef = null;
  296. this.clearBtn = null;
  297. this.hideBtn = null;
  298. this.showBtn = null;
  299. this.currentLine = null;
  300. const cNode = this.parent.cloneNode(false);
  301. this.parent.parentNode.replaceChild(cNode, this.parent);
  302. if (this.cursorInterval != null) {
  303. clearInterval(this.cursorInterval);
  304. }
  305. if (this.idleInterval != null) {
  306. clearInterval(this.idleInterval);
  307. }
  308. }
  309. showInput () {
  310. this.needInput = true;
  311. this.inputDiv.style.display = "block";
  312. this.inputCMD.click();
  313. //this.inputCMD.scrollIntoView();
  314. this.scrollTerm();
  315. }
  316. hideInput () {
  317. this.needInput = false;
  318. this.inputDiv.style.display = " none";
  319. clearInterval(this.cursorInterval);
  320. this.cursorInterval = null;
  321. }
  322. requestInput (anyKey = false) {
  323. const promise = new Promise((resolve, _) => {
  324. this.inputListeners.push(resolve);
  325. this.anyKey = anyKey;
  326. if (this.idleInterval == null) this.scheduleNotify();
  327. this.showInput();
  328. });
  329. return promise;
  330. }
  331. sendOutput (text) {
  332. // console.debug(text);
  333. let output = "" + text;
  334. if (output.indexOf("\n") !== -1) {
  335. // console.debug("newline");
  336. const outputList = output.split("\n");
  337. let i = 0;
  338. for (; i < outputList.length - 1; i += 1) {
  339. // console.debug("newline write");
  340. let t = outputList[i];
  341. t = t.replace(/\t/g, "&#x0020;&#x0020;");
  342. t = t.replace(/\s/g, "&#x0020;");
  343. if (t.length == 0) {
  344. // t = "&nbsp;";
  345. // console.debug('Empty string');
  346. this.currentLine = null;
  347. } else this.write(t, true);
  348. }
  349. let t = outputList[i];
  350. t = t.replace(/\t/g, "&#x0020;&#x0020;");
  351. t = t.replace(/\s/g, "&#x0020;");
  352. if (t.length != 0) this.write(t);
  353. } else {
  354. // console.debug("no newline");
  355. output = output.replace(/\t/g, "&#x0020;&#x0020;");
  356. output = output.replace(/\s/g, "&#x0020;");
  357. this.write(output);
  358. }
  359. }
  360. clearPendingWrites () {
  361. this.last_clear = Date.now();
  362. }
  363. clear () {
  364. this.clearPendingWrites();
  365. while (this.inputDiv.parentElement.childNodes.length > 1) {
  366. this.inputDiv.parentElement.removeChild(
  367. this.inputDiv.parentElement.firstChild
  368. );
  369. }
  370. this.input.value = "";
  371. this.inputSpan.innerHTML = "";
  372. this.currentLine = null;
  373. }
  374. clearBtnClick () {
  375. this.clear();
  376. }
  377. showBtnClick () {
  378. this.focus();
  379. }
  380. hideBtnClick () {
  381. this.hide();
  382. }
  383. notifyIdle () {
  384. this.info(LocalizedStrings.getMessage("awaiting_input_message"));
  385. this.inputCMD.click();
  386. }
  387. scheduleNotify () {
  388. this.idleInterval = window.setInterval(
  389. this.notifyIdle.bind(this),
  390. Config.idle_input_interval
  391. );
  392. }
  393. cancelPendingInputRequests () {
  394. this.inputListeners.forEach((resolve) => resolve(""));
  395. this.inputListeners.splice(0, this.inputListeners.length);
  396. if (this.idleInterval != null) {
  397. clearInterval(this.idleInterval);
  398. this.idleInterval = null;
  399. }
  400. this.input.value = "";
  401. this.inputSpan.innerHTML = "";
  402. this.currentLine = null;
  403. this.hideInput();
  404. this.anyKey = false;
  405. }
  406. }