Nenhuma Descrição

quiz.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. this.each( function ( i ) {
  14. $( this ).replaceWith( $( shuffled[ i ] ) );
  15. } );
  16. return $( shuffled );
  17. };
  18. } )( jQuery );
  19. jQuery( function ( $ ) {
  20. $( '.jetpack-quiz' ).each( function () {
  21. var quiz = $( this );
  22. quiz.find( 'div.jetpack-quiz-answer' ).shuffleQuiz();
  23. quiz.find( 'div[data-correct]' ).removeAttr( 'data-correct' ).data( 'correct', 1 );
  24. quiz.find( 'div.jetpack-quiz-answer:last' ).addClass( 'last' );
  25. } );
  26. $( 'div.jetpack-quiz' ).on( 'click', 'div.jetpack-quiz-answer', function () {
  27. var trackid,
  28. answer = $( this ),
  29. quiz = answer.closest( 'div.jetpack-quiz' );
  30. if ( quiz.data( 'a8ctraining' ) ) {
  31. new Image().src =
  32. '//pixel.wp.com/b.gif?v=wpcom-no-pv&x_trainingchaos-' +
  33. quiz.data( 'username' ) +
  34. '=' +
  35. quiz.data( 'a8ctraining' ) +
  36. '&rand=' +
  37. Math.random();
  38. quiz.data( 'a8ctraining', false );
  39. quiz.data( 'trackid', false );
  40. }
  41. trackid = quiz.data( 'trackid' );
  42. if ( answer.data( 'correct' ) ) {
  43. answer.addClass( 'correct' );
  44. if ( trackid ) {
  45. new Image().src =
  46. '//pixel.wp.com/b.gif?v=wpcom-no-pv&x_quiz-' + trackid + '=correct&rand=' + Math.random();
  47. }
  48. } else {
  49. answer.addClass( 'wrong' );
  50. if ( trackid ) {
  51. new Image().src =
  52. '//pixel.wp.com/b.gif?v=wpcom-no-pv&x_quiz-' + trackid + '=wrong&rand=' + Math.random();
  53. }
  54. }
  55. // only track the first answer
  56. quiz.data( 'trackid', false );
  57. } );
  58. } );
  59. document.querySelectorAll( '.jetpack-quiz-wrapper' ).forEach( function ( quiz ) {
  60. quiz.childNodes.forEach( function ( element, number ) {
  61. element.style.display = 'none';
  62. element.setAttribute( 'quiz-number', number );
  63. element.querySelector( '.jetpack-quiz-count' ).innerHTML =
  64. number + 1 + '/' + quiz.childElementCount;
  65. } );
  66. quiz.childNodes[ 0 ].style.display = 'block';
  67. } );
  68. document.querySelectorAll( '.jetpack-quiz-option-button' ).forEach( function ( element ) {
  69. element.addEventListener( 'click', function () {
  70. var currentQuiz = element.parentElement.parentElement;
  71. currentQuiz.style.display = 'none';
  72. var switchNumber = element.getAttribute( 'data-quiz-option' ) === 'next' ? 1 : -1;
  73. var newQuiz =
  74. currentQuiz.parentElement.childNodes[
  75. parseInt( currentQuiz.getAttribute( 'quiz-number' ) ) + switchNumber
  76. ];
  77. newQuiz.style.display = 'block';
  78. var newQuizQuestionEl = newQuiz.querySelector( '.jetpack-quiz-question' );
  79. if ( newQuizQuestionEl ) {
  80. newQuizQuestionEl.focus();
  81. }
  82. } );
  83. } );