Skip to content

Commit 89fcb6a

Browse files
refactor(NodeService): Clean up code (#2023)
1 parent 133de22 commit 89fcb6a

File tree

6 files changed

+73
-114
lines changed

6 files changed

+73
-114
lines changed

src/assets/wise5/components/dialogGuidance/dialog-guidance-authoring/dialog-guidance-authoring.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { EditComponentPrompt } from '../../../../../app/authoring-tool/edit-comp
1212
import { ProjectAssetService } from '../../../../../app/services/projectAssetService';
1313
import { AnnotationService } from '../../../services/annotationService';
1414
import { ConfigService } from '../../../services/configService';
15-
import { NodeService } from '../../../services/nodeService';
1615
import { ProjectService } from '../../../services/projectService';
1716
import { SessionService } from '../../../services/sessionService';
1817
import { StudentDataService } from '../../../services/studentDataService';
@@ -24,6 +23,7 @@ import { ComputerAvatarService } from '../../../services/computerAvatarService';
2423
import { DialogGuidanceService } from '../dialogGuidanceService';
2524
import { FeedbackRuleHelpComponent } from '../../common/feedbackRule/feedback-rule-help/feedback-rule-help.component';
2625
import { ComponentAuthoringModule } from '../../component-authoring.module';
26+
import { TeacherNodeService } from '../../../services/teacherNodeService';
2727

2828
@NgModule({
2929
declarations: [
@@ -49,12 +49,12 @@ import { ComponentAuthoringModule } from '../../component-authoring.module';
4949
ComputerAvatarService,
5050
ConfigService,
5151
DialogGuidanceService,
52-
NodeService,
5352
ProjectAssetService,
5453
ProjectService,
5554
SessionService,
5655
StudentDataService,
5756
TagService,
57+
TeacherNodeService,
5858
TeacherProjectService
5959
],
6060
exports: [

src/assets/wise5/components/embedded/embedded-authoring/embedded-authoring.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { EditComponentPrompt } from '../../../../../app/authoring-tool/edit-comp
1212
import { ProjectAssetService } from '../../../../../app/services/projectAssetService';
1313
import { AnnotationService } from '../../../services/annotationService';
1414
import { ConfigService } from '../../../services/configService';
15-
import { NodeService } from '../../../services/nodeService';
1615
import { ProjectService } from '../../../services/projectService';
1716
import { SessionService } from '../../../services/sessionService';
1817
import { StudentAssetService } from '../../../services/studentAssetService';
@@ -22,6 +21,7 @@ import { TeacherProjectService } from '../../../services/teacherProjectService';
2221
import { EmbeddedService } from '../embeddedService';
2322
import { EmbeddedAuthoring } from './embedded-authoring.component';
2423
import { ComponentAuthoringModule } from '../../component-authoring.module';
24+
import { TeacherNodeService } from '../../../services/teacherNodeService';
2525

2626
@NgModule({
2727
declarations: [EmbeddedAuthoring, AuthorUrlParametersComponent],
@@ -41,13 +41,13 @@ import { ComponentAuthoringModule } from '../../component-authoring.module';
4141
AnnotationService,
4242
ConfigService,
4343
EmbeddedService,
44-
NodeService,
4544
ProjectAssetService,
4645
ProjectService,
4746
SessionService,
4847
StudentAssetService,
4948
StudentDataService,
5049
TagService,
50+
TeacherNodeService,
5151
TeacherProjectService
5252
],
5353
exports: [EmbeddedAuthoring, EditComponentPrompt, AuthorUrlParametersComponent]

src/assets/wise5/services/gradingNodeService.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ import { TeacherNodeService } from './teacherNodeService';
33

44
@Injectable()
55
export class GradingNodeService extends TeacherNodeService {
6-
/**
7-
* Get the next node id in the project sequence that captures student work
8-
* @param currentId (optional)
9-
* @returns next node id
10-
*/
116
getNextNodeId(currentId = null): Promise<string> {
127
return super.getNextNodeId(currentId).then((nextNodeId: string) => {
138
if (!nextNodeId) return null;
@@ -17,31 +12,10 @@ export class GradingNodeService extends TeacherNodeService {
1712
});
1813
}
1914

20-
/**
21-
* Go to the next node that captures work
22-
* @return a promise that will return the next node id
23-
*/
24-
goToNextNode(): Promise<string> {
25-
return this.getNextNodeId().then((nextNodeId: string) => {
26-
if (nextNodeId) {
27-
this.setCurrentNode(nextNodeId);
28-
}
29-
return nextNodeId;
30-
});
31-
}
32-
33-
/**
34-
* Go to the previous node that captures work
35-
*/
3615
goToPrevNode(): void {
3716
this.setCurrentNode(this.getPrevNodeId());
3817
}
3918

40-
/**
41-
* Get the previous node id in the project sequence that captures student work
42-
* @param currentId (optional)
43-
* @returns next node id
44-
*/
4519
getPrevNodeId(currentId = null) {
4620
const prevNodeId = super.getPrevNodeId(currentId);
4721
if (!prevNodeId) return null;

src/assets/wise5/services/nodeService.ts

Lines changed: 21 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { ConstraintService } from './constraintService';
99
import { TransitionLogic } from '../common/TransitionLogic';
1010

1111
@Injectable()
12-
export class NodeService {
12+
export abstract class NodeService {
1313
private transitionResults = {};
1414
private chooseTransitionPromises = {};
1515
private nodeSubmitClickedSource: Subject<any> = new Subject<any>();
@@ -18,83 +18,33 @@ export class NodeService {
1818
public doneRenderingComponent$ = this.doneRenderingComponentSource.asObservable();
1919

2020
constructor(
21+
protected dataService: DataService,
2122
protected dialog: MatDialog,
2223
protected configService: ConfigService,
2324
protected constraintService: ConstraintService,
24-
protected projectService: ProjectService,
25-
protected dataService: DataService
25+
protected projectService: ProjectService
2626
) {}
2727

2828
setCurrentNode(nodeId: string): void {
2929
this.dataService.setCurrentNodeByNodeId(nodeId);
3030
}
3131

3232
goToNextNode(): Promise<string> {
33-
return this.getNextNodeId().then((nextNodeId) => {
33+
return this.getNextNodeId().then((nextNodeId: string) => {
3434
if (nextNodeId != null) {
3535
this.setCurrentNode(nextNodeId);
3636
}
3737
return nextNodeId;
3838
});
3939
}
4040

41-
/**
42-
* This function should be implemented by the child service classes
43-
*/
44-
getNextNodeId(currentId?: string): Promise<any> {
45-
return null;
46-
}
41+
abstract getNextNodeId(currentId?: string): Promise<any>;
4742

48-
goToPrevNode() {
49-
const prevNodeId = this.getPrevNodeId();
50-
this.setCurrentNode(prevNodeId);
43+
goToPrevNode(): void {
44+
this.setCurrentNode(this.getPrevNodeId());
5145
}
5246

53-
/**
54-
* Get the previous node in the project sequence
55-
* @param currentId (optional)
56-
*/
57-
getPrevNodeId(currentId?: string): string {
58-
let prevNodeId = null;
59-
const currentNodeId = currentId ?? this.dataService.getCurrentNodeId();
60-
if (currentNodeId) {
61-
if (['author', 'classroomMonitor'].includes(this.configService.getMode())) {
62-
const currentNodeOrder = this.projectService.getNodeOrderById(currentNodeId);
63-
if (currentNodeOrder) {
64-
const prevId = this.projectService.getNodeIdByOrder(currentNodeOrder - 1);
65-
if (prevId) {
66-
prevNodeId = this.projectService.isApplicationNode(prevId)
67-
? prevId
68-
: this.getPrevNodeId(prevId);
69-
}
70-
}
71-
} else {
72-
// get all the nodes that transition to the current node
73-
const nodeIdsByToNodeId = this.projectService
74-
.getNodesByToNodeId(currentNodeId)
75-
.map((node) => node.id);
76-
if (nodeIdsByToNodeId.length === 1) {
77-
// there is only one node that transitions to the current node
78-
prevNodeId = nodeIdsByToNodeId[0];
79-
} else if (nodeIdsByToNodeId.length > 1) {
80-
// there are multiple nodes that transition to the current node
81-
82-
const stackHistory = this.dataService.getStackHistory();
83-
84-
// loop through the stack history node ids from newest to oldest
85-
for (let s = stackHistory.length - 1; s >= 0; s--) {
86-
const stackHistoryNodeId = stackHistory[s];
87-
if (nodeIdsByToNodeId.indexOf(stackHistoryNodeId) != -1) {
88-
// we have found a node that we previously visited that transitions to the current node
89-
prevNodeId = stackHistoryNodeId;
90-
break;
91-
}
92-
}
93-
}
94-
}
95-
}
96-
return prevNodeId;
97-
}
47+
abstract getPrevNodeId(currentId?: string): string;
9848

9949
/**
10050
* Close the current node (and open the current node's parent group)
@@ -159,7 +109,7 @@ export class NodeService {
159109
* they last chose and not ask them again
160110
*/
161111
} else {
162-
this.letUserChooseTransition(availableTransitions, nodeId, resolve);
112+
this.letUserChooseTransition(availableTransitions, resolve);
163113
}
164114
} else {
165115
transitionResult = this.chooseTransitionAutomatically(
@@ -184,28 +134,18 @@ export class NodeService {
184134
);
185135
}
186136

187-
private letUserChooseTransition(
188-
availableTransitions: any[],
189-
nodeId: string,
190-
resolve: (value: any) => void
191-
): void {
192-
const paths = [];
193-
for (const availableTransition of availableTransitions) {
194-
const toNodeId = availableTransition.to;
195-
const path = {
196-
nodeId: toNodeId,
197-
nodeTitle: this.projectService.getNodePositionAndTitle(toNodeId),
198-
transition: availableTransition
199-
};
200-
paths.push(path);
201-
}
202-
const dialogRef = this.dialog.open(ChooseBranchPathDialogComponent, {
203-
data: paths,
204-
disableClose: true
205-
});
206-
dialogRef.afterClosed().subscribe((result) => {
207-
resolve(result);
208-
});
137+
private letUserChooseTransition(transitions: any[], resolve: (value: any) => void): void {
138+
this.dialog
139+
.open(ChooseBranchPathDialogComponent, {
140+
data: transitions.map((transition) => ({
141+
nodeId: transition.to,
142+
nodeTitle: this.projectService.getNodePositionAndTitle(transition.to),
143+
transition: transition
144+
})),
145+
disableClose: true
146+
})
147+
.afterClosed()
148+
.subscribe((result) => resolve(result));
209149
}
210150

211151
private chooseTransitionAutomatically(

src/assets/wise5/services/studentNodeService.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ import { TransitionLogic } from '../common/TransitionLogic';
1313
@Injectable()
1414
export class StudentNodeService extends NodeService {
1515
constructor(
16+
protected dataService: DataService,
1617
protected dialog: MatDialog,
1718
protected configService: ConfigService,
1819
protected constraintService: ConstraintService,
1920
private nodeStatusService: NodeStatusService,
20-
protected projectService: ProjectService,
21-
protected dataService: DataService
21+
protected projectService: ProjectService
2222
) {
23-
super(dialog, configService, constraintService, projectService, dataService);
23+
super(dataService, dialog, configService, constraintService, projectService);
2424
}
2525

2626
setCurrentNode(nodeId: string): void {
@@ -71,6 +71,34 @@ export class StudentNodeService extends NodeService {
7171
.join('<br/>');
7272
}
7373

74+
getPrevNodeId(currentId?: string): string {
75+
let prevNodeId = null;
76+
const currentNodeId = currentId ?? this.dataService.getCurrentNodeId();
77+
if (currentNodeId) {
78+
// get all the nodes that transition to the current node
79+
const nodeIdsByToNodeId = this.projectService
80+
.getNodesByToNodeId(currentNodeId)
81+
.map((node) => node.id);
82+
if (nodeIdsByToNodeId.length === 1) {
83+
// there is only one node that transitions to the current node
84+
prevNodeId = nodeIdsByToNodeId[0];
85+
} else if (nodeIdsByToNodeId.length > 1) {
86+
// there are multiple nodes that transition to the current node
87+
const stackHistory = this.dataService.getStackHistory();
88+
// loop through the stack history node ids from newest to oldest
89+
for (let s = stackHistory.length - 1; s >= 0; s--) {
90+
const stackHistoryNodeId = stackHistory[s];
91+
if (nodeIdsByToNodeId.indexOf(stackHistoryNodeId) != -1) {
92+
// we have found a node that we previously visited that transitions to the current node
93+
prevNodeId = stackHistoryNodeId;
94+
break;
95+
}
96+
}
97+
}
98+
}
99+
return prevNodeId;
100+
}
101+
74102
/**
75103
* Get the next node in the project sequence. We return a promise because in preview mode we allow
76104
* the user to specify which branch path they want to go to. In all other cases we will resolve

src/assets/wise5/services/teacherNodeService.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ export class TeacherNodeService extends NodeService {
2424
this.starterStateResponseSource.next(args);
2525
}
2626

27+
getPrevNodeId(currentId?: string): string {
28+
let prevNodeId = null;
29+
const currentNodeId = currentId ?? this.dataService.getCurrentNodeId();
30+
if (currentNodeId) {
31+
const currentNodeOrder = this.projectService.getNodeOrderById(currentNodeId);
32+
if (currentNodeOrder) {
33+
const prevId = this.projectService.getNodeIdByOrder(currentNodeOrder - 1);
34+
if (prevId) {
35+
prevNodeId = this.projectService.isApplicationNode(prevId)
36+
? prevId
37+
: this.getPrevNodeId(prevId);
38+
}
39+
}
40+
}
41+
return prevNodeId;
42+
}
43+
2744
getNextNodeId(currentId?: string): Promise<any> {
2845
return new Promise((resolve, reject) => {
2946
let nextNodeId = null;

0 commit comments

Comments
 (0)