|
| 1 | +import { |
| 2 | + Component, |
| 3 | + HostBinding, |
| 4 | + Input, |
| 5 | + ViewChild, |
| 6 | + ElementRef, |
| 7 | + AfterContentInit |
| 8 | +} from "@angular/core"; |
| 9 | +import { I18n } from "./../i18n/i18n.module"; |
| 10 | + |
| 11 | +@Component({ |
| 12 | + selector: "ibm-expandable-tile", |
| 13 | + template: ` |
| 14 | + <div |
| 15 | + class="bx--tile bx--tile--expandable" |
| 16 | + [ngClass]="{'bx--tile--is-expanded' : expanded}" |
| 17 | + [ngStyle]="{'max-height': expandedHeight + 'px'}" |
| 18 | + role="button" |
| 19 | + tabindex="0" |
| 20 | + (click)="onClick()"> |
| 21 | + <button [attr.aria-label]="(expanded ? collapse : expand) | async" class="bx--tile__chevron"> |
| 22 | + <svg *ngIf="!expanded" width="12" height="7" viewBox="0 0 12 7" role="img"> |
| 23 | + <title>{{expand | async}}</title> |
| 24 | + <path fill-rule="nonzero" d="M6.002 5.55L11.27 0l.726.685L6.003 7 0 .685.726 0z"/> |
| 25 | + </svg> |
| 26 | + <svg *ngIf="expanded" width="12" height="7" viewBox="0 0 12 7" role="img"> |
| 27 | + <title>{{collapse | async}}</title> |
| 28 | + <path fill-rule="nonzero" d="M6.002 5.55L11.27 0l.726.685L6.003 7 0 .685.726 0z"/> |
| 29 | + </svg> |
| 30 | + </button> |
| 31 | + <div class="bx--tile-content"> |
| 32 | + <ng-content select=".bx--tile-content__above-the-fold"></ng-content> |
| 33 | + <ng-content select=".bx--tile-content__below-the-fold"></ng-content> |
| 34 | + </div> |
| 35 | + </div> |
| 36 | + ` |
| 37 | +}) |
| 38 | +export class ExpandableTile implements AfterContentInit { |
| 39 | + @Input() expanded = false; |
| 40 | + /** |
| 41 | + * Expects an object that contains some or all of: |
| 42 | + * ``` |
| 43 | + * { |
| 44 | + * "EXPAND": "Expand", |
| 45 | + * "COLLAPSE": "Collapse", |
| 46 | + * } |
| 47 | + * ``` |
| 48 | + */ |
| 49 | + @Input() |
| 50 | + set translations (value) { |
| 51 | + if (value.EXPAND) { |
| 52 | + this.expand.next(value.EXPAND); |
| 53 | + } |
| 54 | + if (value.COLLAPSE) { |
| 55 | + this.collapse.next(value.COLLAPSE); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + tileMaxHeight = 0; |
| 60 | + element = this.elementRef.nativeElement; |
| 61 | + |
| 62 | + expand = this.i18n.get("TILES.EXPAND"); |
| 63 | + collapse = this.i18n.get("TILES.COLLAPSE"); |
| 64 | + |
| 65 | + constructor(protected i18n: I18n, protected elementRef: ElementRef) {} |
| 66 | + |
| 67 | + ngAfterContentInit() { |
| 68 | + this.updateMaxHeight(); |
| 69 | + } |
| 70 | + |
| 71 | + get expandedHeight() { |
| 72 | + return this.tileMaxHeight + parseInt(getComputedStyle(this.element.querySelector(".bx--tile")).paddingBottom, 10); |
| 73 | + } |
| 74 | + |
| 75 | + updateMaxHeight() { |
| 76 | + if (this.expanded) { |
| 77 | + this.tileMaxHeight = this.element.querySelector(".bx--tile-content").getBoundingClientRect().height; |
| 78 | + } else { |
| 79 | + this.tileMaxHeight = this.element.querySelector(".bx--tile-content__above-the-fold").getBoundingClientRect().height; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + onClick() { |
| 84 | + this.expanded = !this.expanded; |
| 85 | + this.updateMaxHeight(); |
| 86 | + } |
| 87 | +} |
0 commit comments