|
| 1 | +import {Renderer2, RendererStyleFlags2} from '@angular/core'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Adapts the 'deprecated' Angular Renderer v1 API to use the new Renderer2 instance |
| 5 | + * This is required for older versions of NgStyle and NgClass that require |
| 6 | + * the v1 API (but should use the v2 instances) |
| 7 | + */ |
| 8 | +export class RendererAdapter { |
| 9 | + constructor(private _renderer: Renderer2) { } |
| 10 | + |
| 11 | + setElementClass(el: any, className: string, isAdd: boolean): void { |
| 12 | + if (isAdd) { |
| 13 | + this._renderer.addClass(el, className); |
| 14 | + } else { |
| 15 | + this._renderer.removeClass(el, className); |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + setElementStyle(el: any, styleName: string, styleValue: string): void { |
| 20 | + if (styleValue) { |
| 21 | + this._renderer.setStyle(el, styleName, styleValue); |
| 22 | + } else { |
| 23 | + this._renderer.removeStyle(el, styleName); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + // new API is forwarded |
| 28 | + addClass(el: any, name: string): void { |
| 29 | + this._renderer.addClass(el, name); |
| 30 | + } |
| 31 | + |
| 32 | + removeClass(el: any, name: string): void { |
| 33 | + this._renderer.removeClass(el, name); |
| 34 | + } |
| 35 | + |
| 36 | + setStyle(el: any, style: string, value: any, flags?: RendererStyleFlags2): void { |
| 37 | + this._renderer.setStyle(el, style, value, flags); |
| 38 | + } |
| 39 | + |
| 40 | + removeStyle(el: any, style: string, flags?: RendererStyleFlags2): void { |
| 41 | + this._renderer.removeStyle(el, style, flags); |
| 42 | + } |
| 43 | + |
| 44 | + // ****************************************************************** |
| 45 | + // !! Renderer is an abstract class with abstract methods |
| 46 | + // |
| 47 | + // These are implementation of those methods... and do NOTHING since |
| 48 | + // we only use setElementStyle() and setElementClass() |
| 49 | + // ****************************************************************** |
| 50 | + |
| 51 | + /* tslint:disable */ |
| 52 | + animate() : any { throw _notImplemented('animate'); } |
| 53 | + attachViewAfter() : void { throw _notImplemented('attachViewAfter'); } |
| 54 | + detachView() : void { throw _notImplemented('detachView'); } |
| 55 | + destroyView() : void { throw _notImplemented('destroyView'); } |
| 56 | + createElement() : any { throw _notImplemented('createElement'); } |
| 57 | + createViewRoot() : any { throw _notImplemented('createViewRoot'); } |
| 58 | + createTemplateAnchor(): any { throw _notImplemented('createTemplateAnchor'); } |
| 59 | + createText() : any { throw _notImplemented('createText'); } |
| 60 | + invokeElementMethod() : void { throw _notImplemented('invokeElementMethod'); } |
| 61 | + projectNodes() : void { throw _notImplemented('projectNodes'); } |
| 62 | + selectRootElement() : any { throw _notImplemented('selectRootElement'); } |
| 63 | + setBindingDebugInfo() : void { throw _notImplemented('setBindingDebugInfo'); } |
| 64 | + setElementProperty() : void { throw _notImplemented('setElementProperty'); } |
| 65 | + setElementAttribute() : void { throw _notImplemented('setElementAttribute'); } |
| 66 | + setText() : void { throw _notImplemented('setText'); } |
| 67 | + listen() : Function { throw _notImplemented('listen'); } |
| 68 | + listenGlobal() : Function { throw _notImplemented('listenGlobal'); } |
| 69 | + /* tslint:enable */ |
| 70 | +} |
| 71 | + |
| 72 | +function _notImplemented(methodName: string) { |
| 73 | + return new Error(`The method RendererAdapter::${methodName}() has not been implemented`); |
| 74 | +} |
0 commit comments