1
0

domConsole.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import $ from 'jquery';
  2. export class DOMConsole {
  3. static get USER () {
  4. return 0;
  5. }
  6. static get INFO () {
  7. return 1;
  8. }
  9. static get ERR () {
  10. return 2;
  11. }
  12. constructor (elementID) {
  13. this.input = null;
  14. this.needInput = false;
  15. this.termDiv = null;
  16. this.anyKey = false;
  17. this.parent = $(elementID);
  18. this.setup();
  19. this.inputListeners = [];
  20. }
  21. setup () {
  22. this._setupDom();
  23. this._setupEvents();
  24. }
  25. _setupEvents () {
  26. this.input.on("keydown", (event) => {
  27. if (!this.needInput) {
  28. event.preventDefault();
  29. return;
  30. }
  31. const keyCode = event.which;
  32. if (keyCode === 13 || this.anyKey) {
  33. let text = this.input.val();
  34. text = text.replace('[\n\r]+', '');
  35. this.notifyListeners(text);
  36. this.write(text);
  37. this.input.val("");
  38. }
  39. });
  40. }
  41. _setupDom () {
  42. this.termDiv = $("<div></div>");
  43. this.termDiv.addClass("ivprog-term-div");
  44. this.input = $('<input text="type">')
  45. this.input.addClass("ivprog-term-input");
  46. this.termDiv.append(this.input);
  47. this.parent.append(this.termDiv);
  48. }
  49. notifyListeners (text) {
  50. this.inputListeners.forEach(resolve => resolve(text));
  51. this.inputListeners.splice(0, this.inputListeners.length);
  52. this.hideInput();
  53. this.anyKey = false;
  54. }
  55. write (text) {
  56. this._appendText(text, DOMConsole.USER);
  57. }
  58. info (text) {
  59. this._appendText(text, DOMConsole.INFO);
  60. }
  61. err (text) {
  62. this._appendText(text, DOMConsole.ERR);
  63. }
  64. _appendText (text, type) {
  65. const divClass = this.getClassForType(type);
  66. const textDiv = $("<div></div>");
  67. textDiv.addClass(divClass);
  68. textDiv.append(text);
  69. textDiv.insertBefore(this.input);
  70. this.scrollTerm();
  71. }
  72. scrollTerm () {
  73. this.termDiv.animate({
  74. scrollTop: this.termDiv.prop('scrollHeight')
  75. }, 0);
  76. }
  77. getClassForType (type) {
  78. switch (type) {
  79. case DOMConsole.USER:
  80. return "ivprog-term-userText";
  81. case DOMConsole.INFO:
  82. return "ivprog-term-info";
  83. case DOMConsole.ERR:
  84. return "ivprog-term-error";
  85. }
  86. }
  87. dispose () {
  88. this.parent.off();
  89. this.input.off();
  90. this.input = null;
  91. this.parent.empty();
  92. }
  93. showInput () {
  94. this.needInput = true;
  95. this.input.show();
  96. this.input.focus();
  97. }
  98. hideInput () {
  99. this.needInput = false;
  100. this.input.hide();
  101. }
  102. requestInput (callback, anyKey = false) {
  103. this.inputListeners.push(callback);
  104. this.anyKey = anyKey;
  105. this.showInput();
  106. }
  107. sendOutput (text) {
  108. const output = ""+text;
  109. output.split("\n").forEach(t => {
  110. t = t.replace(/\t/g,'&#9;');
  111. this.write(t)
  112. });
  113. }
  114. clear () {
  115. this.input.parent().children().not(this.input).remove();
  116. this.input.val("");
  117. }
  118. }