1function initialize_quiz() { 2 if (jQuery(".quiz-widget-container").length) { 3 jQuery(".quiz-widget-container").each(function () { 4 let selectedQuestionId = null 5 let selectedOption = null 6 7 jQuery(".grid-poll-answer-container-overlay").hide() 8 const quizId = jQuery('input[name="quiz-id"]').val() 9 const quizContent = JSON.parse(jQuery('input[name="quiz-content"]').val()) 10 const cookieName = `585720315${quizId}` 11 12 const widgetContainer = jQuery(this) 13 14 let questionIndex = 1 15 let userAnswers = [] 16 17 checkQuizStatus() 18 19 function checkQuizQuestion(question) { 20 const foundAnswer = userAnswers.find(function (a) { 21 return a.question_id == question.id 22 }) 23 if (!foundAnswer) return null 24 const foundMatchedAnswer = question.answers.find(function (a) { 25 return a.id == foundAnswer.answer_id 26 }) 27 28 return foundMatchedAnswer 29 } 30 31 widgetContainer.on("click", ".quiz-start-button", function (e) { 32 e.preventDefault() 33 widgetContainer.find(".quiz-intro-screen").fadeOut(400, function () { 34 widgetContainer.find(".quiz-header-img-container").hide(400) 35 widgetContainer.find(".quiz-question-screen-" + quizContent[0].id).fadeIn(400, function () {}) 36 }) 37 }) 38 39 function handleQuizResult(result) { 40 hide_results_loader() 41 widgetContainer.find(".calculating-results-text").fadeOut(400, function () { 42 widgetContainer.find(".quiz-results-text").fadeIn() 43 widgetContainer.find(".retake-quiz").fadeIn() 44 widgetContainer 45 .find(".quiz-result-answer") 46 .text(result.content ? result.content.replace(/\\/g, "") : "") 47 widgetContainer 48 .find(".quiz-desc-text") 49 .text(result.description ? result.description.replace(/\\/g, "") : "") 50 if (result.image_src) { 51 widgetContainer.find(".quiz-result-img").attr("src", result.image_src).fadeIn() 52 } 53 }) 54 } 55 56 function sendQuizVote(questionId, answerId) { 57 var data = { 58 action: "sno_quiz_vote", 59 question_id: questionId, 60 answer_id: answerId, 61 } 62 return jQuery.post(sno_polls_ajax_object.ajax_url, data, null, "json") 63 } 64 65 function sendQuizResults(pollId, results) { 66 var data = { 67 action: "sno_quiz_results", 68 poll_id: pollId, 69 results: results, 70 } 71 return jQuery.post(sno_polls_ajax_object.ajax_url, data, null, "json") 72 } 73 74 widgetContainer.find(".quiz-question-screen").on("click", ".quiz-submit-button", function (e) { 75 e.preventDefault() 76 77 widgetContainer.find(".quiz-question-screen").find(".quiz-submit-button").prop("disabled", true) 78 widgetContainer.find(".quiz-question-screen").find(".quiz-submit-button").addClass("disabled-button") 79 80 let questionId, answerId 81 if (selectedQuestionId && selectedOption) { 82 questionId = selectedQuestionId 83 answerId = selectedOption 84 85 selectedQuestionId = null 86 selectedOption = null 87 } else { 88 questionId = jQuery(this).closest(".quiz-question-screen").attr("data-question-id") 89 answerId = widgetContainer 90 .find(".quiz-question-screen") 91 .find('input[name="quiz-answer"]:checked') 92 .val() 93 } 94 95 sendQuizVote(questionId, answerId).then(function (res) { 96 if (res.success === true) { 97 userAnswers.push(res.data) 98 updateCookieState(res.data) 99 } else { 100 console.log("error", res) 101 displayError() 102 } 103 }) 104 105 widgetContainer 106 .find(".quiz-question-screen-" + quizContent[questionIndex - 1].id) 107 .fadeOut(400, function () { 108 if (questionIndex >= quizContent.length) { 109 const results = quizContent.map(function (t) { 110 return checkQuizQuestion(t) 111 }) 112 widgetContainer.find(".quiz-finish-screen").fadeIn(400, function () { 113 sendQuizResults(quizId, results).then(function (res) { 114 if (res.success === true) { 115 if (res.data.result) { 116 setTimeout(function () { 117 handleQuizResult(res.data.result) 118 }, 1200) 119 } 120 } else { 121 console.log("error getting quiz results", res) 122 displayError() 123 } 124 }) 125 }) 126 questionIndex = 1 127 } else { 128 widgetContainer 129 .find(".quiz-question-screen-" + quizContent[questionIndex].id.toString()) 130 .fadeIn(400, function () { 131 questionIndex++ 132 }) 133 } 134 }) 135 }) 136 137 widgetContainer.find(".quiz-question-screen").on("click", 'input[name="quiz-answer"]', function (e) { 138 if (jQuery(this).prop("checked") === true) { 139 widgetContainer.find(".quiz-question-screen").find(".quiz-submit-button").prop("disabled", false) 140 widgetContainer 141 .find(".quiz-question-screen") 142 .find(".quiz-submit-button") 143 .removeClass("disabled-button") 144 } 145 }) 146 147 widgetContainer.on("click", ".grid-quiz-answer-container", handleAnswerClick) 148 149 function handleAnswerClick(e) { 150 e.preventDefault() 151 // get vote 152 selectedQuestionId = jQuery(this).closest(".quiz-question-screen").attr("data-question-id") 153 const answerId = jQuery(this).find('input[name="quiz-answer"]').val() 154 155 selectedOption = answerId 156 157 widgetContainer.find('input[name="quiz-answer"]').each(function (index, el) { 158 const id = jQuery(this).val() 159 160 if (id === answerId) { 161 jQuery(this).prev(".grid-poll-answer-container-overlay").show() 162 } else { 163 jQuery(this).prev(".grid-poll-answer-container-overlay").hide() 164 } 165 }) 166 167 widgetContainer.find(".quiz-question-screen").find(".quiz-submit-button").prop("disabled", false) 168 widgetContainer.find(".quiz-question-screen").find(".quiz-submit-button").removeClass("disabled-button") 169 } 170 171 widgetContainer.on("click", ".retake-quiz", function (e) { 172 e.preventDefault() 173 eraseCookie(cookieName) 174 175 widgetContainer.find(".calculating-results-text").show() 176 widgetContainer.find(".quiz-results-text").hide() 177 widgetContainer.find(".retake-quiz").hide() 178 widgetContainer.find(".quiz-result-answer").text("") 179 widgetContainer.find(".quiz-desc-text").text("") 180 181 widgetContainer.find(".quiz-result-img").hide() 182 183 widgetContainer.find(".quiz-finish-screen").fadeOut(400, function () { 184 jQuery(".quiz-intro-screen").fadeIn(400, function () {}) 185 }) 186 187 selectedQuestionId = null 188 selectedOption = null 189 190 questionIndex = 1 191 userAnswers = [] 192 193 checkQuizStatus() 194 }) 195 196 function checkQuizStatus() { 197 show_loader() 198 const quizCookie = readCookie(cookieName) 199 200 if (quizCookie) { 201 const parsedCookie = JSON.parse(quizCookie) 202 203 for (let answer of parsedCookie) { 204 userAnswers.push(answer) 205 } 206 207 widgetContainer.find(".test-header-img-container").hide(200) 208 209 if (parsedCookie.length && parsedCookie.length == quizContent.length) { 210 handleAlreadyVoted() 211 } else if (parsedCookie.length && parsedCookie.length < quizContent.length) { 212 questionIndex = parsedCookie.length + 1 213 handlePartiallyVoted() 214 } 215 } 216 217 hide_loader() 218 } 219 220 function updateCookieState(userAnswer) { 221 const quizCookie = readCookie(cookieName) 222 if (!quizCookie) { 223 const cookieData = [userAnswer] 224 createCookie(cookieName, JSON.stringify(cookieData), 30) 225 } else { 226 const cookieData = [...JSON.parse(quizCookie), userAnswer] 227 eraseCookie(cookieName) 228 createCookie(cookieName, JSON.stringify(cookieData), 30) 229 } 230 } 231 232 function handleAlreadyVoted() { 233 const results = quizContent.map(function (t) { 234 return checkQuizQuestion(t) 235 }) 236 237 widgetContainer.find(".quiz-header-img-container").hide(400) 238 widgetContainer.find(".quiz-intro-screen").fadeOut(400, function () { 239 widgetContainer.find(".quiz-finish-screen").fadeIn(400, function () { 240 sendQuizResults(quizId, results).then(function (res) { 241 if (res.success === true) { 242 if (res.data.result) { 243 setTimeout(function () { 244 handleQuizResult(res.data.result) 245 }, 1200) 246 } 247 } else { 248 console.log("error getting quiz results", res) 249 displayError() 250 } 251 }) 252 }) 253 }) 254 } 255 256 function handlePartiallyVoted() { 257 widgetContainer.find(".quiz-intro-screen").fadeOut(400, function () { 258 widgetContainer.find(".quiz-header-img-container").hide(400) 259 widgetContainer.find(".quiz-question-screen-" + quizContent[questionIndex - 1].id).fadeIn(400) 260 }) 261 } 262 263 function show_loader() { 264 widgetContainer.find(".quiz-content").fadeOut(400, function () {}) 265 widgetContainer.find(".quiz-loader").fadeIn(400, function () {}) 266 } 267 268 function hide_loader() { 269 widgetContainer.find(".quiz-loader").fadeOut(400, function () {}) 270 widgetContainer.find(".quiz-content").fadeIn(400, function () {}) 271 } 272 273 function hide_results_loader() { 274 widgetContainer.find("#quiz-results-loader").fadeOut(400, function () {}) 275 widgetContainer.find(".quiz-content").fadeIn(400, function () {}) 276 } 277 278 function displayError() { 279 widgetContainer.find(".quiz-loader").fadeOut(400, function () {}) 280 widgetContainer.find(".poll-answers-container").fadeOut(400, function () {}) 281 widgetContainer.find(".quiz-error-container").fadeIn(400, function () {}) 282 } 283 }) 284 } 285}
285 286initialize_quiz() 287 288function createCookie(name, value, days) { 289 let expires 290 291 if (days) { 292 const date = new Date() 293 date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000) 294 expires = "; expires=" + date.toGMTString() 295 } else { 296 expires = "" 297 } 298 document.cookie = encodeURIComponent(name) + "=" + encodeURIComponent(value) + expires + "; path=/" 299} 300 301function readCookie(cname) { 302 let name = encodeURIComponent(cname) + "=" 303 let ca = document.cookie.split(";") 304 for (let i = 0; i < ca.length; i++) { 305 let c = ca[i] 306 while (c.charAt(0) === " ") c = c.substring(1, c.length) 307 if (c.indexOf(name) === 0) return decodeURIComponent(c.substring(name.length, c.length)) 308 } 309 return null 310} 311 312function eraseCookie(name) { 313 createCookie(name, "", -1) 314}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.