UNPKG

6.8 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2018 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
18 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
19 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
20 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
21 return c > 3 && r && Object.defineProperty(target, key, r), r;
22};
23var __metadata = (this && this.__metadata) || function (k, v) {
24 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
25};
26Object.defineProperty(exports, "__esModule", { value: true });
27exports.TreeSelectionServiceImpl = void 0;
28const inversify_1 = require("inversify");
29const tree_1 = require("./tree");
30const common_1 = require("../../common");
31const tree_selection_state_1 = require("./tree-selection-state");
32const tree_selection_1 = require("./tree-selection");
33const tree_focus_service_1 = require("./tree-focus-service");
34let TreeSelectionServiceImpl = class TreeSelectionServiceImpl {
35 constructor() {
36 this.onSelectionChangedEmitter = new common_1.Emitter();
37 }
38 init() {
39 this.state = new tree_selection_state_1.TreeSelectionState(this.tree);
40 }
41 dispose() {
42 this.onSelectionChangedEmitter.dispose();
43 }
44 get selectedNodes() {
45 return this.state.selection();
46 }
47 get onSelectionChanged() {
48 return this.onSelectionChangedEmitter.event;
49 }
50 fireSelectionChanged() {
51 this.onSelectionChangedEmitter.fire(this.state.selection());
52 }
53 addSelection(selectionOrTreeNode) {
54 const selection = ((arg) => {
55 const type = tree_selection_1.TreeSelection.SelectionType.DEFAULT;
56 if (tree_selection_1.TreeSelection.is(arg)) {
57 return {
58 type,
59 ...arg
60 };
61 }
62 return {
63 type,
64 node: arg
65 };
66 })(selectionOrTreeNode);
67 const node = this.validateNode(selection.node);
68 if (node === undefined) {
69 return;
70 }
71 Object.assign(selection, { node });
72 const newState = this.state.nextState(selection);
73 this.transiteTo(newState);
74 }
75 clearSelection() {
76 this.transiteTo(new tree_selection_state_1.TreeSelectionState(this.tree), false);
77 }
78 transiteTo(newState, setFocus = true) {
79 const oldNodes = this.state.selection();
80 const newNodes = newState.selection();
81 const toUnselect = this.difference(oldNodes, newNodes);
82 const toSelect = this.difference(newNodes, oldNodes);
83 this.unselect(toUnselect);
84 this.select(toSelect);
85 this.removeFocus(oldNodes, newNodes);
86 if (setFocus) {
87 this.addFocus(newState.node);
88 }
89 this.state = newState;
90 this.fireSelectionChanged();
91 }
92 unselect(nodes) {
93 nodes.forEach(node => node.selected = false);
94 }
95 select(nodes) {
96 nodes.forEach(node => node.selected = true);
97 }
98 removeFocus(...nodes) {
99 nodes.forEach(node => node.forEach(n => n.focus = false));
100 }
101 addFocus(node) {
102 if (node) {
103 node.focus = true;
104 }
105 this.focusService.setFocus(node);
106 }
107 /**
108 * Returns an array of the difference of two arrays. The returned array contains all elements that are contained by
109 * `left` and not contained by `right`. `right` may also contain elements not present in `left`: these are simply ignored.
110 */
111 difference(left, right) {
112 return left.filter(item => right.indexOf(item) === -1);
113 }
114 /**
115 * Returns a reference to the argument if the node exists in the tree. Otherwise, `undefined`.
116 */
117 validateNode(node) {
118 const result = this.tree.validateNode(node);
119 return tree_selection_1.SelectableTreeNode.is(result) ? result : undefined;
120 }
121 storeState() {
122 return {
123 selectionStack: this.state.selectionStack.map(s => ({
124 focus: s.focus && s.focus.id || undefined,
125 node: s.node && s.node.id || undefined,
126 type: s.type
127 }))
128 };
129 }
130 restoreState(state) {
131 const selectionStack = [];
132 for (const selection of state.selectionStack) {
133 const node = selection.node && this.tree.getNode(selection.node) || undefined;
134 if (!tree_selection_1.SelectableTreeNode.is(node)) {
135 break;
136 }
137 const focus = selection.focus && this.tree.getNode(selection.focus) || undefined;
138 selectionStack.push({
139 node,
140 focus: tree_selection_1.SelectableTreeNode.is(focus) && focus || undefined,
141 type: selection.type
142 });
143 }
144 if (selectionStack.length) {
145 this.transiteTo(new tree_selection_state_1.TreeSelectionState(this.tree, selectionStack));
146 }
147 }
148};
149__decorate([
150 (0, inversify_1.inject)(tree_1.Tree),
151 __metadata("design:type", Object)
152], TreeSelectionServiceImpl.prototype, "tree", void 0);
153__decorate([
154 (0, inversify_1.inject)(tree_focus_service_1.TreeFocusService),
155 __metadata("design:type", Object)
156], TreeSelectionServiceImpl.prototype, "focusService", void 0);
157__decorate([
158 (0, inversify_1.postConstruct)(),
159 __metadata("design:type", Function),
160 __metadata("design:paramtypes", []),
161 __metadata("design:returntype", void 0)
162], TreeSelectionServiceImpl.prototype, "init", null);
163TreeSelectionServiceImpl = __decorate([
164 (0, inversify_1.injectable)()
165], TreeSelectionServiceImpl);
166exports.TreeSelectionServiceImpl = TreeSelectionServiceImpl;
167//# sourceMappingURL=tree-selection-impl.js.map
\No newline at end of file