Skip to content

Commit

Permalink
refactor(ProjectAuthoringComponent): deleteNodes() and selectNode() (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
hirokiterashima authored Sep 11, 2023
1 parent 36787d2 commit d6d4518
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
mat-raised-button
color="primary"
(click)="createNewLesson()"
[disabled]="stepNodeSelected || activityNodeSelected"
[disabled]="stepNodeSelected || groupNodeSelected"
matTooltip="Create New Lesson"
matTooltipPosition="above"
i18n-matTooltip
Expand All @@ -35,7 +35,7 @@
mat-raised-button
color="primary"
(click)="createNewStep()"
[disabled]="stepNodeSelected || activityNodeSelected"
[disabled]="stepNodeSelected || groupNodeSelected"
matTooltip="Create New Step"
matTooltipPosition="above"
i18n-matTooltip
Expand All @@ -46,7 +46,7 @@
mat-raised-button
color="primary"
(click)="addStructure()"
[disabled]="stepNodeSelected || activityNodeSelected"
[disabled]="stepNodeSelected || groupNodeSelected"
matTooltip="Add Lesson Structure"
matTooltipPosition="above"
i18n-matTooltip
Expand All @@ -57,7 +57,7 @@
mat-raised-button
color="primary"
(click)="importStep()"
[disabled]="stepNodeSelected || activityNodeSelected"
[disabled]="stepNodeSelected || groupNodeSelected"
matTooltip="Import Step"
matTooltipPosition="above"
i18n-matTooltip
Expand Down Expand Up @@ -101,7 +101,7 @@
mat-raised-button
color="primary"
(click)="goToAdvancedAuthoring()"
[disabled]="stepNodeSelected || activityNodeSelected"
[disabled]="stepNodeSelected || groupNodeSelected"
matTooltip="Advanced"
matTooltipPosition="above"
i18n-matTooltip
Expand Down Expand Up @@ -139,10 +139,10 @@
<mat-checkbox
color="primary"
[(ngModel)]="item.checked"
(ngModelChange)="projectItemClicked()"
(ngModelChange)="selectNode()"
[disabled]="
(isGroupNode(item.key) && stepNodeSelected) ||
(!isGroupNode(item.key) && activityNodeSelected)
(!isGroupNode(item.key) && groupNodeSelected)
"
aria-label="{{ getNodePositionById(item.key) }} {{ getNodeTitle(item.key) }}"
>
Expand Down Expand Up @@ -238,7 +238,7 @@ <h6 class="unused-header">Unused Lessons</h6>
<mat-checkbox
color="primary"
[(ngModel)]="inactiveNode.checked"
(ngModelChange)="projectItemClicked()"
(ngModelChange)="selectNode()"
[disabled]="stepNodeSelected"
aria-label="{{ getNodeTitle(inactiveNode.id) }}"
>
Expand Down Expand Up @@ -270,9 +270,9 @@ <h6 class="pointer">
<mat-checkbox
color="primary"
[(ngModel)]="idToNode[inactiveChildId].checked"
(ngModelChange)="projectItemClicked()"
[disabled]="activityNodeSelected"
aria-label="{{ getNodeTitle(inactiveChildId.id) }}"
(ngModelChange)="selectNode()"
[disabled]="groupNodeSelected"
aria-label="{{ getNodeTitle(inactiveChildId) }}"
>
</mat-checkbox>
</div>
Expand Down Expand Up @@ -310,8 +310,8 @@ <h6 class="unused-header" i18n>Unused Steps</h6>
<mat-checkbox
color="primary"
[(ngModel)]="inactiveNode.checked"
(ngModelChange)="projectItemClicked()"
[disabled]="activityNodeSelected"
(ngModelChange)="selectNode()"
[disabled]="groupNodeSelected"
aria-label="{{ getNodeTitle(inactiveNode.id) }}"
>
</mat-checkbox>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';
styleUrls: ['./project-authoring.component.scss']
})
export class ProjectAuthoringComponent {
protected activityNodeSelected: boolean = false;
protected groupNodeSelected: boolean = false;
protected authors: string[] = [];
private idToNode: any;
protected inactiveGroupNodes: any[];
Expand Down Expand Up @@ -132,47 +132,12 @@ export class ProjectAuthoringComponent {
? $localize`Are you sure you want to delete the selected item?`
: $localize`Are you sure you want to delete the ${selectedNodeIds.length} selected items?`;
if (confirm(confirmMessage)) {
this.deleteNodesById(selectedNodeIds);
selectedNodeIds.forEach((nodeId) => this.deleteNodeService.deleteNode(nodeId));
this.projectService.saveProject();
this.refreshProject();
}
}

private deleteNodesById(nodeIds: string[]): void {
let deletedStartNodeId = false;
const stepsDeleted = [];
const activitiesDeleted = [];
for (const nodeId of nodeIds) {
const node = this.projectService.getNodeById(nodeId);
const tempNode = {
nodeId: node.id,
title: this.projectService.getNodePositionAndTitle(node.id),
stepsInActivityDeleted: []
};
if (this.projectService.isStartNodeId(nodeId)) {
deletedStartNodeId = true;
}
if (this.projectService.isGroupNode(nodeId)) {
const stepsInActivityDeleted = [];
for (const stepNodeId of node.ids) {
const stepObject = {
nodeId: stepNodeId,
title: this.projectService.getNodePositionAndTitle(stepNodeId)
};
stepsInActivityDeleted.push(stepObject);
}
tempNode.stepsInActivityDeleted = stepsInActivityDeleted;
activitiesDeleted.push(tempNode);
} else {
stepsDeleted.push(tempNode);
}
this.deleteNodeService.deleteNode(nodeId);
}
if (deletedStartNodeId) {
this.updateStartNodeId();
}
this.projectService.saveProject();
this.refreshProject();
}

private getSelectedNodeIds(): string[] {
const selectedNodeIds = [];
this.items.forEach((item: any) => {
Expand All @@ -199,7 +164,7 @@ export class ProjectAuthoringComponent {
inactiveStepNode.checked = false;
});
this.stepNodeSelected = false;
this.activityNodeSelected = false;
this.groupNodeSelected = false;
}

protected createNewLesson(): void {
Expand All @@ -214,35 +179,6 @@ export class ProjectAuthoringComponent {
this.router.navigate([`/teacher/edit/unit/${this.projectId}/structure/choose`]);
}

private updateStartNodeId(): void {
let newStartNodeId = null;
let startGroupId = this.projectService.getStartGroupId();
let node = this.projectService.getNodeById(startGroupId);
let done = false;

// recursively traverse the start ids
while (!done) {
if (node == null) {
// base case in case something went wrong
done = true;
} else if (this.projectService.isGroupNode(node.id)) {
// the node is a group node so we will get its start node
node = this.projectService.getNodeById(node.startId);
} else if (this.projectService.isApplicationNode(node.id)) {
// the node is a step node so we have found the new start node id
newStartNodeId = node.id;
done = true;
} else {
// base case in case something went wrong
done = true;
}
}

if (newStartNodeId) {
this.projectService.setStartNodeId(newStartNodeId);
}
}

private refreshProject(): void {
this.projectService.parseProject();
this.items = this.projectService.getNodesInOrder();
Expand Down Expand Up @@ -322,39 +258,19 @@ export class ProjectAuthoringComponent {
}

/**
* The checkbox for a node was clicked. We will determine whether there are
* any activity nodes that are selected or whether there are any step nodes
* that are selected. We do this because we do not allow selecting a mix of
* activities and steps. If there are any activity nodes that are selected,
* we will disable all the step node check boxes. Alternatively, if there are
* any step nodes selected, we will disable all the activity node check boxes.
* @param nodeId The node id of the node that was clicked.
* The checkbox for a node was clicked. We do not allow selecting a mix of group and step nodes.
* If any group nodes are selected, disable all step node checkboxes, and vise-versa.
*/
protected projectItemClicked(): void {
this.stepNodeSelected = false;
this.activityNodeSelected = false;

// this will check the items that are used in the project
for (let item of this.items) {
if (item.checked) {
if (this.isGroupNode(item.key)) {
this.activityNodeSelected = true;
} else {
this.stepNodeSelected = true;
}
}
}

// this will check the items that are unused in the project
for (let key in this.idToNode) {
let node = this.idToNode[key];
if (node.checked) {
if (this.isGroupNode(key)) {
this.activityNodeSelected = true;
} else {
this.stepNodeSelected = true;
}
}
protected selectNode(): void {
const checkedNodes = this.items
.concat(Object.values(this.idToNode))
.filter((item) => item.checked);
if (checkedNodes.length === 0) {
this.groupNodeSelected = false;
this.stepNodeSelected = false;
} else {
this.groupNodeSelected = this.isGroupNode(checkedNodes[0].id);
this.stepNodeSelected = !this.groupNodeSelected;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/assets/wise5/services/teacherProjectService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3107,7 +3107,7 @@ export class TeacherProjectService extends ProjectService {
getNodesInOrder(): any[] {
return Object.entries(this.idToOrder)
.map((entry: any) => {
return { key: entry[0], order: entry[1].order };
return { key: entry[0], id: entry[0], order: entry[1].order };
})
.sort((a: any, b: any) => {
return a.order - b.order;
Expand Down

0 comments on commit d6d4518

Please sign in to comment.