tip-manager.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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.82
  5. */
  6. import { Component, Element, Event, Method, Prop, State, Watch, 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. * Closed state of the `calcite-tip-manager`.
  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.tipManagerKeyUpHandler = (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 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 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 },
  147. h("calcite-action", { class: CSS.pagePrevious, icon: dir === "ltr" ? ICONS.chevronLeft : ICONS.chevronRight, onClick: this.previousClicked, scale: "m", text: previousLabel }),
  148. h("span", { class: CSS.pagePosition }, `${paginationLabel} ${selectedIndex + 1}/${total}`),
  149. h("calcite-action", { class: CSS.pageNext, icon: dir === "ltr" ? ICONS.chevronRight : ICONS.chevronLeft, onClick: this.nextClicked, scale: "m", text: nextLabel }))) : null;
  150. }
  151. render() {
  152. const { closed, direction, headingLevel, groupTitle, selectedIndex, intlClose, total } = this;
  153. const closeLabel = intlClose || TEXT.close;
  154. if (total === 0) {
  155. return null;
  156. }
  157. return (h("section", { "aria-hidden": toAriaBoolean(closed), class: CSS.container, hidden: closed, onKeyUp: this.tipManagerKeyUpHandler, ref: this.storeContainerRef, tabIndex: 0 },
  158. h("header", { class: CSS.header },
  159. h(Heading, { class: CSS.heading, level: headingLevel || HEADING_LEVEL }, groupTitle),
  160. h("calcite-action", { class: CSS.close, onClick: this.hideTipManager, scale: "m", text: closeLabel },
  161. h("calcite-icon", { icon: ICONS.close, scale: "m" }))),
  162. h("div", { class: {
  163. [CSS.tipContainer]: true,
  164. [CSS.tipContainerAdvancing]: !closed && direction === "advancing",
  165. [CSS.tipContainerRetreating]: !closed && direction === "retreating"
  166. }, key: selectedIndex, tabIndex: 0 },
  167. h("slot", null)),
  168. this.renderPagination()));
  169. }
  170. static get is() { return "calcite-tip-manager"; }
  171. static get encapsulation() { return "shadow"; }
  172. static get originalStyleUrls() { return {
  173. "$": ["tip-manager.scss"]
  174. }; }
  175. static get styleUrls() { return {
  176. "$": ["tip-manager.css"]
  177. }; }
  178. static get properties() { return {
  179. "closed": {
  180. "type": "boolean",
  181. "mutable": true,
  182. "complexType": {
  183. "original": "boolean",
  184. "resolved": "boolean",
  185. "references": {}
  186. },
  187. "required": false,
  188. "optional": false,
  189. "docs": {
  190. "tags": [],
  191. "text": "Closed state of the `calcite-tip-manager`."
  192. },
  193. "attribute": "closed",
  194. "reflect": true,
  195. "defaultValue": "false"
  196. },
  197. "headingLevel": {
  198. "type": "number",
  199. "mutable": false,
  200. "complexType": {
  201. "original": "HeadingLevel",
  202. "resolved": "1 | 2 | 3 | 4 | 5 | 6",
  203. "references": {
  204. "HeadingLevel": {
  205. "location": "import",
  206. "path": "../functional/Heading"
  207. }
  208. }
  209. },
  210. "required": false,
  211. "optional": false,
  212. "docs": {
  213. "tags": [],
  214. "text": "Number at which section headings should start for this component."
  215. },
  216. "attribute": "heading-level",
  217. "reflect": false
  218. },
  219. "intlClose": {
  220. "type": "string",
  221. "mutable": false,
  222. "complexType": {
  223. "original": "string",
  224. "resolved": "string",
  225. "references": {}
  226. },
  227. "required": false,
  228. "optional": true,
  229. "docs": {
  230. "tags": [],
  231. "text": "Alternate text for closing the tip."
  232. },
  233. "attribute": "intl-close",
  234. "reflect": false
  235. },
  236. "intlDefaultTitle": {
  237. "type": "string",
  238. "mutable": false,
  239. "complexType": {
  240. "original": "string",
  241. "resolved": "string",
  242. "references": {}
  243. },
  244. "required": false,
  245. "optional": true,
  246. "docs": {
  247. "tags": [],
  248. "text": "The default group title for the `calcite-tip-manager`."
  249. },
  250. "attribute": "intl-default-title",
  251. "reflect": false
  252. },
  253. "intlNext": {
  254. "type": "string",
  255. "mutable": false,
  256. "complexType": {
  257. "original": "string",
  258. "resolved": "string",
  259. "references": {}
  260. },
  261. "required": false,
  262. "optional": true,
  263. "docs": {
  264. "tags": [],
  265. "text": "Alternate text for navigating to the next tip."
  266. },
  267. "attribute": "intl-next",
  268. "reflect": false
  269. },
  270. "intlPaginationLabel": {
  271. "type": "string",
  272. "mutable": false,
  273. "complexType": {
  274. "original": "string",
  275. "resolved": "string",
  276. "references": {}
  277. },
  278. "required": false,
  279. "optional": true,
  280. "docs": {
  281. "tags": [],
  282. "text": "Label that appears on hover of pagination icon."
  283. },
  284. "attribute": "intl-pagination-label",
  285. "reflect": false
  286. },
  287. "intlPrevious": {
  288. "type": "string",
  289. "mutable": false,
  290. "complexType": {
  291. "original": "string",
  292. "resolved": "string",
  293. "references": {}
  294. },
  295. "required": false,
  296. "optional": true,
  297. "docs": {
  298. "tags": [],
  299. "text": "Alternate text for navigating to the previous tip."
  300. },
  301. "attribute": "intl-previous",
  302. "reflect": false
  303. }
  304. }; }
  305. static get states() { return {
  306. "selectedIndex": {},
  307. "tips": {},
  308. "total": {},
  309. "direction": {},
  310. "groupTitle": {}
  311. }; }
  312. static get events() { return [{
  313. "method": "calciteTipManagerToggle",
  314. "name": "calciteTipManagerToggle",
  315. "bubbles": true,
  316. "cancelable": true,
  317. "composed": true,
  318. "docs": {
  319. "tags": [{
  320. "name": "deprecated",
  321. "text": "use calciteTipManagerClose instead."
  322. }],
  323. "text": "Emitted when the `calcite-tip-manager` has been toggled open or closed."
  324. },
  325. "complexType": {
  326. "original": "any",
  327. "resolved": "any",
  328. "references": {}
  329. }
  330. }, {
  331. "method": "calciteTipManagerClose",
  332. "name": "calciteTipManagerClose",
  333. "bubbles": true,
  334. "cancelable": true,
  335. "composed": true,
  336. "docs": {
  337. "tags": [],
  338. "text": "Emitted when the `calcite-tip-manager` has been closed."
  339. },
  340. "complexType": {
  341. "original": "any",
  342. "resolved": "any",
  343. "references": {}
  344. }
  345. }]; }
  346. static get methods() { return {
  347. "nextTip": {
  348. "complexType": {
  349. "signature": "() => Promise<void>",
  350. "parameters": [],
  351. "references": {
  352. "Promise": {
  353. "location": "global"
  354. }
  355. },
  356. "return": "Promise<void>"
  357. },
  358. "docs": {
  359. "text": "Selects the next tip to display",
  360. "tags": []
  361. }
  362. },
  363. "previousTip": {
  364. "complexType": {
  365. "signature": "() => Promise<void>",
  366. "parameters": [],
  367. "references": {
  368. "Promise": {
  369. "location": "global"
  370. }
  371. },
  372. "return": "Promise<void>"
  373. },
  374. "docs": {
  375. "text": "Selects the previous tip to display",
  376. "tags": []
  377. }
  378. }
  379. }; }
  380. static get elementRef() { return "el"; }
  381. static get watchers() { return [{
  382. "propName": "closed",
  383. "methodName": "closedChangeHandler"
  384. }, {
  385. "propName": "selectedIndex",
  386. "methodName": "selectedChangeHandler"
  387. }]; }
  388. }