domConsole.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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.$(this.clearBtn).popup({content:LocalizedStrings.getUI("tooltip_terminal_clear")});
  115. window.$(this.showBtn).popup({content:LocalizedStrings.getUI("tooltip_terminal_show")});
  116. window.$(this.hideBtn).popup({content:LocalizedStrings.getUI("tooltip_terminal_hide")});
  117. }
  118. _setupCursor () {
  119. this.inputCMD.addEventListener('click', this.blinkCaretAndFocus.bind(this));
  120. //this.inputCMD.click();
  121. this.input.addEventListener('keyup', this.updateSpanText.bind(this));
  122. this.input.addEventListener('blur', this.stopBlinkCaret.bind(this));
  123. }
  124. blinkCaretAndFocus () {
  125. if (this.cursorInterval != null) {
  126. return;
  127. }
  128. this.input.focus();
  129. this.cursorInterval = window.setInterval(() => {
  130. if (this.cursorRef.style.visibility === 'visible') {
  131. this.cursorRef.style.visibility = 'hidden';
  132. } else {
  133. this.cursorRef.style.visibility = 'visible';
  134. }
  135. }, 500);
  136. }
  137. updateSpanText () {
  138. this.inputSpan.innerHTML = this.input.value;
  139. if (this.idleInterval != null)
  140. window.clearInterval(this.idleInterval);
  141. this.scheduleNotify()
  142. }
  143. stopBlinkCaret () {
  144. clearInterval(this.cursorInterval);
  145. this.cursorInterval = null;
  146. this.cursorRef.style.visibility = 'visible';
  147. }
  148. notifyListeners (text) {
  149. this.inputListeners.forEach(resolve => resolve(text));
  150. this.inputListeners.splice(0, this.inputListeners.length);
  151. this.hideInput();
  152. this.anyKey = false;
  153. }
  154. writeRawHTML (text, channel) {
  155. this._appendTextLn(text, channel, false);
  156. }
  157. write (text, newLine = false) {
  158. this._appendText(text, DOMConsole.USER, newLine);
  159. }
  160. info (text) {
  161. this._appendTextLn(text, DOMConsole.INFO);
  162. }
  163. err (text) {
  164. this._appendTextLn(text, DOMConsole.ERR);
  165. }
  166. async _appendText (text, type, newLine = false) {
  167. console.debug('Caling appendText');
  168. const write_time = Date.now();
  169. this.pending_writes.push(0);
  170. await Utils.sleep(5);
  171. this.pending_writes.pop();
  172. if (this.last_clear >= write_time) {
  173. return;
  174. }
  175. if (this.currentLine == null) {
  176. const divClass = this.getClassForType(type);
  177. const textDiv = document.createElement('div');
  178. textDiv.classList.add(divClass);
  179. this.termDiv.insertBefore(textDiv, this.inputDiv);
  180. this.currentLine = textDiv
  181. }
  182. this.currentLine.innerHTML += this.getOutputText(text);
  183. if (newLine) {
  184. console.debug("append newline");
  185. this.currentLine = null;
  186. }
  187. this.scrollTerm();
  188. }
  189. async _appendTextLn (text, type, filter = true) {
  190. const write_time = Date.now();
  191. this.pending_writes.push(0);
  192. await Utils.sleep(5);
  193. this.pending_writes.pop();
  194. if (this.last_clear >= write_time) {
  195. return;
  196. }
  197. const divClass = this.getClassForType(type);
  198. const textDiv = document.createElement('div');
  199. textDiv.classList.add(divClass);
  200. if (filter)
  201. textDiv.innerHTML = this.getOutputText(text);
  202. else
  203. textDiv.innerHTML = `<span>${text}</span>`;
  204. this.termDiv.insertBefore(textDiv, this.inputDiv);
  205. this.currentLine = null;
  206. this.scrollTerm();
  207. }
  208. async _appendUserInput (text) {
  209. const write_time = Date.now();
  210. this.pending_writes.push(0);
  211. await Utils.sleep(5);
  212. this.pending_writes.pop();
  213. if (this.last_clear >= write_time) {
  214. return;
  215. }
  216. const divClass = this.getClassForType(DOMConsole.INPUT);
  217. const textDiv = document.createElement('div');
  218. textDiv.innerHTML = this.getUserInputText(text);
  219. textDiv.classList.add(divClass);
  220. this.termDiv.insertBefore(textDiv, this.inputDiv);
  221. this.currentLine = null;
  222. this.scrollTerm();
  223. }
  224. getOutputText (text) {
  225. text = text.replace(/\s/g, "&#160;");
  226. return `<span>${text}</span>`;
  227. }
  228. getUserInputText (text) {
  229. if (text.trim().length == 0) {
  230. text = "&nbsp;";
  231. }
  232. return `<i class="icon keyboard outline" style="float:left"></i><span>${text}</span>`;
  233. }
  234. scrollTerm () {
  235. //scrollIt(this.inputDiv.previousSibling,200);
  236. this.termDiv.scrollTop = this.termDiv.scrollHeight;
  237. }
  238. focus () {
  239. this.termDiv.style.display = 'block';
  240. // Is in draggable mode?
  241. if (!this.disableMarginTop && this.parent.style.top.length == 0) {
  242. this.parent.style.marginTop = "-160px";
  243. }
  244. if (this.needInput) {
  245. this.showInput();
  246. this.scheduleNotify();
  247. }
  248. if (!Utils.isElementInViewport(this.termDiv))
  249. this.termDiv.scrollIntoView(false);
  250. this.scrollTerm();
  251. }
  252. hide () {
  253. if (this.needInput) {
  254. clearInterval(this.idleInterval);
  255. this.hideInput();
  256. this.needInput = true;
  257. }
  258. // Is in draggable mode?
  259. if (!this.disableMarginTop && this.parent.style.top.length == 0) {
  260. this.parent.style.marginTop = "0";
  261. }
  262. this.termDiv.style.display = 'none';
  263. }
  264. getClassForType (type) {
  265. switch (type) {
  266. case DOMConsole.INPUT:
  267. return "ivprog-term-userInput";
  268. case DOMConsole.USER:
  269. return "ivprog-term-userText";
  270. case DOMConsole.INFO:
  271. return "ivprog-term-info";
  272. case DOMConsole.ERR:
  273. return "ivprog-term-error";
  274. }
  275. }
  276. dispose () {
  277. this.input.removeEventListener('keyup', this.updateSpanText.bind(this));
  278. this.input.removeEventListener('blur', this.stopBlinkCaret.bind(this));
  279. this.input.removeEventListener('keydown', this.registerInput.bind(this));
  280. this.inputCMD.removeEventListener('click', this.blinkCaretAndFocus.bind(this));
  281. this.clearBtn.removeEventListener('click', this.clearBtnClick.bind(this));
  282. this.hideBtn.removeEventListener('click', this.hideBtnClick.bind(this));
  283. this.showBtn.removeEventListener('click', this.showBtnClick.bind(this));
  284. this.input = null;
  285. this.inputCMD = null;
  286. this.inputDiv = null;
  287. this.termDiv = null;
  288. this.inputSpan = null;
  289. this.cursorRef = null;
  290. this.clearBtn = null;
  291. this.hideBtn = null;
  292. this.showBtn = null;
  293. this.currentLine = null;
  294. const cNode = this.parent.cloneNode(false);
  295. this.parent.parentNode.replaceChild(cNode, this.parent);
  296. if (this.cursorInterval != null) {
  297. clearInterval(this.cursorInterval);
  298. }
  299. if (this.idleInterval != null) {
  300. clearInterval(this.idleInterval);
  301. }
  302. }
  303. showInput () {
  304. this.needInput = true;
  305. this.inputDiv.style.display = 'block';
  306. this.inputCMD.click();
  307. //this.inputCMD.scrollIntoView();
  308. this.scrollTerm();
  309. }
  310. hideInput () {
  311. this.needInput = false;
  312. this.inputDiv.style.display = ' none';
  313. clearInterval(this.cursorInterval);
  314. this.cursorInterval = null;
  315. }
  316. requestInput (anyKey = false) {
  317. const promise = new Promise( (resolve, _) => {
  318. this.inputListeners.push(resolve);
  319. this.anyKey = anyKey;
  320. if (this.idleInterval == null)
  321. this.scheduleNotify();
  322. this.showInput();
  323. });
  324. return promise;
  325. }
  326. sendOutput (text) {
  327. console.debug(text);
  328. let output = ""+text;
  329. if (output.indexOf('\n') !== -1) {
  330. console.debug("newline");
  331. const outputList = output.split('\n');
  332. let i = 0;
  333. for ( ; i < outputList.length - 1; i += 1) {
  334. console.debug("newline write");
  335. let t = outputList[i];
  336. t = t.replace(/\t/g,'&#x0020;&#x0020;');
  337. t = t.replace(/\s/g,"&#x0020;");
  338. if (t.length == 0) {
  339. // t = "&nbsp;";
  340. console.debug('Empty string');
  341. this.currentLine = null;
  342. } else
  343. this.write(t, true);
  344. }
  345. let t = outputList[i];
  346. t = t.replace(/\t/g,'&#x0020;&#x0020;');
  347. t = t.replace(/\s/g,"&#x0020;");
  348. if (t.length != 0)
  349. this.write(t);
  350. } else {
  351. console.debug("no newline");
  352. output = output.replace(/\t/g,'&#x0020;&#x0020;');
  353. output = output.replace(/\s/g,"&#x0020;");
  354. this.write(output);
  355. }
  356. }
  357. clearPendingWrites () {
  358. this.last_clear = Date.now();
  359. }
  360. clear () {
  361. this.clearPendingWrites()
  362. while (this.inputDiv.parentElement.childNodes.length > 1) {
  363. this.inputDiv.parentElement.removeChild(this.inputDiv.parentElement.firstChild);
  364. }
  365. this.input.value = '';
  366. this.inputSpan.innerHTML = '';
  367. this.currentLine = null;
  368. }
  369. clearBtnClick () {
  370. this.clear();
  371. }
  372. showBtnClick () {
  373. this.focus();
  374. }
  375. hideBtnClick () {
  376. this.hide();
  377. }
  378. notifyIdle () {
  379. this.info(LocalizedStrings.getMessage('awaiting_input_message'));
  380. this.inputCMD.click();
  381. }
  382. scheduleNotify () {
  383. this.idleInterval = window.setInterval(this.notifyIdle.bind(this), Config.idle_input_interval);
  384. }
  385. cancelPendingInputRequests () {
  386. this.inputListeners.forEach(resolve => resolve(''));
  387. this.inputListeners.splice(0, this.inputListeners.length);
  388. if (this.idleInterval != null) {
  389. clearInterval(this.idleInterval);
  390. this.idleInterval = null;
  391. }
  392. this.input.value = '';
  393. this.inputSpan.innerHTML = '';
  394. this.currentLine = null;
  395. this.hideInput();
  396. this.anyKey = false;
  397. }
  398. }