UNPKG

5.4 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 _DuoyunLegendElement_instances, _DuoyunLegendElement_currentValueSet_get, _DuoyunLegendElement_onWheel, _DuoyunLegendElement_onChange;
13import { adoptedStyle, customElement, emitter, property } from '@mantou/gem/lib/decorators';
14import { html } from '@mantou/gem/lib/element';
15import { createCSSSheet, css, styleMap, classMap } from '@mantou/gem/lib/utils';
16import { theme } from '../lib/theme';
17import { commonColors } from '../lib/color';
18import { DuoyunScrollBaseElement } from './base/scroll';
19import './tooltip';
20const 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 * @customElement dy-legend
49 */
50let 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);
113DuoyunLegendElement = __decorate([
114 customElement('dy-legend'),
115 adoptedStyle(style)
116], DuoyunLegendElement);
117export { DuoyunLegendElement };
118//# sourceMappingURL=legend.js.map
\No newline at end of file