|
1 |
| -import { |
2 |
| - AfterViewInit, |
3 |
| - ChangeDetectionStrategy, |
4 |
| - Component, |
5 |
| - Directive, |
6 |
| - ElementRef, |
7 |
| - Input, |
8 |
| - OnDestroy |
9 |
| -} from '@angular/core'; |
| 1 | +import {AfterViewInit, Directive, ElementRef, Input, OnDestroy} from '@angular/core'; |
10 | 2 | import {coerceBooleanProperty} from '@angular/cdk/coercion';
|
11 | 3 |
|
12 | 4 | import {MdcRipple, MDCRippleCapableSurface} from './ripple.service';
|
13 | 5 |
|
14 |
| -@Component({ |
15 |
| - selector: 'mdc-ripple, [mdc-ripple]', |
16 |
| - template: '<ng-content></ng-content>', |
17 |
| - providers: [MdcRipple], |
18 |
| - changeDetection: ChangeDetectionStrategy.OnPush |
| 6 | +// rapropos 2019.11.02 |
| 7 | +// Ivy doesn't allow directives to inherit from components, so this merges |
| 8 | +// what used to be the two. see the constructor for more information |
| 9 | + |
| 10 | +@Directive({ |
| 11 | + selector: 'mdc-ripple, [mdc-ripple], [mdcRipple]', |
| 12 | + providers: [MdcRipple], |
19 | 13 | })
|
20 |
| -export class MdcRippleComponent implements AfterViewInit, OnDestroy, MDCRippleCapableSurface { |
21 |
| - _root!: Element; |
22 |
| - |
23 |
| - get ripple(): MdcRipple { |
24 |
| - return this._ripple; |
25 |
| - } |
26 |
| - |
27 |
| - @Input() |
28 |
| - get attachTo(): any { |
29 |
| - return this._attachTo; |
30 |
| - } |
31 |
| - set attachTo(element: any) { |
32 |
| - if (this._attachTo) { |
33 |
| - this._attachTo.classList.remove('mdc-ripple-surface'); |
| 14 | +export class MdcRippleDirective implements AfterViewInit, OnDestroy, MDCRippleCapableSurface { |
| 15 | + _root!: Element; |
| 16 | + |
| 17 | + get ripple(): MdcRipple { |
| 18 | + return this._ripple; |
34 | 19 | }
|
35 |
| - this._attachTo = element; |
36 |
| - if (this._attachTo) { |
37 |
| - this._attachTo.classList.add('mdc-ripple-surface'); |
| 20 | + |
| 21 | + @Input() |
| 22 | + get attachTo(): any { |
| 23 | + return this._attachTo; |
38 | 24 | }
|
39 |
| - } |
40 |
| - private _attachTo: any; |
41 |
| - |
42 |
| - @Input() |
43 |
| - get primary(): boolean { |
44 |
| - return this._primary; |
45 |
| - } |
46 |
| - set primary(value: boolean) { |
47 |
| - this._primary = coerceBooleanProperty(value); |
48 |
| - this._primary ? this.attachTo.classList.add('mdc-ripple-surface--primary') |
49 |
| - : this.attachTo.classList.remove('mdc-ripple-surface--primary'); |
50 |
| - } |
51 |
| - private _primary: boolean = false; |
52 |
| - |
53 |
| - @Input() |
54 |
| - get secondary(): boolean { |
55 |
| - return this._secondary; |
56 |
| - } |
57 |
| - set secondary(value: boolean) { |
58 |
| - this._secondary = coerceBooleanProperty(value); |
59 |
| - this._secondary ? this.attachTo.classList.add('mdc-ripple-surface--accent') |
60 |
| - : this.attachTo.classList.remove('mdc-ripple-surface--accent'); |
61 |
| - } |
62 |
| - private _secondary: boolean = false; |
63 |
| - |
64 |
| - @Input() |
65 |
| - get disabled(): boolean { |
66 |
| - return this._disabled; |
67 |
| - } |
68 |
| - set disabled(value: boolean) { |
69 |
| - this._disabled = coerceBooleanProperty(value); |
70 |
| - } |
71 |
| - private _disabled: boolean = false; |
72 |
| - |
73 |
| - @Input() |
74 |
| - get unbounded(): boolean { |
75 |
| - return this._unbounded; |
76 |
| - } |
77 |
| - set unbounded(value: boolean) { |
78 |
| - this._unbounded = coerceBooleanProperty(value); |
79 |
| - } |
80 |
| - protected _unbounded: boolean = false; |
81 |
| - |
82 |
| - constructor( |
83 |
| - private _ripple: MdcRipple, |
84 |
| - public elementRef: ElementRef<HTMLElement>) { |
85 |
| - this._root = this.elementRef.nativeElement; |
86 |
| - } |
87 |
| - |
88 |
| - ngAfterViewInit(): void { |
89 |
| - this._ripple = new MdcRipple(this.elementRef); |
90 |
| - this._ripple.init(); |
91 |
| - } |
92 |
| - |
93 |
| - ngOnDestroy(): void { |
94 |
| - this.ripple.destroy(); |
95 |
| - } |
96 |
| -} |
97 | 25 |
|
98 |
| -@Directive({ |
99 |
| - selector: '[mdcRipple]', |
100 |
| - providers: [MdcRipple] |
101 |
| -}) |
102 |
| -export class MdcRippleDirective extends MdcRippleComponent { |
103 |
| - constructor( |
104 |
| - _ripple: MdcRipple, |
105 |
| - elementRef: ElementRef) { |
| 26 | + set attachTo(element: any) { |
| 27 | + if (this._attachTo) { |
| 28 | + this._attachTo.classList.remove('mdc-ripple-surface'); |
| 29 | + } |
| 30 | + this._attachTo = element; |
| 31 | + if (this._attachTo) { |
| 32 | + this._attachTo.classList.add('mdc-ripple-surface'); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + private _attachTo: any; |
| 37 | + |
| 38 | + @Input() |
| 39 | + get primary(): boolean { |
| 40 | + return this._primary; |
| 41 | + } |
| 42 | + |
| 43 | + set primary(value: boolean) { |
| 44 | + this._primary = coerceBooleanProperty(value); |
| 45 | + this._primary ? this.attachTo.classList.add('mdc-ripple-surface--primary') |
| 46 | + : this.attachTo.classList.remove('mdc-ripple-surface--primary'); |
| 47 | + } |
| 48 | + |
| 49 | + private _primary: boolean = false; |
| 50 | + |
| 51 | + @Input() |
| 52 | + get secondary(): boolean { |
| 53 | + return this._secondary; |
| 54 | + } |
| 55 | + |
| 56 | + set secondary(value: boolean) { |
| 57 | + this._secondary = coerceBooleanProperty(value); |
| 58 | + this._secondary ? this.attachTo.classList.add('mdc-ripple-surface--accent') |
| 59 | + : this.attachTo.classList.remove('mdc-ripple-surface--accent'); |
| 60 | + } |
106 | 61 |
|
107 |
| - super(_ripple, elementRef); |
| 62 | + private _secondary: boolean = false; |
108 | 63 |
|
109 |
| - this._unbounded = true; |
110 |
| - this.elementRef.nativeElement.setAttribute('data-mdc-ripple-is-unbounded', ''); |
111 |
| - } |
| 64 | + @Input() |
| 65 | + get disabled(): boolean { |
| 66 | + return this._disabled; |
| 67 | + } |
| 68 | + |
| 69 | + set disabled(value: boolean) { |
| 70 | + this._disabled = coerceBooleanProperty(value); |
| 71 | + } |
| 72 | + |
| 73 | + private _disabled: boolean = false; |
| 74 | + |
| 75 | + @Input() |
| 76 | + get unbounded(): boolean { |
| 77 | + return this._unbounded; |
| 78 | + } |
| 79 | + |
| 80 | + set unbounded(value: boolean) { |
| 81 | + this._unbounded = coerceBooleanProperty(value); |
| 82 | + } |
| 83 | + |
| 84 | + protected _unbounded: boolean = false; |
| 85 | + |
| 86 | + constructor( |
| 87 | + private _ripple: MdcRipple, |
| 88 | + public elementRef: ElementRef<HTMLElement>) { |
| 89 | + this._root = this.elementRef.nativeElement; |
| 90 | + |
| 91 | + // rapropos 2019.11.02 |
| 92 | + // the difference between the old component and directive was boundedness |
| 93 | + // and the presence of the 'data-mdc-ripple-is-unbounded' attribute. we |
| 94 | + // recreate that here by checking our tag name. if it is `MDC-RIPPLE', |
| 95 | + // then we are acting as the component did. otherwise, we act as the directive |
| 96 | + if (this._root.tagName !== 'MDC-RIPPLE') { |
| 97 | + this.unbounded = true; |
| 98 | + this._root.setAttribute('data-mdc-ripple-is-unbounded', ''); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + ngAfterViewInit(): void { |
| 103 | + this._ripple = new MdcRipple(this.elementRef); |
| 104 | + this._ripple.init(); |
| 105 | + } |
| 106 | + |
| 107 | + ngOnDestroy(): void { |
| 108 | + this.ripple.destroy(); |
| 109 | + } |
112 | 110 | }
|
| 111 | + |
0 commit comments