1 | var __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 | };
|
7 | var __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 | };
|
12 | var _DuoyunLegendElement_instances, _DuoyunLegendElement_currentValueSet_get, _DuoyunLegendElement_onWheel, _DuoyunLegendElement_onChange;
|
13 | import { adoptedStyle, customElement, emitter, property } from '@mantou/gem/lib/decorators';
|
14 | import { html } from '@mantou/gem/lib/element';
|
15 | import { createCSSSheet, css, styleMap, classMap } from '@mantou/gem/lib/utils';
|
16 | import { theme } from '../lib/theme';
|
17 | import { commonColors } from '../lib/color';
|
18 | import { DuoyunScrollBaseElement } from './base/scroll';
|
19 | import './tooltip';
|
20 | const style = createCSSSheet(css `
|
21 | :host(:where(:not([hidden]))) {
|
22 | --color: initial;
|
23 | display: flex;
|
24 | gap: 1em;
|
25 | align-items: center;
|
26 | font-size: 0.875em;
|
27 | }
|
28 | .item {
|
29 | cursor: default;
|
30 | display: flex;
|
31 | align-items: baseline;
|
32 | gap: 4px;
|
33 | color: var(--color);
|
34 | white-space: nowrap;
|
35 | }
|
36 | .item.unselect {
|
37 | --color: ${theme.disabledColor};
|
38 | }
|
39 | .item span {
|
40 | line-height: 1;
|
41 | width: 0.8em;
|
42 | height: 0.6em;
|
43 | border-radius: ${theme.smallRound};
|
44 | background-color: currentColor;
|
45 | }
|
46 | `);
|
47 |
|
48 |
|
49 |
|
50 | let DuoyunLegendElement = class DuoyunLegendElement extends DuoyunScrollBaseElement {
|
51 | constructor() {
|
52 | super();
|
53 | _DuoyunLegendElement_instances.add(this);
|
54 | this.colors = commonColors;
|
55 | this.legends = [];
|
56 | _DuoyunLegendElement_onWheel.set(this, (evt) => {
|
57 | this.scrollBy(Math.sign(evt.deltaY) * 30, 0);
|
58 | });
|
59 | _DuoyunLegendElement_onChange.set(this, (currentValue, value, exclude = false) => {
|
60 | const total = this.legends.length;
|
61 | if (total === 1)
|
62 | return;
|
63 | if (currentValue.size === total) {
|
64 | return this.change(exclude ? [...currentValue].filter((e) => e !== value) : [value]);
|
65 | }
|
66 | if (currentValue.has(value)) {
|
67 | if (currentValue.size === 1) {
|
68 | return this.change(this.legends.map(({ label, value }) => value !== null && value !== void 0 ? value : label));
|
69 | }
|
70 | else {
|
71 | return this.change([...currentValue].filter((e) => e !== value));
|
72 | }
|
73 | }
|
74 | return this.change([...currentValue, value]);
|
75 | });
|
76 | this.render = () => {
|
77 | var _a;
|
78 | const currentValue = __classPrivateFieldGet(this, _DuoyunLegendElement_instances, "a", _DuoyunLegendElement_currentValueSet_get);
|
79 | return html `
|
80 | ${(_a = this.legends) === null || _a === void 0 ? void 0 : _a.map(({ value, label, tooltip }, index) => html `
|
81 | <dy-tooltip .content=${tooltip}>
|
82 | <div
|
83 | class=${classMap({ item: true, unselect: !currentValue.has(value !== null && value !== void 0 ? value : label) })}
|
84 | @click=${(e) => __classPrivateFieldGet(this, _DuoyunLegendElement_onChange, "f").call(this, currentValue, value !== null && value !== void 0 ? value : label, e.metaKey || e.ctrlKey)}
|
85 | >
|
86 | <span style=${styleMap({ color: `var(--color, ${this.colors[index % this.colors.length]})` })}></span>
|
87 | ${label}
|
88 | </div>
|
89 | </dy-tooltip>
|
90 | `)}
|
91 | `;
|
92 | };
|
93 | this.addEventListener('wheel', __classPrivateFieldGet(this, _DuoyunLegendElement_onWheel, "f"));
|
94 | this.internals.ariaHidden = 'true';
|
95 | }
|
96 | };
|
97 | _DuoyunLegendElement_onWheel = new WeakMap(), _DuoyunLegendElement_onChange = new WeakMap(), _DuoyunLegendElement_instances = new WeakSet(), _DuoyunLegendElement_currentValueSet_get = function _DuoyunLegendElement_currentValueSet_get() {
|
98 | var _a, _b;
|
99 | return new Set((_a = this.value) !== null && _a !== void 0 ? _a : (_b = this.legends) === null || _b === void 0 ? void 0 : _b.map(({ value, label }) => value !== null && value !== void 0 ? value : label));
|
100 | };
|
101 | __decorate([
|
102 | property
|
103 | ], DuoyunLegendElement.prototype, "colors", void 0);
|
104 | __decorate([
|
105 | property
|
106 | ], DuoyunLegendElement.prototype, "legends", void 0);
|
107 | __decorate([
|
108 | property
|
109 | ], DuoyunLegendElement.prototype, "value", void 0);
|
110 | __decorate([
|
111 | emitter
|
112 | ], DuoyunLegendElement.prototype, "change", void 0);
|
113 | DuoyunLegendElement = __decorate([
|
114 | customElement('dy-legend'),
|
115 | adoptedStyle(style)
|
116 | ], DuoyunLegendElement);
|
117 | export { DuoyunLegendElement };
|
118 |
|
\ | No newline at end of file |