Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ProjectAuthoringComponent): Code cleanup #1408

Merged
merged 3 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,7 @@ export class ProjectAuthoringComponent {
ngOnInit(): void {
this.updateShowProjectView();
this.projectId = Number(this.route.snapshot.paramMap.get('unitId'));
this.items = Object.entries(this.projectService.idToOrder)
.map((entry: any) => {
return { key: entry[0], order: entry[1].order };
})
.sort((a: any, b: any) => {
return a.order - b.order;
});
this.items = this.projectService.getNodesInOrder();
this.inactiveGroupNodes = this.projectService.getInactiveGroupNodes();
this.inactiveStepNodes = this.projectService.getInactiveStepNodes();
this.inactiveNodes = this.projectService.getInactiveNodes();
Expand Down Expand Up @@ -133,12 +127,10 @@ export class ProjectAuthoringComponent {

protected deleteSelectedNodes(): void {
const selectedNodeIds = this.getSelectedNodeIds();
let confirmMessage = '';
if (selectedNodeIds.length === 1) {
confirmMessage = $localize`Are you sure you want to delete the selected item?`;
} else {
confirmMessage = $localize`Are you sure you want to delete the ${selectedNodeIds.length} selected items?`;
}
const confirmMessage =
selectedNodeIds.length === 1
? $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);
}
Expand Down Expand Up @@ -188,48 +180,14 @@ export class ProjectAuthoringComponent {
selectedNodeIds.push(item.key);
}
});

if (this.inactiveNodes != null) {
for (const inactiveNode of this.inactiveNodes) {
if (inactiveNode.checked) {
selectedNodeIds.push(inactiveNode.id);
}
for (const inactiveNode of this.inactiveNodes) {
if (inactiveNode.checked) {
selectedNodeIds.push(inactiveNode.id);
}
}
return selectedNodeIds;
}

/**
* Get the distinct types of the selected items, both active and inactive.
* @returns an array of item types. possible items are group or node.
*/
private getSelectedItemTypes(): string[] {
const selectedItemTypes = [];
this.items.forEach((item: any) => {
if (item.checked) {
const node = this.projectService.getNodeById(item.key);
if (node != null) {
let nodeType = node.type;
if (selectedItemTypes.indexOf(nodeType) == -1) {
selectedItemTypes.push(nodeType);
}
}
}
});

if (this.inactiveNodes != null) {
for (let inactiveNode of this.inactiveNodes) {
if (inactiveNode != null && inactiveNode.checked) {
let inactiveNodeType = inactiveNode.type;
if (selectedItemTypes.indexOf(inactiveNodeType) == -1) {
selectedItemTypes.push(inactiveNodeType);
}
}
}
}
return selectedItemTypes;
}

private unselectAllItems(): void {
this.items.forEach((item: any) => {
item.checked = false;
Expand Down Expand Up @@ -287,13 +245,7 @@ export class ProjectAuthoringComponent {

private refreshProject(): void {
this.projectService.parseProject();
this.items = Object.entries(this.projectService.idToOrder)
.map((entry: any) => {
return { key: entry[0], order: entry[1].order };
})
.sort((a: any, b: any) => {
return a.order - b.order;
});
this.items = this.projectService.getNodesInOrder();
this.inactiveGroupNodes = this.projectService.getInactiveGroupNodes();
this.inactiveStepNodes = this.projectService.getInactiveStepNodes();
this.inactiveNodes = this.projectService.getInactiveNodes();
Expand Down Expand Up @@ -350,16 +302,7 @@ export class ProjectAuthoringComponent {
}

protected getNumberOfInactiveGroups(): number {
let count = 0;
for (let n = 0; n < this.inactiveNodes.length; n++) {
let inactiveNode = this.inactiveNodes[n];
if (inactiveNode != null) {
if (inactiveNode.type == 'group') {
count++;
}
}
}
return count;
return this.inactiveNodes.filter((node) => node.type === 'group').length;
}

/**
Expand All @@ -369,19 +312,9 @@ export class ProjectAuthoringComponent {
* are in an inactive group).
*/
protected getNumberOfInactiveSteps(): number {
let count = 0;
for (let n = 0; n < this.inactiveNodes.length; n++) {
let inactiveNode = this.inactiveNodes[n];
if (inactiveNode != null) {
if (
inactiveNode.type == 'node' &&
this.projectService.getParentGroup(inactiveNode.id) == null
) {
count++;
}
}
}
return count;
return this.inactiveNodes.filter(
(node) => node.type === 'node' && this.projectService.getParentGroup(node.id) == null
).length;
}

protected getParentGroup(nodeId: string): any {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { WiseLinkAuthoringDialogComponent } from './wise-link-authoring-dialog.component';
import { MatDialogRef } from '@angular/material/dialog';
import { ProjectService } from '../../services/projectService';
import { StudentTeacherCommonServicesModule } from '../../../../app/student-teacher-common-services.module';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { MatFormFieldModule } from '@angular/material/form-field';
Expand All @@ -10,6 +9,7 @@ import { MatRadioModule } from '@angular/material/radio';
import { MatInputModule } from '@angular/material/input';
import { FormsModule } from '@angular/forms';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { TeacherProjectService } from '../../services/teacherProjectService';

describe('WiseLinkAuthoringDialogComponent', () => {
let component: WiseLinkAuthoringDialogComponent;
Expand All @@ -28,7 +28,7 @@ describe('WiseLinkAuthoringDialogComponent', () => {
MatSelectModule,
StudentTeacherCommonServicesModule
],
providers: [{ provide: MatDialogRef, useValue: {} }, ProjectService]
providers: [{ provide: MatDialogRef, useValue: {} }, TeacherProjectService]
}).compileComponents();

fixture = TestBed.createComponent(WiseLinkAuthoringDialogComponent);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component } from '@angular/core';
import { MatDialogRef } from '@angular/material/dialog';
import { ProjectService } from '../../services/projectService';
import { TeacherProjectService } from '../../services/teacherProjectService';

@Component({
selector: 'wise-link-authoring-dialog',
Expand All @@ -16,17 +16,11 @@ export class WiseLinkAuthoringDialogComponent {

constructor(
protected dialogRef: MatDialogRef<WiseLinkAuthoringDialogComponent>,
private projectService: ProjectService
private projectService: TeacherProjectService
) {}

ngOnInit(): void {
this.items = Object.entries(this.projectService.idToOrder)
.map((entry: any) => {
return { key: entry[0], order: entry[1].order };
})
.sort((a: any, b: any) => {
return a.order - b.order;
});
this.items = this.projectService.getNodesInOrder();
}

protected wiseLinkNodeIdChanged(): void {
Expand Down
10 changes: 10 additions & 0 deletions src/assets/wise5/services/teacherProjectService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3103,4 +3103,14 @@ export class TeacherProjectService extends ProjectService {
objects.splice(index + 1, 0, object);
}
}

getNodesInOrder(): any[] {
return Object.entries(this.idToOrder)
.map((entry: any) => {
return { key: entry[0], order: entry[1].order };
})
.sort((a: any, b: any) => {
return a.order - b.order;
});
}
}
8 changes: 4 additions & 4 deletions src/messages.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -11991,14 +11991,14 @@ Click &quot;Cancel&quot; to keep the invalid JSON open so you can fix it.</sourc
<source>Are you sure you want to delete the selected item?</source>
<context-group purpose="location">
<context context-type="sourcefile">src/assets/wise5/authoringTool/project-authoring/project-authoring.component.ts</context>
<context context-type="linenumber">138</context>
<context context-type="linenumber">132</context>
</context-group>
</trans-unit>
<trans-unit id="1189930234736223663" datatype="html">
<source>Are you sure you want to delete the <x id="PH" equiv-text="selectedNodeIds.length"/> selected items?</source>
<context-group purpose="location">
<context context-type="sourcefile">src/assets/wise5/authoringTool/project-authoring/project-authoring.component.ts</context>
<context context-type="linenumber">140</context>
<context context-type="linenumber">133</context>
</context-group>
</trans-unit>
<trans-unit id="e8fb2ceb6f8d4c3e90a6a688e9024461e67f44f0" datatype="html">
Expand Down Expand Up @@ -12430,14 +12430,14 @@ Click &quot;Cancel&quot; to keep the invalid JSON open so you can fix it.</sourc
<source>You must select a step.</source>
<context-group purpose="location">
<context context-type="sourcefile">src/assets/wise5/authoringTool/wise-link-authoring-dialog/wise-link-authoring-dialog.component.ts</context>
<context context-type="linenumber">59</context>
<context context-type="linenumber">53</context>
</context-group>
</trans-unit>
<trans-unit id="4796036023179559940" datatype="html">
<source>You must enter text.</source>
<context-group purpose="location">
<context context-type="sourcefile">src/assets/wise5/authoringTool/wise-link-authoring-dialog/wise-link-authoring-dialog.component.ts</context>
<context context-type="linenumber">61</context>
<context context-type="linenumber">55</context>
</context-group>
</trans-unit>
<trans-unit id="8633038537118051284" datatype="html">
Expand Down
Loading