|
@@ -12,14 +12,24 @@ export class DOMConsole {
|
|
|
return 2;
|
|
|
}
|
|
|
|
|
|
+ static get INPUT () {
|
|
|
+ return 3;
|
|
|
+ }
|
|
|
+
|
|
|
constructor (elementID) {
|
|
|
this.input = null;
|
|
|
+ this.cursorInterval = null;
|
|
|
+ this.inputDiv = null;
|
|
|
+ this.inputCMD = null;
|
|
|
+ this.inputSpan = null;
|
|
|
+ this.cursorRef = null;
|
|
|
this.needInput = false;
|
|
|
this.termDiv = null;
|
|
|
this.anyKey = false;
|
|
|
this.parent = $(elementID);
|
|
|
this.setup();
|
|
|
this.inputListeners = [];
|
|
|
+ this.hideInput();
|
|
|
}
|
|
|
|
|
|
setup () {
|
|
@@ -30,7 +40,6 @@ export class DOMConsole {
|
|
|
_setupEvents () {
|
|
|
this.input.on("keydown", (event) => {
|
|
|
if (!this.needInput) {
|
|
|
- event.preventDefault();
|
|
|
return;
|
|
|
}
|
|
|
const keyCode = event.which;
|
|
@@ -38,8 +47,9 @@ export class DOMConsole {
|
|
|
let text = this.input.val();
|
|
|
text = text.replace('[\n\r]+', '');
|
|
|
this.notifyListeners(text);
|
|
|
- this.write(text);
|
|
|
+ this._appendUserInput(text);
|
|
|
this.input.val("");
|
|
|
+ this.inputSpan.text("");
|
|
|
}
|
|
|
});
|
|
|
}
|
|
@@ -47,10 +57,52 @@ export class DOMConsole {
|
|
|
_setupDom () {
|
|
|
this.termDiv = $("<div></div>");
|
|
|
this.termDiv.addClass("ivprog-term-div");
|
|
|
- this.input = $('<input text="type">')
|
|
|
- this.input.addClass("ivprog-term-input");
|
|
|
- this.termDiv.append(this.input);
|
|
|
+ this.inputDiv = $(`
|
|
|
+ <div id="ivprog-terminal-inputdiv">
|
|
|
+ <div id="cmd">
|
|
|
+ <span></span>
|
|
|
+ <div id="cursor"></div>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ `);
|
|
|
+ this.input = $('<input type="text" name="command" value=""/>');
|
|
|
+ this.inputDiv.append(this.input);
|
|
|
+ this.cursorRef = $(this.inputDiv.find("#cursor")[0]);
|
|
|
+ this.inputSpan = $(this.inputDiv.find("#cmd").children('span')[0]);
|
|
|
+ this.inputCMD = $(this.inputDiv.find("#cmd")[0]);
|
|
|
+ //this.input.addClass("ivprog-term-input");
|
|
|
+ this.termDiv.append(this.inputDiv);
|
|
|
this.parent.append(this.termDiv);
|
|
|
+ this._setupCursor();
|
|
|
+ }
|
|
|
+
|
|
|
+ _setupCursor () {
|
|
|
+ const outerRef = this
|
|
|
+ this.inputCMD.click(function() {
|
|
|
+ if(outerRef.cursorInterval != null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ outerRef.input.focus();
|
|
|
+ outerRef.cursorInterval = window.setInterval(function() {
|
|
|
+ if (outerRef.cursorRef.css('visibility') === 'visible') {
|
|
|
+ outerRef.cursorRef.css({visibility: 'hidden'});
|
|
|
+ } else {
|
|
|
+ outerRef.cursorRef.css({visibility: 'visible'});
|
|
|
+ }
|
|
|
+ }, 550);
|
|
|
+ });
|
|
|
+
|
|
|
+ this.inputCMD.click();
|
|
|
+
|
|
|
+ this.input.keyup(function() {
|
|
|
+ outerRef.inputSpan.text(outerRef.input.val());
|
|
|
+ });
|
|
|
+
|
|
|
+ this.input.blur(function() {
|
|
|
+ clearInterval(outerRef.cursorInterval);
|
|
|
+ outerRef.cursorInterval = null;
|
|
|
+ outerRef.cursorRef.css({visibility: 'visible'});
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
notifyListeners (text) {
|
|
@@ -77,7 +129,18 @@ export class DOMConsole {
|
|
|
const textDiv = $("<div></div>");
|
|
|
textDiv.addClass(divClass);
|
|
|
textDiv.append(text);
|
|
|
- textDiv.insertBefore(this.input);
|
|
|
+ textDiv.insertBefore(this.inputDiv);
|
|
|
+ this.scrollTerm();
|
|
|
+ }
|
|
|
+
|
|
|
+ _appendUserInput (text) {
|
|
|
+ const divClass = this.getClassForType(DOMConsole.INPUT);
|
|
|
+ const textDiv = $(`<div>
|
|
|
+ <i class="icon keyboard outline" style="float:left"></i>
|
|
|
+ <span>${text}</span>
|
|
|
+ </div>`);
|
|
|
+ textDiv.addClass(divClass);
|
|
|
+ textDiv.insertBefore(this.inputDiv);
|
|
|
this.scrollTerm();
|
|
|
}
|
|
|
|
|
@@ -87,8 +150,21 @@ export class DOMConsole {
|
|
|
}, 0);
|
|
|
}
|
|
|
|
|
|
+ focus () {
|
|
|
+ this.parent.show();
|
|
|
+ const prev = this.inputDiv.prev();
|
|
|
+ if(prev.length > 0)
|
|
|
+ prev[0].scrollIntoView();
|
|
|
+ }
|
|
|
+
|
|
|
+ hide () {
|
|
|
+ this.parent.hide();
|
|
|
+ }
|
|
|
+
|
|
|
getClassForType (type) {
|
|
|
switch (type) {
|
|
|
+ case DOMConsole.INPUT:
|
|
|
+ return "ivprog-term-userInput";
|
|
|
case DOMConsole.USER:
|
|
|
return "ivprog-term-userText";
|
|
|
case DOMConsole.INFO:
|
|
@@ -103,17 +179,24 @@ export class DOMConsole {
|
|
|
this.input.off();
|
|
|
this.input = null;
|
|
|
this.parent.empty();
|
|
|
+ if(this.cursorInterval != null) {
|
|
|
+ clearInterval(this.cursorInterval);
|
|
|
+ }
|
|
|
+ this.inputCMD.off();
|
|
|
}
|
|
|
|
|
|
showInput () {
|
|
|
this.needInput = true;
|
|
|
- this.input.show();
|
|
|
- this.input.focus();
|
|
|
+ this.inputDiv.show();
|
|
|
+ this.inputCMD.click();
|
|
|
+ this.inputCMD[0].scrollIntoView();
|
|
|
}
|
|
|
|
|
|
hideInput () {
|
|
|
this.needInput = false;
|
|
|
- this.input.hide();
|
|
|
+ this.inputDiv.hide();
|
|
|
+ clearInterval(this.cursorInterval);
|
|
|
+ this.cursorInterval = null;
|
|
|
}
|
|
|
|
|
|
requestInput (callback, anyKey = false) {
|
|
@@ -131,7 +214,8 @@ export class DOMConsole {
|
|
|
}
|
|
|
|
|
|
clear () {
|
|
|
- this.input.parent().children().not(this.input).remove();
|
|
|
+ this.inputDiv.parent().children().not(this.inputDiv).remove();
|
|
|
this.input.val("");
|
|
|
+ this.inputSpan.text("");
|
|
|
}
|
|
|
}
|