domConsole.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. if(text.trim().length == 0) {
  196. text = "&nbsp;";
  197. }
  198. return `<span>${text}</span>`;
  199. }
  200. getUserInputText (text) {
  201. if(text.trim().length == 0) {
  202. text = "&nbsp;";
  203. }
  204. return `<i class="icon keyboard outline" style="float:left"></i><span>${text}</span>`;
  205. }
  206. scrollTerm () {
  207. //scrollIt(this.inputDiv.previousSibling,200);
  208. this.termDiv.scrollTop = this.termDiv.scrollHeight;
  209. }
  210. focus () {
  211. this.termDiv.style.display = 'block';
  212. // Is in draggable mode?
  213. if(!this.disableMarginTop && this.parent.style.top.length == 0) {
  214. this.parent.style.marginTop = "-160px";
  215. }
  216. if(this.needInput) {
  217. this.showInput();
  218. this.scheduleNotify();
  219. }
  220. if(!isElementInViewport(this.termDiv))
  221. this.termDiv.scrollIntoView(false);
  222. this.scrollTerm();
  223. }
  224. hide () {
  225. if(this.needInput) {
  226. clearInterval(this.idleInterval);
  227. this.hideInput();
  228. this.needInput = true;
  229. }
  230. // Is in draggable mode?
  231. if(!this.disableMarginTop && this.parent.style.top.length == 0) {
  232. this.parent.style.marginTop = "0";
  233. }
  234. this.termDiv.style.display = 'none';
  235. }
  236. getClassForType (type) {
  237. switch (type) {
  238. case DOMConsole.INPUT:
  239. return "ivprog-term-userInput";
  240. case DOMConsole.USER:
  241. return "ivprog-term-userText";
  242. case DOMConsole.INFO:
  243. return "ivprog-term-info";
  244. case DOMConsole.ERR:
  245. return "ivprog-term-error";
  246. }
  247. }
  248. dispose () {
  249. this.input.removeEventListener('keyup', this.updateSpanText.bind(this));
  250. this.input.removeEventListener('blur', this.stopBlinkCaret.bind(this));
  251. this.input.removeEventListener('keydown', this.registerInput.bind(this));
  252. this.inputCMD.removeEventListener('click', this.blinkCaretAndFocus.bind(this));
  253. this.clearBtn.removeEventListener('click', this.clearBtnClick.bind(this));
  254. this.hideBtn.removeEventListener('click', this.hideBtnClick.bind(this));
  255. this.showBtn.removeEventListener('click', this.showBtnClick.bind(this));
  256. this.input = null;
  257. this.inputCMD = null;
  258. this.inputDiv = null;
  259. this.termDiv = null;
  260. this.inputSpan = null;
  261. this.cursorRef = null;
  262. this.clearBtn = null;
  263. this.hideBtn = null;
  264. this.showBtn = null;
  265. const cNode = this.parent.cloneNode(false);
  266. this.parent.parentNode.replaceChild(cNode, this.parent);
  267. if(this.cursorInterval != null) {
  268. clearInterval(this.cursorInterval);
  269. }
  270. if(this.idleInterval != null) {
  271. clearInterval(this.idleInterval);
  272. }
  273. }
  274. showInput () {
  275. this.needInput = true;
  276. this.inputDiv.style.display = 'block';
  277. this.inputCMD.click();
  278. //this.inputCMD.scrollIntoView();
  279. this.scrollTerm();
  280. }
  281. hideInput () {
  282. this.needInput = false;
  283. this.inputDiv.style.display = ' none';
  284. clearInterval(this.cursorInterval);
  285. this.cursorInterval = null;
  286. }
  287. requestInput (callback, anyKey = false) {
  288. this.inputListeners.push(callback);
  289. this.anyKey = anyKey;
  290. if(this.idleInterval == null)
  291. this.scheduleNotify();
  292. this.showInput();
  293. }
  294. sendOutput (text) {
  295. const output = ""+text;
  296. output.split("\n").forEach(t => {
  297. t = t.replace(/\t/g,'&nbsp;&nbsp;');
  298. t = t.replace(/\s/g,"&nbsp;");
  299. this.write(t)
  300. });
  301. }
  302. clearPendingWrites () {
  303. this.last_clear = Date.now();
  304. for(const id in this.pending_writes) {
  305. clearTimeout(id);
  306. }
  307. }
  308. clear () {
  309. this.clearPendingWrites();
  310. this.pending_writes = [];
  311. while(this.inputDiv.parentElement.childNodes.length > 1) {
  312. this.inputDiv.parentElement.removeChild(this.inputDiv.parentElement.firstChild);
  313. }
  314. this.input.value = "";
  315. this.inputSpan.innerHTML = '';
  316. }
  317. clearBtnClick () {
  318. this.clear();
  319. }
  320. showBtnClick () {
  321. this.focus();
  322. }
  323. hideBtnClick () {
  324. this.hide();
  325. }
  326. notifyIdle () {
  327. this.info(LocalizedStrings.getMessage('awaiting_input_message'));
  328. this.inputCMD.click();
  329. }
  330. scheduleNotify () {
  331. this.idleInterval = window.setInterval(this.notifyIdle.bind(this), Config.idle_input_interval);
  332. }
  333. }