-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathProgramChangeController.js
More file actions
140 lines (119 loc) · 4.71 KB
/
ProgramChangeController.js
File metadata and controls
140 lines (119 loc) · 4.71 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// @flow
import type {IntlShape} from 'react-intl';
import type {AudioManager} from './types';
import {App} from './App';
import {ProgramBlockEditor} from './ProgramBlockEditor';
// The ProgramChangeController is responsible for making changes to the
// App 'state.programSequence' and coordinating any user interface
// activities associated with the change, such as making announcements,
// setting focus, or setting up animations.
export default class ProgramChangeController {
app: App;
intl: IntlShape;
audioManager: AudioManager;
constructor(app: App, intl: IntlShape, audioManager: AudioManager) {
this.app = app;
this.intl = intl;
this.audioManager = audioManager;
}
insertSelectedActionIntoProgram(programBlockEditor: ?ProgramBlockEditor,
index: number, selectedAction: ?string) {
this.app.setState((state) => {
if (selectedAction) {
this.playAnnouncementForAdd(selectedAction);
this.doActivitiesForAdd(programBlockEditor, index);
return {
programSequence: state.programSequence.insertStep(index,
selectedAction)
};
} else {
return {};
}
});
}
addSelectedActionToProgramEnd(programBlockEditor: ?ProgramBlockEditor,
selectedAction: ?string) {
this.app.setState((state) => {
if (selectedAction) {
this.playAnnouncementForAdd(selectedAction);
const index = state.programSequence.getProgramLength();
this.doActivitiesForAdd(programBlockEditor, index);
return {
programSequence: state.programSequence.insertStep(index,
selectedAction)
};
} else {
return {};
}
});
}
deleteProgramStep(programBlockEditor: ?ProgramBlockEditor,
index: number, command: string) {
this.app.setState((state) => {
// Check that the step to delete hasn't changed since the
// user made the deletion
if (command === state.programSequence.getProgramStepAt(index)) {
this.playAnnouncementForDelete(command);
// If there are steps following the one being deleted, focus
// the next step. Otherwise, focus the final add node.
if (programBlockEditor) {
if (index < state.programSequence.getProgramLength() - 1) {
programBlockEditor.focusCommandBlockAfterUpdate(index);
} else {
programBlockEditor.focusAddNodeAfterUpdate(index);
}
}
return {
programSequence: state.programSequence.deleteStep(index)
};
} else {
// If the step to delete has changed, make no changes to the
// program
return {};
}
});
}
deleteLastStep(programBlockEditor: ?ProgramBlockEditor) {
this.app.setState((state) => {
const index = state.programSequence.getProgramLength() - 1;
if (index >= 0) {
const command = state.programSequence.getProgramStepAt(index);
this.playAnnouncementForDelete(command);
// As we are deleting the last step, focus the final add node.
if (programBlockEditor) {
programBlockEditor.focusAddNodeAfterUpdate(index);
}
return {
programSequence: state.programSequence.deleteStep(index)
};
} else {
return {};
}
});
}
// Internal methods
playAnnouncementForAdd(command: string) {
this.playAnnouncementForChange(command, 'add');
}
playAnnouncementForDelete(command:string) {
this.playAnnouncementForChange(command, 'delete');
}
playAnnouncementForChange(command:string, changeType: string) {
const commandString = this.intl.formatMessage({
id: "Announcement." + command
});
this.audioManager.playAnnouncement(
changeType,
this.intl,
{ command: commandString }
);
}
doActivitiesForAdd(programBlockEditor: ?ProgramBlockEditor, index: number) {
// Set up focus, scrolling, and animation
if (programBlockEditor) {
programBlockEditor.focusCommandBlockAfterUpdate(index);
programBlockEditor.scrollToAddNodeAfterUpdate(index + 1);
programBlockEditor.setUpdatedCommandBlock(index);
}
}
};