-
Notifications
You must be signed in to change notification settings - Fork 0
/
komando-src.js
193 lines (171 loc) · 5 KB
/
komando-src.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
* komandoJS, by muratkemaldar
* https://github.com/muratkemaldar/komandojs
* version 1.0.0
*/
const komando = {
// the input where the user enters commands.
input: undefined,
// array of command objects with command-string and action.
commands: undefined,
// display of commandLine, output field.
display: {
panel: undefined,
mostRecentLine: undefined,
print(content, lineClass = 'default', robot = true) {
if (this.panel === undefined) return;
let line = document.createElement('p');
line.className = 'line ' + lineClass + " " + (robot ? 'robot' : 'user');
line.innerHTML = content;
this.panel.appendChild(line);
this.mostRecentLine = line;
komando.input.value = '';
komando.triggerEvent('displayprint', {line: line, robot: robot});
}
},
// options
options: {
focusInput: false,
defaultCommandNotFoundMessage: '¯\\_(ツ)_/¯'
},
// state properties set on runtime
state: {
commandsEntered: false,
numberOfCommandsEntered: 0
},
// history, where user can use arrow keys to navigate
history: {
cursor: undefined,
commands: [],
add(command) {
this.commands.unshift(command);
this.cursor = undefined;
komando.triggerEvent('historyadd', {command: command});
},
navigate(direction) {
if (this.commands.length === 0) return;
if (this.cursor === undefined && direction == 'up') {
this.cursor = 0;
} else if (direction == 'up' && this.cursor < this.commands.length - 1) {
this.cursor = this.cursor + 1;
} else if (direction == 'down' && this.cursor > 0) {
this.cursor = this.cursor - 1;
}
if (this.cursor !== undefined) {
komando.input.value = this.commands[this.cursor];
}
}
},
init(initParams) {
// set commands
this.commands = initParams.commands;
// extend options
if (initParams.options) {
for (var option in initParams.options) {
if (this.options.hasOwnProperty(option)) {
this.options[option] = initParams.options[option];
}
}
}
// create command map
this.commandMap = {};
for (var cid = 0; cid < this.commands.length; cid++) {
this.commandMap[this.commands[cid].command] = cid;
this.commands[cid]._commandId = cid;
}
// init input
if (initParams.input) {
this.input = initParams.input;
this.input.addEventListener('keydown', event => {
if (event.keyCode == 13 && this.input.value) { //Enter
this.handleCommand(this.input.value);
}
if (event.keyCode == 38) { //ArrowUp
this.history.navigate('up');
event.preventDefault();
}
if (event.keyCode == 40) { //ArrowDown
this.history.navigate('down');
event.preventDefault();
}
});
if (this.options.focusInput) {
this.input.focus();
}
} else {
if (console) {
console.warn('input not found, you need a DOM element in komando.init({...}).')
}
}
// init display
if (initParams.display) {
this.display.panel = initParams.display;
} else {
if (console) {
console.warn('display not found, you need a DOM element in komando.init({...}).')
}
}
// callback
if (initParams.callback) {
initParams.callback();
}
},
// called after input was entered
handleCommand(command){
// first time handled
if (this.display.panel) {
if (!this.state.commandsEntered) {
this.display.panel.innerHTML = "";
this.display.panel.classList.add(['active']);
this.state.commandsEntered = true;
}
this.display.print(command, 'default', false);
}
// check for help
if (command == 'help' || command == '/help'){
for (let c of this.commands) {
let text = c.command + (c.parameterHint ? " " + c.parameterHint : "");
this.display.print(text, 'info', true);
}
this.triggerEvent('handlecommand', {command: command});
return;
}
// core functionality
if (this.commandMap.hasOwnProperty(command)){
// if the exact command is defined in commandMap, call the action
this.commands[this.commandMap[command]].action(command, this.display);
} else {
// if not, loop through commands and filter (to check if it has paramaters)
let commandObj = this.commands.filter(c => command.split(" ").indexOf(c.command) === 0)[0];
if (commandObj) {
let paramString = command.slice(commandObj.command.length).trim();
let paramArray = paramString.split(" ");
commandObj.action(command, this.display, {string: paramString, array: paramArray});
} else {
// if all fails, display print error
this.display.print(this.options.defaultCommandNotFoundMessage, 'error');
}
}
// put it in history
this.history.add(command);
// dispatch the event
this.triggerEvent('handlecommand', {command: command});
},
// internal event triggering
triggerEvent(eventName, komandoArgs) {
var e = document.createEvent('Event');
for (var arg in komandoArgs) {
if (komandoArgs.hasOwnProperty(arg)) {
e[arg] = komandoArgs[arg];
}
}
e.initEvent(eventName, true, true);
window.dispatchEvent(e);
},
// trigger callbacks
on(event, callback) {
window.addEventListener(event, callback);
},
}
// attach to window
window.komando = komando;