UNPKG

4.75 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// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.TreeSourceNode = exports.CompositeTreeElementNode = exports.TreeElementNode = exports.SourceTree = void 0;
19const tslib_1 = require("tslib");
20/* eslint-disable @typescript-eslint/no-explicit-any */
21const inversify_1 = require("inversify");
22const tree_1 = require("../tree");
23const tree_source_1 = require("./tree-source");
24let SourceTree = class SourceTree extends tree_1.TreeImpl {
25 async resolveChildren(parent) {
26 const elements = await this.resolveElements(parent);
27 const nodes = [];
28 let index = 0;
29 for (const element of elements) {
30 if (element.visible !== false) {
31 nodes.push(this.toNode(element, index++, parent));
32 }
33 }
34 return nodes;
35 }
36 resolveElements(parent) {
37 if (TreeSourceNode.is(parent)) {
38 return parent.source.getElements();
39 }
40 return parent.element.getElements();
41 }
42 toNode(element, index, parent) {
43 const id = element.id ? String(element.id) : (parent.id + ':' + index);
44 const name = id;
45 const existing = this.getNode(id);
46 const updated = existing && Object.assign(existing, { element, parent });
47 if (tree_source_1.CompositeTreeElement.hasElements(element)) {
48 if (updated) {
49 if (!tree_1.ExpandableTreeNode.is(updated)) {
50 Object.assign(updated, { expanded: false });
51 }
52 if (!tree_1.CompositeTreeNode.is(updated)) {
53 Object.assign(updated, { children: [] });
54 }
55 return updated;
56 }
57 return {
58 element,
59 parent,
60 id,
61 name,
62 selected: false,
63 expanded: false,
64 children: []
65 };
66 }
67 if (CompositeTreeElementNode.is(updated)) {
68 delete updated.expanded;
69 delete updated.children;
70 }
71 if (updated) {
72 if (tree_1.ExpandableTreeNode.is(updated)) {
73 delete updated.expanded;
74 }
75 if (tree_1.CompositeTreeNode.is(updated)) {
76 delete updated.children;
77 }
78 return updated;
79 }
80 return {
81 element,
82 parent,
83 id,
84 name,
85 selected: false
86 };
87 }
88};
89SourceTree = (0, tslib_1.__decorate)([
90 (0, inversify_1.injectable)()
91], SourceTree);
92exports.SourceTree = SourceTree;
93var TreeElementNode;
94(function (TreeElementNode) {
95 function is(node) {
96 return tree_1.SelectableTreeNode.is(node) && 'element' in node;
97 }
98 TreeElementNode.is = is;
99})(TreeElementNode = exports.TreeElementNode || (exports.TreeElementNode = {}));
100var CompositeTreeElementNode;
101(function (CompositeTreeElementNode) {
102 function is(node) {
103 return TreeElementNode.is(node) && tree_1.CompositeTreeNode.is(node) && tree_1.ExpandableTreeNode.is(node) && !!node.visible;
104 }
105 CompositeTreeElementNode.is = is;
106})(CompositeTreeElementNode = exports.CompositeTreeElementNode || (exports.CompositeTreeElementNode = {}));
107var TreeSourceNode;
108(function (TreeSourceNode) {
109 function is(node) {
110 return tree_1.CompositeTreeNode.is(node) && !node.visible && 'source' in node;
111 }
112 TreeSourceNode.is = is;
113 function to(source) {
114 if (!source) {
115 return source;
116 }
117 const id = source.id || '__source__';
118 return {
119 id,
120 name: id,
121 visible: false,
122 children: [],
123 source,
124 parent: undefined,
125 selected: false
126 };
127 }
128 TreeSourceNode.to = to;
129})(TreeSourceNode = exports.TreeSourceNode || (exports.TreeSourceNode = {}));
130//# sourceMappingURL=source-tree.js.map
\No newline at end of file