domConsole.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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.was_waiting_input = false;
  58. }
  59. setup () {
  60. this._setupDom();
  61. this._setupEvents();
  62. }
  63. _setupEvents () {
  64. this.input.addEventListener('keydown', this.registerInput.bind(this));
  65. this.clearBtn.addEventListener('click', this.clearBtnClick.bind(this));
  66. this.hideBtn.addEventListener('click', this.hideBtnClick.bind(this));
  67. this.showBtn.addEventListener('click', this.showBtnClick.bind(this));
  68. }
  69. registerInput (event) {
  70. if (!this.needInput) {
  71. return;
  72. }
  73. const keyCode = event.which;
  74. if (keyCode === 13 || this.anyKey) {
  75. if(this.idleInterval != null) {
  76. clearInterval(this.idleInterval);
  77. this.idleInterval = null;
  78. }
  79. let text = this.input.value;
  80. text = text.replace('[\n\r]+', '');
  81. this.notifyListeners(text);
  82. this._appendUserInput(text);
  83. this.input.value = '';
  84. this.inputSpan.innerHTML = '';
  85. }
  86. }
  87. _setupDom () {
  88. const bashNode = document.createElement('div');
  89. bashNode.classList.add('bash');
  90. bashNode.innerHTML = DOMConsole.BASH_TEMPLATE;
  91. this.termDiv = bashNode.querySelector("#ivprog-term");
  92. this.termDiv.classList.add("ivprog-term-div");
  93. this.inputDiv = document.createElement("div");
  94. this.inputDiv.id = "ivprog-terminal-inputdiv";
  95. this.inputDiv.innerHTML = DOMConsole.INPUT_CARET_TEMPLATE;
  96. this.input = document.createElement("input");
  97. this.input.setAttribute("name", "command");
  98. this.input.setAttribute("value", "");
  99. this.input.setAttribute("type", "text");
  100. this.inputDiv.append(this.input);
  101. this.termDiv.append(this.inputDiv);
  102. bashNode.append(this.termDiv);
  103. this.parent.append(bashNode);
  104. this.inputCMD = this.inputDiv.querySelector("#cmd");
  105. this.cursorRef = this.inputCMD.querySelector("#cursor");
  106. this.inputSpan = this.inputCMD.querySelector('span');
  107. this.clearBtn = bashNode.querySelector('#ivprog-console-clearbtn');
  108. this.hideBtn = bashNode.querySelector('#ivprog-console-hidebtn');
  109. this.showBtn = bashNode.querySelector('#ivprog-console-showbtn');
  110. this._setupCursor();
  111. //Jquery tooltips....
  112. window.$(this.clearBtn).popup({content:LocalizedStrings.getUI("tooltip_terminal_clear")});
  113. window.$(this.showBtn).popup({content:LocalizedStrings.getUI("tooltip_terminal_show")});
  114. window.$(this.hideBtn).popup({content:LocalizedStrings.getUI("tooltip_terminal_hide")});
  115. }
  116. _setupCursor () {
  117. this.inputCMD.addEventListener('click', this.blinkCaretAndFocus.bind(this));
  118. //this.inputCMD.click();
  119. this.input.addEventListener('keyup', this.updateSpanText.bind(this));
  120. this.input.addEventListener('blur', this.stopBlinkCaret.bind(this));
  121. }
  122. blinkCaretAndFocus () {
  123. if(this.cursorInterval != null) {
  124. return;
  125. }
  126. this.input.focus();
  127. const that = this;
  128. this.cursorInterval = window.setInterval(function() {
  129. if (that.cursorRef.style.visibility === 'visible') {
  130. that.cursorRef.style.visibility = 'hidden';
  131. } else {
  132. that.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. setTimeout(() => {
  164. const divClass = this.getClassForType(type);
  165. const textDiv = document.createElement('div');
  166. textDiv.classList.add(divClass);
  167. textDiv.innerHTML = this.getOutputText(text);
  168. this.termDiv.insertBefore(textDiv, this.inputDiv);
  169. this.scrollTerm();
  170. }, 50)
  171. }
  172. _appendUserInput (text) {
  173. const divClass = this.getClassForType(DOMConsole.INPUT);
  174. const textDiv = document.createElement('div');
  175. textDiv.innerHTML = this.getUserInputText(text);
  176. textDiv.classList.add(divClass);
  177. this.termDiv.insertBefore(textDiv, this.inputDiv);
  178. this.scrollTerm();
  179. }
  180. getOutputText (text) {
  181. if(text.trim().length == 0) {
  182. text = "&nbsp;";
  183. }
  184. return `<span>${text}</span>`;
  185. }
  186. getUserInputText (text) {
  187. if(text.trim().length == 0) {
  188. text = "&nbsp;";
  189. }
  190. return `<i class="icon keyboard outline" style="float:left"></i><span>${text}</span>`;
  191. }
  192. scrollTerm () {
  193. //scrollIt(this.inputDiv.previousSibling,200);
  194. this.termDiv.scrollTop = this.termDiv.scrollHeight;
  195. }
  196. focus () {
  197. this.termDiv.style.display = 'block';
  198. // Is in draggable mode?
  199. if(!this.disableMarginTop && this.parent.style.top.length == 0) {
  200. this.parent.style.marginTop = "-160px";
  201. }
  202. if(this.needInput) {
  203. this.showInput();
  204. this.scheduleNotify();
  205. }
  206. if(!isElementInViewport(this.termDiv))
  207. this.termDiv.scrollIntoView(false);
  208. this.scrollTerm();
  209. }
  210. hide () {
  211. if(this.needInput) {
  212. clearInterval(this.idleInterval);
  213. this.hideInput();
  214. this.needInput = true;
  215. }
  216. // Is in draggable mode?
  217. if(!this.disableMarginTop && this.parent.style.top.length == 0) {
  218. this.parent.style.marginTop = "0";
  219. }
  220. this.termDiv.style.display = 'none';
  221. }
  222. getClassForType (type) {
  223. switch (type) {
  224. case DOMConsole.INPUT:
  225. return "ivprog-term-userInput";
  226. case DOMConsole.USER:
  227. return "ivprog-term-userText";
  228. case DOMConsole.INFO:
  229. return "ivprog-term-info";
  230. case DOMConsole.ERR:
  231. return "ivprog-term-error";
  232. }
  233. }
  234. dispose () {
  235. this.input.removeEventListener('keyup', this.updateSpanText.bind(this));
  236. this.input.removeEventListener('blur', this.stopBlinkCaret.bind(this));
  237. this.input.removeEventListener('keydown', this.registerInput.bind(this));
  238. this.inputCMD.removeEventListener('click', this.blinkCaretAndFocus.bind(this));
  239. this.clearBtn.removeEventListener('click', this.clearBtnClick.bind(this));
  240. this.hideBtn.removeEventListener('click', this.hideBtnClick.bind(this));
  241. this.showBtn.removeEventListener('click', this.showBtnClick.bind(this));
  242. this.input = null;
  243. this.inputCMD = null;
  244. this.inputDiv = null;
  245. this.termDiv = null;
  246. this.inputSpan = null;
  247. this.cursorRef = null;
  248. this.clearBtn = null;
  249. this.hideBtn = null;
  250. this.showBtn = null;
  251. const cNode = this.parent.cloneNode(false);
  252. this.parent.parentNode.replaceChild(cNode, this.parent);
  253. if(this.cursorInterval != null) {
  254. clearInterval(this.cursorInterval);
  255. }
  256. if(this.idleInterval != null) {
  257. clearInterval(this.idleInterval);
  258. }
  259. }
  260. showInput () {
  261. this.needInput = true;
  262. this.inputDiv.style.display = 'block';
  263. this.inputCMD.click();
  264. //this.inputCMD.scrollIntoView();
  265. this.scrollTerm();
  266. }
  267. hideInput () {
  268. this.needInput = false;
  269. this.inputDiv.style.display = ' none';
  270. clearInterval(this.cursorInterval);
  271. this.cursorInterval = null;
  272. }
  273. requestInput (callback, anyKey = false) {
  274. this.inputListeners.push(callback);
  275. this.anyKey = anyKey;
  276. if(this.idleInterval == null)
  277. this.scheduleNotify();
  278. this.showInput();
  279. }
  280. sendOutput (text) {
  281. const output = ""+text;
  282. output.split("\n").forEach(t => {
  283. t = t.replace(/\t/g,'&nbsp;&nbsp;');
  284. t = t.replace(/\s/g,"&nbsp;");
  285. this.write(t)
  286. });
  287. }
  288. clear () {
  289. while(this.inputDiv.parentElement.childNodes.length > 1) {
  290. this.inputDiv.parentElement.removeChild(this.inputDiv.parentElement.firstChild);
  291. }
  292. this.input.value = "";
  293. this.inputSpan.innerHTML = '';
  294. }
  295. clearBtnClick () {
  296. this.clear();
  297. }
  298. showBtnClick () {
  299. this.focus();
  300. }
  301. hideBtnClick () {
  302. this.hide();
  303. }
  304. notifyIdle () {
  305. this.info(LocalizedStrings.getMessage('awaiting_input_message'));
  306. this.inputCMD.click();
  307. }
  308. scheduleNotify () {
  309. this.idleInterval = window.setInterval(this.notifyIdle.bind(this), Config.idle_input_interval);
  310. }
  311. }