-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.terminaltype.js
119 lines (104 loc) · 3.19 KB
/
jquery.terminaltype.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
(function ($){
$.fn.terminalType = function(options){
var elementType = this.prop('tagName');
if(elementType !== "TEXTAREA"){
throw new Error("The container needs to be a textarea. Check documentation for further help. ")
}
var self = this;
var options = $.extend({
introText: "Hello, I am some introduction text",
typeText: true,
prependUser: true,
prependTerminal: true,
prependUserText: "> ",
prependTerminalText: "$ ",
focusOnFinish: true,
validCommands: {
hello: "Hi there buddy! You checked the docs?",
},
error: [
"I AM AN ERROR"
]
}, options);
var placeText = function(string){
var result = string.split('');
var i = 0;
if(options.prependTerminal){
self.val(self.val()+ options.prependTerminalText);
}
var place_this = function(){
self.val(self.val() + string);
self.val(self.val()+ "\n");
if(options.prependUser){
self.val(self.val()+ options.prependUserText);
}
if(options.focusOnFinish){
self.focus();
}
return;
}
var type_this = function(){
var typeDelay = Math.random() * (100 - 30) + 10;
if(i < result.length){
self.val(self.val() + result[i]);
i++;
setTimeout(type_this, typeDelay);
} else {
self.val(self.val()+ "\n");
if(options.prependUser){
self.val(self.val()+ options.prependUserText);
}
if(options.focusOnFinish){
self.focus();
}
return;
}
}
if(options.typeText){
type_this();
} else {
place_this();
}
}
placeText(options.introText);
var checkInput = function(input){
var checkUserInput = input.toLowerCase().replace(options.prependUserText, "").trim();
if(options.validCommands[checkUserInput] !== undefined){
placeText(options.validCommands[checkUserInput]);
if(self._onSuccessCallback != undefined){
self._onSuccessCallback(checkUserInput);
}
} else {
placeText(options.error[Math.floor(Math.random() * options.error.length)]);
if(self._onFailureCallback != undefined){
self._onFailureCallback(checkUserInput);
}
}
}
this.keypress(function(e){
if(e.which == 13){
e.preventDefault();
var userInput = $(this).val().substr($(this).val().lastIndexOf("\n") + 1);
$(this).val($(this).val()+ "\n");
checkInput(userInput, $(this));
}
});
return this;
};
$.fn.onTerminalSuccess = function(providedCallback){
if(typeof (providedCallback) === 'function'){
this._onSuccessCallback = providedCallback;
} else {
throw new Error('Provided callback for onTerminalSuccess is not a function.');
}
return this;
};
$.fn.onTerminalFailure = function(providedCallback){
if(typeof (providedCallback) === 'function'){
this._onFailureCallback = providedCallback;
} else {
throw new Error('Provided callback for onTerminalFailure is not a function.');
}
return this;
};
}(jQuery));