domConsole.js 12 KB

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