domConsole.js 9.8 KB

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