Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit c175adb

Browse files
fix(api): use only Renderer2 instances
NgClass and NgStyle use Renderer1 instances; which has been deprecated. Instead use Renderer2 and a RendererAdapter. Thx @mhevery for his solution! Refs #386.
1 parent 8b8b595 commit c175adb

File tree

3 files changed

+82
-6
lines changed

3 files changed

+82
-6
lines changed

src/lib/api/core/renderer-adapter.ts

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
}

src/lib/api/ext/class.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
OnChanges,
1616
OnDestroy,
1717
Optional,
18-
Renderer,
1918
Renderer2,
2019
SimpleChanges,
2120
Self
@@ -26,6 +25,7 @@ import {BaseFxDirective} from '../core/base';
2625
import {BaseFxDirectiveAdapter} from '../core/base-adapter';
2726
import {MediaChange} from '../../media-query/media-change';
2827
import {MediaMonitor} from '../../media-query/media-monitor';
28+
import {RendererAdapter} from '../core/renderer-adapter';
2929

3030
/** NgClass allowed inputs **/
3131
export type NgClassType = string | string[] | Set<string> | {[klass: string]: any};
@@ -115,7 +115,7 @@ export class ClassDirective extends BaseFxDirective
115115

116116
/* tslint:enable */
117117
constructor(protected monitor: MediaMonitor,
118-
_ngEl: ElementRef, _renderer: Renderer2, _oldRenderer: Renderer,
118+
_ngEl: ElementRef, _renderer: Renderer2,
119119
_iterableDiffers: IterableDiffers, _keyValueDiffers: KeyValueDiffers,
120120
@Optional() @Self() private _ngClassInstance: NgClass) {
121121

@@ -127,7 +127,8 @@ export class ClassDirective extends BaseFxDirective
127127
// Create an instance NgClass Directive instance only if `ngClass=""` has NOT been defined on
128128
// the same host element; since the responsive variations may be defined...
129129
if ( !this._ngClassInstance ) {
130-
this._ngClassInstance = new NgClass(_iterableDiffers, _keyValueDiffers, _ngEl, _oldRenderer);
130+
let adapter = new RendererAdapter(_renderer);
131+
this._ngClassInstance = new NgClass(_iterableDiffers, _keyValueDiffers, _ngEl, <any> adapter);
131132
}
132133
}
133134

src/lib/api/ext/style.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
OnDestroy,
1515
OnChanges,
1616
Optional,
17-
Renderer,
1817
Renderer2,
1918
SecurityContext,
2019
Self,
@@ -35,6 +34,7 @@ import {
3534
NgStyleSanitizer,
3635
ngStyleUtils as _
3736
} from '../../utils/style-transforms';
37+
import {RendererAdapter} from '../core/renderer-adapter';
3838

3939
/**
4040
* Directive to add responsive support for ngStyle.
@@ -106,7 +106,7 @@ export class StyleDirective extends BaseFxDirective
106106
constructor(private monitor: MediaMonitor,
107107
protected _sanitizer: DomSanitizer,
108108
_ngEl: ElementRef, _renderer: Renderer2,
109-
_differs: KeyValueDiffers, _oldRenderer: Renderer,
109+
_differs: KeyValueDiffers,
110110
@Optional() @Self() private _ngStyleInstance: NgStyle) {
111111

112112
super(monitor, _ngEl, _renderer);
@@ -118,7 +118,8 @@ export class StyleDirective extends BaseFxDirective
118118
// Create an instance NgStyle Directive instance only if `ngStyle=""` has NOT been defined on
119119
// the same host element; since the responsive versions may be defined...
120120
if ( !this._ngStyleInstance ) {
121-
this._ngStyleInstance = new NgStyle(_differs, _ngEl, _oldRenderer);
121+
let adapter = new RendererAdapter(_renderer);
122+
this._ngStyleInstance = new NgStyle(_differs, _ngEl, <any> adapter);
122123
}
123124
}
124125

0 commit comments

Comments
 (0)