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:back-top): refactor back-top #2547

Merged
merged 4 commits into from
Nov 30, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 12 additions & 0 deletions components/back-top/back-top.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,18 @@ describe('Component:nz-back-top', () => {

expect(componentObject.backTopButton() === null).toBe(false);
}));

it('element (use string id) scroll shows the button', fakeAsync(() => {
component.nzTarget = '#fakeTarget';

const throttleTime = 50;

componentObject.scrollTo(fakeTarget, defaultVisibilityHeight + 1);
tick(throttleTime + 1);
fixture.detectChanges();

expect(componentObject.backTopButton() === null).toBe(false);
}));
});

describe('#nzTemplate', () => {
Expand Down
4 changes: 2 additions & 2 deletions components/back-top/doc/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ title: BackTop
| Property | Description | Type | Default |
| --- | --- | --- | --- |
| `[nzTemplate]` | custom content | `TemplateRef<void>` | - |
| `[nzVisibilityHeight]` | the `nz-back-top` button will not show until the scroll height reaches this value | number | `400` |
| `[nzTarget]` | specifies the scrollable area dom node | Element | `window` |
| `[nzVisibilityHeight]` | the `nz-back-top` button will not show until the scroll height reaches this value | `number` | `400` |
| `[nzTarget]` | specifies the scrollable area dom node | `string, Element` | `window` |
| `(nzClick)` | a callback function, which can be executed when you click the button | `EventEmitter<boolean>` | - |

4 changes: 2 additions & 2 deletions components/back-top/doc/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ title: BackTop
| 成员 | 说明 | 类型 | 默认值 |
| --- | --- | --- | --- |
| `[nzTemplate]` | 自定义内容,见示例 | `TemplateRef<void>` | - |
| `[nzVisibilityHeight]` | 滚动高度达到此参数值才出现 `nz-back-top` | number | `400` |
| `[nzTarget]` | 设置需要监听其滚动事件的元素,值为一个返回对应 DOM 元素的函数 | Element | `window` |
| `[nzVisibilityHeight]` | 滚动高度达到此参数值才出现 `nz-back-top` | `number` | `400` |
| `[nzTarget]` | 设置需要监听其滚动事件的元素,值为一个返回对应 DOM 元素的函数 | `string, Element` | `window` |
| `(nzClick)` | 点击按钮的回调函数 | `EventEmitter<boolean>` | - |
24 changes: 13 additions & 11 deletions components/back-top/nz-back-top.component.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import {
animate,
style,
transition,
trigger
} from '@angular/animations';
import { DOCUMENT } from '@angular/common';
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
EventEmitter,
Inject,
Input,
OnDestroy,
OnInit,
Output,
TemplateRef
} from '@angular/core';

import {
animate,
style,
transition,
trigger
} from '@angular/animations';

import { fromEvent, Subscription } from 'rxjs';
import { distinctUntilChanged, throttleTime } from 'rxjs/operators';

Expand Down Expand Up @@ -62,14 +63,15 @@ export class NzBackTopComponent implements OnInit, OnDestroy {
}

@Input()
set nzTarget(el: HTMLElement) {
this.target = el;
set nzTarget(el: string | HTMLElement) {
this.target = typeof el === 'string' ? this.doc.querySelector(el) : el;
this.registerScrollEvent();
}

@Output() readonly nzClick: EventEmitter<boolean> = new EventEmitter();

constructor(private scrollSrv: NzScrollService, private cd: ChangeDetectorRef) {
// tslint:disable-next-line:no-any
constructor(private scrollSrv: NzScrollService, @Inject(DOCUMENT) private doc: any, private cd: ChangeDetectorRef) {
}

ngOnInit(): void {
Expand Down Expand Up @@ -105,7 +107,7 @@ export class NzBackTopComponent implements OnInit, OnDestroy {
this.removeListen();
this.handleScroll();
this.scroll$ = fromEvent(this.getTarget(), 'scroll').pipe(throttleTime(50), distinctUntilChanged())
.subscribe(e => this.handleScroll());
.subscribe(() => this.handleScroll());
}

ngOnDestroy(): void {
Expand Down