domConsole.js 2.7 KB

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