我怎样才能使一个MatDialog拖动/角材料

问题描述 投票:20回答:2

是否有可能使一个角材料对话框可拖动?我安装angular2-拖动和当然可以用在所有其他元素的功能。

但由于对话是动态创建的,我不能一个特殊元素上使用ngDraggable或者可以使用模板变量。

angular dialog draggable material
2个回答
42
投票

由于角材料7更新

你可以简单地使用cdkDrag指令从@angular/cdk/drag-drop

dialog.html

<h1 mat-dialog-title 
   cdkDrag
   cdkDragRootElement=".cdk-overlay-pane" 
   cdkDragHandle>
     Hi {{data.name}}
</h1>

Stackblitz Example

以前的答案:

由于是针对没有官方的解决办法,我要写下在一个对话框的标题应用和做所有的工作为我们的自定义指令:

dialog.html

@Component({
  selector: 'app-simple-dialog',
  template: `
    <h1 mat-dialog-title mat-dialog-draggable-title>Hi {{data.name}}</h1>
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    <div mat-dialog-content>
      ...
    </div>
    <div mat-dialog-actions>
      ...
    </div>
  `
})
export class SimpleDialogComponent {

Ng-run Example

enter image description here

这里的基本思想是用MatDialogRef.updatePosition方法来更新对话框位置。引擎盖下这种方法改变的margin-top |利润率左值,有人可以说,它是不是在这里的最佳选择,如果我们使用改造它会更好,但我只是想展示我们如何能做到这一点没有一些例子技巧和与内置服务的帮助。

我们还需要注入MatDialogContainer在我们的指令,使我们可以得到对话框容器的初始位置。我们要计算初始偏移,因为角材料库使用弯曲到中心对话框,它不会让我们具体的顶部/左值。

对话框的拖动,title.directive.ts

import { Directive, HostListener, OnInit } from '@angular/core';
import { MatDialogContainer, MatDialogRef } from '@angular/material';
import { Subscription } from 'rxjs/Subscription';
import { Observable } from 'rxjs/Observable';
import { takeUntil } from 'rxjs/operators/takeUntil';
import 'rxjs/add/observable/fromEvent';
import { take } from 'rxjs/operators/take';

@Directive({
  selector: '[mat-dialog-draggable-title]'
})
export class DialogDraggableTitleDirective implements OnInit {

  private _subscription: Subscription;

  mouseStart: Position;

  mouseDelta: Position;

  offset: Position;

  constructor(
    private matDialogRef: MatDialogRef<any>,
    private container: MatDialogContainer) {}

  ngOnInit() {
    this.offset = this._getOffset();
  }

  @HostListener('mousedown', ['$event'])
  onMouseDown(event: MouseEvent) {
    this.mouseStart = {x: event.pageX, y: event.pageY};

    const mouseup$ = Observable.fromEvent(document, 'mouseup');
    this._subscription = mouseup$.subscribe(() => this.onMouseup());

    const mousemove$ = Observable.fromEvent(document, 'mousemove')
      .pipe(takeUntil(mouseup$))
      .subscribe((e: MouseEvent) => this.onMouseMove(e));

    this._subscription.add(mousemove$);
  }

  onMouseMove(event: MouseEvent) {
      this.mouseDelta = {x: (event.pageX - this.mouseStart.x), y: (event.pageY - this.mouseStart.y)};

      this._updatePosition(this.offset.y + this.mouseDelta.y, this.offset.x + this.mouseDelta.x);
  }

  onMouseup() {
    if (this._subscription) {
      this._subscription.unsubscribe();
      this._subscription = undefined;
    }

    if (this.mouseDelta) {
      this.offset.x += this.mouseDelta.x;
      this.offset.y += this.mouseDelta.y;
    }
  }

  private _updatePosition(top: number, left: number) {
    this.matDialogRef.updatePosition({
      top: top + 'px',
      left: left + 'px'
    });
  }

  private _getOffset(): Position {
    const box = this.container['_elementRef'].nativeElement.getBoundingClientRect();
    return {
      x: box.left + pageXOffset,
      y: box.top + pageYOffset
    };
  }
}


export interface Position {
  x: number;
  y: number;
}

记住位置

由于@Rolando问:

我想“记住”,其中的模态被定位,这样当按钮打开模态被击中,模态开辟了空间,“这是最后位置”。

让我们试着来支持它。

为了做到这一点,你可以创建一些服务,您将存储对话框的位置:

莫代尔,position.cache.ts

@Injectable()
export class ModalPositionCache {
  private _cache = new Map<Type<any>, Position>();

  set(dialog: Type<any>, position: Position) {
    this._cache.set(dialog, position);
  }

  get(dialog: Type<any>): Position|null {
    return this._cache.get(dialog);
  }
}

现在你需要在我们的指令注入该服务:

对话框的拖动,title.directive.ts

export class DialogDraggableTitleDirective implements OnInit {
  ...

  constructor(
    private matDialogRef: MatDialogRef<any>,
    private container: MatDialogContainer,
    private positionCache: ModalPositionCache
  ) {}

  ngOnInit() {
    const dialogType = this.matDialogRef.componentInstance.constructor;
    const cachedValue = this.positionCache.get(dialogType);
    this.offset = cachedValue || this._getOffset();
    this._updatePosition(this.offset.y, this.offset.x);

    this.matDialogRef.beforeClose().pipe(take(1))
      .subscribe(() => this.positionCache.set(dialogType, this.offset));
  }

正如你可以尽快对话框将被关闭,我省最后一个偏移量。

Ng-run Example

这样对话框记住了它是封闭

enter image description here


0
投票

angular2-draggable,您使用ngDraggable使元素可拖动。其中ngDraggable是一个指令,并在你的情况,你需要与你的对话框,它是动态创建动态附加ngDraggable指令。

虽然正式,也没有办法动态地添加指令,但有些卑鄙手段已经在以下几个问题进行了讨论,以动态地添加指令。

How to dynamically add a directive?

Use Angular2 Directive in host of another Directive

© www.soinside.com 2019 - 2024. All rights reserved.