domConsole.js 9.3 KB

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