UNPKG

12.8 kBJavaScriptView Raw
1import * as i0 from '@angular/core';
2import { Component, ChangeDetectionStrategy, HostBinding, ContentChildren, Input, NgModule } from '@angular/core';
3import * as i1 from '@angular/common';
4import { CommonModule } from '@angular/common';
5import * as i2 from '@angular/material/icon';
6import { MatIconModule } from '@angular/material/icon';
7import { Subject, fromEvent } from 'rxjs';
8import { debounceTime, takeUntil, startWith } from 'rxjs/operators';
9
10class TdBreadcrumbComponent {
11 _elementRef;
12 _changeDetectorRef;
13 _displayCrumb = true;
14 _width = 0;
15 _displayIcon = true;
16 _separatorIcon = 'chevron_right';
17 matButtonClass = true;
18 tdBreadCrumbClass = true;
19 // Sets the icon url shown between breadcrumbs. Defaults to 'chevron_right'
20 get separatorIcon() {
21 return this._separatorIcon;
22 }
23 set separatorIcon(separatorIcon) {
24 this._separatorIcon = separatorIcon;
25 setTimeout(() => {
26 this._changeDetectorRef.markForCheck();
27 });
28 }
29 // Should show the right chevron or not before the label
30 get displayIcon() {
31 return this._displayIcon;
32 }
33 set displayIcon(displayIcon) {
34 this._displayIcon = displayIcon;
35 setTimeout(() => {
36 this._changeDetectorRef.markForCheck();
37 });
38 }
39 get displayCrumb() {
40 return this._displayCrumb;
41 }
42 /**
43 * Whether to display the crumb or not
44 */
45 set displayCrumb(shouldDisplay) {
46 this._displayCrumb = shouldDisplay;
47 setTimeout(() => {
48 this._changeDetectorRef.markForCheck();
49 });
50 }
51 /**
52 * Width of the DOM element of the crumb
53 */
54 get width() {
55 return this._width;
56 }
57 /**
58 * Gets the display style of the crumb
59 */
60 get displayBinding() {
61 // Set the display to none on the component, just in case the end user is hiding
62 // and showing them instead of the component doing itself for reasons like responsive
63 return this._displayCrumb ? undefined : 'none';
64 }
65 constructor(_elementRef, _changeDetectorRef) {
66 this._elementRef = _elementRef;
67 this._changeDetectorRef = _changeDetectorRef;
68 }
69 ngAfterViewInit() {
70 // set the width from the actual rendered DOM element
71 setTimeout(() => {
72 this._width = (this._elementRef.nativeElement).getBoundingClientRect().width;
73 this._changeDetectorRef.markForCheck();
74 });
75 }
76 /**
77 * Stop click propagation when clicking on icon
78 */
79 _handleIconClick(event) {
80 event.stopPropagation();
81 event.preventDefault();
82 }
83 static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.1.2", ngImport: i0, type: TdBreadcrumbComponent, deps: [{ token: i0.ElementRef }, { token: i0.ChangeDetectorRef }], target: i0.ɵɵFactoryTarget.Component });
84 static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "17.1.2", type: TdBreadcrumbComponent, selector: "td-breadcrumb, a[td-breadcrumb]", host: { properties: { "class.mdc-button": "this.matButtonClass", "class.td-breadcrumb": "this.tdBreadCrumbClass", "style.display": "this.displayBinding" } }, ngImport: i0, template: "<ng-content></ng-content>\n<mat-icon\n *ngIf=\"displayIcon\"\n class=\"td-breadcrumb-separator-icon\"\n [style.cursor]=\"'default'\"\n (click)=\"_handleIconClick($event)\"\n >{{ separatorIcon }}</mat-icon\n>\n", styles: [":host.td-breadcrumb{display:inline-flex;box-sizing:border-box;flex-direction:row;align-items:center;align-content:center;flex-shrink:0;justify-content:flex-end}:host.td-breadcrumb ::ng-deep>*{margin:0 10px}:host mat-icon{font-size:16px;width:16px;height:16px}:host.mdc-button{min-width:0;padding:0}\n"], dependencies: [{ kind: "directive", type: i1.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "component", type: i2.MatIcon, selector: "mat-icon", inputs: ["color", "inline", "svgIcon", "fontSet", "fontIcon"], exportAs: ["matIcon"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush });
85}
86i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.1.2", ngImport: i0, type: TdBreadcrumbComponent, decorators: [{
87 type: Component,
88 args: [{ selector: 'td-breadcrumb, a[td-breadcrumb]', changeDetection: ChangeDetectionStrategy.OnPush, template: "<ng-content></ng-content>\n<mat-icon\n *ngIf=\"displayIcon\"\n class=\"td-breadcrumb-separator-icon\"\n [style.cursor]=\"'default'\"\n (click)=\"_handleIconClick($event)\"\n >{{ separatorIcon }}</mat-icon\n>\n", styles: [":host.td-breadcrumb{display:inline-flex;box-sizing:border-box;flex-direction:row;align-items:center;align-content:center;flex-shrink:0;justify-content:flex-end}:host.td-breadcrumb ::ng-deep>*{margin:0 10px}:host mat-icon{font-size:16px;width:16px;height:16px}:host.mdc-button{min-width:0;padding:0}\n"] }]
89 }], ctorParameters: () => [{ type: i0.ElementRef }, { type: i0.ChangeDetectorRef }], propDecorators: { matButtonClass: [{
90 type: HostBinding,
91 args: ['class.mdc-button']
92 }], tdBreadCrumbClass: [{
93 type: HostBinding,
94 args: ['class.td-breadcrumb']
95 }], displayBinding: [{
96 type: HostBinding,
97 args: ['style.display']
98 }] } });
99
100class TdBreadcrumbsComponent {
101 _elementRef;
102 _changeDetectorRef;
103 _resizing = false;
104 _separatorIcon = 'chevron_right';
105 _destroy$ = new Subject();
106 tdBreadCrumbsClass = true;
107 // all the sub components, which are the individual breadcrumbs
108 _breadcrumbs;
109 // the list of hidden breadcrumbs not shown right now (responsive)
110 hiddenBreadcrumbs = [];
111 /**
112 * Sets the icon url shown between breadcrumbs. Defaults to 'chevron_right'.
113 */
114 set separatorIcon(separatorIcon) {
115 this._separatorIcon = separatorIcon;
116 this.setCrumbIcons();
117 }
118 get separatorIcon() {
119 return this._separatorIcon;
120 }
121 constructor(_elementRef, _changeDetectorRef) {
122 this._elementRef = _elementRef;
123 this._changeDetectorRef = _changeDetectorRef;
124 }
125 ngOnInit() {
126 fromEvent(window, 'resize')
127 .pipe(debounceTime(10), takeUntil(this._destroy$))
128 .subscribe(() => {
129 if (!this._resizing) {
130 this._resizing = true;
131 setTimeout(() => {
132 this._calculateVisibility();
133 this._resizing = false;
134 this._changeDetectorRef.markForCheck();
135 }, 100);
136 }
137 });
138 }
139 ngAfterViewInit() {
140 this._waitToCalculateVisibility();
141 }
142 ngAfterContentInit() {
143 // Note: doesn't need to unsubscribe since `QueryList.changes`
144 // gets completed by Angular when the view is destroyed.
145 this._breadcrumbs.changes
146 .pipe(startWith(this._breadcrumbs))
147 .subscribe(() => {
148 this._waitToCalculateVisibility();
149 this.setCrumbIcons();
150 this._changeDetectorRef.markForCheck();
151 });
152 }
153 ngOnDestroy() {
154 this._destroy$.next();
155 }
156 /*
157 * Current width of the element container
158 */
159 get nativeElementWidth() {
160 const element = this._elementRef.nativeElement;
161 // Need to take into account border, margin and padding that might be around all the crumbs
162 const style = window.getComputedStyle(element);
163 const borderLeft = parseInt(style.borderLeft, 10);
164 const borderRight = parseInt(style.borderRight, 10);
165 const marginLeft = parseInt(style.marginLeft, 10);
166 const marginRight = parseInt(style.marginRight, 10);
167 const paddingLeft = parseInt(style.paddingLeft, 10);
168 const paddingRight = parseInt(style.paddingRight, 10);
169 return (element.getBoundingClientRect().width -
170 borderLeft -
171 borderRight -
172 marginLeft -
173 marginRight -
174 paddingLeft -
175 paddingRight);
176 }
177 /**
178 * The total count of individual breadcrumbs
179 */
180 get count() {
181 return this._breadcrumbs ? this._breadcrumbs.length : 0;
182 }
183 /**
184 * Set the crumb icon separators
185 */
186 setCrumbIcons() {
187 if (this._breadcrumbs) {
188 const breadcrumbArray = this._breadcrumbs.toArray();
189 breadcrumbArray.forEach((breadcrumb, index) => {
190 breadcrumb.separatorIcon = this.separatorIcon;
191 // don't show the icon on the last breadcrumb
192 breadcrumb.displayIcon = index < breadcrumbArray.length - 1;
193 });
194 }
195 }
196 _calculateVisibility() {
197 const crumbsArray = this._breadcrumbs.toArray();
198 let crumbWidthSum = 0;
199 const hiddenCrumbs = [];
200 // loop through crumbs in reverse order to calculate which ones should be removed
201 for (let i = crumbsArray.length - 1; i >= 0; i--) {
202 const breadcrumb = crumbsArray[i];
203 // if crumb exceeds width, then we skip it from the sum and add it into the hiddencrumbs array
204 // and hide it
205 if (crumbWidthSum + breadcrumb.width > this.nativeElementWidth) {
206 breadcrumb.displayCrumb = false;
207 hiddenCrumbs.push(breadcrumb);
208 }
209 else {
210 // else we show it
211 breadcrumb.displayCrumb = true;
212 }
213 crumbWidthSum += breadcrumb.width;
214 }
215 this.hiddenBreadcrumbs = hiddenCrumbs;
216 this._changeDetectorRef.markForCheck();
217 }
218 _waitToCalculateVisibility() {
219 setTimeout(() => {
220 this._calculateVisibility();
221 });
222 }
223 static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.1.2", ngImport: i0, type: TdBreadcrumbsComponent, deps: [{ token: i0.ElementRef }, { token: i0.ChangeDetectorRef }], target: i0.ɵɵFactoryTarget.Component });
224 static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "17.1.2", type: TdBreadcrumbsComponent, selector: "td-breadcrumbs", inputs: { separatorIcon: "separatorIcon" }, host: { properties: { "class.td-breadcrumbs": "this.tdBreadCrumbsClass" } }, queries: [{ propertyName: "_breadcrumbs", predicate: TdBreadcrumbComponent, descendants: true }], ngImport: i0, template: "<ng-content></ng-content>\n", styles: [":host{display:flex;align-items:center}:host.td-breadcrumbs{white-space:nowrap}\n"], changeDetection: i0.ChangeDetectionStrategy.OnPush });
225}
226i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.1.2", ngImport: i0, type: TdBreadcrumbsComponent, decorators: [{
227 type: Component,
228 args: [{ selector: 'td-breadcrumbs', changeDetection: ChangeDetectionStrategy.OnPush, template: "<ng-content></ng-content>\n", styles: [":host{display:flex;align-items:center}:host.td-breadcrumbs{white-space:nowrap}\n"] }]
229 }], ctorParameters: () => [{ type: i0.ElementRef }, { type: i0.ChangeDetectorRef }], propDecorators: { tdBreadCrumbsClass: [{
230 type: HostBinding,
231 args: ['class.td-breadcrumbs']
232 }], _breadcrumbs: [{
233 type: ContentChildren,
234 args: [TdBreadcrumbComponent, { descendants: true }]
235 }], separatorIcon: [{
236 type: Input
237 }] } });
238
239class CovalentBreadcrumbsModule {
240 static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.1.2", ngImport: i0, type: CovalentBreadcrumbsModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
241 static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "17.1.2", ngImport: i0, type: CovalentBreadcrumbsModule, declarations: [TdBreadcrumbsComponent, TdBreadcrumbComponent], imports: [CommonModule, MatIconModule], exports: [TdBreadcrumbsComponent, TdBreadcrumbComponent] });
242 static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "17.1.2", ngImport: i0, type: CovalentBreadcrumbsModule, imports: [CommonModule, MatIconModule] });
243}
244i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.1.2", ngImport: i0, type: CovalentBreadcrumbsModule, decorators: [{
245 type: NgModule,
246 args: [{
247 imports: [CommonModule, MatIconModule],
248 declarations: [TdBreadcrumbsComponent, TdBreadcrumbComponent],
249 exports: [TdBreadcrumbsComponent, TdBreadcrumbComponent],
250 }]
251 }] });
252
253/**
254 * Generated bundle index. Do not edit.
255 */
256
257export { CovalentBreadcrumbsModule, TdBreadcrumbComponent, TdBreadcrumbsComponent };
258//# sourceMappingURL=covalent-core-breadcrumbs.mjs.map