Bin
2025-12-17 d616898802dfe7e5dd648bcf53c6d1f86b6d3642
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { types } from "mobx-state-tree";
import { isDefined } from "../utils/utilities";
 
const SelectedChoiceMixin = types.model().views((self) => ({
  findSelectedChoice(aliasOrValue) {
    let item;
 
    if (self.findLabel) {
      item = self.findLabel(aliasOrValue);
    } else if (self.findItemByValueOrAlias) {
      item = self.findItemByValueOrAlias(aliasOrValue);
    }
 
    return item?.alias || item?.value;
  },
  selectedChoicesMatch(aliasOrValue1, aliasOrValue2) {
    const choice1 = self.findSelectedChoice(aliasOrValue1);
    const choice2 = self.findSelectedChoice(aliasOrValue2);
 
    return isDefined(choice1) && isDefined(choice2) && choice1 === choice2;
  },
  // @todo it's better to only take final values into account
  // @todo (meaning alias only, not alias + value when alias is present)
  // @todo so this should be the final and simpliest method
  hasChoiceSelectionSimple(choiceValue) {
    if (choiceValue?.length) {
      // grab the string value; for taxonomy, it's the last value in the array
      const selectedValues = self.selectedValues().map((s) => (Array.isArray(s) ? s.at(-1) : s));
      return choiceValue.some((value) => selectedValues.includes(value));
    }
 
    return self.isSelected;
  },
  hasChoiceSelection(choiceValue, selectedValues = []) {
    if (choiceValue?.length) {
      // @todo Revisit this and make it more consistent, and refactor this
      // behaviour out of the SelectedModel mixin and use a singular approach.
      // This is the original behaviour of other SelectedModel mixin usages
      // as they are using alias lookups for choices. For now we will keep it as is since it works for all the
      // other cases currently.
      if (self.findLabel && self.type !== "taxonomy") {
        return choiceValue.map((v) => self.findLabel(v)).some((c) => c && c.sel);
      }
 
      // Check the selected values of the choices for existence of the choiceValue(s)
      if (selectedValues.length) {
        const includesValue = (v) => {
          if (self.findItemByValueOrAlias) {
            const item = self.findItemByValueOrAlias(v);
 
            v = item?.alias || item?.value || v;
          }
 
          return selectedValues.map((s) => (Array.isArray(s) ? s.at(-1) : s)).includes(v);
        };
 
        return choiceValue.some(includesValue);
      }
 
      return false;
    }
 
    return self.isSelected;
  },
}));
 
export default SelectedChoiceMixin;