brightcove.js
5 years ago
dependencies.js
7 months ago
main.js
7 months ago
quiz.js
4 years ago
recipes.js
5 years ago
slideshow-shortcode.js
7 months ago
quiz.js
93 lines
| 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 |