1
- /* This Source Code Form is subject to the terms of the Mozilla Public
2
- * License, v. 2.0. If a copy of the MPL was not distributed with this
3
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
-
5
1
/*
6
2
Function Reference
7
3
==================
4
+
8
5
loadLevel() - loads up the level
9
6
fireRule() - fires the css rule
10
7
updateProgressUI() - adds a checkmark to the level menu and header when a correct guess is made, removes it if incorrect
11
- ..to be continued
8
+ hideTooltip() - hides markup tooltip that hovers over the elements
9
+ showHelp() - Loads help text & examples for each level
10
+
11
+ ..to be continued!
12
12
*/
13
13
14
14
var level ; // Holds current level info
@@ -50,6 +50,7 @@ $(document).ready(function(){
50
50
}
51
51
} ) ;
52
52
53
+ // Custom scrollbar plugin
53
54
$ ( ".left-col, .level-menu" ) . mCustomScrollbar ( {
54
55
scrollInertia : 0 ,
55
56
autoHideScrollbar : true
@@ -68,6 +69,37 @@ $(document).ready(function(){
68
69
}
69
70
} ) ;
70
71
72
+ $ ( ".level-nav" ) . on ( "click" , "a" , function ( ) {
73
+
74
+ var direction ;
75
+ if ( $ ( this ) . hasClass ( "next" ) ) {
76
+ direction = "next" ;
77
+ }
78
+
79
+ addAnimation ( $ ( this ) , "link-jiggle" ) ;
80
+
81
+ if ( direction == "next" ) {
82
+ currentLevel ++ ;
83
+ if ( currentLevel >= levels . length ) {
84
+ currentLevel = levels . length - 1 ;
85
+ }
86
+ } else {
87
+ currentLevel -- ;
88
+ if ( currentLevel < 0 ) {
89
+ currentLevel = 0 ;
90
+ }
91
+ }
92
+
93
+ loadLevel ( ) ;
94
+ return false ;
95
+ } ) ;
96
+
97
+ // Resets progress and progress indicators
98
+ $ ( ".reset-progress" ) . on ( "click" , function ( ) {
99
+ resetProgress ( ) ;
100
+ return false ;
101
+ } )
102
+
71
103
//Handle inputs from the input box on enter
72
104
$ ( "input" ) . on ( "keypress" , function ( e ) {
73
105
e . stopPropagation ( ) ;
@@ -106,13 +138,13 @@ $(document).ready(function(){
106
138
e . stopPropagation ( ) ;
107
139
} ) ;
108
140
109
- //Shows the tooltip on the table
141
+ // Shows the tooltip on the table
110
142
$ ( ".markup" ) . on ( "mouseout" , "*" , function ( e ) {
111
143
e . stopPropagation ( ) ;
112
144
hideTooltip ( ) ;
113
145
} ) ;
114
146
115
- $ ( ".table" ) . on ( "mouseout" , "*" , function ( e ) {
147
+ $ ( ".table" ) . on ( "mouseout" , "*" , function ( e ) {
116
148
hideTooltip ( ) ;
117
149
e . stopPropagation ( ) ;
118
150
} ) ;
@@ -129,9 +161,29 @@ $(document).ready(function(){
129
161
loadLevel ( ) ;
130
162
$ ( ".table-wrapper,.table-edge" ) . css ( "opacity" , 1 ) ;
131
163
} , 50 ) ;
132
-
133
164
} ) ;
134
165
166
+ function addAnimation ( el , className ) {
167
+ el . addClass ( "link-jiggle" ) ;
168
+ el . one ( "animationend" , function ( e ) {
169
+ $ ( e . target ) . removeClass ( "link-jiggle" ) ;
170
+ } )
171
+ }
172
+
173
+ // Reset all progress!
174
+ // Should also scroll the menu to the top...
175
+ function resetProgress ( ) {
176
+ currentLevel = 0 ;
177
+ progress = blankProgress ;
178
+ localStorage . setItem ( "progress" , JSON . stringify ( progress ) ) ;
179
+
180
+ $ ( ".completed" ) . removeClass ( "completed" ) ;
181
+ loadLevel ( ) ;
182
+ closeMenu ( ) ; // what does it do??
183
+ $ ( "#mCSB_2_container" ) . css ( "top" , 0 ) ; // weird scroll reset - because of the
184
+ }
185
+
186
+
135
187
//Checks if the level is completed
136
188
137
189
function checkCompleted ( levelNumber ) {
@@ -181,6 +233,7 @@ function hideTooltip(){
181
233
}
182
234
183
235
function showTooltip ( el ) {
236
+
184
237
if ( finished ) {
185
238
return ;
186
239
}
@@ -215,6 +268,12 @@ function showTooltip(el){
215
268
helpertext = helpertext + ' class="' + elClass + '"' ;
216
269
}
217
270
271
+ var elFor = el . attr ( "for" ) ;
272
+
273
+ if ( elFor ) {
274
+ helpertext = helpertext + ' for="' + elFor + '"' ;
275
+ }
276
+
218
277
var id = el . attr ( "id" ) ;
219
278
if ( id ) {
220
279
helpertext = helpertext + ' id="' + id + '"' ;
@@ -223,7 +282,6 @@ function showTooltip(el){
223
282
helpertext = helpertext + '></' + elType + '>' ;
224
283
helper . show ( ) ;
225
284
helper . text ( helpertext ) ;
226
-
227
285
}
228
286
229
287
@@ -244,18 +302,12 @@ function handleInput(text){
244
302
loadLevel ( ) ;
245
303
return ;
246
304
}
247
-
248
- if ( text == "help" ) {
249
- showHelp ( ) ;
250
- } else {
251
- fireRule ( text ) ;
252
- }
305
+ fireRule ( text ) ;
253
306
}
254
307
255
- //Shows help
308
+ // Loads up the help text & examples for each level
256
309
function showHelp ( ) {
257
310
258
- $ ( "input" ) . val ( "" ) ;
259
311
var helpTitle = level . helpTitle || "" ;
260
312
var help = level . help || "" ;
261
313
var examples = level . examples || [ ] ;
@@ -269,11 +321,14 @@ function showHelp() {
269
321
$ ( ".display-help .selector-name" ) . html ( selectorName ) ;
270
322
$ ( ".display-help .title" ) . html ( helpTitle ) ;
271
323
$ ( ".display-help .examples" ) . html ( "" ) ;
324
+ $ ( ".display-help .examples-title" ) . hide ( ) ; // Hide the "Examples" heading
272
325
273
326
for ( var i = 0 ; i < examples . length ; i ++ ) {
274
327
var example = $ ( "<div class='example'>" + examples [ i ] + "</div>" ) ;
275
328
$ ( ".display-help .examples" ) . append ( example ) ;
329
+ $ ( ".display-help .examples-title" ) . show ( ) ; // Show it if there are examples
276
330
}
331
+
277
332
$ ( ".display-help .hint" ) . html ( help ) ;
278
333
$ ( ".display-help .selector" ) . text ( selector ) ;
279
334
}
@@ -285,9 +340,12 @@ function resetTable(){
285
340
$ ( "input" ) . addClass ( "input-strobe" ) ;
286
341
$ ( ".table *" ) . each ( function ( ) {
287
342
$ ( this ) . width ( $ ( this ) . width ( ) ) ;
288
- $ ( this ) . removeAttr ( "style" ) ;
343
+ // $(this).removeAttr("style");
344
+ // TODO - needed?? Probably not, everything gets removed anyway
289
345
} ) ;
290
- $ ( ".table-edge" ) . width ( $ ( ".table" ) . outerWidth ( ) ) ;
346
+
347
+ var tableWidth = $ ( ".table" ) . outerWidth ( ) ;
348
+ $ ( ".table-wrapper, .table-edge" ) . width ( tableWidth ) ;
291
349
}
292
350
293
351
function fireRule ( rule ) {
@@ -317,17 +375,24 @@ function fireRule(rule) {
317
375
* Relatedly, watching that happen made me nearly spill my drink.
318
376
*/
319
377
320
- var baseTable = $ ( '.table-wrapper > .table' ) ;
378
+ // var baseTable = $('.table-wrapper > .table, .table-wrapper > .nametags, .table-wrapper > .table-surface');
379
+ var baseTable = $ ( '.table' ) ;
321
380
322
- // var ruleSelected = $(".table-wrapper " + rule).not(baseTable);
323
- // var levelSelected = $(".table-wrapper " + level.selector).not(baseTable);
381
+ // Check if jQuery will throw an error trying the mystery rule
382
+ // If it errors out, change the rule to null so the wrong-guess animation will work
383
+ try {
384
+ $ ( ".table" ) . find ( rule ) . not ( baseTable ) ;
385
+ }
386
+ catch ( err ) {
387
+ rule = null ;
388
+ }
324
389
325
- var ruleSelected = $ ( ".table-wrapper " ) . find ( rule ) . not ( baseTable ) ;
326
- var levelSelected = $ ( ".table-wrapper " ) . find ( level . selector ) . not ( baseTable ) ;
390
+ var ruleSelected = $ ( ".table" ) . find ( rule ) . not ( baseTable ) ; // What the correct rule finds
391
+ var levelSelected = $ ( ".table" ) . find ( level . selector ) . not ( baseTable ) ; // What the person finds
327
392
328
393
var win = false ;
329
394
330
- //If nothing is selected
395
+ // If nothing is selected
331
396
if ( ruleSelected . length == 0 ) {
332
397
$ ( ".editor" ) . addClass ( "shake" ) ;
333
398
}
@@ -339,7 +404,6 @@ function fireRule(rule) {
339
404
if ( win ) {
340
405
ruleSelected . removeClass ( "strobe" ) ;
341
406
ruleSelected . addClass ( "clean" ) ;
342
- // $(".result").text("Good job!");
343
407
$ ( "input" ) . val ( "" ) ;
344
408
$ ( ".input-wrapper" ) . css ( "opacity" , .2 ) ;
345
409
updateProgressUI ( currentLevel , true ) ;
@@ -374,7 +438,8 @@ function fireRule(rule) {
374
438
}
375
439
}
376
440
377
-
441
+ // Marks an individual number as completed or incompleted
442
+ // Just in the level heading though, not the level list
378
443
function updateProgressUI ( levelNumber , completed ) {
379
444
if ( completed ) {
380
445
$ ( ".levels a:nth-child(" + ( levelNumber + 1 ) + ")" ) . addClass ( "completed" ) ;
@@ -385,7 +450,6 @@ function updateProgressUI(levelNumber, completed){
385
450
}
386
451
387
452
function trackProgress ( levelNumber , type ) {
388
-
389
453
if ( ! progress . guessHistory [ levelNumber ] ) {
390
454
progress . guessHistory [ levelNumber ] = {
391
455
correct : false ,
@@ -433,6 +497,7 @@ function sendEvent(category, action, label){
433
497
434
498
function winGame ( ) {
435
499
$ ( ".table" ) . html ( '<span class="winner"><strong>You did it!</strong><br>You rock at CSS.</span>' ) ;
500
+ addNametags ( ) ;
436
501
finished = true ;
437
502
resetTable ( ) ;
438
503
}
@@ -444,72 +509,90 @@ function checkResults(ruleSelected,levelSelected,rule){
444
509
return ( $ ( ".table" ) . html ( ) == ruleTable . html ( ) ) ;
445
510
}
446
511
512
+ // Returns all formatted markup within an element...
513
+
514
+ function getMarkup ( el ) {
515
+ var hasChildren = el . children . length > 0 ? true : false ;
516
+ var elName = el . tagName . toLowerCase ( ) ;
517
+ var wrapperEl = $ ( "<div/>" ) ;
518
+ var attributeString = "" ;
519
+ $ . each ( el . attributes , function ( ) {
520
+ if ( this . specified ) {
521
+ attributeString = attributeString + ' ' + this . name + '="' + this . value + '"' ;
522
+ }
523
+ } ) ;
524
+ var attributeSpace = "" ;
525
+ if ( attributeString . length > 0 ) {
526
+ attributeSpace = " " ;
527
+ }
528
+ if ( hasChildren ) {
529
+ wrapperEl . text ( "<" + elName + attributeSpace + attributeString + ">" ) ;
530
+ $ ( el . children ) . each ( function ( i , el ) {
531
+ wrapperEl . append ( getMarkup ( el ) ) ;
532
+ } ) ;
533
+ wrapperEl . append ( "<" + elName + "/>" ) ;
534
+ } else {
535
+ wrapperEl . text ( "<" + elName + attributeSpace + attributeString + "/>" ) ;
536
+ }
537
+ return wrapperEl ;
538
+ }
539
+
540
+ //new board loader...
541
+
447
542
function loadBoard ( ) {
448
- var boardString = level . board ;
449
- boardMarkup = "" ;
450
- var tableMarkup = "" ;
451
- var markup = "" ;
543
+
544
+ var boardString = level . board ; // just a placeholder to iterate over...
545
+ boardMarkup = "" ; // what is this
546
+ var tableMarkup = "" ; // what is this
547
+ var editorMarkup = "" ; // this is a string that represents the HTML
452
548
showHelp ( ) ;
453
549
454
- for ( var i = 0 ; i < boardString . length ; i ++ ) {
455
- var c = boardString . charAt ( i ) ;
550
+ var markupHolder = $ ( "<div/>" )
456
551
457
- if ( c == "C" ) {
458
- boardMarkup = boardMarkup + '<carrot/>\n'
459
- markup = markup + "<div><carrot/></div>" ;
460
- }
461
- if ( c == "A" ) {
462
- boardMarkup = boardMarkup + '<apple/>\n'
463
- markup = markup + "<div><apple/></div>" ;
552
+ $ ( level . boardMarkup ) . each ( function ( i , el ) {
553
+ if ( el . nodeType == 1 ) {
554
+ var result = getMarkup ( el ) ;
555
+ markupHolder . append ( result ) ;
464
556
}
465
- if ( c == "O" ) {
466
- boardMarkup = boardMarkup + '<orange/>\n'
467
- markup = markup + "<div><orange/></div>" ;
468
- }
469
- if ( c == "P" ) {
470
- boardMarkup = boardMarkup + '<pickle/>\n'
471
- markup = markup + '<div><pickle/></div>' ;
472
- }
473
- if ( c == "a" ) {
474
- boardMarkup = boardMarkup + '<apple class="small"/>\n'
475
- markup = markup + '<div><apple class="small"/></div>' ;
476
- }
477
- if ( c == "o" ) {
478
- boardMarkup = boardMarkup + '<orange class="small"/>\n'
479
- markup = markup + '<div><orange class="small"/></div>' ;
480
- }
481
- if ( c == "p" ) {
482
- boardMarkup = boardMarkup + '<pickle class="small"/>\n'
483
- markup = markup + '<div><pickle class="small"/></div>' ;
484
- }
485
- if ( c == "{" ) {
486
- boardMarkup = boardMarkup + '<plate id="fancy">'
487
- markup = markup + '<div><plate id="fancy">' ;
488
- }
489
- if ( c == "(" ) {
490
- boardMarkup = boardMarkup + '<plate>'
491
- markup = markup + '<div><plate>'
492
- }
493
- if ( c == ")" || c == "}" ) {
494
- boardMarkup = boardMarkup + '</plate>\n'
495
- markup = markup + '</plate></div>'
496
- }
497
- if ( c == "[" ) {
498
- boardMarkup = boardMarkup + '<bento>'
499
- markup = markup + '<div><bento>'
500
- }
501
- if ( c == "]" ) {
502
- boardMarkup = boardMarkup + '</bento>\n'
503
- markup = markup + '</bento></div>'
557
+ } ) ;
558
+
559
+ $ ( ".table" ) . html ( level . boardMarkup ) ;
560
+ addNametags ( ) ;
561
+ $ ( ".table *" ) . addClass ( "pop" ) ;
562
+
563
+
564
+ $ ( ".markup" ) . html ( '<div><div class="table">' + markupHolder . html ( ) + '</div></div>' ) ;
565
+ }
566
+
567
+ // Adds nametags to the items on the table
568
+ function addNametags ( ) {
569
+ $ ( ".nametags *" ) . remove ( ) ;
570
+ $ ( ".table-wrapper" ) . css ( "transform" , "rotateX(0)" ) ;
571
+ $ ( ".table-wrapper" ) . width ( $ ( ".table-wrapper" ) . width ( ) ) ;
572
+
573
+ $ ( ".table *" ) . each ( function ( ) {
574
+ if ( $ ( this ) . attr ( "for" ) ) {
575
+ var pos = $ ( this ) . position ( ) ;
576
+ var width = $ ( this ) . width ( ) ;
577
+ var nameTag = $ ( "<div class='nametag'>" + $ ( this ) . attr ( "for" ) + "</div>" ) ;
578
+ $ ( ".nametags" ) . append ( nameTag ) ;
579
+ var tagPos = pos . left + ( width / 2 ) - nameTag . width ( ) / 2 + 12 ;
580
+ nameTag . css ( "left" , tagPos ) ;
504
581
}
582
+ } ) ;
505
583
506
- }
507
- $ ( ".table" ) . html ( boardMarkup ) ;
508
- $ ( ".markup" ) . html ( '<div><div class="table">' + markup + '</div></div>' ) ;
584
+ $ ( ".table-wrapper" ) . css ( "transform" , "rotateX(20deg)" ) ;
509
585
}
510
586
511
- //Loads up a level
587
+
512
588
function loadLevel ( ) {
589
+ // Make sure we dont' load something out of range
590
+ if ( currentLevel < 0 || currentLevel > levels . length - 1 ) {
591
+ currentLevel = 0 ;
592
+ }
593
+
594
+ hideTooltip ( ) ;
595
+
513
596
level = levels [ currentLevel ] ;
514
597
515
598
// Show the help link only for the first three levels
@@ -537,31 +620,34 @@ function loadLevel(){
537
620
$ ( ".order" ) . text ( level . doThis ) ;
538
621
$ ( "input" ) . val ( "" ) . focus ( ) ;
539
622
540
-
541
623
$ ( ".input-wrapper" ) . css ( "opacity" , 1 ) ;
542
624
$ ( ".result" ) . text ( "" ) ;
543
625
544
626
//Strobe what's supposed to be selected
545
- $ ( ".table " + level . selector ) . addClass ( "strobe" ) ;
546
- }
627
+ setTimeout ( function ( ) {
628
+ $ ( ".table " + level . selector ) . addClass ( "strobe" ) ;
629
+ $ ( ".pop" ) . removeClass ( "pop" ) ;
630
+ } , 200 ) ;
547
631
632
+ }
548
633
634
+ // Popup positioning code from...
549
635
// http://stackoverflow.com/questions/4068373/center-a-popup-window-on-screen
550
636
551
637
function PopupCenter ( url , title , w , h ) {
552
- // Fixes dual-screen position Most browsers Firefox
553
- var dualScreenLeft = window . screenLeft != undefined ? window . screenLeft : screen . left ;
554
- var dualScreenTop = window . screenTop != undefined ? window . screenTop : screen . top ;
638
+ // Fixes dual-screen position Most browsers Firefox
639
+ var dualScreenLeft = window . screenLeft != undefined ? window . screenLeft : screen . left ;
640
+ var dualScreenTop = window . screenTop != undefined ? window . screenTop : screen . top ;
555
641
556
- var width = window . innerWidth ? window . innerWidth : document . documentElement . clientWidth ? document . documentElement . clientWidth : screen . width ;
557
- var height = window . innerHeight ? window . innerHeight : document . documentElement . clientHeight ? document . documentElement . clientHeight : screen . height ;
642
+ var width = window . innerWidth ? window . innerWidth : document . documentElement . clientWidth ? document . documentElement . clientWidth : screen . width ;
643
+ var height = window . innerHeight ? window . innerHeight : document . documentElement . clientHeight ? document . documentElement . clientHeight : screen . height ;
558
644
559
- var left = ( ( width / 2 ) - ( w / 2 ) ) + dualScreenLeft ;
560
- var top = ( ( height / 2 ) - ( h / 2 ) ) + dualScreenTop ;
561
- var newWindow = window . open ( url , title , 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left ) ;
645
+ var left = ( ( width / 2 ) - ( w / 2 ) ) + dualScreenLeft ;
646
+ var top = ( ( height / 2 ) - ( h / 2 ) ) + dualScreenTop ;
647
+ var newWindow = window . open ( url , title , 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left ) ;
562
648
563
- // Puts focus on the newWindow
564
- if ( window . focus ) {
565
- newWindow . focus ( ) ;
566
- }
567
- }
649
+ // Puts focus on the newWindow
650
+ if ( window . focus ) {
651
+ newWindow . focus ( ) ;
652
+ }
653
+ }
0 commit comments