domConsole.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. // Is in draggable mode?
  178. console.log(this.parent.style.top.length);
  179. if(this.parent.style.top.length == 0) {
  180. this.parent.style.marginTop = "-160px";
  181. }
  182. const prev = this.inputDiv.closest('div');
  183. if(prev != null)
  184. prev.scrollIntoView();
  185. }
  186. hide () {
  187. // Is in draggable mode?
  188. if(this.parent.style.top.length == 0) {
  189. this.parent.style.marginTop = "0";
  190. }
  191. this.termDiv.style.display = 'none';
  192. }
  193. getClassForType (type) {
  194. switch (type) {
  195. case DOMConsole.INPUT:
  196. return "ivprog-term-userInput";
  197. case DOMConsole.USER:
  198. return "ivprog-term-userText";
  199. case DOMConsole.INFO:
  200. return "ivprog-term-info";
  201. case DOMConsole.ERR:
  202. return "ivprog-term-error";
  203. }
  204. }
  205. dispose () {
  206. this.input.removeEventListener('keyup', this.updateSpanText.bind(this));
  207. this.input.removeEventListener('blur', this.stopBlinkCaret.bind(this));
  208. this.input.removeEventListener('keydown', this.registerInput.bind(this));
  209. this.inputCMD.removeEventListener('click', this.blinkCaretAndFocus.bind(this));
  210. this.clearBtn.removeEventListener('click', this.clearBtnClick.bind(this));
  211. this.hideBtn.removeEventListener('click', this.hideBtnClick.bind(this));
  212. this.showBtn.removeEventListener('click', this.showBtnClick.bind(this));
  213. this.input = null;
  214. this.inputCMD = null;
  215. this.inputDiv = null;
  216. this.termDiv = null;
  217. this.inputSpan = null;
  218. this.cursorRef = null;
  219. this.clearBtn = null;
  220. this.hideBtn = null;
  221. this.showBtn = null;
  222. const cNode = this.parent.cloneNode(false);
  223. this.parent.parentNode.replaceChild(cNode, this.parent);
  224. if(this.cursorInterval != null) {
  225. clearInterval(this.cursorInterval);
  226. }
  227. if(this.idleInterval != null) {
  228. clearInterval(this.idleInterval);
  229. }
  230. }
  231. showInput () {
  232. this.needInput = true;
  233. this.inputDiv.style.display = 'block';
  234. this.inputCMD.click();
  235. this.inputCMD.scrollIntoView();
  236. }
  237. hideInput () {
  238. this.needInput = false;
  239. this.inputDiv.style.display = ' none';
  240. clearInterval(this.cursorInterval);
  241. this.cursorInterval = null;
  242. }
  243. requestInput (callback, anyKey = false) {
  244. this.inputListeners.push(callback);
  245. this.anyKey = anyKey;
  246. if(this.idleInterval == null)
  247. this.scheduleNotify();
  248. this.showInput();
  249. }
  250. sendOutput (text) {
  251. const output = ""+text;
  252. output.split("\n").forEach(t => {
  253. t = t.replace(/\t/g,'&#9;');
  254. this.write(t)
  255. });
  256. }
  257. clear () {
  258. while(this.inputDiv.parentElement.childNodes.length > 1) {
  259. this.inputDiv.parentElement.removeChild(this.inputDiv.parentElement.firstChild);
  260. }
  261. this.input.value = "";
  262. this.inputSpan.innerHTML = '';
  263. }
  264. clearBtnClick () {
  265. this.clear();
  266. }
  267. showBtnClick () {
  268. this.focus();
  269. }
  270. hideBtnClick () {
  271. this.hide();
  272. }
  273. notifyIdle () {
  274. this.info(LocalizedStrings.getMessage('awaiting_input_message'));
  275. this.inputCMD.click();
  276. }
  277. scheduleNotify () {
  278. this.idleInterval = window.setInterval(this.notifyIdle.bind(this), Config.idle_input_interval);
  279. }
  280. }