123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392 |
- /*!
- * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
- * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
- * v1.0.0-beta.97
- */
- import { h } from "@stencil/core";
- import { CSS, ICONS, TEXT, HEADING_LEVEL } from "./resources";
- import { getElementDir, toAriaBoolean } from "../../utils/dom";
- import { Heading } from "../functional/Heading";
- import { createObserver } from "../../utils/observers";
- /**
- * @slot - A slot for adding `calcite-tip`s.
- */
- export class TipManager {
- constructor() {
- // --------------------------------------------------------------------------
- //
- // Properties
- //
- // --------------------------------------------------------------------------
- /**
- * When `true`, does not display or position the component.
- */
- this.closed = false;
- this.mutationObserver = createObserver("mutation", () => this.setUpTips());
- this.hideTipManager = () => {
- this.closed = true;
- this.calciteTipManagerToggle.emit();
- this.calciteTipManagerClose.emit();
- };
- this.previousClicked = () => {
- this.previousTip();
- };
- this.nextClicked = () => {
- this.nextTip();
- };
- this.tipManagerKeyDownHandler = (event) => {
- if (event.target !== this.container) {
- return;
- }
- switch (event.key) {
- case "ArrowRight":
- event.preventDefault();
- this.nextTip();
- break;
- case "ArrowLeft":
- event.preventDefault();
- this.previousTip();
- break;
- case "Home":
- event.preventDefault();
- this.selectedIndex = 0;
- break;
- case "End":
- event.preventDefault();
- this.selectedIndex = this.total - 1;
- break;
- }
- };
- this.storeContainerRef = (el) => {
- this.container = el;
- };
- }
- closedChangeHandler() {
- this.direction = null;
- this.calciteTipManagerToggle.emit();
- }
- selectedChangeHandler() {
- this.showSelectedTip();
- this.updateGroupTitle();
- }
- // --------------------------------------------------------------------------
- //
- // Lifecycle
- //
- // --------------------------------------------------------------------------
- connectedCallback() {
- var _a;
- this.setUpTips();
- (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el, { childList: true, subtree: true });
- }
- disconnectedCallback() {
- var _a;
- (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
- }
- // --------------------------------------------------------------------------
- //
- // Public Methods
- //
- // --------------------------------------------------------------------------
- /** Selects the next `calcite-tip` to display. */
- async nextTip() {
- this.direction = "advancing";
- const nextIndex = this.selectedIndex + 1;
- this.selectedIndex = (nextIndex + this.total) % this.total;
- }
- /** Selects the previous `calcite-tip` to display. */
- async previousTip() {
- this.direction = "retreating";
- const previousIndex = this.selectedIndex - 1;
- this.selectedIndex = (previousIndex + this.total) % this.total;
- }
- // --------------------------------------------------------------------------
- //
- // Private Methods
- //
- // --------------------------------------------------------------------------
- setUpTips() {
- const tips = Array.from(this.el.querySelectorAll("calcite-tip"));
- this.total = tips.length;
- if (this.total === 0) {
- return;
- }
- const selectedTip = this.el.querySelector("calcite-tip[selected]");
- this.tips = tips;
- this.selectedIndex = selectedTip ? tips.indexOf(selectedTip) : 0;
- tips.forEach((tip) => {
- tip.nonDismissible = true;
- });
- this.showSelectedTip();
- this.updateGroupTitle();
- }
- showSelectedTip() {
- this.tips.forEach((tip, index) => {
- const isSelected = this.selectedIndex === index;
- tip.selected = isSelected;
- tip.hidden = !isSelected;
- });
- }
- updateGroupTitle() {
- const selectedTip = this.tips[this.selectedIndex];
- const tipParent = selectedTip.closest("calcite-tip-group");
- this.groupTitle = (tipParent === null || tipParent === void 0 ? void 0 : tipParent.groupTitle) || this.intlDefaultTitle || TEXT.defaultGroupTitle;
- }
- // --------------------------------------------------------------------------
- //
- // Render Methods
- //
- // --------------------------------------------------------------------------
- renderPagination() {
- const dir = getElementDir(this.el);
- const { selectedIndex, tips, total, intlNext, intlPrevious, intlPaginationLabel } = this;
- const nextLabel = intlNext || TEXT.next;
- const previousLabel = intlPrevious || TEXT.previous;
- const paginationLabel = intlPaginationLabel || TEXT.defaultPaginationLabel;
- return tips.length > 1 ? (h("footer", { class: CSS.pagination }, h("calcite-action", { class: CSS.pagePrevious, icon: dir === "ltr" ? ICONS.chevronLeft : ICONS.chevronRight, onClick: this.previousClicked, scale: "m", text: previousLabel }), h("span", { class: CSS.pagePosition }, `${paginationLabel} ${selectedIndex + 1}/${total}`), h("calcite-action", { class: CSS.pageNext, icon: dir === "ltr" ? ICONS.chevronRight : ICONS.chevronLeft, onClick: this.nextClicked, scale: "m", text: nextLabel }))) : null;
- }
- render() {
- const { closed, direction, headingLevel, groupTitle, selectedIndex, intlClose, total } = this;
- const closeLabel = intlClose || TEXT.close;
- if (total === 0) {
- return null;
- }
- return (h("section", { "aria-hidden": toAriaBoolean(closed), class: CSS.container, hidden: closed, onKeyDown: this.tipManagerKeyDownHandler, ref: this.storeContainerRef, tabIndex: 0 }, h("header", { class: CSS.header }, h(Heading, { class: CSS.heading, level: headingLevel || HEADING_LEVEL }, groupTitle), h("calcite-action", { class: CSS.close, onClick: this.hideTipManager, scale: "m", text: closeLabel }, h("calcite-icon", { icon: ICONS.close, scale: "m" }))), h("div", { class: {
- [CSS.tipContainer]: true,
- [CSS.tipContainerAdvancing]: !closed && direction === "advancing",
- [CSS.tipContainerRetreating]: !closed && direction === "retreating"
- }, key: selectedIndex, tabIndex: 0 }, h("slot", null)), this.renderPagination()));
- }
- static get is() { return "calcite-tip-manager"; }
- static get encapsulation() { return "shadow"; }
- static get originalStyleUrls() {
- return {
- "$": ["tip-manager.scss"]
- };
- }
- static get styleUrls() {
- return {
- "$": ["tip-manager.css"]
- };
- }
- static get properties() {
- return {
- "closed": {
- "type": "boolean",
- "mutable": true,
- "complexType": {
- "original": "boolean",
- "resolved": "boolean",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "When `true`, does not display or position the component."
- },
- "attribute": "closed",
- "reflect": true,
- "defaultValue": "false"
- },
- "headingLevel": {
- "type": "number",
- "mutable": false,
- "complexType": {
- "original": "HeadingLevel",
- "resolved": "1 | 2 | 3 | 4 | 5 | 6",
- "references": {
- "HeadingLevel": {
- "location": "import",
- "path": "../functional/Heading"
- }
- }
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "Specifies the number at which section headings should start."
- },
- "attribute": "heading-level",
- "reflect": true
- },
- "intlClose": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "string",
- "resolved": "string",
- "references": {}
- },
- "required": false,
- "optional": true,
- "docs": {
- "tags": [],
- "text": "Accessible name for the component's close button."
- },
- "attribute": "intl-close",
- "reflect": false
- },
- "intlDefaultTitle": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "string",
- "resolved": "string",
- "references": {}
- },
- "required": false,
- "optional": true,
- "docs": {
- "tags": [],
- "text": "Accessible name for the `calcite-tip-group` title."
- },
- "attribute": "intl-default-title",
- "reflect": false
- },
- "intlNext": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "string",
- "resolved": "string",
- "references": {}
- },
- "required": false,
- "optional": true,
- "docs": {
- "tags": [],
- "text": "Accessible name for navigating to the next tip."
- },
- "attribute": "intl-next",
- "reflect": false
- },
- "intlPaginationLabel": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "string",
- "resolved": "string",
- "references": {}
- },
- "required": false,
- "optional": true,
- "docs": {
- "tags": [],
- "text": "Text that accompanies the component's pagination."
- },
- "attribute": "intl-pagination-label",
- "reflect": false
- },
- "intlPrevious": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "string",
- "resolved": "string",
- "references": {}
- },
- "required": false,
- "optional": true,
- "docs": {
- "tags": [],
- "text": "Accessible name for navigating to the previous tip."
- },
- "attribute": "intl-previous",
- "reflect": false
- }
- };
- }
- static get states() {
- return {
- "selectedIndex": {},
- "tips": {},
- "total": {},
- "direction": {},
- "groupTitle": {}
- };
- }
- static get events() {
- return [{
- "method": "calciteTipManagerToggle",
- "name": "calciteTipManagerToggle",
- "bubbles": true,
- "cancelable": false,
- "composed": true,
- "docs": {
- "tags": [{
- "name": "deprecated",
- "text": "use `calciteTipManagerClose` instead."
- }],
- "text": "Emits when the component has been open or closed."
- },
- "complexType": {
- "original": "void",
- "resolved": "void",
- "references": {}
- }
- }, {
- "method": "calciteTipManagerClose",
- "name": "calciteTipManagerClose",
- "bubbles": true,
- "cancelable": false,
- "composed": true,
- "docs": {
- "tags": [],
- "text": "Emits when the component has been closed."
- },
- "complexType": {
- "original": "void",
- "resolved": "void",
- "references": {}
- }
- }];
- }
- static get methods() {
- return {
- "nextTip": {
- "complexType": {
- "signature": "() => Promise<void>",
- "parameters": [],
- "references": {
- "Promise": {
- "location": "global"
- }
- },
- "return": "Promise<void>"
- },
- "docs": {
- "text": "Selects the next `calcite-tip` to display.",
- "tags": []
- }
- },
- "previousTip": {
- "complexType": {
- "signature": "() => Promise<void>",
- "parameters": [],
- "references": {
- "Promise": {
- "location": "global"
- }
- },
- "return": "Promise<void>"
- },
- "docs": {
- "text": "Selects the previous `calcite-tip` to display.",
- "tags": []
- }
- }
- };
- }
- static get elementRef() { return "el"; }
- static get watchers() {
- return [{
- "propName": "closed",
- "methodName": "closedChangeHandler"
- }, {
- "propName": "selectedIndex",
- "methodName": "selectedChangeHandler"
- }];
- }
- }
|