@exadel/esl
Version:
Exadel Smart Library (ESL) is the lightweight custom elements library that provide a set of super-flexible components
102 lines (101 loc) • 4.39 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
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;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { ExportNs } from '../../../esl-utils/environment/export-ns';
import { attr, listen, memoize, ready } from '../../../esl-utils/decorators';
import { ESLMixinElement } from '../../../esl-mixin-element/core';
import { ESLCarouselChangeEvent, ESLCarouselMoveEvent, ESLCarouselSlideEvent } from '../../core/esl-carousel.events';
/**
* ESLCarousel navigation helper to define triggers for carousel navigation.
*
* State attributes applied to the host element:
* - `active` : the related carousel is present and initialized
* - `disabled` : navigation command cannot be executed at the moment (see canNavigate)
* - `current` : navigation command points to the currently active slide/group
* (mirrors {@link ESLCarousel.isCurrent} / utils isCurrent semantics:
* only absolute targets are considered, relative forms like `next`, `prev`, `+1`, etc. never set it)
*
* Example:
* ```
* <div class="esl-carousel-nav-container">
* <button esl-carousel-nav="group:prev">Prev</button>
* <esl-carousel>...</esl-carousel>
* <button esl-carousel-nav="group:next">Next</button>
* </div>
* ```
*/
let ESLCarouselNavMixin = class ESLCarouselNavMixin extends ESLMixinElement {
/** @returns ESLCarousel instance; based on {@link carousel} attribute */
get $carousel() {
return this.$$find(this.carousel);
}
/** @returns accessible target ID */
get targetID() {
var _a;
return ((_a = this.$carousel) === null || _a === void 0 ? void 0 : _a.id) || '';
}
connectedCallback() {
super.connectedCallback();
if (!this.$carousel)
return;
if (this.$carousel.renderer)
this._onUpdate();
}
disconnectedCallback() {
super.disconnectedCallback();
memoize.clear(this, '$carousel');
this.$$attr('active', false);
this.$$attr('disabled', false);
this.$$attr('current', false);
}
/** Handles carousel state changes */
_onUpdate() {
var _a, _b;
const isActive = !!((_a = this.$carousel) === null || _a === void 0 ? void 0 : _a.renderer) && !this.$carousel.incomplete;
const isCurrent = isActive && ((_b = this.$carousel) === null || _b === void 0 ? void 0 : _b.isCurrent(this.command));
const isDisabled = isActive && !this.$carousel.canNavigate(this.command);
this.$$attr('active', isActive);
this.$$attr('disabled', isDisabled);
this.$$attr('current', isCurrent);
this.$$attr('aria-controls', this.targetID);
}
/** Handles $host element click */
_onClick(e) {
if (!this.$carousel || typeof this.$carousel.goTo !== 'function')
return;
this.$carousel.goTo(this.command, { activator: this.$host }).catch(console.debug);
e.preventDefault();
}
};
ESLCarouselNavMixin.is = 'esl-carousel-nav';
__decorate([
attr({ name: ESLCarouselNavMixin.is })
], ESLCarouselNavMixin.prototype, "command", void 0);
__decorate([
attr({
name: ESLCarouselNavMixin.is + '-target',
defaultValue: '::parent(.esl-carousel-nav-container)::find(esl-carousel)'
})
], ESLCarouselNavMixin.prototype, "carousel", void 0);
__decorate([
memoize()
], ESLCarouselNavMixin.prototype, "$carousel", null);
__decorate([
ready
], ESLCarouselNavMixin.prototype, "connectedCallback", null);
__decorate([
listen({
event: `${ESLCarouselChangeEvent.TYPE} ${ESLCarouselMoveEvent.TYPE} ${ESLCarouselSlideEvent.AFTER}`,
target: ($nav) => $nav.$carousel
})
], ESLCarouselNavMixin.prototype, "_onUpdate", null);
__decorate([
listen('click')
], ESLCarouselNavMixin.prototype, "_onClick", null);
ESLCarouselNavMixin = __decorate([
ExportNs('Carousel.Nav')
], ESLCarouselNavMixin);
export { ESLCarouselNavMixin };