domConsole.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. import { LocalizedStrings } from "./../services/localizedStringsService";
  2. import { isElementInViewport } 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);
  158. }
  159. err (text) {
  160. this._appendText(text, DOMConsole.ERR);
  161. }
  162. _appendText (text, type) {
  163. const write_time = Date.now();
  164. const pending_write = setTimeout(() => {
  165. this.pending_writes.shift();
  166. if(this.last_clear >= write_time) {
  167. return;
  168. }
  169. const divClass = this.getClassForType(type);
  170. const textDiv = document.createElement('div');
  171. textDiv.classList.add(divClass);
  172. textDiv.innerHTML = this.getOutputText(text);
  173. this.termDiv.insertBefore(textDiv, this.inputDiv);
  174. this.scrollTerm();
  175. }, 5);
  176. this.pending_writes.push(pending_write);
  177. }
  178. _appendUserInput (text) {
  179. const write_time = Date.now();
  180. const pending_write = setTimeout(() => {
  181. this.pending_writes.shift();
  182. if(this.last_clear >= write_time) {
  183. return;
  184. }
  185. const divClass = this.getClassForType(DOMConsole.INPUT);
  186. const textDiv = document.createElement('div');
  187. textDiv.innerHTML = this.getUserInputText(text);
  188. textDiv.classList.add(divClass);
  189. this.termDiv.insertBefore(textDiv, this.inputDiv);
  190. this.scrollTerm();
  191. }, 5);
  192. this.pending_writes.push(pending_write);
  193. }
  194. getOutputText (text) {
  195. text = text.replace(/\s/g, "&#160;");
  196. return `<span>${text}</span>`;
  197. }
  198. getUserInputText (text) {
  199. if(text.trim().length == 0) {
  200. text = "&nbsp;";
  201. }
  202. return `<i class="icon keyboard outline" style="float:left"></i><span>${text}</span>`;
  203. }
  204. scrollTerm () {
  205. //scrollIt(this.inputDiv.previousSibling,200);
  206. this.termDiv.scrollTop = this.termDiv.scrollHeight;
  207. }
  208. focus () {
  209. this.termDiv.style.display = 'block';
  210. // Is in draggable mode?
  211. if(!this.disableMarginTop && this.parent.style.top.length == 0) {
  212. this.parent.style.marginTop = "-160px";
  213. }
  214. if(this.needInput) {
  215. this.showInput();
  216. this.scheduleNotify();
  217. }
  218. if(!isElementInViewport(this.termDiv))
  219. this.termDiv.scrollIntoView(false);
  220. this.scrollTerm();
  221. }
  222. hide () {
  223. if(this.needInput) {
  224. clearInterval(this.idleInterval);
  225. this.hideInput();
  226. this.needInput = true;
  227. }
  228. // Is in draggable mode?
  229. if(!this.disableMarginTop && this.parent.style.top.length == 0) {
  230. this.parent.style.marginTop = "0";
  231. }
  232. this.termDiv.style.display = 'none';
  233. }
  234. getClassForType (type) {
  235. switch (type) {
  236. case DOMConsole.INPUT:
  237. return "ivprog-term-userInput";
  238. case DOMConsole.USER:
  239. return "ivprog-term-userText";
  240. case DOMConsole.INFO:
  241. return "ivprog-term-info";
  242. case DOMConsole.ERR:
  243. return "ivprog-term-error";
  244. }
  245. }
  246. dispose () {
  247. this.input.removeEventListener('keyup', this.updateSpanText.bind(this));
  248. this.input.removeEventListener('blur', this.stopBlinkCaret.bind(this));
  249. this.input.removeEventListener('keydown', this.registerInput.bind(this));
  250. this.inputCMD.removeEventListener('click', this.blinkCaretAndFocus.bind(this));
  251. this.clearBtn.removeEventListener('click', this.clearBtnClick.bind(this));
  252. this.hideBtn.removeEventListener('click', this.hideBtnClick.bind(this));
  253. this.showBtn.removeEventListener('click', this.showBtnClick.bind(this));
  254. this.input = null;
  255. this.inputCMD = null;
  256. this.inputDiv = null;
  257. this.termDiv = null;
  258. this.inputSpan = null;
  259. this.cursorRef = null;
  260. this.clearBtn = null;
  261. this.hideBtn = null;
  262. this.showBtn = null;
  263. const cNode = this.parent.cloneNode(false);
  264. this.parent.parentNode.replaceChild(cNode, this.parent);
  265. if(this.cursorInterval != null) {
  266. clearInterval(this.cursorInterval);
  267. }
  268. if(this.idleInterval != null) {
  269. clearInterval(this.idleInterval);
  270. }
  271. }
  272. showInput () {
  273. this.needInput = true;
  274. this.inputDiv.style.display = 'block';
  275. this.inputCMD.click();
  276. //this.inputCMD.scrollIntoView();
  277. this.scrollTerm();
  278. }
  279. hideInput () {
  280. this.needInput = false;
  281. this.inputDiv.style.display = ' none';
  282. clearInterval(this.cursorInterval);
  283. this.cursorInterval = null;
  284. }
  285. requestInput (callback, anyKey = false) {
  286. this.inputListeners.push(callback);
  287. this.anyKey = anyKey;
  288. if(this.idleInterval == null)
  289. this.scheduleNotify();
  290. this.showInput();
  291. }
  292. sendOutput (text) {
  293. const output = ""+text;
  294. output.split("\n").forEach(t => {
  295. t = t.replace(/\t/g,'&#x0020;&#x0020;');
  296. t = t.replace(/\s/g,"&#x0020;");
  297. this.write(t)
  298. });
  299. }
  300. clearPendingWrites () {
  301. this.last_clear = Date.now();
  302. for(const id in this.pending_writes) {
  303. clearTimeout(id);
  304. }
  305. }
  306. clear () {
  307. this.clearPendingWrites();
  308. this.pending_writes = [];
  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. }