|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | +import {ElementRef, OnChanges, OnDestroy, SimpleChanges} from '@angular/core'; |
| 9 | +import {Subject} from 'rxjs'; |
| 10 | + |
| 11 | +import {StyleDefinition, StyleUtils} from '../style-utils/style-utils'; |
| 12 | +import {StyleBuilder} from '../style-builder/style-builder'; |
| 13 | +import {MediaMarshaller} from '../media-marshaller/media-marshaller'; |
| 14 | +import {buildLayoutCSS} from '../../utils/layout-validator'; |
| 15 | + |
| 16 | +export abstract class BaseDirective2 implements OnChanges, OnDestroy { |
| 17 | + |
| 18 | + protected DIRECTIVE_KEY = ''; |
| 19 | + protected inputs: string[] = []; |
| 20 | + protected destroySubject: Subject<void> = new Subject(); |
| 21 | + |
| 22 | + /** Access to host element's parent DOM node */ |
| 23 | + protected get parentElement(): HTMLElement | null { |
| 24 | + return this.elementRef.nativeElement.parentElement; |
| 25 | + } |
| 26 | + |
| 27 | + /** Access to the HTMLElement for the directive */ |
| 28 | + protected get nativeElement(): HTMLElement { |
| 29 | + return this.elementRef.nativeElement; |
| 30 | + } |
| 31 | + |
| 32 | + /** Access to the activated value for the directive */ |
| 33 | + get activatedValue(): string { |
| 34 | + return this.marshal.getValue(this.nativeElement, this.DIRECTIVE_KEY); |
| 35 | + } |
| 36 | + set activatedValue(value: string) { |
| 37 | + this.marshal.setValue(this.nativeElement, this.DIRECTIVE_KEY, value, |
| 38 | + this.marshal.activatedBreakpoint); |
| 39 | + } |
| 40 | + |
| 41 | + /** Cache map for style computation */ |
| 42 | + protected styleCache: Map<string, StyleDefinition> = new Map(); |
| 43 | + |
| 44 | + protected constructor(protected elementRef: ElementRef, |
| 45 | + protected styleBuilder: StyleBuilder, |
| 46 | + protected styler: StyleUtils, |
| 47 | + protected marshal: MediaMarshaller) { |
| 48 | + } |
| 49 | + |
| 50 | + /** For @Input changes */ |
| 51 | + ngOnChanges(changes: SimpleChanges) { |
| 52 | + Object.keys(changes).forEach(key => { |
| 53 | + if (this.inputs.indexOf(key) !== -1) { |
| 54 | + const bp = key.split('.')[1] || ''; |
| 55 | + const val = changes[key].currentValue; |
| 56 | + this.setValue(val, bp); |
| 57 | + } |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + ngOnDestroy(): void { |
| 62 | + this.destroySubject.next(); |
| 63 | + this.destroySubject.complete(); |
| 64 | + this.marshal.releaseElement(this.nativeElement); |
| 65 | + } |
| 66 | + |
| 67 | + /** Add styles to the element using predefined style builder */ |
| 68 | + protected addStyles(input: string, parent?: Object) { |
| 69 | + const builder = this.styleBuilder; |
| 70 | + const useCache = builder.shouldCache; |
| 71 | + |
| 72 | + let genStyles: StyleDefinition | undefined = this.styleCache.get(input); |
| 73 | + |
| 74 | + if (!genStyles || !useCache) { |
| 75 | + genStyles = builder.buildStyles(input, parent); |
| 76 | + if (useCache) { |
| 77 | + this.styleCache.set(input, genStyles); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + this.applyStyleToElement(genStyles); |
| 82 | + builder.sideEffect(input, genStyles, parent); |
| 83 | + } |
| 84 | + |
| 85 | + protected triggerUpdate() { |
| 86 | + const val = this.marshal.getValue(this.nativeElement, this.DIRECTIVE_KEY); |
| 87 | + this.marshal.updateElement(this.nativeElement, this.DIRECTIVE_KEY, val); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Determine the DOM element's Flexbox flow (flex-direction). |
| 92 | + * |
| 93 | + * Check inline style first then check computed (stylesheet) style. |
| 94 | + * And optionally add the flow value to element's inline style. |
| 95 | + */ |
| 96 | + protected getFlexFlowDirection(target: HTMLElement, addIfMissing = false): string { |
| 97 | + if (target) { |
| 98 | + const [value, hasInlineValue] = this.styler.getFlowDirection(target); |
| 99 | + |
| 100 | + if (!hasInlineValue && addIfMissing) { |
| 101 | + const style = buildLayoutCSS(value); |
| 102 | + const elements = [target]; |
| 103 | + this.styler.applyStyleToElements(style, elements); |
| 104 | + } |
| 105 | + |
| 106 | + return value.trim(); |
| 107 | + } |
| 108 | + |
| 109 | + return 'row'; |
| 110 | + } |
| 111 | + |
| 112 | + /** Applies styles given via string pair or object map to the directive element */ |
| 113 | + protected applyStyleToElement(style: StyleDefinition, |
| 114 | + value?: string | number, |
| 115 | + element: HTMLElement = this.nativeElement) { |
| 116 | + this.styler.applyStyleToElement(element, style, value); |
| 117 | + } |
| 118 | + |
| 119 | + protected setValue(val: any, bp: string): void { |
| 120 | + this.marshal.setValue(this.nativeElement, this.DIRECTIVE_KEY, val, bp); |
| 121 | + } |
| 122 | +} |
0 commit comments