ion-breadcrumbs.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*!
  2. * (C) Ionic http://ionicframework.com - MIT License
  3. */
  4. import { proxyCustomElement, HTMLElement, createEvent, h, Host } from '@stencil/core/internal/client';
  5. import { c as createColorClasses, h as hostContext } from './theme.js';
  6. import { b as getIonMode } from './ionic-global.js';
  7. const breadcrumbsIosCss = ":host{-moz-osx-font-smoothing:grayscale;-webkit-font-smoothing:antialiased;display:-ms-flexbox;display:flex;-ms-flex-wrap:wrap;flex-wrap:wrap;-ms-flex-align:center;align-items:center}:host(.in-toolbar-color),:host(.in-toolbar-color) .breadcrumbs-collapsed-indicator ion-icon{color:var(--ion-color-contrast)}:host(.in-toolbar-color) .breadcrumbs-collapsed-indicator{background:rgba(var(--ion-color-contrast-rgb), 0.11)}:host(.in-toolbar){-webkit-padding-start:20px;padding-inline-start:20px;-webkit-padding-end:20px;padding-inline-end:20px;padding-top:0;padding-bottom:0;-ms-flex-pack:center;justify-content:center}";
  8. const IonBreadcrumbsIosStyle0 = breadcrumbsIosCss;
  9. const breadcrumbsMdCss = ":host{-moz-osx-font-smoothing:grayscale;-webkit-font-smoothing:antialiased;display:-ms-flexbox;display:flex;-ms-flex-wrap:wrap;flex-wrap:wrap;-ms-flex-align:center;align-items:center}:host(.in-toolbar-color),:host(.in-toolbar-color) .breadcrumbs-collapsed-indicator ion-icon{color:var(--ion-color-contrast)}:host(.in-toolbar-color) .breadcrumbs-collapsed-indicator{background:rgba(var(--ion-color-contrast-rgb), 0.11)}:host(.in-toolbar){-webkit-padding-start:8px;padding-inline-start:8px;-webkit-padding-end:8px;padding-inline-end:8px;padding-top:0;padding-bottom:0}";
  10. const IonBreadcrumbsMdStyle0 = breadcrumbsMdCss;
  11. const Breadcrumbs = /*@__PURE__*/ proxyCustomElement(class Breadcrumbs extends HTMLElement {
  12. constructor() {
  13. super();
  14. this.__registerHost();
  15. this.__attachShadow();
  16. this.ionCollapsedClick = createEvent(this, "ionCollapsedClick", 7);
  17. this.breadcrumbsInit = () => {
  18. this.setBreadcrumbSeparator();
  19. this.setMaxItems();
  20. };
  21. this.resetActiveBreadcrumb = () => {
  22. const breadcrumbs = this.getBreadcrumbs();
  23. // Only reset the active breadcrumb if we were the ones to change it
  24. // otherwise use the one set on the component
  25. const activeBreadcrumb = breadcrumbs.find((breadcrumb) => breadcrumb.active);
  26. if (activeBreadcrumb && this.activeChanged) {
  27. activeBreadcrumb.active = false;
  28. }
  29. };
  30. this.setMaxItems = () => {
  31. const { itemsAfterCollapse, itemsBeforeCollapse, maxItems } = this;
  32. const breadcrumbs = this.getBreadcrumbs();
  33. for (const breadcrumb of breadcrumbs) {
  34. breadcrumb.showCollapsedIndicator = false;
  35. breadcrumb.collapsed = false;
  36. }
  37. // If the number of breadcrumbs exceeds the maximum number of items
  38. // that should show and the items before / after collapse do not
  39. // exceed the maximum items then we need to collapse the breadcrumbs
  40. const shouldCollapse = maxItems !== undefined && breadcrumbs.length > maxItems && itemsBeforeCollapse + itemsAfterCollapse <= maxItems;
  41. if (shouldCollapse) {
  42. // Show the collapsed indicator in the first breadcrumb that collapses
  43. breadcrumbs.forEach((breadcrumb, index) => {
  44. if (index === itemsBeforeCollapse) {
  45. breadcrumb.showCollapsedIndicator = true;
  46. }
  47. // Collapse all breadcrumbs that have an index greater than or equal to
  48. // the number before collapse and an index less than the total number
  49. // of breadcrumbs minus the items that should show after the collapse
  50. if (index >= itemsBeforeCollapse && index < breadcrumbs.length - itemsAfterCollapse) {
  51. breadcrumb.collapsed = true;
  52. }
  53. });
  54. }
  55. };
  56. this.setBreadcrumbSeparator = () => {
  57. const { itemsAfterCollapse, itemsBeforeCollapse, maxItems } = this;
  58. const breadcrumbs = this.getBreadcrumbs();
  59. // Check if an active breadcrumb exists already
  60. const active = breadcrumbs.find((breadcrumb) => breadcrumb.active);
  61. // Set the separator on all but the last breadcrumb
  62. for (const breadcrumb of breadcrumbs) {
  63. // The only time the last breadcrumb changes is when
  64. // itemsAfterCollapse is set to 0, in this case the
  65. // last breadcrumb will be the collapsed indicator
  66. const last = maxItems !== undefined && itemsAfterCollapse === 0
  67. ? breadcrumb === breadcrumbs[itemsBeforeCollapse]
  68. : breadcrumb === breadcrumbs[breadcrumbs.length - 1];
  69. breadcrumb.last = last;
  70. // If the breadcrumb has defined whether or not to show the
  71. // separator then use that value, otherwise check if it's the
  72. // last breadcrumb
  73. const separator = breadcrumb.separator !== undefined ? breadcrumb.separator : last ? undefined : true;
  74. breadcrumb.separator = separator;
  75. // If there is not an active breadcrumb already
  76. // set the last one to active
  77. if (!active && last) {
  78. breadcrumb.active = true;
  79. this.activeChanged = true;
  80. }
  81. }
  82. };
  83. this.getBreadcrumbs = () => {
  84. return Array.from(this.el.querySelectorAll('ion-breadcrumb'));
  85. };
  86. this.slotChanged = () => {
  87. this.resetActiveBreadcrumb();
  88. this.breadcrumbsInit();
  89. };
  90. this.collapsed = undefined;
  91. this.activeChanged = undefined;
  92. this.color = undefined;
  93. this.maxItems = undefined;
  94. this.itemsBeforeCollapse = 1;
  95. this.itemsAfterCollapse = 1;
  96. }
  97. onCollapsedClick(ev) {
  98. const breadcrumbs = this.getBreadcrumbs();
  99. const collapsedBreadcrumbs = breadcrumbs.filter((breadcrumb) => breadcrumb.collapsed);
  100. this.ionCollapsedClick.emit(Object.assign(Object.assign({}, ev.detail), { collapsedBreadcrumbs }));
  101. }
  102. maxItemsChanged() {
  103. this.resetActiveBreadcrumb();
  104. this.breadcrumbsInit();
  105. }
  106. componentWillLoad() {
  107. this.breadcrumbsInit();
  108. }
  109. render() {
  110. const { color, collapsed } = this;
  111. const mode = getIonMode(this);
  112. return (h(Host, { key: 'fe64e9cdf597ede2db140bf5fa05a0359d82db57', class: createColorClasses(color, {
  113. [mode]: true,
  114. 'in-toolbar': hostContext('ion-toolbar', this.el),
  115. 'in-toolbar-color': hostContext('ion-toolbar[color]', this.el),
  116. 'breadcrumbs-collapsed': collapsed,
  117. }) }, h("slot", { key: 'a2c99b579e339055c50a613d5c6b61032f5ddffe', onSlotchange: this.slotChanged })));
  118. }
  119. get el() { return this; }
  120. static get watchers() { return {
  121. "maxItems": ["maxItemsChanged"],
  122. "itemsBeforeCollapse": ["maxItemsChanged"],
  123. "itemsAfterCollapse": ["maxItemsChanged"]
  124. }; }
  125. static get style() { return {
  126. ios: IonBreadcrumbsIosStyle0,
  127. md: IonBreadcrumbsMdStyle0
  128. }; }
  129. }, [33, "ion-breadcrumbs", {
  130. "color": [513],
  131. "maxItems": [2, "max-items"],
  132. "itemsBeforeCollapse": [2, "items-before-collapse"],
  133. "itemsAfterCollapse": [2, "items-after-collapse"],
  134. "collapsed": [32],
  135. "activeChanged": [32]
  136. }, [[0, "collapsedClick", "onCollapsedClick"]], {
  137. "maxItems": ["maxItemsChanged"],
  138. "itemsBeforeCollapse": ["maxItemsChanged"],
  139. "itemsAfterCollapse": ["maxItemsChanged"]
  140. }]);
  141. function defineCustomElement$1() {
  142. if (typeof customElements === "undefined") {
  143. return;
  144. }
  145. const components = ["ion-breadcrumbs"];
  146. components.forEach(tagName => { switch (tagName) {
  147. case "ion-breadcrumbs":
  148. if (!customElements.get(tagName)) {
  149. customElements.define(tagName, Breadcrumbs);
  150. }
  151. break;
  152. } });
  153. }
  154. const IonBreadcrumbs = Breadcrumbs;
  155. const defineCustomElement = defineCustomElement$1;
  156. export { IonBreadcrumbs, defineCustomElement };