-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathjsify.js
311 lines (291 loc) · 9.32 KB
/
jsify.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
var tilt = 0;
var linkify = function(text, url) {
return "<a target='_blank' href='" + url + "'>" + text + "</a>";
};
var specialify = function(text) {
return "<span class='special'>" + text + "</span>";
};
var data = {
hello: "TAMS Computer Science Organization aims to " + specialify("promote Computer Science") + "\n\tin the TAMS community by providing " + specialify("mentorship") + " to those interested in\n\tfurther pursuing the field and attending " + specialify("competitions") + " ranging\n\tfrom " + specialify("major hackathons") + " to " + specialify("algorithm contests") + ".",
help: [{
name: specialify("help"),
description: "Display all commands"
}, {
name: specialify("hello"),
description: "Display introduction message"
}, {
name: specialify("leaderboard"),
description: "See club leaderboard"
}, {
name: specialify("team"),
description: "See the club executives"
}, {
name: specialify("competitions"),
description: "Show upcoming algorithm competitions and hackathons"
}, {
name: specialify("links"),
description: "Displays links to permission slips, resources, and more"
}, {
name: specialify("showcase"),
description: "TAMS students\' cool projects"
}, {
name: specialify("contact"),
description: "Contact TAMS Computer Science Organization club executives"
}, {
name: specialify("facebook"),
description: "Go to the CSO Facebook group"
}],
contact: "Feel free to send us an email (<a href='mailto:[email protected]'>[email protected]</a>) or contact a club executive (type <span class='special'>team</span>) individually via Facebook.",
team: [{
name: specialify("Manasi") + " Ramadurgum",
position: "President"
}, {
name: specialify("Ryan") + " Chhong",
position: "Competitions Coordinator"
}, {
name: specialify("Samuel") + " Liu",
position: "Volunteering Coordinator"
}, {
name: specialify("Justin") + " Cho",
position: "Publicist/Graphic Designer"
}],
competitions: "Competition schedule coming soon!",
links: "Nothing here... yet!",
showcase: [{
name: linkify("Scheduler", "http://schedulerapp.net"),
authors: "By Thomas Hobohm, Jeffrey Wang, Garrett Gu"
}, {
name: linkify("TAMS Translator", "https://chrome.google.com/webstore/detail/tams-translator/nbgijihbcldodkahfdpldfdacpecbkci"),
authors: "By Nimit Kalra"
}, {
name: linkify("Fortify", "http://getfortify.com"),
authors: "By Shashank Bhavimane, Thomas Hobohm"
}, {
name: linkify("Scheme", "http://getsche.me"),
authors: "By Thomas Hobohm, Zain Khoja, Leanne Joseph, Michelle Ya"
}, {
name: linkify("PocketBox", "http://pocketbox.net"),
authors: "By Thomas Hobohm"
}, {
name: specialify("Cool Project"),
authors: "By <span class='special'>you</span>? Contact us if you've made a cool project you'd like featured!"
}],
invalid: "That's not a valid command! Type <span class='special'>help</span> for a list of commands."
};
var describify = function(obj) {
var output = "";
properties = [];
for (var property in obj) {
if (obj.hasOwnProperty(property)) {
properties.push(obj[property]);
}
}
output += properties.join(" | ");
return output;
};
var listify = function(list) {
var output = "";
for (var i = 0; i < list.length; i++) {
output += "\t• ";
var described = describify(list[i]);
output += described;
output += "\n";
}
return output;
};
var format = function(field) {
var unformatted = data[field];
if (typeof unformatted == 'string') {
return "\n\t" + unformatted + "\n\n";
}
return "\n" + listify(unformatted) + "\n";
};
var lambdify = function(format_string) {
return function() { return format(format_string) }
}
var options = {
include: ["score"],
shouldSort: true,
threshold: 0.3,
location: 0,
distance: 100,
maxPatternLength: 32,
keys: ['command']
};
var search = function(list, searchitem) {
options.distance = (searchitem.length<=2? 0 : 100)
return new Fuse(list, options).search(searchitem)
}
var loadBoard = function() {
$.get('http://tamscso.ga/board/rest/fetch_users', function(data, status) {
var people = JSON.parse(data);
$('.board-container').html(
specialify("\n\tName" + new Array(55-4).join(' ') + "Score\n\t")+
people
.sort(function(a, b) {
return b.score - a.score;
})
.map(function(item) {
var name = item.profile.name;
var score = item.score;
if(name.length > 50){
name = name.substring(0, 47) + '...';
}
var spacesToAdd = 55-name.length;
name += new Array(spacesToAdd).join(' ');
return name + score;
})
.join('\n\t')+
"\n\n\t"+linkify('Click here', 'http://tamscso.ga/board')+" to see the full leaderboard."
);
})
}
$(function() {
var intro = "Welcome to the Computer Science Organization (CSO) at TAMS!\nType 'hello' below to learn what we're all about! Try '?' for more.\n\n";
var jqconsole = $('#console').jqconsole(intro, 'cso> ');
var cout = function(input){
jqconsole.Write(" "+input.replace(/\n/g,'\n ')+'\n', 'jqconsole-output', false);
}
function process(input) {
var parsed = input.split(" ");
var commands = [
[["help", "?", "ls"], lambdify('help')],
[["hello", "mission", "description", "why"], lambdify('hello')],
[["team", "execs", "executives", "officers"], lambdify('team')],
[["competitions", "hackathons", "events"], lambdify('competitions')],
[["links", "forms", "info"], lambdify('links')],
[["projects", "showcase"], lambdify('showcase')],
[["contact"], lambdify('contact')],
[["clear", "cls"], function() { jqconsole.Clear(); return '\r'; }],
[["fb", "facebook"], function() {
window.location.href = "https://www.facebook.com/groups/TAMSCompSci2018";
return '\r';
}],
[["tilt"], function() {
tilt += 7;
$(document.documentElement).addClass('rotate_please');
document.documentElement.setAttribute('style', 'transform: rotate(' + String(tilt) + 'deg);');
return "\n\n";
}],
[["qt"], function() {
tilt += 70;
$(document.documentElement).addClass('qt_please');
document.documentElement.setAttribute('style', 'transform: rotate(' + String(tilt) + 'deg);');
return "\n\n";
}],
[["untilt"], function() {
tilt = 0;
$(document.documentElement).removeClass('rotate_please');
$(document.documentElement).removeClass('qt_please');
document.documentElement.setAttribute('style', 'transform: rotate(' + String(tilt) + 'deg);');
return "\n\n";
}],
[["js", "javascript", "code"], function() {}], //since this is processed directly, there is no need for a function here
[["leaderboard"], function() {
jqconsole.Append($('<div class="board-container">Loading...</div>'));
loadBoard();
return '\n';
}]
];
var response = null;
commands.forEach(function(key, index, commands) {
key[0].forEach(function(term, tindex) {
if (term === parsed[0]) {
response = key[1]();
}
});
});
if (response) {
return response;
} else {
var commands_list = [];
commands.forEach(function(key, index, commands) {
key[0].forEach(function(term, tindex) {
if (term !== "qt") {
commands_list.push({
'command': term,
'callback': key[1]
});
}
})
});
var results = search(commands_list, parsed[0]);
if (results.length > 0) {
response = '\nThat command doesn\'t exist. Did you mean ';
results.forEach(function(result, index) {
if (index === results.length - 1 && results.length == 2) {
response = response.substring(0, response.length - 2);
response += ' or ' + specialify(result['item']['command']) + ', ';
} else if (index === results.length - 1 && results.length > 1) {
response = response.substring(0, response.length - 2);
response += ', or ' + specialify(result['item']['command']) + ', ';
} else {
response += specialify(result['item']['command']) + ', ';
}
});
response = response.substring(0, response.length - 2);
response += '?\n\n';
return response;
} else {
return '\nThat command doesn\'t exist. Here is a list of commands you can use:\n' + format('help');
}
}
};
var jsmode = false;
var blockQuery = false;
var block = "";
var processJs = function(input){
if(input.match(/exit/) || input.match(/process.exit\(/)){
jsmode = false;
jqconsole.SetPromptLabel("cso> ");
jqconsole.Write('\n');
startPrompt();
return;
}
if(!blockQuery){
block = "";
}
block+=input;
var leftParens = (block.match(/\{/g)? block.match(/\{/g).length: 0);
var rightParens = (block.match(/\}/g)? block.match(/\}/g).length: 0);
if(leftParens > rightParens){
blockQuery = true;
startPrompt();
return;
}else{
blockQuery = false;
}
var result;
try{
result = eval('eval')(block); //indirect call so it's global
}catch(err){
result = err;
}
result = String(result);
cout(result);
}
var processQuery = function(input) {
if(jsmode){
processJs(input);
startPrompt();
return;
}
if (input) {
if(input == "javascript" || input == "js" || input == "code" || input == "node"){
jsmode = true;
jqconsole.SetPromptLabel(" > ");
jqconsole.Write('\n');
startPrompt();
return;
}
jqconsole.Write(process(input),'jqconsole-output',false);
} else {
jqconsole.Write('\n Here is a list of commands:\n' + format('help'), 'jqconsole-output', false);
}
startPrompt();
}
var startPrompt = function() {
jqconsole.Prompt(true, processQuery);
};
startPrompt();
});