PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.1
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.1
16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 All 503 releases
jetpack / modules / shortcodes / js / quiz.js

quiz.js in Jetpack – WP Security, Backup, Speed, & Growth 16.3-a.1, at modules/shortcodes/js/quiz.js

93 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 ( function ( $ ) {
2 $.fn.shuffleQuiz = function () {
3 var allElems = this.get(),
4 getRandom = function ( max ) {
5 return Math.floor( Math.random() * max );
6 },
7 shuffled = $.map( allElems, function () {
8 var random = getRandom( allElems.length ),
9 randEl = $( allElems[ random ] ).clone( true )[ 0 ];
10 allElems.splice( random, 1 );
11 return randEl;
12 } );
13
14 this.each( function ( i ) {
15 $( this ).replaceWith( $( shuffled[ i ] ) );
16 } );
17
18 return $( shuffled );
19 };
20 } )( jQuery );
21
22 jQuery( function ( $ ) {
23 $( '.jetpack-quiz' ).each( function () {
24 var quiz = $( this );
25 quiz.find( 'div.jetpack-quiz-answer' ).shuffleQuiz();
26 quiz.find( 'div[data-correct]' ).removeAttr( 'data-correct' ).data( 'correct', 1 );
27 quiz.find( 'div.jetpack-quiz-answer:last' ).addClass( 'last' );
28 } );
29
30 $( 'div.jetpack-quiz' ).on( 'click', 'div.jetpack-quiz-answer', function () {
31 var trackid,
32 answer = $( this ),
33 quiz = answer.closest( 'div.jetpack-quiz' );
34
35 if ( quiz.data( 'a8ctraining' ) ) {
36 new Image().src =
37 '//pixel.wp.com/b.gif?v=wpcom-no-pv&x_trainingchaos-' +
38 quiz.data( 'username' ) +
39 '=' +
40 quiz.data( 'a8ctraining' ) +
41 '&rand=' +
42 Math.random();
43 quiz.data( 'a8ctraining', false );
44 quiz.data( 'trackid', false );
45 }
46
47 trackid = quiz.data( 'trackid' );
48 if ( answer.data( 'correct' ) ) {
49 answer.addClass( 'correct' );
50 if ( trackid ) {
51 new Image().src =
52 '//pixel.wp.com/b.gif?v=wpcom-no-pv&x_quiz-' + trackid + '=correct&rand=' + Math.random();
53 }
54 } else {
55 answer.addClass( 'wrong' );
56 if ( trackid ) {
57 new Image().src =
58 '//pixel.wp.com/b.gif?v=wpcom-no-pv&x_quiz-' + trackid + '=wrong&rand=' + Math.random();
59 }
60 }
61 // only track the first answer
62 quiz.data( 'trackid', false );
63 } );
64 } );
65
66 document.querySelectorAll( '.jetpack-quiz-wrapper' ).forEach( function ( quiz ) {
67 quiz.childNodes.forEach( function ( element, number ) {
68 element.style.display = 'none';
69 element.setAttribute( 'quiz-number', number );
70 element.querySelector( '.jetpack-quiz-count' ).innerHTML =
71 number + 1 + '/' + quiz.childElementCount;
72 } );
73
74 quiz.childNodes[ 0 ].style.display = 'block';
75 } );
76
77 document.querySelectorAll( '.jetpack-quiz-option-button' ).forEach( function ( element ) {
78 element.addEventListener( 'click', function () {
79 var currentQuiz = element.parentElement.parentElement;
80 currentQuiz.style.display = 'none';
81 var switchNumber = element.getAttribute( 'data-quiz-option' ) === 'next' ? 1 : -1;
82 var newQuiz =
83 currentQuiz.parentElement.childNodes[
84 parseInt( currentQuiz.getAttribute( 'quiz-number' ) ) + switchNumber
85 ];
86 newQuiz.style.display = 'block';
87 var newQuizQuestionEl = newQuiz.querySelector( '.jetpack-quiz-question' );
88 if ( newQuizQuestionEl ) {
89 newQuizQuestionEl.focus();
90 }
91 } );
92 } );
93