|
| 1 | +import {NgModule, ModuleWithProviders, Directive, Renderer, ElementRef} from '@angular/core'; |
| 2 | + |
| 3 | + |
| 4 | +/** Selector that matches all elements that may have style collisions with material1. */ |
| 5 | +export const ELEMENTS_SELECTOR = ` |
| 6 | + md-card, |
| 7 | + md-card-actions, |
| 8 | + md-card-content, |
| 9 | + md-card-footer, |
| 10 | + md-card-header, |
| 11 | + md-card-subtitle, |
| 12 | + md-card-title, |
| 13 | + md-card-title-group, |
| 14 | + md-checkbox, |
| 15 | + md-dialog-container, |
| 16 | + md-divider, |
| 17 | + md-grid-list, |
| 18 | + md-grid-tile, |
| 19 | + md-grid-tile-footer, |
| 20 | + md-grid-tile-header, |
| 21 | + md-hint, |
| 22 | + md-icon, |
| 23 | + md-ink-bar, |
| 24 | + md-input, |
| 25 | + md-list, |
| 26 | + md-list-item, |
| 27 | + md-menu, |
| 28 | + md-nav-list, |
| 29 | + md-placeholder, |
| 30 | + md-progress-bar, |
| 31 | + md-progress-circle, |
| 32 | + md-radio-button, |
| 33 | + md-radio-group, |
| 34 | + md-select, |
| 35 | + md-sidenav, |
| 36 | + md-slider, |
| 37 | + md-spinner, |
| 38 | + md-tab, |
| 39 | + md-toolbar |
| 40 | +`; |
| 41 | + |
| 42 | +/** |
| 43 | + * Directive to apply to all material2 components that use the same element name as a |
| 44 | + * component in material2. It does two things: |
| 45 | + * 1) Adds the css class "md2" to the host element so that material1 can use this class with a |
| 46 | + * :not() in order to avoid affecting material2 elements. |
| 47 | + * 2) Adds a css class to the element that is identical to the element's tag. E.g., the element |
| 48 | + * `<md-card>` would be given a css class `md-card`. This is done so that material2 can style |
| 49 | + * only these classes instead of defining element rules that would affect material1 components. |
| 50 | + */ |
| 51 | +@Directive({ |
| 52 | + selector: ELEMENTS_SELECTOR, |
| 53 | +}) |
| 54 | +export class StyleCompatibility { |
| 55 | + constructor(renderer: Renderer, elementRef: ElementRef) { |
| 56 | + const element = elementRef.nativeElement as Node; |
| 57 | + renderer.setElementClass(element, 'md2', true); |
| 58 | + renderer.setElementClass(element, element.nodeName.toLowerCase(), true); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | +@NgModule({ |
| 64 | + declarations: [StyleCompatibility], |
| 65 | + exports: [StyleCompatibility], |
| 66 | +}) |
| 67 | +export class StyleCompatibilityModule { |
| 68 | + static forRoot(): ModuleWithProviders { |
| 69 | + return { |
| 70 | + ngModule: StyleCompatibilityModule, |
| 71 | + providers: [], |
| 72 | + }; |
| 73 | + } |
| 74 | +} |
0 commit comments