tip-manager.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.97
  5. */
  6. import { h } from "@stencil/core";
  7. import { CSS, ICONS, TEXT, HEADING_LEVEL } from "./resources";
  8. import { getElementDir, toAriaBoolean } from "../../utils/dom";
  9. import { Heading } from "../functional/Heading";
  10. import { createObserver } from "../../utils/observers";
  11. /**
  12. * @slot - A slot for adding `calcite-tip`s.
  13. */
  14. export class TipManager {
  15. constructor() {
  16. // --------------------------------------------------------------------------
  17. //
  18. // Properties
  19. //
  20. // --------------------------------------------------------------------------
  21. /**
  22. * When `true`, does not display or position the component.
  23. */
  24. this.closed = false;
  25. this.mutationObserver = createObserver("mutation", () => this.setUpTips());
  26. this.hideTipManager = () => {
  27. this.closed = true;
  28. this.calciteTipManagerToggle.emit();
  29. this.calciteTipManagerClose.emit();
  30. };
  31. this.previousClicked = () => {
  32. this.previousTip();
  33. };
  34. this.nextClicked = () => {
  35. this.nextTip();
  36. };
  37. this.tipManagerKeyDownHandler = (event) => {
  38. if (event.target !== this.container) {
  39. return;
  40. }
  41. switch (event.key) {
  42. case "ArrowRight":
  43. event.preventDefault();
  44. this.nextTip();
  45. break;
  46. case "ArrowLeft":
  47. event.preventDefault();
  48. this.previousTip();
  49. break;
  50. case "Home":
  51. event.preventDefault();
  52. this.selectedIndex = 0;
  53. break;
  54. case "End":
  55. event.preventDefault();
  56. this.selectedIndex = this.total - 1;
  57. break;
  58. }
  59. };
  60. this.storeContainerRef = (el) => {
  61. this.container = el;
  62. };
  63. }
  64. closedChangeHandler() {
  65. this.direction = null;
  66. this.calciteTipManagerToggle.emit();
  67. }
  68. selectedChangeHandler() {
  69. this.showSelectedTip();
  70. this.updateGroupTitle();
  71. }
  72. // --------------------------------------------------------------------------
  73. //
  74. // Lifecycle
  75. //
  76. // --------------------------------------------------------------------------
  77. connectedCallback() {
  78. var _a;
  79. this.setUpTips();
  80. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el, { childList: true, subtree: true });
  81. }
  82. disconnectedCallback() {
  83. var _a;
  84. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
  85. }
  86. // --------------------------------------------------------------------------
  87. //
  88. // Public Methods
  89. //
  90. // --------------------------------------------------------------------------
  91. /** Selects the next `calcite-tip` to display. */
  92. async nextTip() {
  93. this.direction = "advancing";
  94. const nextIndex = this.selectedIndex + 1;
  95. this.selectedIndex = (nextIndex + this.total) % this.total;
  96. }
  97. /** Selects the previous `calcite-tip` to display. */
  98. async previousTip() {
  99. this.direction = "retreating";
  100. const previousIndex = this.selectedIndex - 1;
  101. this.selectedIndex = (previousIndex + this.total) % this.total;
  102. }
  103. // --------------------------------------------------------------------------
  104. //
  105. // Private Methods
  106. //
  107. // --------------------------------------------------------------------------
  108. setUpTips() {
  109. const tips = Array.from(this.el.querySelectorAll("calcite-tip"));
  110. this.total = tips.length;
  111. if (this.total === 0) {
  112. return;
  113. }
  114. const selectedTip = this.el.querySelector("calcite-tip[selected]");
  115. this.tips = tips;
  116. this.selectedIndex = selectedTip ? tips.indexOf(selectedTip) : 0;
  117. tips.forEach((tip) => {
  118. tip.nonDismissible = true;
  119. });
  120. this.showSelectedTip();
  121. this.updateGroupTitle();
  122. }
  123. showSelectedTip() {
  124. this.tips.forEach((tip, index) => {
  125. const isSelected = this.selectedIndex === index;
  126. tip.selected = isSelected;
  127. tip.hidden = !isSelected;
  128. });
  129. }
  130. updateGroupTitle() {
  131. const selectedTip = this.tips[this.selectedIndex];
  132. const tipParent = selectedTip.closest("calcite-tip-group");
  133. this.groupTitle = (tipParent === null || tipParent === void 0 ? void 0 : tipParent.groupTitle) || this.intlDefaultTitle || TEXT.defaultGroupTitle;
  134. }
  135. // --------------------------------------------------------------------------
  136. //
  137. // Render Methods
  138. //
  139. // --------------------------------------------------------------------------
  140. renderPagination() {
  141. const dir = getElementDir(this.el);
  142. const { selectedIndex, tips, total, intlNext, intlPrevious, intlPaginationLabel } = this;
  143. const nextLabel = intlNext || TEXT.next;
  144. const previousLabel = intlPrevious || TEXT.previous;
  145. const paginationLabel = intlPaginationLabel || TEXT.defaultPaginationLabel;
  146. 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;
  147. }
  148. render() {
  149. const { closed, direction, headingLevel, groupTitle, selectedIndex, intlClose, total } = this;
  150. const closeLabel = intlClose || TEXT.close;
  151. if (total === 0) {
  152. return null;
  153. }
  154. 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: {
  155. [CSS.tipContainer]: true,
  156. [CSS.tipContainerAdvancing]: !closed && direction === "advancing",
  157. [CSS.tipContainerRetreating]: !closed && direction === "retreating"
  158. }, key: selectedIndex, tabIndex: 0 }, h("slot", null)), this.renderPagination()));
  159. }
  160. static get is() { return "calcite-tip-manager"; }
  161. static get encapsulation() { return "shadow"; }
  162. static get originalStyleUrls() {
  163. return {
  164. "$": ["tip-manager.scss"]
  165. };
  166. }
  167. static get styleUrls() {
  168. return {
  169. "$": ["tip-manager.css"]
  170. };
  171. }
  172. static get properties() {
  173. return {
  174. "closed": {
  175. "type": "boolean",
  176. "mutable": true,
  177. "complexType": {
  178. "original": "boolean",
  179. "resolved": "boolean",
  180. "references": {}
  181. },
  182. "required": false,
  183. "optional": false,
  184. "docs": {
  185. "tags": [],
  186. "text": "When `true`, does not display or position the component."
  187. },
  188. "attribute": "closed",
  189. "reflect": true,
  190. "defaultValue": "false"
  191. },
  192. "headingLevel": {
  193. "type": "number",
  194. "mutable": false,
  195. "complexType": {
  196. "original": "HeadingLevel",
  197. "resolved": "1 | 2 | 3 | 4 | 5 | 6",
  198. "references": {
  199. "HeadingLevel": {
  200. "location": "import",
  201. "path": "../functional/Heading"
  202. }
  203. }
  204. },
  205. "required": false,
  206. "optional": false,
  207. "docs": {
  208. "tags": [],
  209. "text": "Specifies the number at which section headings should start."
  210. },
  211. "attribute": "heading-level",
  212. "reflect": true
  213. },
  214. "intlClose": {
  215. "type": "string",
  216. "mutable": false,
  217. "complexType": {
  218. "original": "string",
  219. "resolved": "string",
  220. "references": {}
  221. },
  222. "required": false,
  223. "optional": true,
  224. "docs": {
  225. "tags": [],
  226. "text": "Accessible name for the component's close button."
  227. },
  228. "attribute": "intl-close",
  229. "reflect": false
  230. },
  231. "intlDefaultTitle": {
  232. "type": "string",
  233. "mutable": false,
  234. "complexType": {
  235. "original": "string",
  236. "resolved": "string",
  237. "references": {}
  238. },
  239. "required": false,
  240. "optional": true,
  241. "docs": {
  242. "tags": [],
  243. "text": "Accessible name for the `calcite-tip-group` title."
  244. },
  245. "attribute": "intl-default-title",
  246. "reflect": false
  247. },
  248. "intlNext": {
  249. "type": "string",
  250. "mutable": false,
  251. "complexType": {
  252. "original": "string",
  253. "resolved": "string",
  254. "references": {}
  255. },
  256. "required": false,
  257. "optional": true,
  258. "docs": {
  259. "tags": [],
  260. "text": "Accessible name for navigating to the next tip."
  261. },
  262. "attribute": "intl-next",
  263. "reflect": false
  264. },
  265. "intlPaginationLabel": {
  266. "type": "string",
  267. "mutable": false,
  268. "complexType": {
  269. "original": "string",
  270. "resolved": "string",
  271. "references": {}
  272. },
  273. "required": false,
  274. "optional": true,
  275. "docs": {
  276. "tags": [],
  277. "text": "Text that accompanies the component's pagination."
  278. },
  279. "attribute": "intl-pagination-label",
  280. "reflect": false
  281. },
  282. "intlPrevious": {
  283. "type": "string",
  284. "mutable": false,
  285. "complexType": {
  286. "original": "string",
  287. "resolved": "string",
  288. "references": {}
  289. },
  290. "required": false,
  291. "optional": true,
  292. "docs": {
  293. "tags": [],
  294. "text": "Accessible name for navigating to the previous tip."
  295. },
  296. "attribute": "intl-previous",
  297. "reflect": false
  298. }
  299. };
  300. }
  301. static get states() {
  302. return {
  303. "selectedIndex": {},
  304. "tips": {},
  305. "total": {},
  306. "direction": {},
  307. "groupTitle": {}
  308. };
  309. }
  310. static get events() {
  311. return [{
  312. "method": "calciteTipManagerToggle",
  313. "name": "calciteTipManagerToggle",
  314. "bubbles": true,
  315. "cancelable": false,
  316. "composed": true,
  317. "docs": {
  318. "tags": [{
  319. "name": "deprecated",
  320. "text": "use `calciteTipManagerClose` instead."
  321. }],
  322. "text": "Emits when the component has been open or closed."
  323. },
  324. "complexType": {
  325. "original": "void",
  326. "resolved": "void",
  327. "references": {}
  328. }
  329. }, {
  330. "method": "calciteTipManagerClose",
  331. "name": "calciteTipManagerClose",
  332. "bubbles": true,
  333. "cancelable": false,
  334. "composed": true,
  335. "docs": {
  336. "tags": [],
  337. "text": "Emits when the component has been closed."
  338. },
  339. "complexType": {
  340. "original": "void",
  341. "resolved": "void",
  342. "references": {}
  343. }
  344. }];
  345. }
  346. static get methods() {
  347. return {
  348. "nextTip": {
  349. "complexType": {
  350. "signature": "() => Promise<void>",
  351. "parameters": [],
  352. "references": {
  353. "Promise": {
  354. "location": "global"
  355. }
  356. },
  357. "return": "Promise<void>"
  358. },
  359. "docs": {
  360. "text": "Selects the next `calcite-tip` to display.",
  361. "tags": []
  362. }
  363. },
  364. "previousTip": {
  365. "complexType": {
  366. "signature": "() => Promise<void>",
  367. "parameters": [],
  368. "references": {
  369. "Promise": {
  370. "location": "global"
  371. }
  372. },
  373. "return": "Promise<void>"
  374. },
  375. "docs": {
  376. "text": "Selects the previous `calcite-tip` to display.",
  377. "tags": []
  378. }
  379. }
  380. };
  381. }
  382. static get elementRef() { return "el"; }
  383. static get watchers() {
  384. return [{
  385. "propName": "closed",
  386. "methodName": "closedChangeHandler"
  387. }, {
  388. "propName": "selectedIndex",
  389. "methodName": "selectedChangeHandler"
  390. }];
  391. }
  392. }