domConsole.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import { LocalizedStrings } from "./../services/localizedStringsService";
  2. import { Config } from "./../util/config";
  3. export class DOMConsole {
  4. static get USER () {
  5. return 0;
  6. }
  7. static get INFO () {
  8. return 1;
  9. }
  10. static get ERR () {
  11. return 2;
  12. }
  13. static get INPUT () {
  14. return 3;
  15. }
  16. constructor (elementID) {
  17. this.input = null;
  18. this.cursorInterval = null;
  19. this.idleInterval = null;
  20. this.inputDiv = null;
  21. this.inputCMD = null;
  22. this.inputSpan = null;
  23. this.cursorRef = null;
  24. this.needInput = false;
  25. this.clearBtn = null;
  26. this.hideBtn = null;
  27. this.showBtn = null;
  28. this.termDiv = null;
  29. this.anyKey = false;
  30. let actualID = elementID
  31. if (elementID[0] === '#') {
  32. actualID = elementID.substring(1);
  33. }
  34. this.parent = document.getElementById(actualID)
  35. this.setup();
  36. this.inputListeners = [];
  37. this.hideInput();
  38. }
  39. setup () {
  40. this._setupDom();
  41. this._setupEvents();
  42. }
  43. _setupEvents () {
  44. this.input.addEventListener('keydown', this.registerInput.bind(this));
  45. this.clearBtn.addEventListener('click', this.clearBtnClick.bind(this));
  46. this.hideBtn.addEventListener('click', this.hideBtnClick.bind(this));
  47. this.showBtn.addEventListener('click', this.showBtnClick.bind(this));
  48. }
  49. registerInput (event) {
  50. if (!this.needInput) {
  51. return;
  52. }
  53. const keyCode = event.which;
  54. if (keyCode === 13 || this.anyKey) {
  55. if(this.idleInterval != null) {
  56. clearInterval(this.idleInterval);
  57. this.idleInterval = null;
  58. }
  59. let text = this.input.value;
  60. text = text.replace('[\n\r]+', '');
  61. this.notifyListeners(text);
  62. this._appendUserInput(text);
  63. this.input.value = '';
  64. this.inputSpan.innerHTML = '';
  65. }
  66. }
  67. _setupDom () {
  68. const bashNode = document.createElement('div');
  69. bashNode.classList.add('bash');
  70. bashNode.innerHTML = `
  71. <div class="bash-title">
  72. <i id="ivprog-console-clearbtn" class="icon eraser" style="float:left;padding-left: 5px"></i>
  73. <span>Terminal</span>
  74. <i id="ivprog-console-showbtn" class="icon window maximize outline" style="float:right"></i>
  75. <i id="ivprog-console-hidebtn" class="icon window minimize outline" style="float:right"></i>
  76. </div>
  77. <div id='ivprog-term' class="bash-body"></div>`;
  78. this.termDiv = bashNode.querySelector("#ivprog-term");
  79. this.termDiv.classList.add("ivprog-term-div");
  80. this.inputDiv = document.createElement("div");
  81. this.inputDiv.id = "ivprog-terminal-inputdiv";
  82. this.inputDiv.innerHTML = `
  83. <div id="cmd">
  84. <span></span>
  85. <div id="cursor"></div>
  86. </div>`;
  87. this.input = document.createElement("input");
  88. this.input.setAttribute("name", "command");
  89. this.input.setAttribute("value", "");
  90. this.input.setAttribute("type", "text");
  91. this.inputDiv.append(this.input);
  92. this.termDiv.append(this.inputDiv);
  93. bashNode.append(this.termDiv);
  94. this.parent.append(bashNode);
  95. this.inputCMD = this.inputDiv.querySelector("#cmd");
  96. this.cursorRef = this.inputCMD.querySelector("#cursor");
  97. this.inputSpan = this.inputCMD.querySelector('span');
  98. this.clearBtn = bashNode.querySelector('#ivprog-console-clearbtn');
  99. this.hideBtn = bashNode.querySelector('#ivprog-console-hidebtn');
  100. this.showBtn = bashNode.querySelector('#ivprog-console-showbtn');
  101. this._setupCursor();
  102. //Jquery tooltips....
  103. $(this.clearBtn).popup({content:LocalizedStrings.getUI("terminal_clear")});
  104. $(this.showBtn).popup({content:LocalizedStrings.getUI("terminal_show")});
  105. $(this.hideBtn).popup({content:LocalizedStrings.getUI("terminal_hide")});
  106. }
  107. _setupCursor () {
  108. this.inputCMD.addEventListener('click', this.blinkCaretAndFocus.bind(this));
  109. this.inputCMD.click();
  110. this.input.addEventListener('keyup', this.updateSpanText.bind(this));
  111. this.input.addEventListener('blur', this.stopBlinkCaret.bind(this));
  112. }
  113. blinkCaretAndFocus () {
  114. if(this.cursorInterval != null) {
  115. return;
  116. }
  117. this.input.focus();
  118. const outerRef = this;
  119. this.cursorInterval = window.setInterval(function() {
  120. if (outerRef.cursorRef.style.visibility === 'visible') {
  121. outerRef.cursorRef.style.visibility = 'hidden';
  122. } else {
  123. outerRef.cursorRef.style.visibility = 'visible';
  124. }
  125. }, 500);
  126. }
  127. updateSpanText () {
  128. this.inputSpan.innerHTML = this.input.value;
  129. if(this.idleInterval != null)
  130. window.clearInterval(this.idleInterval);
  131. this.scheduleNotify()
  132. }
  133. stopBlinkCaret () {
  134. clearInterval(this.cursorInterval);
  135. this.cursorInterval = null;
  136. this.cursorRef.style.visibility = 'visible';
  137. }
  138. notifyListeners (text) {
  139. this.inputListeners.forEach(resolve => resolve(text));
  140. this.inputListeners.splice(0, this.inputListeners.length);
  141. this.hideInput();
  142. this.anyKey = false;
  143. }
  144. write (text) {
  145. this._appendText(text, DOMConsole.USER);
  146. }
  147. info (text) {
  148. this._appendText(text, DOMConsole.INFO);
  149. }
  150. err (text) {
  151. this._appendText(text, DOMConsole.ERR);
  152. }
  153. _appendText (text, type) {
  154. const divClass = this.getClassForType(type);
  155. const textDiv = document.createElement('div');
  156. textDiv.classList.add(divClass);
  157. textDiv.innerHTML = `<span>${text}</span>`;
  158. this.termDiv.insertBefore(textDiv, this.inputDiv);
  159. this.scrollTerm();
  160. }
  161. _appendUserInput (text) {
  162. const divClass = this.getClassForType(DOMConsole.INPUT);
  163. const textDiv = document.createElement('div');
  164. textDiv.innerHTML = `
  165. <i class="icon keyboard outline" style="float:left"></i>
  166. <span>${text}</span>`;
  167. textDiv.classList.add(divClass);
  168. this.termDiv.insertBefore(textDiv, this.inputDiv);
  169. this.scrollTerm();
  170. }
  171. scrollTerm () {
  172. //scrollIt(this.inputDiv.previousSibling,200);
  173. this.inputDiv.previousSibling.scrollIntoView();
  174. }
  175. focus () {
  176. this.termDiv.style.display = 'block';
  177. const prev = this.inputDiv.closest('div');
  178. if(prev != null)
  179. prev.scrollIntoView();
  180. }
  181. hide () {
  182. this.termDiv.style.display = 'none';
  183. }
  184. getClassForType (type) {
  185. switch (type) {
  186. case DOMConsole.INPUT:
  187. return "ivprog-term-userInput";
  188. case DOMConsole.USER:
  189. return "ivprog-term-userText";
  190. case DOMConsole.INFO:
  191. return "ivprog-term-info";
  192. case DOMConsole.ERR:
  193. return "ivprog-term-error";
  194. }
  195. }
  196. dispose () {
  197. this.input.removeEventListener('keyup', this.updateSpanText.bind(this));
  198. this.input.removeEventListener('blur', this.stopBlinkCaret.bind(this));
  199. this.input.removeEventListener('keydown', this.registerInput.bind(this));
  200. this.inputCMD.removeEventListener('click', this.blinkCaretAndFocus.bind(this));
  201. this.clearBtn.removeEventListener('click', this.clearBtnClick.bind(this));
  202. this.hideBtn.removeEventListener('click', this.hideBtnClick.bind(this));
  203. this.showBtn.removeEventListener('click', this.showBtnClick.bind(this));
  204. this.input = null;
  205. this.inputCMD = null;
  206. this.inputDiv = null;
  207. this.termDiv = null;
  208. this.inputSpan = null;
  209. this.cursorRef = null;
  210. this.clearBtn = null;
  211. this.hideBtn = null;
  212. this.showBtn = null;
  213. const cNode = this.parent.cloneNode(false);
  214. this.parent.parentNode.replaceChild(cNode, this.parent);
  215. if(this.cursorInterval != null) {
  216. clearInterval(this.cursorInterval);
  217. }
  218. if(this.idleInterval != null) {
  219. clearInterval(this.idleInterval);
  220. }
  221. }
  222. showInput () {
  223. this.needInput = true;
  224. this.inputDiv.style.display = 'block';
  225. this.inputCMD.click();
  226. this.inputCMD.scrollIntoView();
  227. }
  228. hideInput () {
  229. this.needInput = false;
  230. this.inputDiv.style.display = ' none';
  231. clearInterval(this.cursorInterval);
  232. this.cursorInterval = null;
  233. }
  234. requestInput (callback, anyKey = false) {
  235. this.inputListeners.push(callback);
  236. this.anyKey = anyKey;
  237. if(this.idleInterval == null)
  238. this.scheduleNotify();
  239. this.showInput();
  240. }
  241. sendOutput (text) {
  242. const output = ""+tthis.inputCMD.click();ext;
  243. output.split("\n").forEach(t => {
  244. t = t.replace(/\t/g,'&#9;');
  245. this.write(t)
  246. });
  247. }
  248. clear () {
  249. while(this.inputDiv.parentElement.childNodes.length > 1) {
  250. this.inputDiv.parentElement.removeChild(this.inputDiv.parentElement.firstChild);
  251. }
  252. this.input.value = "";
  253. this.inputSpan.innerHTML = '';
  254. }
  255. clearBtnClick () {
  256. this.clear();
  257. }
  258. showBtnClick () {
  259. this.focus();
  260. }
  261. hideBtnClick () {
  262. this.hide();
  263. }
  264. notifyIdle () {
  265. this.info(LocalizedStrings.getMessage('awaiting_input_message'));
  266. this.inputCMD.click();
  267. }
  268. scheduleNotify () {
  269. this.idleInterval = window.setInterval(this.notifyIdle.bind(this), Config.idle_input_interval);
  270. }
  271. }