Skip to content

Commit

Permalink
fix: prevent disabling reduce by word
Browse files Browse the repository at this point in the history
  • Loading branch information
arildm committed Mar 8, 2025
1 parent 3e98961 commit c870d8e
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions app/scripts/components/reduce-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type ReduceSelectScope = IScope & {
keyItems: Record<string, Item>
hasWordAttrs: boolean
hasStructAttrs: boolean
isOnlyWordSelected: boolean
onDropdownToggle: (open: boolean) => void
toggleSelected: (value: string, event: MouseEvent) => void
toggleWordInsensitive: (event: MouseEvent) => void
Expand Down Expand Up @@ -55,7 +56,7 @@ angular.module("korpApp").component("reduceSelect", {
type="checkbox"
class="reduce-check"
ng-checked="keyItems['word'].selected"
ng-disabled="keyItems['word'].selected && $ctrl.selected.length == 1"
ng-disabled="isOnlyWordSelected"
/>
<span class="reduce-label">{{keyItems['word'].label | locObj:$root.lang }}</span>
<span
Expand Down Expand Up @@ -113,10 +114,12 @@ angular.module("korpApp").component("reduceSelect", {
for (const name of $ctrl.insensitive || []) {
if (name in scope.keyItems) scope.keyItems[name].insensitive = true
}

updateIsOnlyWordSelected()
}

/** Report any changes upwards */
function updateSelected() {
function notify() {
validate()

const selected = $ctrl.items.filter((item) => item.selected).map((item) => item.value)
Expand All @@ -132,6 +135,11 @@ angular.module("korpApp").component("reduceSelect", {
if (changes.selected || changes.insensitive) $ctrl.onChange(changes)
}

/** Update the local flag to whether only the word attr is selected. */
function updateIsOnlyWordSelected() {
scope.isOnlyWordSelected = $ctrl.items?.every((item) => item.value == "word" || !item.selected)
}

/** Fix state inconsistencies */
function validate() {
// If no selection given, default to selecting the word option
Expand All @@ -154,8 +162,12 @@ angular.module("korpApp").component("reduceSelect", {
// Unselect all options and select only the given option
$ctrl.items.forEach((item) => (item.selected = false))
item.selected = true
} else {
}
// Toggle given value, unless it is "word" and it is the only one selected.
else if (value != "word" || !scope.isOnlyWordSelected) {
item.selected = !item.selected
// Make sure the word option is locked if it's the only one remaining.
if (scope.keyItems["word"].selected) updateIsOnlyWordSelected()
}
}

Expand All @@ -173,7 +185,7 @@ angular.module("korpApp").component("reduceSelect", {
scope.keyItems["word"].selected = true
}

updateSelected()
notify()
}
},
],
Expand Down

0 comments on commit c870d8e

Please sign in to comment.