forked from alejandrogarciasalas/restaurant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restaurant.js
413 lines (331 loc) · 10.1 KB
/
restaurant.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
var level;
var currentLevel = parseInt(localStorage.currentLevel,10) || 0;
var levelTimeout = 1000;
var fails = 0;
$(document).ready(function(){
$(".note-toggle").on("click", function(){
$(this).hide();
$(".note").slideToggle();
});
$(".level-menu-toggle-wrapper").on("click",function(){
$(".level-menu").toggleClass("open");
});
//Handle inputs from the input box on enter
$("input").on("keypress",function(e){
e.stopPropagation();
if(e.keyCode == 13){
enterHit();
return false;
}
});
$("input").on("keyup",function(e){
e.stopPropagation();
var length = $(this).val().length;
if(length > 0) {
$("input").removeClass("input-strobe");
} else {
$("input").addClass("input-strobe");
}
});
$(".editor").on("click",function(){
$("input").focus();
});
//Add tooltips
$(".table").on("mouseover","*",function(e){
e.stopPropagation();
showTooltip($(this));
});
//Shows the tooltip on the table
$(".markup").on("mouseover","div *",function(e){
el = $(this);
var markupElements = $(".markup *");
var index = markupElements.index(el) -1;
showTooltip($(".table *").eq(index));
e.stopPropagation();
});
//Shows the tooltip on the table
$(".markup").on("mouseout","*",function(e){
e.stopPropagation();
hideTooltip();
});
$(".table").on("mouseout","*",function(e){
hideTooltip();
e.stopPropagation();
});
$(".enter-button").on("click",function(){
enterHit();
})
$(".table-wrapper,.table-edge").css("opacity",0);
buildLevelmenu();
setTimeout(function(){
loadLevel();
$(".table-wrapper,.table-edge").css("opacity",1);
},50);
});
function buildLevelmenu(){
for(var i = 0; i < levels.length; i++){
var level = levels[i];
var item = document.createElement("a");
$(item).html(level.syntax);
$(".level-menu .levels").append(item);
$(item).on("click",function(){
currentLevel = $(this).index();
loadLevel();
});
}
}
function hideTooltip(){
$(".enhance").removeClass("enhance");
$("[data-hovered]").removeAttr("data-hovered");
$(".helper").hide();
}
function showTooltip(el){
el.attr("data-hovered",true);
var tableElements = $(".table *");
var index = tableElements.index(el);
var that = el;
$(".markup > div *").eq(index).addClass("enhance").find("*").addClass("enhance");
var helper = $(".helper");
var pos = el.offset();
helper.css("top",pos.top - 65);
helper.css("left",pos.left + (el.width()/2));
var helpertext;
var elType = el.get(0).tagName;
elType = elType.toLowerCase();
helpertext = '<' + elType;
var elClass = el.attr("class");
if(elClass) {
if(elClass.indexOf("strobe") > -1){
elClass = elClass.replace("strobe","");
}
}
if(elClass) {
helpertext = helpertext + ' class="' + elClass + '"';
}
var id = el.attr("id");
if(id) {
helpertext = helpertext + ' id="' + id + '"';
}
helpertext = helpertext + '></' + elType + '>';
helper.show();
helper.text(helpertext);
}
//Animate the enter button
function enterHit(){
$(".enter-button").removeClass("enterhit");
$(".enter-button").width($(".enter-button").width());
$(".enter-button").addClass("enterhit");
var value = $("input").val();
handleInput(value);
}
//Parses text from the input field
function handleInput(text){
if(text == ""){
text = "blammojammo";
}
if(parseInt(text,10) > 0 && parseInt(text,10) < levels.length+1) {
currentLevel = parseInt(text,10) - 1;
loadLevel();
return;
}
if(text == "help") {
showHelp();
} else {
fireRule(text);
}
}
//Shows help
function showHelp() {
$("input").val("");
var helpTitle = level.helpTitle || "";
var help = level.help || "";
var examples = level.examples ||[];
var selector = level.selector || "";
var syntax = level.syntax || "";
var syntaxExample = level.syntaxExample || "";
var selectorName = level.selectorName || "";
$(".display-help .syntax").html(syntax);
$(".display-help .syntax-example").html(syntaxExample);
$(".display-help .selector-name").html(selectorName);
$(".display-help .title").html(helpTitle);
$(".display-help .examples").html("");
for(var i = 0; i < examples.length; i++){
var example = $("<div class='example'>" + examples[i] + "</div>");
$(".display-help .examples").append(example);
}
$(".display-help .hint").html(help);
$(".display-help .selector").text(selector);
}
function resetTable(){
$(".display-help").removeClass("open-help");
$(".clean,.strobe").removeClass("clean,strobe");
$(".clean,.strobe").removeClass("clean,strobe");
$("input").addClass("input-strobe");
$(".table *").each(function(){
$(this).width($(this).width());
$(this).removeAttr("style");
});
$(".table-edge").width($(".table").outerWidth());
}
function fireRule(rule) {
// prevent cheating
if(rule === ".strobe") {
rule = null;
}
$(".shake").removeClass("shake");
$(".strobe,.clean,.shake").each(function(){
$(this).width($(this).width());
$(this).removeAttr("style");
});
/*
* Sean Nessworthy <sean@nessworthy.me>
* On 03/17/14
*
* Allow [div][.table] to preceed the answer.
* Makes sense if div.table is going to be included in the HTML viewer
* and users want to try and use it in their selectors.
*
* However, if it is included as a specific match, filter it out.
* This resolves the "Match all the things!" level from beheading the table too.
* Relatedly, watching that happen made me nearly spill my drink.
*/
var baseTable = $('.table-wrapper > .table');
// var ruleSelected = $(".table-wrapper " + rule).not(baseTable);
// var levelSelected = $(".table-wrapper " + level.selector).not(baseTable);
var ruleSelected = $(".table-wrapper").find(rule).not(baseTable);
var levelSelected = $(".table-wrapper").find(level.selector).not(baseTable);
var win = false;
//If nothing is selected
if(ruleSelected.length == 0) {
$(".editor").addClass("shake");
}
if(ruleSelected.length == levelSelected.length && ruleSelected.length > 0){
win = checkResults(ruleSelected,levelSelected,rule);
}
if(win){
ruleSelected.removeClass("strobe");
ruleSelected.addClass("clean");
// $(".result").text("Good job!");
$("input").val("");
$(".input-wrapper").css("opacity",.2);
currentLevel++;
if(currentLevel >= levels.length) {
winGame();
} else {
setTimeout(function(){
loadLevel();
},levelTimeout);
}
} else {
continueRule();
ruleSelected.removeClass("strobe");
ruleSelected.addClass("shake");
setTimeout(function(){
$(".shake").removeClass("shake");
$(".strobe").removeClass("strobe");
levelSelected.addClass("strobe");
},500);
$(".result").fadeOut();
}
}
function winGame(){
$(".table").html('<span class="winner"><strong>You did it!</strong><br>You are a CSS God.</span>');
resetTable();
}
function checkResults(ruleSelected,levelSelected,rule){
var ruleTable = $(".table").clone();
ruleTable.find(".strobe").removeClass("strobe");
ruleTable.find(rule).addClass("strobe");
return($(".table").html() == ruleTable.html());
}
var d = 2;
function continueRule() {
console.log("Fails thus far: " + ++fails)
}
function loadBoard(){
var boardString = level.board;
boardMarkup = "";
var tableMarkup = "";
var markup = "";
showHelp();
for(var i = 0;i < boardString.length;i++){
var c = boardString.charAt(i);
if(c == "C") {
boardMarkup = boardMarkup + '<carrot/>\n'
markup = markup + "<div><carrot/></div>";
}
if(c == "A") {
boardMarkup = boardMarkup + '<apple/>\n'
markup = markup + "<div><apple/></div>";
}
if(c == "O") {
boardMarkup = boardMarkup + '<orange/>\n'
markup = markup + "<div><orange/></div>";
}
if(c == "P") {
boardMarkup = boardMarkup + '<pickle/>\n'
markup = markup + '<div><pickle/></div>';
}
if(c == "a") {
boardMarkup = boardMarkup + '<apple class="small"/>\n'
markup = markup + '<div><apple class="small"/></div>';
}
if(c == "o") {
boardMarkup = boardMarkup + '<orange class="small"/>\n'
markup = markup + '<div><orange class="small"/></div>';
}
if(c == "p") {
boardMarkup = boardMarkup + '<pickle class="small"/>\n'
markup = markup + '<div><pickle class="small"/></div>';
}
if(c == "{") {
boardMarkup = boardMarkup + '<plate id="fancy">'
markup = markup + '<div><plate id="fancy">';
}
if(c == "(") {
boardMarkup = boardMarkup + '<plate>'
markup = markup + '<div><plate>'
}
if(c == ")" || c == "}") {
boardMarkup = boardMarkup + '</plate>\n'
markup = markup + '</plate></div>'
}
if(c == "[") {
boardMarkup = boardMarkup + '<bento>'
markup = markup + '<div><bento>'
}
if(c == "]") {
boardMarkup = boardMarkup + '</bento>\n'
markup = markup + '</bento></div>'
}
}
$(".table").html(boardMarkup);
$(".markup").html('<div><div class="table">' + markup + '</div></div>');
}
//Loads up a level
function loadLevel(){
level = levels[currentLevel];
// Show the help link only for the first three levels
if(currentLevel < 3) {
$(".note-toggle").show();
} else {
$(".note-toggle").hide();
}
$(".level-menu .current").removeClass("current");
$(".level-menu div a").eq(currentLevel).addClass("current");
var percent = (currentLevel+1)/levels.length * 100;
$(".progress").css("width",percent + "%");
localStorage.setItem("currentLevel",currentLevel);
loadBoard();
resetTable();
$(".level-header").html("Level " + (currentLevel+1) + " of " + levels.length);
$(".order").text(level.doThis);
$("input").val("").focus();
$(".input-wrapper").css("opacity",1);
$(".result").text("");
//Strobe what's supposed to be selected
$(".table " + level.selector).addClass("strobe");
}