Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(module:dropdown): improve performance #148

Merged
merged 1 commit into from
Aug 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions src/components/dropdown/nz-dropdown-button.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,21 @@ export class NzDropDownButtonComponent extends NzDropDownComponent implements On
@Output() nzClick = new EventEmitter();
@ViewChild(NzDropDownDirective) _nzOrigin;

ngOnInit() {
debounceTime.call(this._$mouseSubject, 300).subscribe((data: boolean) => {
if (this.nzDisable) {
return;
_onVisibleChange = (visible: boolean) => {
if (this.nzDisable) {
return;
}
if (visible) {
if (!this._triggerWidth) {
this._setTriggerWidth();
}
this.nzVisible = data;
if (this.nzVisible) {
if (!this._triggerWidth) {
this._setTriggerWidth();
}
}
this.nzVisibleChange.emit(this.nzVisible);
});
this._nzMenu.setDropDown(true);
}
this.nzVisible = visible;
this._changeDetector.markForCheck();
}

/** rewrite afterViewInit hook */
ngAfterViewInit() {

this._startSubscribe(this.nzVisibleChange.asObservable());
}
}
81 changes: 57 additions & 24 deletions src/components/dropdown/nz-dropdown.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ import {
Renderer2,
ContentChild,
Output,
EventEmitter, AfterViewInit
EventEmitter,
AfterViewInit,
ChangeDetectionStrategy,
ChangeDetectorRef
} from '@angular/core';
import { Subject } from 'rxjs/Subject';
import { merge } from 'rxjs/observable/merge';
import { debounceTime } from 'rxjs/operator/debounceTime';
import { Observable } from 'rxjs/Observable';
import { Subscription } from 'rxjs/Subscription';
import { Observer } from 'rxjs/Observer'
import { NzMenuComponent } from '../menu/nz-menu.component';
import { DropDownAnimation } from '../core/animation/dropdown-animations';
import { NzDropDownDirective } from './nz-dropdown.directive';
Expand All @@ -25,6 +31,7 @@ export type NzPlacement = 'bottomLeft' | 'bottomCenter' | 'bottomRight' | 'topLe
animations : [
DropDownAnimation
],
changeDetection: ChangeDetectionStrategy.OnPush,
template : `
<div>
<ng-content></ng-content>
Expand Down Expand Up @@ -57,10 +64,10 @@ export type NzPlacement = 'bottomLeft' | 'bottomCenter' | 'bottomRight' | 'topLe

export class NzDropDownComponent implements OnInit, OnDestroy, AfterViewInit {
_triggerWidth = 0;
_$mouseSubject = new Subject();
_placement: NzPlacement = 'bottomLeft';
_dropDownPosition: 'top' | 'bottom' = 'bottom';
_positions: ConnectionPositionPair[] = [ ...DEFAULT_DROPDOWN_POSITIONS ];
_subscription: Subscription;
@ContentChild(NzDropDownDirective) _nzOrigin;
@ContentChild(NzMenuComponent) _nzMenu;
@Input() nzTrigger: 'click' | 'hover' = 'hover';
Expand Down Expand Up @@ -97,6 +104,14 @@ export class NzDropDownComponent implements OnInit, OnDestroy, AfterViewInit {
}
}

_hide() {
this.nzVisibleChange.emit(false);
}

_show() {
this.nzVisibleChange.emit(true);
}

_onPositionChange(position) {
this._dropDownPosition = position.connectionPair.originY;
}
Expand All @@ -112,49 +127,67 @@ export class NzDropDownComponent implements OnInit, OnDestroy, AfterViewInit {
this._triggerWidth = this._nzOrigin.elementRef.nativeElement.getBoundingClientRect().width;
}

_show() {
this._$mouseSubject.next(true);
_onVisibleChange = (visible: boolean) => {
if (visible) {
if (!this._triggerWidth) {
this._setTriggerWidth();
}
}
this.nzVisible = visible;
this._changeDetector.markForCheck();
}

_hide() {
this._$mouseSubject.next(false);
_startSubscribe(observable$: Observable<boolean>) {
this._subscription = observable$
.subscribe(this._onVisibleChange)
}

ngOnInit() {
debounceTime.call(this._$mouseSubject, 300).subscribe((data: boolean) => {
this.nzVisible = data;
if (this.nzVisible) {
if (!this._triggerWidth) {
this._setTriggerWidth();
}
}
this.nzVisibleChange.emit(this.nzVisible);
});
this._nzMenu.setDropDown(true);
}

ngOnDestroy() {
this._$mouseSubject.unsubscribe();
this._subscription.unsubscribe();
}


ngAfterViewInit() {
let mouse$: Observable<boolean>
if (this.nzTrigger === 'hover') {
this._renderer.listen(this._nzOrigin.elementRef.nativeElement, 'mouseenter', () => this._show());
this._renderer.listen(this._nzOrigin.elementRef.nativeElement, 'mouseleave', () => this._hide());
mouse$ = Observable.create((observer: Observer<boolean>) => {
const disposeMouseEnter = this._renderer.listen(this._nzOrigin.elementRef.nativeElement, 'mouseenter', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should check whether will fire ngzone. although this component will not check

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zone propagation for RxJS has been fixed on Zone.js side already.

observer.next(true);
});
const disposeMouseLeave = this._renderer.listen(this._nzOrigin.elementRef.nativeElement, 'mouseleave', () => {
observer.next(false);
});
return () => {
disposeMouseEnter();
disposeMouseLeave();
}
});
}
if (this.nzTrigger === 'click') {
this._renderer.listen(this._nzOrigin.elementRef.nativeElement, 'click', (e) => {
e.preventDefault();
this._show()
mouse$ = Observable.create((observer: Observer<boolean>) => {
const dispose = this._renderer.listen(this._nzOrigin.elementRef.nativeElement, 'click', (e) => {
e.preventDefault();
observer.next(true);
});
return () => dispose();
});
}
const observable$ = debounceTime.call(
merge(
mouse$,
this.nzVisibleChange.asObservable()
)
, 300);
this._startSubscribe(observable$);
}

get _hasBackdrop() {
return this.nzTrigger === 'click';
}

constructor(private _renderer: Renderer2) {
constructor(private _renderer: Renderer2, protected _changeDetector: ChangeDetectorRef) {
}
}