|
1 | 1 | import {
|
| 2 | + AfterContentInit, |
2 | 3 | Component,
|
| 4 | + ContentChildren, |
| 5 | + Directive, |
| 6 | + ElementRef, |
3 | 7 | HostBinding,
|
| 8 | + Inject, |
4 | 9 | Input,
|
| 10 | + QueryList, |
| 11 | + Renderer2, |
5 | 12 | ViewEncapsulation,
|
6 | 13 | } from '@angular/core';
|
| 14 | +import { toBoolean } from '../common/boolean-property'; |
| 15 | + |
| 16 | +import { Ripple } from '../ripple/ripple.directive'; |
| 17 | +import { ButtonComponent } from '../button/button.component'; |
| 18 | + |
| 19 | +@Directive({ |
| 20 | + selector: 'mdc-card-primary' |
| 21 | +}) |
| 22 | +export class CardPrimaryDirective { |
| 23 | + @HostBinding('class.mdc-card__primary') isHostClass = true; |
| 24 | +} |
| 25 | + |
| 26 | +@Component({ |
| 27 | + selector: 'mdc-card-horizontal', |
| 28 | + template: '<ng-content select="mdc-card-primary, mdc-card-title, mdc-card-subtitle, [mdc-card-media-item], mdc-card-actions"></ng-content>', |
| 29 | + encapsulation: ViewEncapsulation.None |
| 30 | +}) |
| 31 | +export class CardHorizontalComponent { |
| 32 | + @HostBinding('class.mdc-card__horizontal-block') isHostClass = true; |
| 33 | +} |
| 34 | + |
| 35 | +@Directive({ |
| 36 | + selector: '[mdc-card-title], mdc-card-title' |
| 37 | +}) |
| 38 | +export class CardTitleDirective { |
| 39 | + @Input() large: boolean = true; |
| 40 | + @HostBinding('class.mdc-card__title') isHostClass = true; |
| 41 | + @HostBinding('class.mdc-card__title--large') get classTitleLarge(): string { |
| 42 | + return this.large ? 'mdc-card__title--large' : ''; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +@Directive({ |
| 47 | + selector: '[mdc-card-subtitle], mdc-card-subtitle' |
| 48 | +}) |
| 49 | +export class CardSubtitleComponent { |
| 50 | + @HostBinding('class.mdc-card__subtitle') isHostClass = true; |
| 51 | +} |
| 52 | + |
| 53 | +@Directive({ |
| 54 | + selector: '[mdc-card-supporting-text], mdc-card-supporting-text' |
| 55 | +}) |
| 56 | +export class CardSupportingTextDirective { |
| 57 | + @HostBinding('class.mdc-card__supporting-text') isHostClass = true; |
| 58 | +} |
| 59 | + |
| 60 | +@Directive({ |
| 61 | + selector: '[mdc-card-media-item]' |
| 62 | +}) |
| 63 | +export class CardMediaItemDirective { |
| 64 | + @Input() size: number; |
| 65 | + @HostBinding('class.mdc-card__media-item') isHostClass = true; |
| 66 | + @HostBinding('class.mdc-card__media-item--1dot5x') get classMediaItemOne(): string { |
| 67 | + return this.size === 1 ? 'mdc-card__media-item--1x' : ''; |
| 68 | + } |
| 69 | + @HostBinding('class.mdc-card__media-item--1dot5x') get classMediaItemOneDotFive(): string { |
| 70 | + return this.size === 1.5 ? 'mdc-card__media-item--1dot5x' : ''; |
| 71 | + } |
| 72 | + @HostBinding('class.mdc-card__media-item--2x') get classMediaItemTwo(): string { |
| 73 | + return this.size === 2 ? 'mdc-card__media-item--2x' : ''; |
| 74 | + } |
| 75 | + @HostBinding('class.mdc-card__media-item--3x') get classMediaItemThree(): string { |
| 76 | + return this.size === 3 ? 'mdc-card__media-item--3x' : ''; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +@Component({ |
| 81 | + selector: 'mdc-card-media', |
| 82 | + template: '<ng-content></ng-content>', |
| 83 | + encapsulation: ViewEncapsulation.None |
| 84 | +}) |
| 85 | +export class CardMediaComponent { |
| 86 | + @HostBinding('class.mdc-card__media') isHostClass = true; |
| 87 | +} |
| 88 | + |
| 89 | +@Directive({ |
| 90 | + selector: 'mdc-card-actions' |
| 91 | +}) |
| 92 | +export class CardActionsDirective { |
| 93 | + @Input() vertical: boolean; |
| 94 | + @HostBinding('class.mdc-card__actions') isHostClass = true; |
| 95 | + @HostBinding('class.mdc-card__actions--vertical') get classCardActionVertical(): string { |
| 96 | + return this.vertical ? 'mdc-card__actions--vertical' : ''; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +@Directive({ |
| 101 | + selector: 'button[mdc-card-button], a[mdc-card-button]', |
| 102 | + providers: [Ripple] |
| 103 | +}) |
| 104 | +export class CardActionButtonDirective extends ButtonComponent { |
| 105 | + constructor( |
| 106 | + @Inject(Renderer2) renderer: Renderer2, |
| 107 | + @Inject(ElementRef) elementRef: ElementRef, |
| 108 | + @Inject(Ripple) ripple: Ripple) { |
| 109 | + super(renderer, elementRef, ripple); |
| 110 | + } |
| 111 | +} |
7 | 112 |
|
8 | 113 | @Component({
|
9 | 114 | selector: 'mdc-card',
|
10 | 115 | template: '<ng-content></ng-content>',
|
11 | 116 | encapsulation: ViewEncapsulation.None
|
12 | 117 | })
|
13 |
| -export class CardComponent { |
14 |
| - @Input() darkTheme: boolean; |
| 118 | +export class CardComponent implements AfterContentInit { |
15 | 119 | @HostBinding('class.mdc-card') isHostClass = true;
|
16 |
| - @HostBinding('class.mdc-card--theme-dark') get classDarkTheme(): string { |
17 |
| - return this.darkTheme ? 'mdc-card--theme-dark' : ''; |
| 120 | + @ContentChildren(CardActionButtonDirective, { descendants: true }) cardButtons: QueryList<CardActionButtonDirective>; |
| 121 | + |
| 122 | + constructor( |
| 123 | + private _renderer: Renderer2, |
| 124 | + public elementRef: ElementRef) { } |
| 125 | + |
| 126 | + ngAfterContentInit() { |
| 127 | + this.cardButtons.forEach((_) => { |
| 128 | + this._renderer.addClass(_.elementRef.nativeElement, 'mdc-card__action'); |
| 129 | + _.compact = true; |
| 130 | + }); |
18 | 131 | }
|
19 | 132 | }
|
0 commit comments