domConsole.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. }
  87. }
  88. _setupDom () {
  89. const bashNode = document.createElement('div');
  90. bashNode.classList.add('bash');
  91. bashNode.innerHTML = DOMConsole.BASH_TEMPLATE;
  92. this.termDiv = bashNode.querySelector("#ivprog-term");
  93. this.termDiv.classList.add("ivprog-term-div");
  94. this.inputDiv = document.createElement("div");
  95. this.inputDiv.id = "ivprog-terminal-inputdiv";
  96. this.inputDiv.innerHTML = DOMConsole.INPUT_CARET_TEMPLATE;
  97. this.input = document.createElement("input");
  98. this.input.setAttribute("name", "command");
  99. this.input.setAttribute("value", "");
  100. this.input.setAttribute("type", "text");
  101. this.inputDiv.append(this.input);
  102. this.termDiv.append(this.inputDiv);
  103. bashNode.append(this.termDiv);
  104. this.parent.append(bashNode);
  105. this.inputCMD = this.inputDiv.querySelector("#cmd");
  106. this.cursorRef = this.inputCMD.querySelector("#cursor");
  107. this.inputSpan = this.inputCMD.querySelector('span');
  108. this.clearBtn = bashNode.querySelector('#ivprog-console-clearbtn');
  109. this.hideBtn = bashNode.querySelector('#ivprog-console-hidebtn');
  110. this.showBtn = bashNode.querySelector('#ivprog-console-showbtn');
  111. this._setupCursor();
  112. //Jquery tooltips....
  113. window.$(this.clearBtn).popup({content:LocalizedStrings.getUI("tooltip_terminal_clear")});
  114. window.$(this.showBtn).popup({content:LocalizedStrings.getUI("tooltip_terminal_show")});
  115. window.$(this.hideBtn).popup({content:LocalizedStrings.getUI("tooltip_terminal_hide")});
  116. }
  117. _setupCursor () {
  118. this.inputCMD.addEventListener('click', this.blinkCaretAndFocus.bind(this));
  119. //this.inputCMD.click();
  120. this.input.addEventListener('keyup', this.updateSpanText.bind(this));
  121. this.input.addEventListener('blur', this.stopBlinkCaret.bind(this));
  122. }
  123. blinkCaretAndFocus () {
  124. if (this.cursorInterval != null) {
  125. return;
  126. }
  127. this.input.focus();
  128. this.cursorInterval = window.setInterval(() => {
  129. if (this.cursorRef.style.visibility === 'visible') {
  130. this.cursorRef.style.visibility = 'hidden';
  131. } else {
  132. this.cursorRef.style.visibility = 'visible';
  133. }
  134. }, 500);
  135. }
  136. updateSpanText () {
  137. this.inputSpan.innerHTML = this.input.value;
  138. if (this.idleInterval != null)
  139. window.clearInterval(this.idleInterval);
  140. this.scheduleNotify()
  141. }
  142. stopBlinkCaret () {
  143. clearInterval(this.cursorInterval);
  144. this.cursorInterval = null;
  145. this.cursorRef.style.visibility = 'visible';
  146. }
  147. notifyListeners (text) {
  148. this.inputListeners.forEach(resolve => resolve(text));
  149. this.inputListeners.splice(0, this.inputListeners.length);
  150. this.hideInput();
  151. this.anyKey = false;
  152. }
  153. write (text) {
  154. this._appendText(text, DOMConsole.USER);
  155. }
  156. info (text) {
  157. this._appendText(text, DOMConsole.INFO, false);
  158. }
  159. err (text) {
  160. this._appendText(text, DOMConsole.ERR, false);
  161. }
  162. async _appendText (text, type, filter = true) {
  163. const write_time = Date.now();
  164. this.pending_writes.push(0);
  165. await Utils.sleep(5);
  166. this.pending_writes.pop();
  167. if (this.last_clear >= write_time) {
  168. return;
  169. }
  170. const divClass = this.getClassForType(type);
  171. const textDiv = document.createElement('div');
  172. textDiv.classList.add(divClass);
  173. if (filter)
  174. textDiv.innerHTML = this.getOutputText(text);
  175. else
  176. textDiv.innerHTML = `<span>${text}</span>`;
  177. this.termDiv.insertBefore(textDiv, this.inputDiv);
  178. this.scrollTerm();
  179. }
  180. async _appendUserInput (text) {
  181. const write_time = Date.now();
  182. this.pending_writes.push(0);
  183. await Utils.sleep(5);
  184. this.pending_writes.pop();
  185. if (this.last_clear >= write_time) {
  186. return;
  187. }
  188. const divClass = this.getClassForType(DOMConsole.INPUT);
  189. const textDiv = document.createElement('div');
  190. textDiv.innerHTML = this.getUserInputText(text);
  191. textDiv.classList.add(divClass);
  192. this.termDiv.insertBefore(textDiv, this.inputDiv);
  193. this.scrollTerm();
  194. }
  195. getOutputText (text) {
  196. text = text.replace(/\s/g, "&#160;");
  197. return `<span>${text}</span>`;
  198. }
  199. getUserInputText (text) {
  200. if (text.trim().length == 0) {
  201. text = "&nbsp;";
  202. }
  203. return `<i class="icon keyboard outline" style="float:left"></i><span>${text}</span>`;
  204. }
  205. scrollTerm () {
  206. //scrollIt(this.inputDiv.previousSibling,200);
  207. this.termDiv.scrollTop = this.termDiv.scrollHeight;
  208. }
  209. focus () {
  210. this.termDiv.style.display = 'block';
  211. // Is in draggable mode?
  212. if (!this.disableMarginTop && this.parent.style.top.length == 0) {
  213. this.parent.style.marginTop = "-160px";
  214. }
  215. if (this.needInput) {
  216. this.showInput();
  217. this.scheduleNotify();
  218. }
  219. if (!Utils.isElementInViewport(this.termDiv))
  220. this.termDiv.scrollIntoView(false);
  221. this.scrollTerm();
  222. }
  223. hide () {
  224. if (this.needInput) {
  225. clearInterval(this.idleInterval);
  226. this.hideInput();
  227. this.needInput = true;
  228. }
  229. // Is in draggable mode?
  230. if (!this.disableMarginTop && this.parent.style.top.length == 0) {
  231. this.parent.style.marginTop = "0";
  232. }
  233. this.termDiv.style.display = 'none';
  234. }
  235. getClassForType (type) {
  236. switch (type) {
  237. case DOMConsole.INPUT:
  238. return "ivprog-term-userInput";
  239. case DOMConsole.USER:
  240. return "ivprog-term-userText";
  241. case DOMConsole.INFO:
  242. return "ivprog-term-info";
  243. case DOMConsole.ERR:
  244. return "ivprog-term-error";
  245. }
  246. }
  247. dispose () {
  248. this.input.removeEventListener('keyup', this.updateSpanText.bind(this));
  249. this.input.removeEventListener('blur', this.stopBlinkCaret.bind(this));
  250. this.input.removeEventListener('keydown', this.registerInput.bind(this));
  251. this.inputCMD.removeEventListener('click', this.blinkCaretAndFocus.bind(this));
  252. this.clearBtn.removeEventListener('click', this.clearBtnClick.bind(this));
  253. this.hideBtn.removeEventListener('click', this.hideBtnClick.bind(this));
  254. this.showBtn.removeEventListener('click', this.showBtnClick.bind(this));
  255. this.input = null;
  256. this.inputCMD = null;
  257. this.inputDiv = null;
  258. this.termDiv = null;
  259. this.inputSpan = null;
  260. this.cursorRef = null;
  261. this.clearBtn = null;
  262. this.hideBtn = null;
  263. this.showBtn = null;
  264. const cNode = this.parent.cloneNode(false);
  265. this.parent.parentNode.replaceChild(cNode, this.parent);
  266. if (this.cursorInterval != null) {
  267. clearInterval(this.cursorInterval);
  268. }
  269. if (this.idleInterval != null) {
  270. clearInterval(this.idleInterval);
  271. }
  272. }
  273. showInput () {
  274. this.needInput = true;
  275. this.inputDiv.style.display = 'block';
  276. this.inputCMD.click();
  277. //this.inputCMD.scrollIntoView();
  278. this.scrollTerm();
  279. }
  280. hideInput () {
  281. this.needInput = false;
  282. this.inputDiv.style.display = ' none';
  283. clearInterval(this.cursorInterval);
  284. this.cursorInterval = null;
  285. }
  286. requestInput (anyKey = false) {
  287. const promise = new Promise( (resolve, _) => {
  288. this.inputListeners.push(resolve);
  289. this.anyKey = anyKey;
  290. if (this.idleInterval == null)
  291. this.scheduleNotify();
  292. this.showInput();
  293. });
  294. return promise;
  295. }
  296. sendOutput (text) {
  297. const output = ""+text;
  298. output.split("\n").forEach(t => {
  299. t = t.replace(/\t/g,'&#x0020;&#x0020;');
  300. t = t.replace(/\s/g,"&#x0020;");
  301. this.write(t)
  302. });
  303. }
  304. clearPendingWrites () {
  305. this.last_clear = Date.now();
  306. }
  307. clear () {
  308. this.clearPendingWrites()
  309. while (this.inputDiv.parentElement.childNodes.length > 1) {
  310. this.inputDiv.parentElement.removeChild(this.inputDiv.parentElement.firstChild);
  311. }
  312. this.input.value = "";
  313. this.inputSpan.innerHTML = '';
  314. }
  315. clearBtnClick () {
  316. this.clear();
  317. }
  318. showBtnClick () {
  319. this.focus();
  320. }
  321. hideBtnClick () {
  322. this.hide();
  323. }
  324. notifyIdle () {
  325. this.info(LocalizedStrings.getMessage('awaiting_input_message'));
  326. this.inputCMD.click();
  327. }
  328. scheduleNotify () {
  329. this.idleInterval = window.setInterval(this.notifyIdle.bind(this), Config.idle_input_interval);
  330. }
  331. cancelPendingInputRequests () {
  332. this.inputListeners.forEach(resolve => resolve(''));
  333. this.inputListeners.splice(0, this.inputListeners.length);
  334. if (this.idleInterval != null) {
  335. clearInterval(this.idleInterval);
  336. this.idleInterval = null;
  337. }
  338. this.input.value = '';
  339. this.inputSpan.innerHTML = '';
  340. this.hideInput();
  341. this.anyKey = false;
  342. }
  343. }