|
23 | 23 | this.optionsForm = document.querySelector('form');
|
24 | 24 | this.submitButtonInOptionsForm = this.optionsForm.querySelector('button[type="submit"]');
|
25 | 25 |
|
| 26 | + this.displaySourceTargetBranchesOptionsDiv = document.querySelector('div#display-source-target-branches-options'); |
| 27 | + this.displaySourceAndTargetBranchesCheckbox = document.querySelector('input#display_source_and_target_branches'); |
26 | 28 | this.enableButtonsToCopySourceAndTargetBranchesNameCheckbox = document.querySelector('input#enable_buttons_to_copy_source_and_target_branches_name');
|
27 | 29 |
|
28 | 30 | this.copyMrInfoOptionsDiv = document.querySelector('div#copy-mr-info-options');
|
|
44 | 46 | let self = this;
|
45 | 47 |
|
46 | 48 | this.preferencesManager.getAll(function(preferences) {
|
| 49 | + self.displaySourceAndTargetBranchesCheckbox.checked = preferences.display_source_and_target_branches; |
| 50 | + self.displaySourceAndTargetBranchesCheckbox.dispatchEvent(new CustomEvent('change')); |
| 51 | + |
47 | 52 | self.enableButtonsToCopySourceAndTargetBranchesNameCheckbox.checked = preferences.enable_buttons_to_copy_source_and_target_branches_name;
|
48 | 53 |
|
49 | 54 | self.enableButtonToCopyMrInfoCheckbox.checked = preferences.enable_button_to_copy_mr_info;
|
|
61 | 66 | }).checked = true;
|
62 | 67 |
|
63 | 68 | self.enableButtonToToggleWipStatusCheckbox.checked = preferences.enable_button_to_toggle_wip_status;
|
| 69 | + self.enableButtonToToggleWipStatusCheckbox.dispatchEvent(new CustomEvent('change')); |
64 | 70 | });
|
65 | 71 | }
|
66 | 72 |
|
|
73 | 79 | this.optionsForm.addEventListener('submit', function(e) {
|
74 | 80 | e.preventDefault();
|
75 | 81 |
|
| 82 | + if (self.hasUserDisabledAllFeatures()) { |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
76 | 86 | if (!self.initializeVisualFeedbackOnSubmitButton()) {
|
77 | 87 | return false;
|
78 | 88 | }
|
79 | 89 |
|
80 | 90 | self.saveOptionsToStorage();
|
81 | 91 | });
|
82 | 92 |
|
| 93 | + this.displaySourceAndTargetBranchesCheckbox.addEventListener('change', function() { |
| 94 | + self.displaySourceTargetBranchesOptionsDiv.classList.toggle('is-hidden', !this.checked); |
| 95 | + |
| 96 | + self.forceUserToEnableAtLeastOneFeatureIfNecessarily(); |
| 97 | + }); |
| 98 | + |
83 | 99 | this.enableButtonToCopyMrInfoCheckbox.addEventListener('change', function() {
|
84 | 100 | self.copyMrInfoOptionsDiv.classList.toggle('is-hidden', !this.checked);
|
85 | 101 | self.copyMrInfoFormatTextarea.toggleAttribute('required', this.checked);
|
| 102 | + |
| 103 | + self.forceUserToEnableAtLeastOneFeatureIfNecessarily(); |
86 | 104 | });
|
87 | 105 |
|
88 | 106 | this.enableJiraTicketLinkCheckbox.addEventListener('change', function() {
|
|
92 | 110 | self.jiraTicketLinkLabelTypeRadioButtons.forEach(function(el) {
|
93 | 111 | el.toggleAttribute('required', this.checked);
|
94 | 112 | }, this);
|
| 113 | + |
| 114 | + self.forceUserToEnableAtLeastOneFeatureIfNecessarily(); |
| 115 | + }); |
| 116 | + |
| 117 | + this.enableButtonToToggleWipStatusCheckbox.addEventListener('change', function() { |
| 118 | + self.forceUserToEnableAtLeastOneFeatureIfNecessarily(); |
95 | 119 | });
|
96 | 120 | }
|
97 | 121 |
|
|
107 | 131 |
|
108 | 132 | this.preferencesManager.setAll(
|
109 | 133 | {
|
| 134 | + display_source_and_target_branches: this.displaySourceAndTargetBranchesCheckbox.checked, |
110 | 135 | enable_buttons_to_copy_source_and_target_branches_name: this.enableButtonsToCopySourceAndTargetBranchesNameCheckbox.checked,
|
111 | 136 | enable_button_to_copy_mr_info: this.enableButtonToCopyMrInfoCheckbox.checked,
|
112 | 137 | copy_mr_info_format: this.copyMrInfoFormatTextarea.value,
|
|
124 | 149 | );
|
125 | 150 | }
|
126 | 151 |
|
| 152 | + /** |
| 153 | + * Force the user to enable at least one feature if he disabled all the features of |
| 154 | + * the extension (which is useless). |
| 155 | + */ |
| 156 | + forceUserToEnableAtLeastOneFeatureIfNecessarily() { |
| 157 | + if (this.hasUserDisabledAllFeatures() && !this.submitButtonInOptionsForm.disabled) { |
| 158 | + this.submitButtonInOptionsForm.disabled = true; |
| 159 | + this.submitButtonInOptionsForm.dataset.originalTextContent = this.submitButtonInOptionsForm.textContent; |
| 160 | + this.submitButtonInOptionsForm.textContent = '⚠️ Please enable at least one feature'; |
| 161 | + } else if (this.submitButtonInOptionsForm.disabled) { |
| 162 | + this.submitButtonInOptionsForm.disabled = false; |
| 163 | + this.submitButtonInOptionsForm.textContent = this.submitButtonInOptionsForm.dataset.originalTextContent; |
| 164 | + |
| 165 | + delete this.submitButtonInOptionsForm.dataset.originalTextContent; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Determine if the user has disabled all the features of the extension (which is useless). |
| 171 | + */ |
| 172 | + hasUserDisabledAllFeatures() { |
| 173 | + return !this.displaySourceAndTargetBranchesCheckbox.checked |
| 174 | + && !this.enableButtonToCopyMrInfoCheckbox.checked |
| 175 | + && !this.enableJiraTicketLinkCheckbox.checked |
| 176 | + && !this.enableButtonToToggleWipStatusCheckbox.checked; |
| 177 | + } |
| 178 | + |
127 | 179 | /**
|
128 | 180 | * Returns the browser name the extension is currently running on.
|
129 | 181 | */
|
|
0 commit comments