1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.StatusBarViewModel = void 0;
|
19 | const tslib_1 = require("tslib");
|
20 | const inversify_1 = require("inversify");
|
21 | const common_1 = require("../../common");
|
22 | const status_bar_types_1 = require("./status-bar-types");
|
23 | let StatusBarViewModel = class StatusBarViewModel {
|
24 | constructor() {
|
25 | this.leftTree = new Array();
|
26 | this.rightTree = new Array();
|
27 | this.containerPointers = new Map();
|
28 | this.onDidChangeEmitter = new common_1.Emitter();
|
29 | }
|
30 | get onDidChange() {
|
31 | return this.onDidChangeEmitter.event;
|
32 | }
|
33 | *getLeft() {
|
34 | yield* this.getEntries(this.leftTree);
|
35 | }
|
36 | *getRight() {
|
37 | yield* this.getEntries(this.rightTree);
|
38 | }
|
39 | *getEntries(list) {
|
40 | for (const item of list) {
|
41 | yield* this.getChildren(item.leftChildren, status_bar_types_1.StatusBarAlignment.LEFT);
|
42 | yield { entry: item.head, id: item.id };
|
43 | yield* this.getChildren(item.rightChildren, status_bar_types_1.StatusBarAlignment.RIGHT);
|
44 | }
|
45 | }
|
46 | *getChildren(list, alignment, compact) {
|
47 | var _a, _b, _c;
|
48 | for (const item of list) {
|
49 | if (item.leftChildren.length || item.rightChildren.length) {
|
50 | console.warn(`Found embedded entries with affinity to ${item.id}. They will inherit alignment and compactness of parent.`);
|
51 | }
|
52 | yield* this.getChildren(item.leftChildren, alignment, (_a = item.head.affinity) === null || _a === void 0 ? void 0 : _a.compact);
|
53 | yield { entry: item.head, id: item.id, alignment, compact: compact || ((_b = item.head.affinity) === null || _b === void 0 ? void 0 : _b.compact) };
|
54 | yield* this.getChildren(item.rightChildren, alignment, (_c = item.head.affinity) === null || _c === void 0 ? void 0 : _c.compact);
|
55 | }
|
56 | }
|
57 | set(id, entry) {
|
58 | const existing = this.findElement(id);
|
59 | if (existing) {
|
60 | const oldEntry = existing.entry.head;
|
61 | existing.entry.head = entry;
|
62 | if (!this.shareSameContainer(entry, oldEntry)) {
|
63 | this.relocate(existing);
|
64 | }
|
65 | else if (!this.shareSamePriority(entry, oldEntry)) {
|
66 | this.sort(existing.container);
|
67 | }
|
68 | }
|
69 | else {
|
70 | const container = this.getContainerFor(entry);
|
71 | const viewModelEntry = { id, head: entry, leftChildren: [], rightChildren: [] };
|
72 | container.push(viewModelEntry);
|
73 | this.containerPointers.set(id, container);
|
74 | const pendingDependents = this.getDependentsOf(id);
|
75 | pendingDependents.forEach(newChild => this.relocate(newChild, true));
|
76 | this.sortDependents(viewModelEntry.leftChildren);
|
77 | this.sortDependents(viewModelEntry.rightChildren);
|
78 | this.sort(container);
|
79 | }
|
80 | this.onDidChangeEmitter.fire();
|
81 | }
|
82 | relocate(locationData, dontSort) {
|
83 | const newContainer = this.getContainerFor(locationData.entry.head);
|
84 | locationData.container.splice(locationData.index, 1);
|
85 | newContainer.push(locationData.entry);
|
86 | this.containerPointers.set(locationData.entry.id, newContainer);
|
87 | if (!dontSort) {
|
88 | this.sort(newContainer);
|
89 | }
|
90 | }
|
91 | getContainerFor(entry) {
|
92 | const affinityParent = entry.affinity && this.findElement(entry.affinity.id);
|
93 | if (affinityParent) {
|
94 | return entry.affinity.alignment === status_bar_types_1.StatusBarAlignment.LEFT ? affinityParent.entry.leftChildren : affinityParent.entry.rightChildren;
|
95 | }
|
96 | return entry.alignment === status_bar_types_1.StatusBarAlignment.LEFT ? this.leftTree : this.rightTree;
|
97 | }
|
98 | getDependentsOf(id) {
|
99 | var _a, _b, _c, _d;
|
100 | const dependents = [];
|
101 | for (let index = 0; index < this.rightTree.length || index < this.leftTree.length; index++) {
|
102 | if (((_b = (_a = this.rightTree[index]) === null || _a === void 0 ? void 0 : _a.head.affinity) === null || _b === void 0 ? void 0 : _b.id) === id) {
|
103 | dependents.push({ index, container: this.rightTree, entry: this.rightTree[index] });
|
104 | }
|
105 | if (((_d = (_c = this.leftTree[index]) === null || _c === void 0 ? void 0 : _c.head.affinity) === null || _d === void 0 ? void 0 : _d.id) === id) {
|
106 | dependents.push({ index, container: this.leftTree, entry: this.leftTree[index] });
|
107 | }
|
108 | }
|
109 | return dependents;
|
110 | }
|
111 | remove(id) {
|
112 | const location = this.findElement(id);
|
113 | if (location) {
|
114 | this.containerPointers.delete(id);
|
115 | location.container.splice(location.index, 1);
|
116 | const originalLeftLength = this.leftTree.length;
|
117 | const originalRightLength = this.rightTree.length;
|
118 | this.inline(location.entry.leftChildren, location.entry.rightChildren);
|
119 | if (originalLeftLength !== this.leftTree.length) {
|
120 | this.sortTop(this.leftTree);
|
121 | }
|
122 | if (originalRightLength !== this.rightTree.length) {
|
123 | this.sortTop(this.rightTree);
|
124 | }
|
125 | this.onDidChangeEmitter.fire();
|
126 | return true;
|
127 | }
|
128 | return false;
|
129 | }
|
130 | shareSamePositionParameters(left, right) {
|
131 | var _a, _b, _c, _d, _e, _f, _g, _h;
|
132 | if (((_a = left.priority) !== null && _a !== void 0 ? _a : 0) !== ((_b = right.priority) !== null && _b !== void 0 ? _b : 0)) {
|
133 | return false;
|
134 | }
|
135 | if (this.affinityMatters(left, right)) {
|
136 | return ((_c = left.affinity) === null || _c === void 0 ? void 0 : _c.id) === ((_d = right.affinity) === null || _d === void 0 ? void 0 : _d.id) && ((_e = left.affinity) === null || _e === void 0 ? void 0 : _e.alignment) === ((_f = right.affinity) === null || _f === void 0 ? void 0 : _f.alignment) && ((_g = left.affinity) === null || _g === void 0 ? void 0 : _g.compact) === ((_h = right.affinity) === null || _h === void 0 ? void 0 : _h.compact);
|
137 | }
|
138 | return left.alignment === right.alignment;
|
139 | }
|
140 | shareSameContainer(left, right) {
|
141 | var _a, _b, _c, _d;
|
142 | if (this.affinityMatters(left, right)) {
|
143 | return ((_a = left.affinity) === null || _a === void 0 ? void 0 : _a.id) === ((_b = right.affinity) === null || _b === void 0 ? void 0 : _b.id) && ((_c = left.affinity) === null || _c === void 0 ? void 0 : _c.alignment) === ((_d = right.affinity) === null || _d === void 0 ? void 0 : _d.alignment);
|
144 | }
|
145 | return left.alignment === right.alignment;
|
146 | }
|
147 | shareSamePriority(left, right) {
|
148 | var _a, _b, _c, _d;
|
149 | return ((_a = left.priority) !== null && _a !== void 0 ? _a : 0) === ((_b = right.priority) !== null && _b !== void 0 ? _b : 0) && (!this.affinityMatters(left, right) || ((_c = left.affinity) === null || _c === void 0 ? void 0 : _c.compact) === ((_d = right.affinity) === null || _d === void 0 ? void 0 : _d.compact));
|
150 | }
|
151 | affinityMatters(left, right) {
|
152 | return (left.affinity && this.containerPointers.has(left.affinity.id)) || Boolean(right.affinity && this.containerPointers.has(right.affinity.id));
|
153 | }
|
154 | findElement(id) {
|
155 | const container = id !== undefined && this.containerPointers.get(id);
|
156 | if (container) {
|
157 | const index = container.findIndex(candidate => candidate.id === id);
|
158 | if (index !== -1) {
|
159 | return { index, entry: container[index], container };
|
160 | }
|
161 | }
|
162 | }
|
163 | sort(container) {
|
164 | if (container === this.leftTree || container === this.rightTree) {
|
165 | this.sortTop(container);
|
166 | }
|
167 | else {
|
168 | this.sortDependents(container);
|
169 | }
|
170 | }
|
171 | sortTop(container) {
|
172 | container.sort((left, right) => this.comparePriority(left, right));
|
173 | }
|
174 | comparePriority(left, right) {
|
175 | var _a, _b;
|
176 | return ((_a = right.head.priority) !== null && _a !== void 0 ? _a : 0) - ((_b = left.head.priority) !== null && _b !== void 0 ? _b : 0);
|
177 | }
|
178 | sortDependents(container) {
|
179 | container.sort((left, right) => {
|
180 | var _a, _b, _c;
|
181 | if (((_a = left.head.affinity) === null || _a === void 0 ? void 0 : _a.compact) && !((_b = right.head.affinity) === null || _b === void 0 ? void 0 : _b.compact)) {
|
182 | return common_1.ArrayUtils.Sort.LeftBeforeRight;
|
183 | }
|
184 | else if ((_c = right.head.affinity) === null || _c === void 0 ? void 0 : _c.compact) {
|
185 | return common_1.ArrayUtils.Sort.RightBeforeLeft;
|
186 | }
|
187 | return this.comparePriority(left, right);
|
188 | });
|
189 | }
|
190 | inline(left, right) {
|
191 | for (const entry of left) {
|
192 | this.doAddTop(entry);
|
193 | }
|
194 | for (const entry of right) {
|
195 | this.doAddTop(entry);
|
196 | }
|
197 | }
|
198 | doAddTop(entry) {
|
199 | const container = entry.head.alignment === status_bar_types_1.StatusBarAlignment.LEFT ? this.leftTree : this.rightTree;
|
200 | this.containerPointers.set(entry.id, container);
|
201 | container.push(entry);
|
202 | }
|
203 | };
|
204 | StatusBarViewModel = (0, tslib_1.__decorate)([
|
205 | (0, inversify_1.injectable)()
|
206 | ], StatusBarViewModel);
|
207 | exports.StatusBarViewModel = StatusBarViewModel;
|
208 |
|
\ | No newline at end of file |