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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import { destroy, isAlive, types } from "mobx-state-tree";
import { defaultStyle } from "../core/Constants";
import { guidGenerator } from "../core/Helpers";
import Result from "../regions/Result";
import { PER_REGION_MODES } from "./PerRegion";
import { ReadOnlyRegionMixin } from "./ReadOnlyMixin";
import { FF_LSDV_4930, FF_TAXONOMY_LABELING, isFF } from "../utils/feature-flags";
 
let ouid = 1;
 
export const AreaMixinBase = types
  .model({
    id: types.optional(types.identifier, guidGenerator),
    ouid: types.optional(types.number, () => ouid++),
    results: types.array(Result),
    parentID: types.maybeNull(types.string),
  })
  .views((self) => ({
    // self id without annotation id added to uniquiness across all the tree
    get cleanId() {
      return self.id.replace(/#.*/, "");
    },
 
    /**
     * @return {Result[]} all results with labeling (created by *Labels control)
     */
    get labelings() {
      return self.results.filter((r) => r.from_name.isLabeling);
    },
 
    /**
     * @return {Result?} first result with labels (usually it's the only one, but not always)
     */
    get labeling() {
      if (!isAlive(self)) {
        return undefined;
      }
      return self.results.find((r) => r.from_name.isLabeling && r.hasValue);
    },
 
    get emptyLabel() {
      return self.results.find((r) => r.from_name?.emptyLabel)?.from_name?.emptyLabel;
    },
 
    get texting() {
      return isAlive(self) && self.results.find((r) => r.type === "textarea" && r.hasValue);
    },
 
    get tag() {
      return self.labeling?.from_name;
    },
 
    hasLabel(value) {
      const labels = self.labeling?.mainValue;
 
      if (!labels || !value) return false;
      // label can contain comma, so check for full match first
      if (labels.includes(value)) return true;
      if (value.includes(",")) {
        return value.split(",").some((v) => labels.includes(v));
      }
      return false;
    },
 
    get perRegionTags() {
      return self.annotation.toNames.get(self.object.name)?.filter((tag) => tag.perregion) || [];
    },
 
    // special tags that can be used for labeling (only <Taxonomy isLabeling/> for now)
    get labelingTags() {
      if (!isFF(FF_TAXONOMY_LABELING)) return [];
 
      return self.annotation.toNames.get(self.object.name)?.filter((tag) => tag.classification && tag.isLabeling) || [];
    },
 
    get perRegionDescControls() {
      return self.perRegionTags.filter((tag) => tag.displaymode === PER_REGION_MODES.REGION_LIST);
    },
 
    get perRegionFocusTarget() {
      return self.perRegionTags.find((tag) => tag.isVisible !== false && tag.focusable);
    },
 
    get labelName() {
      if (!isAlive(self)) {
        return void 0;
      }
      return self.labeling?.mainValue?.[0] || self.emptyLabel?._value;
    },
 
    get labels() {
      return Array.from(self.labeling?.mainValue ?? []);
    },
 
    // used only in labels on regions for Image and Video tags
    getLabelText(joinstr) {
      const index = self.region_index;
      const label = self.labeling;
      const text = self.texting?.mainValue?.[0]?.replace(/\n\r|\n/, " ");
      const labelNames = label?.getSelectedString(joinstr);
      const labelText = [];
 
      if (index) labelText.push(String(index));
      if (labelNames) labelText.push(labelNames);
      if (text) labelText.push(text);
      return labelText.join(": ");
    },
 
    get parent() {
      if (!isAlive(self)) {
        return void 0;
      }
      return self.object;
    },
 
    get style() {
      if (!isAlive(self)) {
        return void 0;
      }
 
      const styled = self.results.find((r) => r.style);
 
      if (styled && styled.style) {
        return styled.style;
      }
      const emptyStyled = self.results.find((r) => r.emptyStyle);
 
      if (emptyStyled && emptyStyled.emptyStyle) {
        return emptyStyled.emptyStyle;
      }
 
      const controlStyled = self.results.find((r) => self.type.startsWith(r.type));
 
      return controlStyled && controlStyled.controlStyle;
    },
 
    // @todo may be slow, consider to add some code to annotation (un)select* methods
    get selected() {
      return self.annotation?.highlightedNode === self;
    },
 
    getOneColor() {
      return (self.style || defaultStyle).fillcolor;
    },
 
    get highlighted() {
      return self.parent?.selectionArea?.isActive ? self.isInSelectionArea : self._highlighted;
    },
 
    get isInSelectionArea() {
      return (!isFF(FF_LSDV_4930) || !self.hidden) && self.parent?.selectionArea?.isActive
        ? self.parent.selectionArea.intersectsBbox(self.bboxCoords)
        : false;
    },
 
    get supportSuggestions() {
      return self.object.supportSuggestions;
    },
 
    // index of the region in the regions tree (Outliner); will be updated on any order change
    get region_index() {
      if (!self.isRealRegion) {
        return null;
      }
      return self.annotation?.regionStore.regionIndexMap[self.id] || null;
    },
  }))
  .actions((self) => ({
    beforeDestroy() {
      self.results.forEach((r) => destroy(r));
 
      // Some region indexes have to be recalculated after destroying regions
      self.annotation?.updateAppearenceFromState?.();
    },
 
    setSelected(value) {
      self.selected = value;
    },
 
    /**
     * Remove region
     */
    deleteRegion() {
      if (self.annotation.isReadOnly()) return;
      if (self.isReadOnly()) return;
      if (self.selected) self.annotation.unselectAll(true);
      if (self.destroyRegion) self.destroyRegion();
      self.annotation.deleteRegion(self);
    },
 
    addResult(r) {
      self.results.push(r);
    },
 
    /**
     * Applies additional data from the given result.
     * In the results we have almost all data meaningful stored in value but in regions we have two places for it:
     * - region itself (fields in model)
     * - related results (in results array)
     * so for some fields we should control more if we want to apply fields that could be in both places into the region.
     * This method also helps to avoid region type detection at the deserialization stage.
     *
     * @param {Object} result - The result object containing additional data.
     * @returns {void}
     */
    applyAdditionalDataFromResult(_result) {
      // This method should be overridden if we need to get some additional data from result on deserialize
    },
 
    removeResult(r) {
      const index = self.results.indexOf(r);
 
      if (index < 0) return;
      self.results.splice(index, 1);
      destroy(r);
      if (!self.results.length) self.annotation.deleteArea(self);
    },
 
    setValue(tag) {
      const result = self.results.find((r) => r.from_name === tag);
      const values = tag.selectedValues();
 
      if (result) {
        if (tag.holdsState) result.setValue(values);
        else self.removeResult(result);
      } else {
        self.results.push({
          area: self,
          from_name: tag,
          to_name: self.object,
          type: tag.resultType,
          value: {
            [tag.valueType]: values,
          },
        });
      }
      self.updateAppearenceFromState && self.updateAppearenceFromState();
    },
  }));
 
export const AreaMixin = types.compose("AreaMixin", AreaMixinBase, ReadOnlyRegionMixin);