Bin
2025-12-17 611bfe34c3c96199eaaf6cf9e41a75892e44e879
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { types } from "mobx-state-tree";
import { PER_REGION_MODES } from "./PerRegionModes";
 
/**
 * This mixing defines perRegion control tag's parameter and related basic functionality
 * It should be used right after ClassificationBase mixin
 * @see ClassificationBase
 */
const PerRegionMixin = types
  .model({
    perregion: types.optional(types.boolean, false),
    whenlabelvalue: types.maybeNull(types.string),
    displaymode: types.optional(types.enumeration(Object.values(PER_REGION_MODES)), PER_REGION_MODES.TAG),
  })
  .extend((self) => {
    /* Validation */
    if (self.isClassificationTag !== true) {
      throw new Error("The PerRegionMixin mixin should be used only for classification control-tags");
    }
    return {};
  })
  .volatile(() => {
    return {
      focusable: false,
    };
  })
  .views((self) => ({
    get perRegionArea() {
      if (!self.perregion) return null;
      return self.annotation.highlightedNode;
    },
    get _perRegionResult() {
      const area = self.perRegionArea;
 
      if (!area) return null;
 
      return self.annotation.results.find((r) => r.from_name === self && r.area === area);
    },
    perRegionVisible() {
      if (!self.perregion) return true;
 
      const region = self.perRegionArea;
 
      if (!region) {
        // no region is selected return hidden
        return false;
      }
      // check if selected region is the one this tag is connected to
      if (region.parent.name !== self.toname) return false;
 
      // we may need to check for specific value
      if (self.whenlabelvalue !== null && self.whenlabelvalue !== undefined)
        return region.hasLabel(self.whenlabelvalue);
 
      return true;
    },
  }))
  .actions((self) => ({
    /**
     * Validates all values related to the current classification per region.
     *
     * - This method should not be overridden.
     * - It is used only in validate method of the ClassificationBase mixin.
     *
     * @returns {boolean}
     * @private
     */
    _validatePerRegion() {
      const objectTag = self.toNameTag;
 
      for (const reg of objectTag.allRegs) {
        const value = reg.results.find((s) => s.from_name === self)?.mainValue;
        const isValid = self.validateValue(value);
 
        if (!isValid) {
          self.annotation.selectArea(reg);
          return false;
        }
      }
 
      return true;
    },
    createPerRegionResult() {
      self.perRegionArea?.setValue(self);
    },
  }));
 
export default PerRegionMixin;
export { PER_REGION_MODES } from "./PerRegionModes";