domConsole.js 9.2 KB

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