UNPKG

4.96 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 _DuoyunStatisticElement_instances, _DuoyunStatisticElement_neutral_get, _DuoyunStatisticElement_type_get;
13import { adoptedStyle, customElement, attribute, boolattribute, numattribute } from '@mantou/gem/lib/decorators';
14import { GemElement, html } from '@mantou/gem/lib/element';
15import { createCSSSheet, css, classMap } from '@mantou/gem/lib/utils';
16import { parseDuration } from '../lib/time';
17import { theme } from '../lib/theme';
18import { formatBandwidth, formatDecimal, formatPercentage, formatTraffic, formatCurrency } from '../lib/number';
19import './placeholder';
20const style = createCSSSheet(css `
21 :host(:where(:not([hidden]))) {
22 display: block;
23 font-size: 0.875em;
24 line-height: 1.2;
25 }
26 .title {
27 margin-block-end: 1em;
28 }
29 .values {
30 display: flex;
31 justify-content: space-between;
32 align-items: baseline;
33 font-weight: bold;
34 }
35 .value {
36 display: flex;
37 align-items: baseline;
38 gap: 0.3em;
39 }
40 .number {
41 font-size: 2em;
42 }
43 .positive {
44 color: ${theme.positiveColor};
45 }
46 .negative {
47 color: ${theme.negativeColor};
48 }
49`);
50export const formatFnMap = {
51 bandwidth: formatBandwidth,
52 traffic: formatTraffic,
53 decimal: formatDecimal,
54 percentage: formatPercentage,
55 currency: formatCurrency,
56 duration: parseDuration,
57};
58/**
59 * @customElement dy-statistic
60 * @attr loading
61 */
62let DuoyunStatisticElement = class DuoyunStatisticElement extends GemElement {
63 constructor() {
64 super();
65 _DuoyunStatisticElement_instances.add(this);
66 this.render = () => {
67 const { number, unit } = formatFnMap[__classPrivateFieldGet(this, _DuoyunStatisticElement_instances, "a", _DuoyunStatisticElement_type_get)](this.value);
68 const diffValue = this.prevValue !== 0 && this.value === 0
69 ? Infinity
70 : Number((((this.value - this.prevValue) / this.prevValue) * 100).toFixed(2));
71 const [positive, negative] = __classPrivateFieldGet(this, _DuoyunStatisticElement_instances, "a", _DuoyunStatisticElement_neutral_get) === 'positive' ? [diffValue > 0, diffValue < 0] : [diffValue < 0, diffValue > 0];
72 return html `
73 <div class="title">${this.loading ? html `<dy-placeholder width="5em"></dy-placeholder>` : this.text}</div>
74 <div class="values">
75 <div class="value">
76 <span class="number">${this.loading ? html `<dy-placeholder width="6em"></dy-placeholder>` : number}</span>
77 <span class="unit">${this.loading ? '' : unit}</span>
78 </div>
79 ${this.loading || !this.hasAttribute('prev-value')
80 ? ''
81 : html `<span class=${classMap({ positive, negative })}>
82 ${isNaN(diffValue) ? '-' : diffValue === Infinity ? '∞' : diffValue}%
83 </span>`}
84 </div>
85 `;
86 };
87 this.internals.role = 'group';
88 }
89};
90_DuoyunStatisticElement_instances = new WeakSet(), _DuoyunStatisticElement_neutral_get = function _DuoyunStatisticElement_neutral_get() {
91 return this.neutral || 'positive';
92}, _DuoyunStatisticElement_type_get = function _DuoyunStatisticElement_type_get() {
93 return this.type || 'decimal';
94};
95__decorate([
96 attribute
97], DuoyunStatisticElement.prototype, "neutral", void 0);
98__decorate([
99 attribute
100], DuoyunStatisticElement.prototype, "type", void 0);
101__decorate([
102 attribute
103], DuoyunStatisticElement.prototype, "text", void 0);
104__decorate([
105 boolattribute
106], DuoyunStatisticElement.prototype, "loading", void 0);
107__decorate([
108 numattribute
109], DuoyunStatisticElement.prototype, "value", void 0);
110__decorate([
111 numattribute
112], DuoyunStatisticElement.prototype, "prevValue", void 0);
113DuoyunStatisticElement = __decorate([
114 customElement('dy-statistic'),
115 adoptedStyle(style)
116], DuoyunStatisticElement);
117export { DuoyunStatisticElement };
118//# sourceMappingURL=statistic.js.map
\No newline at end of file