-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathadd-your-own-node.component.ts
More file actions
110 lines (100 loc) · 3.66 KB
/
add-your-own-node.component.ts
File metadata and controls
110 lines (100 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { Component } from '@angular/core';
import { CdkDragDrop, DragDropModule, moveItemInArray } from '@angular/cdk/drag-drop';
import {
FormControl,
FormGroup,
Validators,
FormBuilder,
FormsModule,
ReactiveFormsModule
} from '@angular/forms';
import { ComponentTypeService } from '../../../services/componentTypeService';
import { CreateComponentService } from '../../../services/createComponentService';
import { ActivatedRoute, Router, RouterModule } from '@angular/router';
import { TeacherProjectService } from '../../../services/teacherProjectService';
import { CommonModule } from '@angular/common';
import { MatFormFieldModule } from '@angular/material/form-field';
import { FlexLayoutModule } from '@angular/flex-layout';
import { MatIconModule } from '@angular/material/icon';
import { ComponentTypeButtonComponent } from '../../components/component-type-button/component-type-button.component';
import { MatDividerModule } from '@angular/material/divider';
import { MatButtonModule } from '@angular/material/button';
import { MatProgressBarModule } from '@angular/material/progress-bar';
import { MatInputModule } from '@angular/material/input';
@Component({
imports: [
CommonModule,
ComponentTypeButtonComponent,
DragDropModule,
FlexLayoutModule,
FormsModule,
MatButtonModule,
MatDividerModule,
MatFormFieldModule,
MatIconModule,
MatInputModule,
MatProgressBarModule,
ReactiveFormsModule,
RouterModule
],
standalone: true,
styleUrls: ['add-your-own-node.component.scss', '../../add-content.scss'],
templateUrl: 'add-your-own-node.component.html'
})
export class AddYourOwnNodeComponent {
protected addNodeFormGroup: FormGroup = this.fb.group({
title: new FormControl($localize`New Step`, [Validators.required])
});
protected componentTypes: any[];
protected initialComponents: string[] = [];
protected submitting: boolean;
protected targetId: string;
constructor(
private componentTypeService: ComponentTypeService,
private createComponentService: CreateComponentService,
private fb: FormBuilder,
private projectService: TeacherProjectService,
private route: ActivatedRoute,
private router: Router
) {
this.componentTypes = this.componentTypeService.getComponentTypes();
}
ngOnInit(): void {
this.targetId = history.state.targetId;
}
protected addComponent(componentType: any): void {
this.initialComponents.push(componentType);
}
protected deleteComponent(index: number): void {
this.initialComponents.splice(index, 1);
}
protected drop(event: CdkDragDrop<string[]>): void {
moveItemInArray(this.initialComponents, event.previousIndex, event.currentIndex);
}
protected submit(): void {
this.submitting = true;
const newNode = this.projectService.createNode(this.addNodeFormGroup.controls['title'].value);
if (this.isGroupNode(this.targetId)) {
this.projectService.createNodeInside(newNode, this.targetId);
} else {
this.projectService.createNodeAfter(newNode, this.targetId);
}
this.addInitialComponents(newNode.id, this.initialComponents);
this.save().then(() => {
this.router.navigate(['../..'], { relativeTo: this.route });
});
}
protected isGroupNode(nodeId: string): boolean {
return this.projectService.isGroupNode(nodeId);
}
private addInitialComponents(nodeId: string, components: any[]): void {
components
.reverse()
.forEach((component) => this.createComponentService.create(nodeId, component.type));
}
private save(): any {
return this.projectService.checkPotentialStartNodeIdChangeThenSaveProject().then(() => {
this.projectService.refreshProject();
});
}
}