Repository navigation
Expand file tree
/
Copy pathtutorial.js
More file actions
696 lines (679 loc) · 18.5 KB
/
Copy pathtutorial.js
File metadata and controls
696 lines (679 loc) · 18.5 KB
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
YUI.add('tutorial', function(Y) {
/*constants*/
CORRECT_MARK = '<span class="checkmark">✔</span>';
INCORRECT_MARK = '<span class="xmark">✘</span>';
CORRECT_MSG = 'Correct!';
INCORRECT_MSG = 'Try Again.';
/**
@class Navigation
@constructor
Runs the navigation interaction
*/
Y.Navigation = function() {
this.header = Y.one('#header');
this.bar = Y.one('#navigation');
this.navPs = Y.all('p.navigation');
this.navButton = Y.one('#header').one('.mini-nav');
this.init();
};
Y.Navigation.prototype = {
init: function() {
this.bar.addClass('slide-hide');
this.navButton.on('click', this.toggleNav, this);
this.bar.on('click', this.handleClick, this);
this.navPs.on('click', this.handleClick, this);
/*if we've got iOS, force the videos to the small, non-flash version*/
if (Y.UA.ios) {
this.adjustVideos();
}
},
/**
@method handleClick handles
clicks on the navigation bar
@param e is the event object
*/
handleClick: function(e) {
if (e.target.get('tagName') === 'A'){
var section = e.target.getData('title');
var highlightIndex = e.target.getData('hindex');
var anchorEls = this.bar.all('a');
this.toggleNav();
anchorEls.removeClass('active');
if (highlightIndex) {
anchorEls.item(highlightIndex).addClass('active');
}
else {
e.target.addClass('active');
}
this.header.one('h1 > span').set('innerHTML', section);
}
},
/**
@method toggleNav shows and
hides the tray navigation
on mobile devices
@param e is the event object
*/
toggleNav: function(e) {
if(e) {
e.halt();
}
if (this.bar.hasClass('slide-hide')) {
this.bar.removeClass('slide-hide');
}
else {
this.bar.addClass('slide-hide');
}
},
/**
@method adjust videos
toggles videos for iOS
to make them navigable
for non-Flash
*/
adjustVideos: function(){
Y.all('p.video-large').setStyle('display', 'none');
Y.all('p.video-small').setStyle('display', 'block');
}
};
/**
@class Revealer handles
@constructor
Handles the interaction for
all the colorful revealing
lists in the tutorial
*/
Y.Revealer = function() {
this.nodes = Y.all('ol.reveal');
this.init();
};
Y.Revealer.prototype = {
init : function() {
Y.all('ol.reveal li strong').addClass('hide');
this.nodes.on('click', this.handleClick, this);
this.nodes.on('mouseover', this.handleHover, this);
},
/**
@method handleClick handles
clicks on images for non-hoverable
browsers
@param e is the event object
*/
handleClick : function(e) {
if (e.target.get('tagName') === 'IMG') {
this.reveal(e.target.get('parentNode'));
}
},
/**
@method handleHover handles
hovers on each image
@param e is the event object
*/
handleHover : function(e) {
if (e.target.get('tagName') === 'IMG') {
this.reveal(e.target.get('parentNode'));
}
},
/**
@method reveal handles
changing the classname with delay
@param el is the element
being transformed.
*/
reveal : function(el) {
el.addClass('expand');
Y.later(1000, this, this.clearHide, el);
},
/**
@method clearHide handles
revealing the text on the list
item
@param el is the element being
transformed
*/
clearHide : function(el) {
el.one('strong').removeClass('hide');
}
};
/**
@class ShowMe
@constructor
does the hover/reveal
interaction on the
truncation search list
and any similar interactions
desired
*/
Y.ShowMe = function() {
this.lists = Y.all('ul.more-info');
this.triggers = Y.all('ul.more-info a.trigger');
this.init();
};
Y.ShowMe.prototype = {
init: function() {
this.triggers.on('click', this.revealNext, this);
this.lists.on('mouseenter', this.hideTrigger, this);
},
/**
@method revealNext shows
the obscured sub-list
@param e is the event object
*/
revealNext: function(e) {
e.preventDefault();
e.target.ancestor('ul').one('ol').removeClass('hiding');
},
/**
@method hideTrigger hides the
trigger anchor when the interaction
has been revealed
@param e is the event object
*/
hideTrigger: function(e) {
e.target.all('a.trigger').addClass('hide');
}
};
/**
@class Pagination
@constructor
Handles the on the fly
pagination of article tags
and page changes via the
subnav on desktop browsers
*/
Y.Pagination = function(){
this.sections = Y.all('section');
this.init();
};
/**
static properties and methods
needed for hashchange event, which
cannot bind to reference object
*/
Y.Pagination.startSection = Y.one('section#intro');
Y.Pagination.pagination = Y.one('#pagination');
Y.Pagination.subnav = Y.one('div#subnav');
/**
@method manageArticles sets
up pagination of articles
when a section is selected in the main
navigation, triggering a hashchange
event
@param e is the event object
*/
Y.Pagination.manageArticles = function(e) {
//if coming from a click event, make sure target was anchor
if(e && e.target.get('tagName') !== 'A') {
return false;
}
//scroll to top
window.scrollTo(0,0);
var allArticles = Y.Pagination.startSection.all('article'),
artLength = allArticles.size(),
myActive = 0,
sectionId = Y.Pagination.startSection.get('id'),
pNext = Y.Pagination.pagination.one('.next'),
pBack = Y.Pagination.pagination.one('.back'),
pageIndicator = Y.Pagination.pagination.one('p em'),
totalIndicator = Y.Pagination.pagination.one('p em.total'),
active,
page,
currentSubNav = Y.Pagination.subnav.all('ul#sub' + sectionId);
//activate subnav & turn on subnav for the section
Y.Pagination.subnav.removeClass('hide');
//hide all ULs, and turn only the appropriate one one
Y.Pagination.subnav.all('ul').addClass('hide');
currentSubNav.removeClass('hide');
if(e && e.target.get('tagName') === 'A') {
e.halt();
myActive = e.target.getData('gotopage');
}
/*if there is more than one article, hide subsequent
and set up pagination*/
if (artLength > 1) {
Y.Pagination.pagination.removeClass('hide');
for (i = 0; i < artLength; i++) {
allArticles.item(i).addClass('page-hide');
allArticles.item(i).removeClass('active');
allArticles.item(myActive).removeClass('page-hide');
allArticles.item(myActive).addClass('active');
allArticles.item(i).setAttribute('data-page', i);
}
//setup variables for active section and page
active = Y.Pagination.startSection.one('.active'),
page = parseInt(active.getData('page'));
//setup page indicator
pageIndicator.set('innerHTML', page+1);
totalIndicator.set('innerHTML', artLength);
//light up current section in subnav, if applicable
if (currentSubNav.size() > 0){
subNavAnchors = currentSubNav.item(0).all('a');
for (i = 0, x = subNavAnchors.size(); i < x; i++){
var item = subNavAnchors.item(i);
if(parseInt(item.getData('gotopage')) === page){
subNavAnchors.removeClass('active');
item.addClass('active');
}
}
}
if (page < artLength - 1) {
pNext.removeClass('hide');
pNext.setAttribute('data-gotopage', page+1)
}
else {
pNext.addClass('hide');
pNext.setAttribute('data-gotopage', artLength - 1);
}
if (active.getData('page') > 0) {
pBack.removeClass('hide');
pBack.setAttribute('data-gotopage', page-1);
}
else {
pBack.addClass('hide');
pBack.setAttribute('data-gotopage', 0);
}
}
};
/**
instance methods for Pagination
*/
Y.Pagination.prototype = {
init : function() {
this.initializeSections();
Y.Pagination.manageArticles();
Y.on('hashchange', this.swapSection, Y.config.win);
Y.Pagination.pagination.on('click', Y.Pagination.manageArticles, this);
Y.Pagination.subnav.on('click', Y.Pagination.manageArticles, this);
},
/**
@method initializeSections hides
all but the current section we are in
*/
initializeSections : function() {
this.sections.each(function(node){
node.addClass('hide');
});
Y.Pagination.startSection.removeClass('hide');
},
/**
@method swapSection changes current
section on hashchange event
@param e is the event object
*/
swapSection : function(e) {
Y.all('section').addClass('hide');
var selector = '#' + e.newHash,
myNode = Y.one(selector);
myNode.removeClass('hide');
Y.Pagination.startSection = myNode;
Y.Pagination.manageArticles();
}
};
/**
@class DropQuiz
@constructor
handles the interactive
dropdown exercises in the
tutorial
@param formNode is the form
we are attaching interaction
to
*/
Y.DropQuiz = function(formNode) {
this.form = formNode;
this.init();
};
Y.DropQuiz.prototype = {
init : function() {
//if we're not IE <= 8, we can delegate this event
if (Y.UA.ie > 0 && (Y.UA.ie < 9)) {
this.form.all('select').on("change", this.handleChange, this);
}
else {
this.form.on("change", this.handleChange, this);
}
},
/**
@method handleChange
handles the change on any select form
control and gives feedback on correctness
@param e is the event object
*/
handleChange : function(e) {
//check the value and if it matches a valid value
if (this.containsValue(e.target.get('value'),e.target.getData('valid').split('|'))) {
e.target.next('div.scoring').set('innerHTML', CORRECT_MARK + CORRECT_MSG);
}
else {
e.target.next('div.scoring').set('innerHTML', INCORRECT_MARK + INCORRECT_MSG);
}
},
/**
@method containsValue checks
to see if the answer selected
is contained in the set of
correct answers, supporting multiple corrects
@param needle is the answer to find
@param haystack is the list of possible answers
*/
containsValue : function(needle, haystack) {
var l = haystack.length;
for (var i = 0; i < l; i++) {
if(haystack[i] === needle) {
return true;
}
}
return false;
}
};
/**
@class TableToggle
@constructor
handles the interactions
for tables containing
comparisons of scholarly vs. popular
sources and different types of Boolean
searches
@param tableController
is the list element used to select
options for display in the table
*/
Y.TableToggle = function(tableController) {
this.controller = tableController.one('ul.table-driver');
this.table = tableController.one('table');
this.init();
};
Y.TableToggle.prototype = {
init : function(){
this.controller.on('click', this.handleClick, this);
},
/**
@method handleClick
handles clicks on the controller
list
@param e is the event object
*/
handleClick : function(e) {
e.preventDefault();
if (e.target.get('tagName') === 'A') {
this.table.removeClass('ghost');
this.hideAllRows();
this.showRow(e.target.getData('item'));
}
else {
return;
}
},
/**
@method hideAllRows
hides all the rows in the table from
view for visual users only
*/
hideAllRows : function(){
this.table.all('tr.inner-row').addClass('ghost');
this.table.all('img').addClass('hide');
},
/**
@method showRow
reveals the appropriate table
row for visual users only
@param rowItem is the row
to show
*/
showRow : function(rowItem){
this.table.all('tr.inner-row').item(rowItem).removeClass('ghost');
this.table.all('img').removeClass('hide');
}
};
/**
@class SectionQuiz
@constructor
handles the interaction
for all Section knowledge
quizzes
@param form is the form
we are attaching the
interaction to
*/
Y.SectionQuiz = function(form) {
this.form = form;
this.submitBtn = this.form.one('input[type=submit]');
this.resetBtn = this.form.one('input[type=reset]');
this.numCorrect = 0;
this.numRequired = this.form.getData('required');
this.formName = this.form.getData('name');
this.email = this.form.one('input[type=email]');
this.name = this.form.one('input[type=text]');
this.questionEls = this.form.all('ul');
this.init();
};
Y.SectionQuiz.prototype = {
init: function() {
this.submitBtn.on('click', this.validateForm, this);
this.resetBtn.on('click', this.reset, this);
},
/**
@method reset
resets the form when
reset button is clicked
@param e is the event object
*/
reset: function(e) {
e.halt();
var email = this.email.get('value'),
name = this.name.get('value');
this.form.reset();
this.email.set('value', email);
this.name.set('value', name);
//hide all hints/prompts
this.form.all('span.error').addClass('hide');
this.form.all('span.correct').addClass('hide');
this.form.one('p.congrats').addClass('hide');
this.form.one('p.sorry').addClass('hide');
//reset submit button
this.submitBtn.set('disabled', false);
},
/**
@method validateForm
validates that all
questions are attempted
and that name and email are
provided
@param e is the event object
*/
validateForm: function(e) {
var emailValid = this.emailIsValid(),
nameValid = this.nameIsValid(),
allAnswered = this.allQuestionsAnswered();
e.halt();
if (emailValid && nameValid && allAnswered) {
this.gradeQuiz();
}
else {
if (!emailValid) {
alert("Please enter a valid email address for credit.");
}
if (!nameValid) {
alert("Please enter your name for credit.");
}
if (!allAnswered) {
alert("Please answer all questions before submitting.");
}
}
},
/**
@method emailIsValid
checks that email is not empty and contains
@ and . characters.
@return boolean
*/
emailIsValid: function() {
return (this.email.get('value') !== '' &&
this.email.get('value').indexOf('.') !== -1 &&
this.email.get('value').indexOf('@') !== -1);
},
/**
@method nameIsValid
checks that name field is not empty
@return boolean
*/
nameIsValid: function(){
return (this.email.get('value') !== '');
},
/**
@method allQuestionsAnswered
checks that all questions
have at least one box checked
@return boolean
*/
allQuestionsAnswered: function(){
var numQuestions = this.questionEls.size(),
questionsAnswered = [];
for (i=0; i < numQuestions; i++) {
var boxes = this.questionEls.item(i).all('input[type=checkbox]:checked').size();
if (boxes > 0) {
questionsAnswered.push(boxes);
}
}
return (questionsAnswered.length === numQuestions);
},
/**
@method gradeQuiz
determines whether questions are
right or wrong and tallies
the number correct for pass or fail
It also gives the feedback
to the user about each item selected
*/
gradeQuiz: function() {
var results = {
correct: [],
incorrect: [],
list: [],
pass: false
};
//push base details into results
results.list.push(this.form.getData('name'));
results.list.push(this.name.get('value'));
results.list.push(this.email.get('value'));
this.questionEls.each(function(el){
var checkBoxes = el.all('input[type=checkbox]'),
questionIsCorrect = true,
correctPrompt = el.previous('span.correct'),
numTrue = 1;
for (i=0, x=checkBoxes.size(); i<x; i++) {
/*go through the checked boxes first*/
if (checkBoxes.item(i).get('checked')) {
//incorrect item checked, point it out and mark question wrong
if (checkBoxes.item(i).getData('c') === "false") {
if (checkBoxes.item(i).next('span.error')) {
checkBoxes.item(i).next('span.error').removeClass('hide');
}
questionIsCorrect = false;
}
}
/*then go through the unchecked boxes*/
else {
//they missed one that should have been checked, mark question wrong
if (checkBoxes.item(i).getData('c') === "true") {
questionIsCorrect = false;
}
}
/*count number of true boxes for multiple answer*/
if (checkBoxes.item(i).getData('c') === "true") {
numTrue++;
}
}
/*iterate through correctly checked on incomplete multiple answer and reveal hint*/
if (numTrue > 1) {
for (i=0, x=checkBoxes.size(); i<x; i++) {
if (checkBoxes.item(i).get('checked') &&
checkBoxes.item(i).getData('c') === "true" &&
!questionIsCorrect) {
if (checkBoxes.item(i).next('span.error')) {
checkBoxes.item(i).next('span.error').removeClass('hide');
}
}
}
}
//if question is correct, display congrats, increment numCorrect
if (questionIsCorrect) {
correctPrompt.removeClass('hide');
results.correct.push(el.one('input').get('name'));
results.list.push(1);
}
else {
results.incorrect.push(el.one('input').get('name'));
results.list.push(0);
}
return results;
}, results);
this.numCorrect = results.correct.length;
if (this.numCorrect >= this.numRequired) {
this.form.one('p.congrats').removeClass('hide');
results.pass = true;
}
else {
this.form.one('p.sorry').removeClass('hide');
this.submitBtn.set('disabled', true);
}
//post results
this.postResults(results.list, results.pass);
},
/**
@method postResults sends
the results of the quiz
to a back end script for storage/processing
@param list is the list containing
student information and result of each
question
@param pass is a Boolean representing
whether it was a pass or fail
*/
postResults : function(list, pass) {
var list = list;
var pass = pass;
var name = list[1];
var email = list[2];
var quiz = list[0];
var handleSuccess = function(){console.log('Results posted successfully.');};
var handleFailure = function(){console.log('Failed to post results.');};
var uri = 'quiz.php';
var conf = {
method: 'POST',
data: 'list=' + list + '&pass=' + pass + '&email=' + email + '&quiz=' + quiz + '&name=' + name,
headers: { 'X-Transaction': 'Quiz Results'},
on: {
success: handleSuccess,
failure: handleFailure
}
};
Y.io(uri, conf);
}
};
//lazy load images and videos
Y.all('div.article-image img').removeClass('hide');
Y.all('img.feature').removeClass('hide');
Y.all('div.video').removeClass('hide');
}, '0.0.1', { requires: ['node','event','history-hash', 'selector-css3', 'io-base'] });
/*use the tutorial module and instantiate all objects from the classes*/
YUI().use('tutorial', function(Y) {
var tmp = new Y.Navigation();
var tmp2 = new Y.Revealer();
var tmp3 = new Y.Pagination();
var tmp4 = Y.all('form.exercise');
tmp4.each(function(form){
var els = new Y.DropQuiz(form);
});
var tmp5 = Y.all('div.table-driver');
tmp5.each(function(el){
var els = new Y.TableToggle(el);
});
var tmp6 = Y.all('form.quiz');
tmp6.each(function(form){
var els = new Y.SectionQuiz(form);
});
var tmp7 = new Y.ShowMe();
//clear loading state
Y.one('#tutorial-loading').addClass('hide');
});