Skip to content

Commit 2ab6044

Browse files
authored
Merge pull request #4255 from IgniteUI/mvenkov/autoscrollstrategy-outisde-ngzone
Autoscrollstrategy scroll outside NgZone
2 parents 3f2f7e0 + 11a6daa commit 2ab6044

File tree

8 files changed

+49
-10
lines changed

8 files changed

+49
-10
lines changed

projects/igniteui-angular/src/lib/services/overlay/overlay.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import {
2222
Injector,
2323
Type,
2424
OnDestroy,
25-
NgModuleRef
25+
NgModuleRef,
26+
NgZone
2627
} from '@angular/core';
2728
import { AnimationBuilder, AnimationReferenceMetadata, AnimationMetadataType, AnimationAnimateRefMetadata } from '@angular/animations';
2829
import { fromEvent, Subject } from 'rxjs';
@@ -106,7 +107,8 @@ export class IgxOverlayService implements OnDestroy {
106107
private _appRef: ApplicationRef,
107108
private _injector: Injector,
108109
private builder: AnimationBuilder,
109-
@Inject(DOCUMENT) private document: any) {
110+
@Inject(DOCUMENT) private document: any,
111+
private _zone: NgZone) {
110112
this._document = <Document>this.document;
111113
}
112114

@@ -116,7 +118,7 @@ export class IgxOverlayService implements OnDestroy {
116118
* @param settings Display settings for the overlay, such as positioning and scroll/close behavior.
117119
* @returns Id of the created overlay. Valid until `onClosed` is emitted.
118120
*/
119-
attach(element: ElementRef , settings?: OverlaySettings): string;
121+
attach(element: ElementRef, settings?: OverlaySettings): string;
120122
/**
121123
* Generates Id. Provide this Id when call `show(id, settings?)` method
122124
* @param component Component Type to show in overlay
@@ -329,7 +331,7 @@ export class IgxOverlayService implements OnDestroy {
329331
}
330332

331333
private getOverlayInfo(component: any, moduleRef?: NgModuleRef<any>): OverlayInfo {
332-
const info: OverlayInfo = {};
334+
const info: OverlayInfo = { ngZone: this._zone };
333335
if (component instanceof ElementRef) {
334336
info.elementRef = <ElementRef>component;
335337
} else {

projects/igniteui-angular/src/lib/services/overlay/scroll/IScrollStrategy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export interface IScrollStrategy {
77
/**
88
* Initializes the strategy. Should be called once
99
* @param document reference to Document object.
10-
* @param overlayService IgxOverlay service to use in this strategy
10+
* @param overlayService IgxOverlay service to use in this strategy.
1111
* @param id Unique id for this strategy.
1212
* ```typescript
1313
* settings.scrollStrategy.initialize(document, overlay, id);

projects/igniteui-angular/src/lib/services/overlay/scroll/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ import { NoOpScrollStrategy } from "./scroll/NoOpScrollStrategy";
3434

3535
| Name | Description | Parameters |
3636
|-----------------|---------------------------------------------------------------------------------|------------|
37-
|initialize | Initialize the strategy. Should be called once |document, overlayService, id|
37+
|initialize | Initialize the strategy. Should be called once |document, overlayService, id|
3838
|attach | Attaches the strategy |- |
3939
|detach | Detaches the strategy |- |

projects/igniteui-angular/src/lib/services/overlay/scroll/absolute-scroll-strategy.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { IgxOverlayService } from '../overlay';
22
import { ScrollStrategy } from './scroll-strategy';
3+
import { NgZone } from '@angular/core';
34

45
/**
56
* On scroll reposition the overlay content.
@@ -10,6 +11,7 @@ export class AbsoluteScrollStrategy extends ScrollStrategy {
1011
private _overlayService: IgxOverlayService;
1112
private _id: string;
1213
private _scrollContainer: HTMLElement;
14+
private _zone: NgZone;
1315

1416
constructor(scrollContainer?: HTMLElement) {
1517
super(scrollContainer);
@@ -24,15 +26,18 @@ export class AbsoluteScrollStrategy extends ScrollStrategy {
2426
this._overlayService = overlayService;
2527
this._id = id;
2628
this._document = document;
29+
this._zone = overlayService.getOverlayById(id).ngZone;
2730
this._initialized = true;
2831
}
2932

3033
/** @inheritdoc */
3134
public attach(): void {
32-
if (this._scrollContainer) {
33-
this._scrollContainer.addEventListener('scroll', this.onScroll, true);
35+
if (this._zone) {
36+
this._zone.runOutsideAngular(() => {
37+
this.addScrollEventListener();
38+
});
3439
} else {
35-
this._document.addEventListener('scroll', this.onScroll, true);
40+
this.addScrollEventListener();
3641
}
3742
}
3843

@@ -47,6 +52,14 @@ export class AbsoluteScrollStrategy extends ScrollStrategy {
4752
this._initialized = false;
4853
}
4954

55+
private addScrollEventListener() {
56+
if (this._scrollContainer) {
57+
this._scrollContainer.addEventListener('scroll', this.onScroll, true);
58+
} else {
59+
this._document.addEventListener('scroll', this.onScroll, true);
60+
}
61+
}
62+
5063
private onScroll = () => {
5164
this._overlayService.repositionAll();
5265
}

projects/igniteui-angular/src/lib/services/overlay/utilities.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { IPositionStrategy } from './position/IPositionStrategy';
22

33
import { IScrollStrategy } from './scroll';
44
import { AnimationReferenceMetadata, AnimationPlayer } from '@angular/animations';
5-
import { ComponentRef, ElementRef } from '@angular/core';
5+
import { ComponentRef, ElementRef, NgZone } from '@angular/core';
66
import { IgxOverlayOutletDirective } from '../../directives/toggle/toggle.directive';
77
import { CancelableEventArgs, CancelableBrowserEventArgs, cloneValue } from '../../core/utils';
88

@@ -116,6 +116,7 @@ export interface OverlayInfo {
116116
closeAnimationPlayer?: AnimationPlayer;
117117
openAnimationInnerPlayer?: any;
118118
closeAnimationInnerPlayer?: any;
119+
ngZone: NgZone;
119120
}
120121

121122
/** @hidden @internal */

src/app/overlay/overlay.sample.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
.sample-content {
2+
margin-bottom: 500px;
3+
}
4+
15
.radio-group {
26
display: flex;
37
flex-flow: column nowrap;
@@ -30,4 +34,10 @@
3034
margin: 20px;
3135
padding: 0 20px 20px 20px;
3236
background-color: #444444;
37+
}
38+
39+
.outlet {
40+
width: 600px;
41+
height: 500px;
42+
perspective: 1px;
3343
}

src/app/overlay/overlay.sample.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@
5858
<igx-switch class="settings" [(ngModel)]="modal" name="modal" [value]="modal"
5959
(change)="onSwitchChange($event)">Modal</igx-switch>
6060
</div>
61+
<div>
62+
<igx-switch class="settings" [(ngModel)]="useOutlet" name="outlet" [value]="useOutlet"
63+
(change)="onSwitchChange($event)">Open in outlet</igx-switch>
64+
</div>
65+
</div>
66+
<div #outlet class="outlet column">
67+
Outlet
6168
</div>
6269
<button #button igxButton="raised" igxRipple igxDrag (click)="toggleDropDown()" [style.position]="'absolute'"
6370
(dragEnd)="onDragEnd($event)" (dragStart)="onDragStart($event)">Toggle</button>

src/app/overlay/overlay.sample.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ export class OverlaySampleComponent implements OnInit {
6161

6262
closeOnOutsideClick = true;
6363
modal = true;
64+
useOutlet = false;
6465

6566
@ViewChild(IgxDropDownComponent) public igxDropDown: IgxDropDownComponent;
6667
@ViewChild('button') public button: ElementRef;
6768
@ViewChild('container') public container: ElementRef;
6869
@ViewChild(IgxDragDirective) public igxDrag: IgxDragDirective;
70+
@ViewChild('outlet') public outletElement: ElementRef;
6971

7072
onChange(ev) {
7173
switch (ev.radio.name) {
@@ -210,6 +212,7 @@ export class OverlaySampleComponent implements OnInit {
210212
stringMapping['HorizontalDirection'][this.horizontalDirection];
211213
this._overlaySettings.positionStrategy.settings.horizontalStartPoint =
212214
stringMapping['HorizontalStartPoint'][this.horizontalStartPoint];
215+
this._overlaySettings.outlet = this.useOutlet ? this.outletElement : null;
213216
}
214217

215218
onSwitchChange(ev) {
@@ -220,6 +223,9 @@ export class OverlaySampleComponent implements OnInit {
220223
case 'modal':
221224
this._overlaySettings.modal = ev.checked;
222225
break;
226+
case 'outlet':
227+
this._overlaySettings.outlet = ev.checked ? this.outletElement : null;
228+
break;
223229
}
224230
}
225231

0 commit comments

Comments
 (0)