Skip to content

Commit

Permalink
Some more movement to signals to fix future lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rdamazio committed Dec 7, 2024
1 parent 39c4df3 commit 32fcac1
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
[class.checklist-selected]="node.checklist && selectedChecklist === node.checklist"
(click)="onNodeSelect(node)"
checklistDrag
[allDropLists]="allDropLists"
[allDropLists]="allDropLists()"
[checklistDragNode]="item"
cdkDragLockAxis="y"
[cdkDragData]="node"
Expand Down
5 changes: 2 additions & 3 deletions src/app/checklists/checklist-tree/checklist-tree.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ import {
model,
OnInit,
output,
QueryList,
viewChild,
ViewChildren,
viewChildren,
} from '@angular/core';
import { MatButtonModule } from '@angular/material/button';
import { MatDialog, MatDialogModule } from '@angular/material/dialog';
Expand Down Expand Up @@ -66,7 +65,7 @@ export class ChecklistTreeComponent implements OnInit, AfterViewInit {
readonly selectedChecklistGroup = model<ChecklistGroup>();
readonly groupDropListIds = model<string[]>([]);
readonly tree = viewChild.required(MatTree);
@ViewChildren(CdkDropList) allDropLists?: QueryList<CdkDropList<ChecklistTreeNode>>;
readonly allDropLists = viewChildren(CdkDropList<ChecklistTreeNode>);
dataSource = new MatTreeNestedDataSource<ChecklistTreeNode>();

dragging = false;
Expand Down
18 changes: 10 additions & 8 deletions src/app/checklists/checklist-tree/drag.directive.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CdkDrag, CdkDropList } from '@angular/cdk/drag-drop';
import { AfterViewInit, Directive, EventEmitter, Input, OnDestroy, QueryList, input } from '@angular/core';
import { AfterViewInit, Directive, EventEmitter, OnDestroy, computed, effect, input } from '@angular/core';
import { takeUntil } from 'rxjs';
import { ChecklistTreeNode } from './node/node';
import { ChecklistTreeNodeComponent } from './node/node.component';
Expand All @@ -9,23 +9,24 @@ import { ChecklistTreeNodeComponent } from './node/node.component';
standalone: true,
})
export class ChecklistDragDirective extends CdkDrag<ChecklistTreeNode> implements AfterViewInit, OnDestroy {
@Input() allDropLists?: QueryList<CdkDropList<ChecklistTreeNode>>;
readonly allDropLists = input<readonly CdkDropList<ChecklistTreeNode>[]>();
readonly checklistDragNode = input<ChecklistTreeNodeComponent>();

private readonly _dropList = computed(() => this._findContainer());
private readonly _destroyed2 = new EventEmitter<boolean>();

override ngAfterViewInit() {
private readonly _updateContainerEffect =
// We have to dynamically detect drop lists because they're not bound at injection time,
// due to the cdkDrag being outside of the cdkDropList on the template.
this.allDropLists?.changes.pipe(takeUntil(this._destroyed2)).subscribe(() => {
const dropList = this._findContainer();
effect(() => {
const dropList = this._dropList();
if (dropList) {
setTimeout(() => {
this._updateContainer(dropList);
});
}
});

override ngAfterViewInit() {
// Also dynamically register drag handles that were passed in to us.
// This must be done before the parent's ngAfterViewInit, which uses them.
const handle = this.checklistDragNode()?.dragHandle();
Expand All @@ -42,12 +43,13 @@ export class ChecklistDragDirective extends CdkDrag<ChecklistTreeNode> implement
}

private _findContainer(): CdkDropList<ChecklistTreeNode> | undefined {
if (!this.allDropLists) return undefined;
const dropLists = this.allDropLists();
if (!dropLists) return undefined;

// Finds the CdkDropList that's actually been assigned as our parent after
// view setup, since the association doesn't exist in the template.
const containerEl = this.element.nativeElement.closest('.cdk-drop-list');
for (const dropList of this.allDropLists) {
for (const dropList of dropLists) {
if (dropList.element.nativeElement === containerEl) {
return dropList;
}
Expand Down
3 changes: 1 addition & 2 deletions src/app/checklists/checklist-tree/node/node.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ import { ChecklistTreeNode } from './node';
styleUrl: './node.component.scss',
})
export class ChecklistTreeNodeComponent {
node = input.required<ChecklistTreeNode>();

readonly node = input.required<ChecklistTreeNode>();
readonly disableButtonHover = input(false);
readonly nodeRename = output<ChecklistTreeNode>();
readonly nodeDelete = output<ChecklistTreeNode>();
Expand Down

0 comments on commit 32fcac1

Please sign in to comment.