UNPKG

9.63 kBJavaScriptView Raw
1var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4 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;
5 return c > 3 && r && Object.defineProperty(target, key, r), r;
6};
7var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8 if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9 if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10 return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11};
12var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
13 if (kind === "m") throw new TypeError("Private method is not writable");
14 if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
15 if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
16 return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
17};
18var _DuoyunTreeElement_highlights, _DuoyunTreeElement_tagsMap, _DuoyunTreeElement_statusMap, _DuoyunTreeElement_onClick, _DuoyunTreeElement_getItemColor, _DuoyunTreeElement_renderItem;
19var DuoyunTreeElement_1;
20// https://spectrum.adobe.com/page/tree-view/
21import { adoptedStyle, customElement, emitter, property, boolattribute, part, } from '@mantou/gem/lib/decorators';
22import { GemElement, html } from '@mantou/gem/lib/element';
23import { createCSSSheet, css, styleMap } from '@mantou/gem/lib/utils';
24import { icons } from '../lib/icons';
25import { commonHandle } from '../lib/hotkeys';
26import { theme, getSemanticColor } from '../lib/theme';
27import { getCascaderBubbleWeakMap } from '../lib/utils';
28import { focusStyle } from '../lib/styles';
29import './use';
30const statusRank = {
31 negative: 3,
32 notice: 2,
33 positive: 1,
34};
35const itemStyle = createCSSSheet(css `
36 :host(:where(:not([hidden]))) {
37 display: flex;
38 align-items: center;
39 gap: 0.3em;
40 cursor: pointer;
41 line-height: 2;
42 user-select: none;
43 }
44 :host(:hover) {
45 background-color: ${theme.lightBackgroundColor};
46 }
47 :host([highlight]) {
48 background-color: ${theme.hoverBackgroundColor};
49 }
50 .icon {
51 width: 1.3em;
52 flex-shrink: 0;
53 }
54 .expandable {
55 margin-inline-start: 0.3em;
56 }
57 .label {
58 flex-grow: 1;
59 overflow: hidden;
60 white-space: nowrap;
61 text-overflow: ellipsis;
62 }
63 .tags {
64 flex-shrink: 1;
65 white-space: nowrap;
66 margin-inline: 1em;
67 }
68 .children {
69 width: 0.5em;
70 aspect-ratio: 1;
71 background: currentColor;
72 border-radius: 10em;
73 }
74`);
75/**
76 * @customElement dy-tree-item
77 */
78let _DuoyunTreeItemElement = class _DuoyunTreeItemElement extends GemElement {
79 constructor() {
80 super({ delegatesFocus: true });
81 this.render = () => {
82 if (!this.item)
83 return html ``;
84 const { label, children, icon, context, tags } = this.item;
85 return html `
86 <dy-use
87 class="icon expandable"
88 .element=${!children ? undefined : this.expanded ? icons.expand : icons.right}
89 ></dy-use>
90 ${icon ? html `<dy-use class="icon" .element=${icon}></dy-use>` : ''}
91 ${context ? html `<div class="context">${context}</div>` : ''}
92 <span class="label">${label}</span>
93 ${this.hastags ? html `<div class="tags children"></div>` : ''}
94 ${tags ? html `<div class="tags">${tags.join(', ')}</div>` : ''}
95 `;
96 };
97 this.internals.role = 'treeitem';
98 }
99};
100__decorate([
101 boolattribute
102], _DuoyunTreeItemElement.prototype, "expanded", void 0);
103__decorate([
104 boolattribute
105], _DuoyunTreeItemElement.prototype, "highlight", void 0);
106__decorate([
107 boolattribute
108], _DuoyunTreeItemElement.prototype, "hastags", void 0);
109__decorate([
110 property
111], _DuoyunTreeItemElement.prototype, "item", void 0);
112_DuoyunTreeItemElement = __decorate([
113 customElement('dy-tree-item'),
114 adoptedStyle(itemStyle)
115], _DuoyunTreeItemElement);
116const style = createCSSSheet(css `
117 :host(:where(:not([hidden]))) {
118 display: block;
119 font-size: 0.875em;
120 }
121`);
122/**
123 * @customElement dy-tree
124 * @fires itemclick
125 */
126let DuoyunTreeElement = DuoyunTreeElement_1 = class DuoyunTreeElement extends GemElement {
127 constructor() {
128 super({ delegatesFocus: true });
129 this.state = {
130 expandItem: new Set(),
131 };
132 _DuoyunTreeElement_highlights.set(this, void 0);
133 // folder
134 _DuoyunTreeElement_tagsMap.set(this, void 0);
135 _DuoyunTreeElement_statusMap.set(this, void 0);
136 _DuoyunTreeElement_onClick.set(this, (item) => {
137 var _a;
138 const value = (_a = item.value) !== null && _a !== void 0 ? _a : item.label;
139 this.itemclick(value);
140 if (item.children) {
141 const { expandItem } = this.state;
142 if (expandItem.has(value)) {
143 expandItem.delete(value);
144 }
145 else {
146 expandItem.add(value);
147 }
148 this.setState({});
149 }
150 });
151 _DuoyunTreeElement_getItemColor.set(this, (item) => {
152 const status = item.status || __classPrivateFieldGet(this, _DuoyunTreeElement_statusMap, "f").get(item);
153 return (status && getSemanticColor(status)) || theme.textColor;
154 });
155 _DuoyunTreeElement_renderItem.set(this, (item, level) => {
156 var _a, _b, _c;
157 const value = (_a = item.value) !== null && _a !== void 0 ? _a : item.label;
158 const expanded = this.state.expandItem.has((_b = item.value) !== null && _b !== void 0 ? _b : item.label);
159 return html `
160 <dy-tree-item
161 tabindex="0"
162 @keydown=${commonHandle}
163 @click=${() => __classPrivateFieldGet(this, _DuoyunTreeElement_onClick, "f").call(this, item)}
164 part=${DuoyunTreeElement_1.item}
165 style=${styleMap({ paddingLeft: `${level}em`, color: __classPrivateFieldGet(this, _DuoyunTreeElement_getItemColor, "f").call(this, item) })}
166 .hastags=${__classPrivateFieldGet(this, _DuoyunTreeElement_tagsMap, "f").has(item)}
167 .item=${item}
168 .expanded=${expanded}
169 .highlight=${__classPrivateFieldGet(this, _DuoyunTreeElement_highlights, "f").has(value)}
170 ></dy-tree-item>
171 ${expanded ? (_c = item.children) === null || _c === void 0 ? void 0 : _c.map((item) => __classPrivateFieldGet(this, _DuoyunTreeElement_renderItem, "f").call(this, item, level + 1)) : ''}
172 `;
173 });
174 this.willMount = () => {
175 this.memo(() => {
176 __classPrivateFieldSet(this, _DuoyunTreeElement_highlights, new Set(this.highlights), "f");
177 }, () => [this.highlights]);
178 this.memo(() => {
179 __classPrivateFieldSet(this, _DuoyunTreeElement_tagsMap, getCascaderBubbleWeakMap(this.data, 'children', (e) => e.tags), "f");
180 __classPrivateFieldSet(this, _DuoyunTreeElement_statusMap, getCascaderBubbleWeakMap(this.data, 'children', (e) => e.status, (a, b) => (statusRank[a] > statusRank[b] ? a : b)), "f");
181 }, () => [this.data]);
182 };
183 this.mounted = () => {
184 this.effect(() => {
185 if (!this.data)
186 return;
187 const getExpandableValues = (list) => list.map((e) => { var _a; return (e.children ? [(_a = e.label) !== null && _a !== void 0 ? _a : e.value, ...getExpandableValues(e.children)] : undefined); });
188 const newValue = new Set(getExpandableValues(this.data).flat());
189 this.state.expandItem.forEach((e) => {
190 if (!newValue.has(e)) {
191 this.state.expandItem.delete(e);
192 this.setState({});
193 }
194 });
195 }, () => [this.data]);
196 };
197 this.render = () => {
198 var _a;
199 return html `${(_a = this.data) === null || _a === void 0 ? void 0 : _a.map((item) => __classPrivateFieldGet(this, _DuoyunTreeElement_renderItem, "f").call(this, item, 0))}`;
200 };
201 this.internals.role = 'tree';
202 }
203};
204_DuoyunTreeElement_highlights = new WeakMap(), _DuoyunTreeElement_tagsMap = new WeakMap(), _DuoyunTreeElement_statusMap = new WeakMap(), _DuoyunTreeElement_onClick = new WeakMap(), _DuoyunTreeElement_getItemColor = new WeakMap(), _DuoyunTreeElement_renderItem = new WeakMap();
205__decorate([
206 property
207], DuoyunTreeElement.prototype, "data", void 0);
208__decorate([
209 property
210], DuoyunTreeElement.prototype, "highlights", void 0);
211__decorate([
212 emitter
213], DuoyunTreeElement.prototype, "itemclick", void 0);
214__decorate([
215 part
216], DuoyunTreeElement, "item", void 0);
217DuoyunTreeElement = DuoyunTreeElement_1 = __decorate([
218 customElement('dy-tree'),
219 adoptedStyle(style),
220 adoptedStyle(focusStyle)
221], DuoyunTreeElement);
222export { DuoyunTreeElement };
223//# sourceMappingURL=tree.js.map
\No newline at end of file