From f1dffd1cb83a62964ae81db51f307f4e58e7f465 Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Wed, 5 Dec 2018 12:01:02 +0000 Subject: [PATCH] if state is unknown on load, then work out which action buttons to show previously, it'd always show nothing-selected-buttons - however, if some items were checked (due to being selected previously, and loading with a form error message), it would be showing the wrong buttons. Now, if the state is unknown, work out which state to show by counting checkboxes, the same as when someone presses the cancel button. --- app/assets/javascripts/templateFolderForm.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/templateFolderForm.js b/app/assets/javascripts/templateFolderForm.js index c09a93599..3b223d424 100644 --- a/app/assets/javascripts/templateFolderForm.js +++ b/app/assets/javascripts/templateFolderForm.js @@ -25,8 +25,10 @@ this.states.filter(state => state.cancellable).forEach((x) => this.addCancelButton(x)); // first off show the new template / new folder buttons - let prevState = this.$form.data('prev-state') || 'unknown'; - this.currentState = (prevState === 'unknown') ? 'nothing-selected-buttons' : prevState; + this.currentState = this.$form.data('prev-state') || 'unknown'; + if (this.currentState === 'unknown') { + this.selectActionButtons(); + } this.$form.on('click', 'button.button-secondary', (event) => this.actionButtonClicked(event)); this.$form.on('change', 'input[type=checkbox]', () => this.templateFolderCheckboxChanged()); @@ -43,15 +45,21 @@ state.$el.find('input:radio').prop('checked', false); state.$el.find('input:text').val(''); - // gross hack - pretend we're in the choose actions state, then pretend a checkbox was clicked to work out - // whether to show zero or non-zero options. This calls a render at the end - this.currentState = 'nothing-selected-buttons'; - this.templateFolderCheckboxChanged(); + // go back to action buttons + this.selectActionButtons(); }); state.$el.append($cancel); }; + this.selectActionButtons = function () { + // If we want to show one of the grey choose actions state, we can pretend we're in the choose actions state, + // and then pretend a checkbox was clicked to work out whether to show zero or non-zero options. + // This calls a render at the end + this.currentState = 'nothing-selected-buttons'; + this.templateFolderCheckboxChanged(); + }; + this.actionButtonClicked = function(event) { event.preventDefault(); this.currentState = $(event.currentTarget).val();