Skip to content

Commit

Permalink
feat(module:drawer): support service to create drawer (NG-ZORRO#1981)
Browse files Browse the repository at this point in the history
  • Loading branch information
hsuanxyz authored and vthinkxie committed Sep 21, 2018
1 parent 0e12355 commit ad0c5cf
Show file tree
Hide file tree
Showing 16 changed files with 599 additions and 37 deletions.
6 changes: 5 additions & 1 deletion components/core/overlay/scroll/nz-block-scroll-strategy.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import { ScrollStrategy } from '@angular/cdk/overlay';
import { Renderer2 } from '@angular/core';
import { NzMeasureScrollbarService } from '../../services/nz-measure-scrollbar.service'

export class NzBlockScrollStrategy implements ScrollStrategy {

constructor(private document: Document, private renderer: Renderer2) {
constructor(private document: Document, private renderer: Renderer2, private nzMeasureScrollbarService: NzMeasureScrollbarService) {
}

attach(): void {}

enable(): void {
this.renderer.setStyle(document.body, 'overflow', 'hidden');
this.renderer.setStyle(this.document.body, 'padding-right', `${this.nzMeasureScrollbarService.scrollBarWidth}px`);

}

disable(): void {
this.renderer.removeStyle(document.body, 'overflow');
this.renderer.removeStyle(document.body, 'padding-right');
}

}
4 changes: 3 additions & 1 deletion components/core/overlay/scroll/nz-scroll-strategy-options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DOCUMENT } from '@angular/common';
import { Inject, Injectable, Renderer2, RendererFactory2 } from '@angular/core';
import { NzMeasureScrollbarService } from '../../services/nz-measure-scrollbar.service'
import { NzBlockScrollStrategy } from './nz-block-scroll-strategy';

@Injectable({providedIn: 'root'})
Expand All @@ -8,12 +9,13 @@ export class NzScrollStrategyOptions {
private renderer: Renderer2;
constructor(
rendererFactory: RendererFactory2,
private nzMeasureScrollbarService: NzMeasureScrollbarService,
// tslint:disable-next-line:no-any
@Inject(DOCUMENT) document: any
) {
this.document = document;
this.renderer = rendererFactory.createRenderer(null, null);
}

block = () => new NzBlockScrollStrategy(this.document, this.renderer);
block = () => new NzBlockScrollStrategy(this.document, this.renderer, this.nzMeasureScrollbarService);
}
2 changes: 1 addition & 1 deletion components/core/services/nz-measure-scrollbar.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Inject, Injectable } from '@angular/core';
import { isNotNil } from '../util/check';

@Injectable({
providedIn: 'root',
providedIn: 'root'
})
export class NzMeasureScrollbarService {
private _scrollbarWidth: number;
Expand Down
18 changes: 18 additions & 0 deletions components/drawer/demo/service.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
order: 5
title:
zh-CN: 服务方式创建
en-US: Drawer's service
---

## zh-CN

Drawer 的 service 用法,示例中演示了用户自定义模板、自定义component。

> **注意** 如果使用Component模式,则需要在NgModule中的 `declarations``entryComponents` 加入自定义的Component
## en-US

Usage of Drawer's service, examples demonstrate user-defined templates, custom components.

> **NOTE** If you use Component mode, you need to add your custom Component into `declarations` and `entryComponents` for a `NgModule`
98 changes: 98 additions & 0 deletions components/drawer/demo/service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* entryComponents: NzDrawerCustomComponent */

import { Component, Input, ViewChild } from '@angular/core';
import { NzDrawerRef, NzDrawerService } from 'ng-zorro-antd';

@Component({
selector: 'nz-demo-drawer-service',
template: `
<ng-template #drawerTemplate let-data let-drawerRef="drawerRef">
value: {{data?.value}}
<br>
<button nz-button nzType="primary" (click)="drawerRef.close()">close</button>
</ng-template>
<div nz-form>
<nz-form-item>
<input nz-input [(ngModel)]="value">
</nz-form-item>
</div>
<button nz-button nzType="primary" (click)="openTemplate()">Use Template</button>&nbsp;
<button nz-button nzType="primary" (click)="openComponent()">Use Component</button>
`
})

export class NzDemoDrawerServiceComponent {

@ViewChild('drawerTemplate') drawerTemplate;
value = 'ng';

constructor(
private drawerService: NzDrawerService
) {

}

openTemplate(): void {
const drawerRef = this.drawerService.create({
nzTitle: 'Template',
nzContent: this.drawerTemplate,
nzContentParams: {
value: this.value
}
});

drawerRef.afterOpen.subscribe(() => {
console.log('Drawer(Template) open');
});

drawerRef.afterClose.subscribe(() => {
console.log('Drawer(Template) close');
});
}

openComponent(): void {
const drawerRef = this.drawerService.create<NzDrawerCustomComponent, { value: string }, string>({
nzTitle: 'Component',
nzContent: NzDrawerCustomComponent,
nzContentParams: {
value: this.value
}
});

drawerRef.afterOpen.subscribe(() => {
console.log('Drawer(Component) open');
});

drawerRef.afterClose.subscribe(data => {
console.log(data);
if (typeof data === 'string') {
this.value = data;
}
});
}

}

@Component({
selector: 'nz-drawer-custom-component',
template: `
<div>
<input nz-input [(ngModel)]="value">
<nz-divider></nz-divider>
<button nzType="primary" (click)="close()" nz-button>Confirm</button>
</div>
`
})
export class NzDrawerCustomComponent {

@Input() value = '';

constructor(
private drawerRef: NzDrawerRef<string>
) {
}

close(): void {
this.drawerRef.close(this.value);
}
}
35 changes: 35 additions & 0 deletions components/drawer/doc/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,38 @@ A Drawer is a panel that is typically overlaid on top of a page and slides in fr
| `[nzWrapClassName]` | The class name of the container of the Drawer dialog. | `string` | - |
| `[nzZIndex]` | The `z-index` of the Drawer. | `number` | `1000` |
| `(nzOnClose)` | Specify a callback that will be called when a user clicks mask, close button or Cancel button. | `EventEmitter<MouseEvent>` | - |

### NzDrawerService

| Method | Description | Params | Return |
| --- | --- | --- | --- |
| create | create and open an Drawer | `NzDrawerOptions<T, D, R>`| `NzDrawerRef<R>` |

### NzDrawerOptions

| Params | Description | Type | Default |
| --- | --- | --- | --- |
| nzContent | The drawer body content. | `TemplateRef<{ $implicit: D, drawerRef: NzDrawerRef }>`, ` Type<T>` | - |
| nzContentParams | The component inputs the param / The Template context. | `D` | - |
| nzClosable | Whether a close (x) button is visible on top right of the Drawer dialog or not. | `boolean` | `true` |
| nzMaskClosable | Clicking on the mask (area outside the Drawer) to close the Drawer or not. | `boolean` | `true` |
| nzMask | Whether to show mask or not. | `boolean` | `true` |
| nzMaskStyle | Style for Drawer's mask element. | `object` | `{}` |
| nzBodyStyle | Body style for modal body element. Such as height, padding etc. | `object` | `{}` |
| nzTitle | The title for Drawer. | `string` `TemplateRef<{}>` | - |
| nzWidth | Width of the Drawer dialog. | `number` `string` | `256` |
| nzHeight | Height of the Drawer dialog, only when placement is `'top'` or `'bottom'`. | `number` `string` | `256` |
| nzWrapClassName | The class name of the container of the Drawer dialog. | `string` | - |
| nzZIndex| The `z-index` of the Drawer. | `number` | `1000` |
| nzPlacement | The placement of the Drawer. | `'top'` `'right'` `'bottom'` `'left'` | `'right'` |
| nzOffsetX | The the X coordinate offset(px). | `number` | `0` |
| nzOffsetY | The the Y coordinate offset(px), only when placement is `'top'` or `'bottom'`. | `number` | `0` |

### NzDrawerRef

| Params | Description | Type |
| --- | --- | --- |
| afterOpen | Callback called after open. | `Observable<void>` |
| afterClose | Callback called after close. | `Observable<R>` |
| close | close the drawer. | `(result?: R) => void` |
| open | open the drawer. | `() => void` |
35 changes: 35 additions & 0 deletions components/drawer/doc/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,38 @@ title: Drawer
| `[nzWrapClassName]` | 对话框外层容器的类名 | `string` | - |
| `[nzZIndex]` | 设置 Drawer 的 `z-index` | `number` | `1000` |
| `(nzOnClose)` | 点击遮罩层或右上角叉或取消按钮的回调 | `EventEmitter<MouseEvent>` | - |

### NzDrawerService

| 方法名 | 说明 | 参数 | 返回 |
| --- | --- | --- | --- |
| create | 创建并打开一个 Drawer | `NzDrawerOptions<T, D, R>`| `NzDrawerRef<R>` |

### NzDrawerOptions

| 参数 | 说明 | 类型 | 默认值 |
| --- | --- | --- | --- |
| nzContent | Drawer body 的内容 | `TemplateRef<{ $implicit: D, drawerRef: NzDrawerRef }>`, ` Type<T>` | - |
| nzClosable | 是否显示右上角的关闭按钮 | `boolean` | `true` |
| nzContentParams | 内容组件的输入参数 / Template的 context | `D` | - |
| nzMaskClosable | 点击蒙层是否允许关闭 | `boolean` | `true` |
| nzMask | 是否展示遮罩 | `boolean` | `true` |
| nzMaskStyle | 遮罩样式 | `object` | `{}` |
| nzBodyStyle | Modal body 样式 | `object` | `{}` |
| nzTitle | 标题 | `string` `TemplateRef<{}>` | - |
| nzWidth | 宽度 | `number` `string` | `256` |
| nzHeight | 高度, 只在方向为 `'top'``'bottom'` 时生效 | `number` `string` | `256` |
| nzWrapClassName | 对话框外层容器的类名 | `string` | - |
| nzZIndex| 设置 Drawer 的 `z-index` | `number` | `1000` |
| nzPlacement | 抽屉的方向 | `'top'` `'right'` `'bottom'` `'left'` | `'right'` |
| nzOffsetX | x 坐标移量(px) | `number` | `0` |
| nzOffsetY | y 坐标移量(px), 高度, 只在方向为 `'top'``'bottom'` 时生效 | `number` | `0` |

### NzDrawerRef

| 参数 | 说明 | 类型 |
| --- | --- | --- |
| afterOpen | 打开之后的回调 | `Observable<void>` |
| afterClose | 关闭之后的回调 | `Observable<R>` |
| close | 关闭 Drawer | `(result?: R) => void` |
| open | 打开 Drawer | `() => void` |
23 changes: 23 additions & 0 deletions components/drawer/nz-drawer-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { TemplateRef, Type } from '@angular/core';
import { NzDrawerRef } from './nz-drawer-ref';

export type NzDrawerPlacement = 'left' | 'right' | 'top' | 'bottom';

// tslint:disable-next-line:no-any
export interface NzDrawerOptions<T = any, D = any> {
nzClosable?: boolean;
nzMaskClosable?: boolean;
nzMask?: boolean;
nzTitle?: string | TemplateRef<{}>;
nzContent?: TemplateRef<{ $implicit: D, drawerRef: NzDrawerRef }> | Type<T>;
nzContentParams?: D;
nzMaskStyle?: object;
nzBodyStyle?: object;
nzWrapClassName?: string;
nzWidth?: number | string;
nzHeight?: number | string;
nzPlacement?: NzDrawerPlacement;
nzZIndex?: number;
nzOffsetX?: number;
nzOffsetY?: number;
}
11 changes: 11 additions & 0 deletions components/drawer/nz-drawer-ref.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Observable } from 'rxjs/index';

// tslint:disable-next-line:no-any
export abstract class NzDrawerRef<R = any> {

abstract afterClose: Observable<R>;
abstract afterOpen: Observable<void>;

abstract close(result?: R): void;
abstract open(): void;
}
10 changes: 7 additions & 3 deletions components/drawer/nz-drawer.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[class.ant-drawer-bottom]="nzPlacement === 'bottom'"
[class.ant-drawer-right]="nzPlacement === 'right'"
[class.ant-drawer-left]="nzPlacement === 'left'">
<div class="ant-drawer-mask" (click)="maskClick($event)" *ngIf="nzMask" [style.zIndex]="nzZIndex" [ngStyle]="nzMaskStyle"></div>
<div class="ant-drawer-mask" (click)="maskClick()" *ngIf="nzMask" [style.zIndex]="nzZIndex" [ngStyle]="nzMaskStyle"></div>
<div class="ant-drawer-content-wrapper {{ nzWrapClassName }}"
[style.zIndex]="nzZIndex"
[style.width]="width"
Expand All @@ -24,9 +24,13 @@
</ng-container>
</div>
</div>
<button *ngIf="nzClosable" (click)="close($event)" aria-label="Close" class="ant-drawer-close"><span class="ant-drawer-close-x"></span></button>
<button *ngIf="nzClosable" (click)="closeClick()" aria-label="Close" class="ant-drawer-close"><span class="ant-drawer-close-x"></span></button>
<div class="ant-drawer-body" [ngStyle]="nzBodyStyle">
<ng-content></ng-content>
<ng-template cdkPortalOutlet></ng-template>
<ng-container *ngIf="isTemplateRef(nzContent)">
<ng-container *ngTemplateOutlet="nzContent; context: templateContext"></ng-container>
</ng-container>
<ng-content *ngIf="!nzContent"></ng-content>
</div>
</div>
</div>
Expand Down
Loading

0 comments on commit ad0c5cf

Please sign in to comment.