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 __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 | };
|
18 | var _DuoyunCarouselElement_instances, _DuoyunCarouselElement_interval_get, _DuoyunCarouselElement_add, _DuoyunCarouselElement_timer, _DuoyunCarouselElement_isFirstRender, _DuoyunCarouselElement_waitLeave, _DuoyunCarouselElement_reset, _DuoyunCarouselElement_oMouseEnter, _DuoyunCarouselElement_clearTimer, _DuoyunCarouselElement_pageVisibleChange;
|
19 | var DuoyunCarouselElement_1;
|
20 | import { GemElement, html } from '@mantou/gem/lib/element';
|
21 | import { adoptedStyle, customElement, property, numattribute, attribute, part, emitter, } from '@mantou/gem/lib/decorators';
|
22 | import { createCSSSheet, css, styleMap, classMap } from '@mantou/gem/lib/utils';
|
23 | import { theme } from '../lib/theme';
|
24 | import { icons } from '../lib/icons';
|
25 | import { commonHandle } from '../lib/hotkeys';
|
26 | import { focusStyle } from '../lib/styles';
|
27 | import './use';
|
28 | import './heading';
|
29 | import './paragraph';
|
30 | import './button';
|
31 | import './more';
|
32 | const style = createCSSSheet(css `
|
33 | :host(:where(:not([hidden]))) {
|
34 | display: block;
|
35 | width: 100%;
|
36 | aspect-ratio: 20 / 7;
|
37 | position: relative;
|
38 | color: ${theme.highlightColor};
|
39 | }
|
40 | .list,
|
41 | .item,
|
42 | .img {
|
43 | margin: 0;
|
44 | padding: 0;
|
45 | width: 100%;
|
46 | height: 100%;
|
47 | }
|
48 | .item {
|
49 | list-style: none;
|
50 | position: relative;
|
51 | overflow: hidden;
|
52 | }
|
53 | [inert] {
|
54 | display: none;
|
55 | }
|
56 | .img {
|
57 | position: absolute;
|
58 | object-fit: cover;
|
59 | --mask-range: 35%;
|
60 | --m: linear-gradient(to right top, transparent, black var(--mask-range));
|
61 | -webkit-mask-image: var(--m);
|
62 | mask-image: var(--m);
|
63 | }
|
64 | .content {
|
65 | position: absolute;
|
66 | display: flex;
|
67 | flex-direction: column;
|
68 | align-items: flex-start;
|
69 | justify-content: center;
|
70 | inset: 0;
|
71 | padding-inline: 4em;
|
72 | max-width: 35%;
|
73 | opacity: 0;
|
74 | animation-duration: 1.3s;
|
75 | }
|
76 | .img,
|
77 | .content {
|
78 | animation: fadeIn ${theme.timingEasingFunction} 0.667s both;
|
79 | }
|
80 | .paused :where(.img, .content) {
|
81 | animation-play-state: paused;
|
82 | animation-direction: reverse;
|
83 | }
|
84 | @keyframes fadeIn {
|
85 | 0% {
|
86 | transform: translateX(calc(var(--direction) * 3em));
|
87 | }
|
88 | 100% {
|
89 | transform: translateX(0);
|
90 | opacity: 1;
|
91 | }
|
92 | }
|
93 | .tag {
|
94 | font-style: italic;
|
95 | padding: 0.2em 0.3em;
|
96 | margin-block-end: 0.5em;
|
97 | background: yellow;
|
98 | color: black;
|
99 | }
|
100 | .heading {
|
101 | margin-block-start: 0;
|
102 | }
|
103 | .item,
|
104 | .heading,
|
105 | .paragraph {
|
106 | color: inherit;
|
107 | }
|
108 | .action {
|
109 | font-weight: bold;
|
110 | }
|
111 | .forward {
|
112 | width: 1.2em;
|
113 | }
|
114 | .nav {
|
115 | position: absolute;
|
116 | left: 0;
|
117 | right: 0;
|
118 | bottom: 0.5em;
|
119 | display: flex;
|
120 | justify-content: center;
|
121 | }
|
122 | .circle {
|
123 | width: 0.5em;
|
124 | padding: 0.2em;
|
125 | }
|
126 | .circle::before {
|
127 | transform: scale(0.99);
|
128 | content: '';
|
129 | color: ${theme.highlightColor};
|
130 | display: block;
|
131 | aspect-ratio: 1;
|
132 | border: 1px solid;
|
133 | border-radius: 10em;
|
134 | }
|
135 | .circle:where(:hover)::before {
|
136 | background: ${theme.describeColor};
|
137 | }
|
138 | .current::before {
|
139 | background: currentColor;
|
140 | }
|
141 | `);
|
142 |
|
143 |
|
144 |
|
145 | let DuoyunCarouselElement = DuoyunCarouselElement_1 = class DuoyunCarouselElement extends GemElement {
|
146 | constructor() {
|
147 | super({ delegatesFocus: true });
|
148 | _DuoyunCarouselElement_instances.add(this);
|
149 | this.state = {
|
150 | currentIndex: 0,
|
151 | direction: 1,
|
152 | };
|
153 | _DuoyunCarouselElement_add.set(this, (direction) => {
|
154 | var _a;
|
155 | const total = (_a = this.data) === null || _a === void 0 ? void 0 : _a.length;
|
156 | if (!total)
|
157 | return;
|
158 | this.setState({ currentIndex: (total + this.state.currentIndex + direction) % total, direction });
|
159 | __classPrivateFieldGet(this, _DuoyunCarouselElement_reset, "f").call(this);
|
160 | });
|
161 | _DuoyunCarouselElement_timer.set(this, 0);
|
162 | _DuoyunCarouselElement_isFirstRender.set(this, true);
|
163 | _DuoyunCarouselElement_waitLeave.set(this, Promise.resolve());
|
164 | _DuoyunCarouselElement_reset.set(this, () => {
|
165 | __classPrivateFieldGet(this, _DuoyunCarouselElement_clearTimer, "f").call(this);
|
166 | __classPrivateFieldSet(this, _DuoyunCarouselElement_timer, window.setTimeout(async () => {
|
167 | await __classPrivateFieldGet(this, _DuoyunCarouselElement_waitLeave, "f");
|
168 | __classPrivateFieldSet(this, _DuoyunCarouselElement_isFirstRender, false, "f");
|
169 | __classPrivateFieldGet(this, _DuoyunCarouselElement_add, "f").call(this, 1);
|
170 | }, __classPrivateFieldGet(this, _DuoyunCarouselElement_instances, "a", _DuoyunCarouselElement_interval_get)), "f");
|
171 | });
|
172 | _DuoyunCarouselElement_oMouseEnter.set(this, (evt) => {
|
173 | __classPrivateFieldSet(this, _DuoyunCarouselElement_waitLeave, new Promise((res) => {
|
174 | var _a;
|
175 | return (_a = evt.target) === null || _a === void 0 ? void 0 : _a.addEventListener('mouseleave', () => res(), {
|
176 | once: true,
|
177 | });
|
178 | }), "f");
|
179 | });
|
180 | _DuoyunCarouselElement_clearTimer.set(this, () => clearTimeout(__classPrivateFieldGet(this, _DuoyunCarouselElement_timer, "f")));
|
181 | _DuoyunCarouselElement_pageVisibleChange.set(this, () => {
|
182 | if (document.visibilityState === 'visible') {
|
183 | __classPrivateFieldGet(this, _DuoyunCarouselElement_reset, "f").call(this);
|
184 | }
|
185 | else {
|
186 | __classPrivateFieldGet(this, _DuoyunCarouselElement_clearTimer, "f").call(this);
|
187 | }
|
188 | });
|
189 | this.mounted = () => {
|
190 | __classPrivateFieldGet(this, _DuoyunCarouselElement_reset, "f").call(this);
|
191 | this.effect(() => this.change(this.state.currentIndex), () => [this.state.currentIndex]);
|
192 | document.addEventListener('visibilitychange', __classPrivateFieldGet(this, _DuoyunCarouselElement_pageVisibleChange, "f"));
|
193 | return () => {
|
194 | document.removeEventListener('visibilitychange', __classPrivateFieldGet(this, _DuoyunCarouselElement_pageVisibleChange, "f"));
|
195 | __classPrivateFieldGet(this, _DuoyunCarouselElement_clearTimer, "f").call(this);
|
196 | };
|
197 | };
|
198 | this.render = () => {
|
199 | var _a, _b;
|
200 | const { currentIndex, direction } = this.state;
|
201 | return html `
|
202 | <ul class="list" role="region">
|
203 | ${(_a = this.data) === null || _a === void 0 ? void 0 : _a.map(({ img, background = 'none', title, description, action, tag, onClick }, index) => html `
|
204 | ${currentIndex === index
|
205 | ? html `
|
206 | <style>
|
207 | :host {
|
208 | background: ${background};
|
209 | }
|
210 | </style>
|
211 | `
|
212 | : ''}
|
213 | <li
|
214 | class=${classMap({ item: true, paused: __classPrivateFieldGet(this, _DuoyunCarouselElement_isFirstRender, "f") })}
|
215 | style=${styleMap({ '--direction': `${direction}` })}
|
216 | ?inert=${currentIndex !== index}
|
217 | @click=${onClick}
|
218 | >
|
219 | <img
|
220 | part=${DuoyunCarouselElement_1.img}
|
221 | class="img"
|
222 | alt=${title || ''}
|
223 | src=${img}
|
224 | crossorigin=${this.crossorigin}
|
225 | />
|
226 | <div class="content" part=${DuoyunCarouselElement_1.content} @mouseenter=${__classPrivateFieldGet(this, _DuoyunCarouselElement_oMouseEnter, "f")}>
|
227 | ${tag ? html `<div part=${DuoyunCarouselElement_1.tag} class="tag">${tag}</div>` : ''}
|
228 | <dy-heading part=${DuoyunCarouselElement_1.title} class="heading" lv="2">${title}</dy-heading>
|
229 | <dy-more expandless>
|
230 | <dy-paragraph part=${DuoyunCarouselElement_1.description} class="paragraph">
|
231 | ${description}
|
232 | </dy-paragraph>
|
233 | </dy-more>
|
234 | ${action
|
235 | ? html `
|
236 | <dy-button
|
237 | class="action"
|
238 | part=${DuoyunCarouselElement_1.button}
|
239 | @click=${(evt) => {
|
240 | if (action.handle) {
|
241 | evt.stopPropagation();
|
242 | action.handle();
|
243 | }
|
244 | }}
|
245 | >
|
246 | ${action.text}
|
247 | <dy-use class="forward" .element=${icons.forward}></dy-use>
|
248 | </dy-button>
|
249 | `
|
250 | : ''}
|
251 | </div>
|
252 | </li>
|
253 | `)}
|
254 | </ul>
|
255 | <div part=${DuoyunCarouselElement_1.nav} class="nav">
|
256 | ${(_b = this.data) === null || _b === void 0 ? void 0 : _b.map((_, index) => html `
|
257 | <div
|
258 | tabindex="0"
|
259 | role="button"
|
260 | @keydown=${commonHandle}
|
261 | @click=${() => this.jump(index)}
|
262 | class=${classMap({ circle: true, current: index === currentIndex })}
|
263 | ></div>
|
264 | `)}
|
265 | </div>
|
266 | `;
|
267 | };
|
268 | this.next = () => {
|
269 | __classPrivateFieldGet(this, _DuoyunCarouselElement_add, "f").call(this, 1);
|
270 | };
|
271 | this.prev = () => {
|
272 | __classPrivateFieldGet(this, _DuoyunCarouselElement_add, "f").call(this, -1);
|
273 | };
|
274 | this.jump = (index) => {
|
275 | this.setState({ currentIndex: index, direction: 1 });
|
276 | __classPrivateFieldGet(this, _DuoyunCarouselElement_reset, "f").call(this);
|
277 | };
|
278 | }
|
279 | };
|
280 | _DuoyunCarouselElement_add = new WeakMap(), _DuoyunCarouselElement_timer = new WeakMap(), _DuoyunCarouselElement_isFirstRender = new WeakMap(), _DuoyunCarouselElement_waitLeave = new WeakMap(), _DuoyunCarouselElement_reset = new WeakMap(), _DuoyunCarouselElement_oMouseEnter = new WeakMap(), _DuoyunCarouselElement_clearTimer = new WeakMap(), _DuoyunCarouselElement_pageVisibleChange = new WeakMap(), _DuoyunCarouselElement_instances = new WeakSet(), _DuoyunCarouselElement_interval_get = function _DuoyunCarouselElement_interval_get() {
|
281 | return this.interval || 3000;
|
282 | };
|
283 | __decorate([
|
284 | attribute
|
285 | ], DuoyunCarouselElement.prototype, "crossorigin", void 0);
|
286 | __decorate([
|
287 | numattribute
|
288 | ], DuoyunCarouselElement.prototype, "interval", void 0);
|
289 | __decorate([
|
290 | emitter
|
291 | ], DuoyunCarouselElement.prototype, "change", void 0);
|
292 | __decorate([
|
293 | property
|
294 | ], DuoyunCarouselElement.prototype, "data", void 0);
|
295 | __decorate([
|
296 | part
|
297 | ], DuoyunCarouselElement, "img", void 0);
|
298 | __decorate([
|
299 | part
|
300 | ], DuoyunCarouselElement, "title", void 0);
|
301 | __decorate([
|
302 | part
|
303 | ], DuoyunCarouselElement, "tag", void 0);
|
304 | __decorate([
|
305 | part
|
306 | ], DuoyunCarouselElement, "content", void 0);
|
307 | __decorate([
|
308 | part
|
309 | ], DuoyunCarouselElement, "button", void 0);
|
310 | __decorate([
|
311 | part
|
312 | ], DuoyunCarouselElement, "description", void 0);
|
313 | __decorate([
|
314 | part
|
315 | ], DuoyunCarouselElement, "nav", void 0);
|
316 | DuoyunCarouselElement = DuoyunCarouselElement_1 = __decorate([
|
317 | customElement('dy-carousel'),
|
318 | adoptedStyle(style),
|
319 | adoptedStyle(focusStyle)
|
320 | ], DuoyunCarouselElement);
|
321 | export { DuoyunCarouselElement };
|
322 |
|
\ | No newline at end of file |