|
1 | 1 | (function($, JSON, localStorage){ |
2 | 2 | const {url} = options = { |
3 | | - url: 'data/quiz.json?' + Date.now() |
| 3 | + url: `data/quiz.json?${Date.now()}` |
4 | 4 | } |
5 | 5 |
|
6 | 6 | $.ajax({ url }).done(function(data) { |
|
16 | 16 |
|
17 | 17 | // Append the progress bar to DOM |
18 | 18 | $('body') |
19 | | - .append('<div style="position: fixed; bottom: 0; background: #eee; width: 100%; height: 6px; ">' + |
20 | | - '<div id="progress" style="background: #1678c2; width: 1%;"> </div>' + |
21 | | - '</div>') |
| 19 | + .append(`<div style="position: fixed; bottom: 0; background: #eee; width: 100%; height: 6px; "> |
| 20 | + <div id="progress" style="background: #1678c2; width: 1%;"> </div> |
| 21 | + </div>`) |
22 | 22 |
|
23 | 23 | // Append title and form to quiz |
24 | 24 | $('#quiz') |
25 | | - .append('<h1 class="ui header">' + data.title + '</h1>') |
| 25 | + .append(`<h1 class="ui header">${data.title}</h1>`) |
26 | 26 | .append('<form id="quiz-form" class="ui form"></form>') |
27 | 27 |
|
28 | 28 | // For each question of the json, |
|
43 | 43 | const {[j]:option} = options |
44 | 44 | const checked = !!responses[i] && responses[i].includes(option.label) ? 'checked' : '' |
45 | 45 |
|
46 | | - inputHtml += '<div class="field">' + |
47 | | - '<div class="ui checkbox ' + type + '">' + |
48 | | - '<input type="' + type + '" ' + checked + ' name="question_' + i + '" id="question_' + i + '_' + j + '" value="' + option.label + '">' + |
49 | | - '<label for="question_' + i + '_' + j + '">' + option.label + '</label>' + |
50 | | - '</div>' + |
51 | | - '</div>' |
| 46 | + inputHtml += `<div class="field"> |
| 47 | + <div class="ui checkbox ${type}"> |
| 48 | + <input type="${type}" ${checked} name="question_${i}" id="question_${i}_${j}" value="${option.label}"> |
| 49 | + <label for="question_${i}_${j}">${option.label}</label> |
| 50 | + </div> |
| 51 | + </div>` |
52 | 52 | } |
53 | 53 | inputHtml += '</div>' |
54 | 54 | break |
55 | 55 |
|
56 | 56 | // Set of inputs (composed response) |
57 | 57 | case 'inputs': |
58 | 58 | inputHtml = '<table>' |
59 | | - for (j = 0; j < options.length; j++) { |
| 59 | + for (let j = 0; j < options.length; j++) { |
60 | 60 | const {[j]:option} = options |
61 | 61 | const value = responses[i] && responses[i][j] || '' |
62 | 62 |
|
63 | | - inputHtml += '<tr>' + |
64 | | - '<td><label for="question_' + i + '_' + j + '">' + option.label + '</label></td>' + |
65 | | - '<td width="15px"></td>' + |
66 | | - '<td><div class="ui input">' + |
67 | | - '<input type="text" placeholder="Response..." name="question_' + i + '" id="question_' + i + '_' + j + '" value="' + value + '" />' + |
68 | | - '</div></td>' + |
69 | | - '</tr>' + |
70 | | - '<tr><td colspan="3"> </tr></tr>' |
| 63 | + inputHtml += `<tr> |
| 64 | + <td><label for="question_${i}_${j}">${option.label}</label></td> |
| 65 | + <td width="15px"></td> |
| 66 | + <td><div class="ui input"> |
| 67 | + <input type="text" placeholder="Response..." name="question_${i}" id="question_${i}_${j}" value="${value}" /> |
| 68 | + </div></td> |
| 69 | + </tr> |
| 70 | + <tr><td colspan="3"> </tr></tr>` |
71 | 71 | } |
72 | 72 | inputHtml += '</table>' |
73 | 73 | break |
74 | 74 |
|
75 | 75 | // Default: simple input |
76 | 76 | default: |
77 | 77 | const value = responses[i] || '' |
78 | | - inputHtml = '<div class="ui input fluid">' + |
79 | | - '<input type="text" placeholder="Response..." name="question_' + i + '" value="' + value + '" />' + |
80 | | - '</div>' |
| 78 | + inputHtml = `<div class="ui input fluid"> |
| 79 | + <input type="text" placeholder="Response..." name="question_${i}" value="${value}" /> |
| 80 | + </div>` |
81 | 81 | } |
82 | 82 |
|
83 | | - $question = $('<div id="question-' + i + '" class="ui card" style="width: 100%;">' + |
84 | | - '<div class="content">' + |
85 | | - '<div class="header">' + problem + '</div>' + |
86 | | - '</div>' + |
87 | | - '<div class="content">' + |
88 | | - inputHtml + |
89 | | - '</div>' + |
90 | | - '</div>' |
| 83 | + $question = $(`<div id="question-${i}" class="ui card" style="width: 100%;"> |
| 84 | + <div class="content"><div class="header">${problem}</div></div> |
| 85 | + <div class="content">${inputHtml}</div> |
| 86 | + </div>` |
91 | 87 | ).css('display', 'none') |
92 | 88 |
|
93 | 89 | $('#quiz-form') |
94 | 90 | .append($question) |
95 | 91 |
|
96 | 92 | // Show current question |
97 | 93 | $('#quiz-form') |
98 | | - .find('#question-' + currentQuestion) |
| 94 | + .find(`#question-${currentQuestion}`) |
99 | 95 | .css('display', 'block') |
100 | 96 |
|
101 | 97 | // Update progress bar |
|
124 | 120 |
|
125 | 121 | // Actions on every response submission |
126 | 122 | $('#submit-response').on('click', function() { |
127 | | - const $inputs = $('[name^=question_' + currentQuestion + ']') |
| 123 | + const $inputs = $(`[name^=question_${currentQuestion}`) |
128 | 124 | const question = questions[currentQuestion] |
129 | 125 | let response = responses[currentQuestion] |
130 | 126 |
|
|
133 | 129 | case 'checkbox': |
134 | 130 | case 'radio': |
135 | 131 | response = [] |
136 | | - $('[name=' + $inputs.attr('name') + ']:checked').each(function(i, input) { |
| 132 | + $(`[name=${$inputs.attr('name')}]:checked`).each(function(i, input) { |
137 | 133 | response.push(input.value) |
138 | 134 | }) |
139 | 135 | response = response.length ? response : null |
|
190 | 186 |
|
191 | 187 | // Display next question |
192 | 188 | $('#quiz-form') |
193 | | - .find('#question-' + currentQuestion).css('display', 'none') |
| 189 | + .find(`#question-${currentQuestion}`).css('display', 'none') |
194 | 190 |
|
195 | 191 |
|
196 | 192 | $('#quiz-form') |
197 | | - .find('#question-' + ++currentQuestion).css('display', 'block') |
| 193 | + .find(`#question-${++currentQuestion}`).css('display', 'block') |
198 | 194 |
|
199 | 195 | // If it was the las question, display final message |
200 | 196 | if (responseCount === questions.length) { |
|
204 | 200 | } |
205 | 201 | } |
206 | 202 |
|
207 | | - |
208 | 203 | // Save current state of the quiz |
209 | 204 | localStorage.setItem('quiz', JSON.stringify({responses, responseCount, currentQuestion})) |
210 | 205 | }) |
|
0 commit comments