Skip to content

Commit e078d8b

Browse files
author
Rob Myers
committed
Re-enable the back button, with a proper object and stack based undo system.
1 parent f8e857c commit e078d8b

File tree

4 files changed

+42
-30
lines changed

4 files changed

+42
-30
lines changed

wordpress-plugin/index.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
</div>
6161
6262
<div class="hidden" id="question-progress-buttons">
63-
<!-- <button class="btn btn-default" id="button-question-back">Back</button> -->
63+
<button class="btn btn-default" id="button-question-back">Back</button>
6464
<button class="btn btn-default" id="button-question-next">Next</button>
6565
<a href="."
6666
class="btn btn-default hidden"

wordpress-plugin/js/questions.js

+17-22
Original file line numberDiff line numberDiff line change
@@ -323,14 +323,14 @@ Questions.resultMap = undefined;
323323
// This data won't be used until after several questions, so this is tolerable.
324324

325325
jQuery.getJSON(jQuery("script[src*='/termination-of-transfer/js/questions.js']")
326-
.attr('src').replace(/questions\.js.*$/, '')
327-
+ 'results.json')
326+
.attr('src').replace(/questions\.js.*$/, '')
327+
+ 'results.json')
328328
.done(function (result) {
329329
resultMap = result;
330330
})
331331
.fail(function (jqxhr, textStatus, error) {
332-
var err = textStatus + ", " + error;
333-
console.log("Request Failed: " + err);
332+
var err = textStatus + ", " + error;
333+
console.log("Request Failed: " + err);
334334
});
335335

336336
Questions.getConclusionDetails = function (specifier) {
@@ -346,11 +346,9 @@ Questions.getConclusionDetails = function (specifier) {
346346
Questions.first_question = 's1q1a';
347347
Questions.last_question = 's2q2fii';
348348

349-
Questions.progress_stack = [];
350-
351349
Questions.start = function () {
352350
jQuery('.questionnaire-section, .question-progress-buttons')
353-
.removeClass('hidden');
351+
.removeClass('hidden');
354352
jQuery('.no-javascript-alert').addClass('hidden');
355353
//Navigation.disablePrevious();
356354
Rendering.transitionTo(Questions.first_question);
@@ -389,12 +387,13 @@ Questions.nextQuestionID = function () {
389387
Questions.nextQuestion = function () {
390388
// If the answer was OK, move on
391389
if (Questions.processAnswer()) {
390+
ValuesStack.push();
392391
var id = Questions.nextQuestionID();
393-
Questions.progress_stack.push(id);
392+
Values.question_id = id;
394393
if (id == 'finish') {
395394
Questions.finish();
396395
} else {
397-
Questions.transitionQuestion(id);
396+
Questions.transitionQuestion(Values.question_id);
398397
// Scroll down to make sure the input UI is visible
399398
jQuery('html,body').animate({
400399
scrollTop: jQuery('#button-question-next').offset().top}, 'slow');
@@ -404,22 +403,18 @@ Questions.nextQuestion = function () {
404403

405404
Questions.previousQuestion = function () {
406405
// If we are going back from *after* the last question, re-enable UI
407-
if (Questions.progress_stack[Questions.progress_stack.length - 1]
408-
== 'finish') {
406+
if (Values.question_id == 'finish') {
409407
Navigation.unfinishQuestions();
410408
}
411409
// Don't pop past the very first item
412-
if (Questions.progress_stack.length > 0) {
413-
// Clear current answer
414-
var current_question = Questions[Questions.current_question];
415-
delete Values[current_question.variable];
416-
Answers.removeAnswer(current_question.variable);
410+
if (ValuesStack.height() > 0) {
417411
// Go back
418-
var previous_question =
419-
Questions.progress_stack[Questions.progress_stack.length - 2];
420-
Questions.transitionQuestion(previous_question);
421-
Questions.progress_stack.pop();
412+
ValuesStack.pop();
413+
Questions.transitionQuestion(Values.question_id);
422414
Notifications.clearAlerts();
415+
// Clear previous answer
416+
var previous_question = Questions[Values.question_id];
417+
Answers.removeAnswer(previous_question.variable);
423418
// Scroll down to make sure the input UI is visible
424419
jQuery('html,body').animate({
425420
scrollTop: jQuery('#button-question-next').offset().top}, 'slow');
@@ -442,7 +437,7 @@ Questions.start = function () {
442437
Navigation.showAnswersTable();
443438
Navigation.showNextPrevious();
444439
jQuery('#button-question-next').click(Questions.nextQuestion);
445-
//jQuery('#button-question-back').click(Questions.previousQuestion);
440+
jQuery('#button-question-back').click(Questions.previousQuestion);
446441
// When the user presses "return" in a text area, move to next question
447442
jQuery('#question-rendering-area').on('submit', function () {
448443
if (jQuery('#button-question-next').is(':enabled')) {
@@ -451,5 +446,5 @@ Questions.start = function () {
451446
return false;
452447
});
453448
Questions.transitionQuestion(Questions.first_question);
454-
Questions.progress_stack.push(Questions.first_question);
449+
Values.question_id = Questions.first_question;
455450
};

wordpress-plugin/js/rules.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -163,30 +163,29 @@ Rules.section203Analysis = function () {
163163
|| (Values.pub_right == 'no')) {
164164
Values.term_begin = Values.k_year + 35;
165165
}
166-
console.log(Values.term_begin);
167166
if (Values.term_begin != undefined) {
168167
Values.term_end = Values.term_begin + 5;
169168
Values.notice_begin = Values.term_begin - 10;
170169
Values.notice_end = Values.term_end - 2;
171170
if (Values.notice_begin > Values.current_year) {
172-
Rules.addFlag('A.i.a');
171+
Rules.addFlag('A.i.a');
173172
} else if (Values.notice_end < Values.current_year) {
174-
Rules.addFlag('A.ii.a');
173+
Rules.addFlag('A.ii.a');
175174
}
176175
}
177176
if (Values.pub_right == 'maybe') {
178177
Values.p_term_begin = Values.k_year + 40;
179178
if (Values.pub_year != undefined) {
180-
Values.p_term_begin = Math.min(Values.pub_year + 35,
181-
Values.p_term_begin);
179+
Values.p_term_begin = Math.min(Values.pub_year + 35,
180+
Values.p_term_begin);
182181
}
183182
Values.p_term_end = Values.p_term_begin + 5;
184183
Values.p_notice_begin = Values.p_term_begin - 10;
185184
Values.p_notice_end = Values.p_term_end - 2;
186185
if (Values.p_notice_begin > Values.current_year) {
187-
Rules.addFlag('A.i.a');
186+
Rules.addFlag('A.i.a');
188187
} else if (Values.p_notice_end < Values.current_year) {
189-
Rules.addFlag('A.ii.a');
188+
Rules.addFlag('A.ii.a');
190189
}
191190
}
192191
}

wordpress-plugin/js/values.js

+18
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,21 @@ var varsToTitles = {
4949
work_agreement_desc: 'Agreement or Transfer Description',
5050
//'': 'Grantor',
5151
};
52+
53+
var ValuesStack = {};
54+
55+
ValuesStack._stack = [];
56+
57+
ValuesStack.height = function () {
58+
return this._stack.length;
59+
};
60+
61+
ValuesStack.push = function () {
62+
this._stack.push(Values);
63+
// Deep clone
64+
Values = JSON.parse(JSON.stringify(Values));
65+
};
66+
67+
ValuesStack.pop = function () {
68+
Values = this._stack.pop();
69+
};

0 commit comments

Comments
 (0)