forked from microsoft/PhETOfficeAddIn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.ts
283 lines (240 loc) · 10.8 KB
/
store.ts
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
module MixAppBrowser {
export interface AppDataDriver {
loadCategories(maxDepth: number): JQueryPromise<AppContent[]>;
loadCategory(parentId: string, searchText: string): JQueryPromise<AppContent[]>;
get(id: string): JQueryPromise<AppContent>;
buildConfiguration(content: AppContent): Labs.Core.IConfiguration;
getIdFromConfiguration(configuration: Labs.Core.IConfiguration) : string;
}
// Top-level ndoes in a topic tree
export interface AppContent {
id: string;
type: string;
title: string;
providerId: string;
readableId: string;
youtubeId: string;
durationInSec: number;
thumbnailUrl: string;
contentUrl: string;
children: AppContent[];
}
/**
* Helper method to create a ILabCallback for the given jQuery deferred object
*/
function createCallback<T>(deferred: JQueryDeferred<T>): Labs.Core.ILabCallback<T> {
return (err, data) => {
if (err) {
deferred.reject(err);
}
else {
deferred.resolve(data);
}
};
}
class MixAppBrowserViewModel {
view: KnockoutComputed<string>;
contentType: KnockoutObservable<string>;
domains: KnockoutObservableArray<AppContent>;
activeDomain: KnockoutObservable<AppContent>;
item: KnockoutObservable<AppContent>;
searchText: KnockoutObservable<string>;
activeItemUrl: KnockoutComputed<string>;
// The view the app is currently within
private _appView: KnockoutObservable<string> = ko.observable();
// The view selected by the user
private _userView: KnockoutObservable<string> = ko.observable("edit");
private _labEditor: Labs.LabEditor;
private _labInstance: Labs.LabInstance;
private _links: AppDataDriver;
private _modeSwitchP: JQueryPromise<void> = $.when();
constructor(links: AppDataDriver, initialMode: Labs.Core.LabMode) {
this._links = links;
// Setup knockout observables
this.domains = ko.observableArray([]);
this.activeDomain = ko.observable();
this.item = ko.observable();
this.searchText = ko.observable("");
this.activeItemUrl = ko.computed(()=> {
var item = this.item();
return item ? item.providerId : null;
});
this.view = ko.computed(()=> {
var appView = this._appView();
var userView = this._userView();
if (appView === "view" || (appView === "edit" && userView === "view")) {
return "viewTemplate";
}
else if (appView === "edit") {
return "editTemplate";
} else {
return "loadingTemplate";
}
});
// Events coming from the lab host
Labs.on(Labs.Core.EventTypes.ModeChanged, (data) => {
var modeChangedEventData = <Labs.Core.ModeChangedEventData> data;
this.switchToMode(Labs.Core.LabMode[modeChangedEventData.mode]);
return $.when();
});
Labs.on(Labs.Core.EventTypes.Activate, (data) => {
});
Labs.on(Labs.Core.EventTypes.Deactivate, (data) => {
});
// Set the initial mode
this.switchToMode(initialMode);
}
switchToMode(mode: Labs.Core.LabMode) {
// wait for any previous mode switch to complete before performing the new one
this._modeSwitchP = this._modeSwitchP.then(() => {
var switchedStateDeferred = $.Deferred();
// End any existing operations
if (this._labInstance) {
this._labInstance.done(createCallback(switchedStateDeferred));
}
else if (this._labEditor) {
this._labEditor.done(createCallback(switchedStateDeferred));
} else {
switchedStateDeferred.resolve();
}
// and now switch the state
return switchedStateDeferred.promise().then(() => {
this._labEditor = null;
this._labInstance = null;
if (mode === Labs.Core.LabMode.Edit) {
return this.switchToEditMode();
} else {
return this.switchToViewMode();
}
});
});
}
private switchToEditMode(): JQueryPromise<void> {
var editLabDeferred = $.Deferred();
Labs.editLab(createCallback(editLabDeferred));
return editLabDeferred.promise().then((labEditor) => {
this._labEditor = labEditor;
this._appView("edit");
var configurationDeferred = $.Deferred();
this._labEditor.getConfiguration(createCallback(configurationDeferred));
return configurationDeferred.promise().then((configuration) => {
if (configuration) {
var id = this._links.getIdFromConfiguration(configuration);
// This should just be a method to show
this._links.get(id).done((item: AppContent) => {
this._userView("view");
this.item(item);
});
} else {
this.loadDomain();
}
});
});
}
private loadDomain(): JQueryPromise<void> {
if (this.domains().length > 0) {
return $.when();
}
return this._links.loadCategories(2).then((domains) => {
this.domains(domains);
this.setActiveDomain(domains[0]);
});
}
private setActiveDomain(domain: AppContent) {
var clonedDomain = $.extend(true, {}, domain);
clonedDomain.children.forEach((category) => { category.children = [] });
this.activeDomain(clonedDomain);
$.each(clonedDomain.children, (index, category) => {
this._links.loadCategory(
category.providerId,
this.searchText()).then((list) => {
category.children = list;
// force a re-render of the page by updating the root binding
// TODO replace this with view models for sub items
this.activeDomain(this.activeDomain());
});
});
}
private switchToViewMode(): JQueryPromise<void> {
var takeLabDeferred = $.Deferred();
Labs.takeLab(createCallback(takeLabDeferred));
return takeLabDeferred.promise().then((labInstance) => {
this._labInstance = labInstance;
this.loadItem(labInstance);
});
}
private loadItem(labInstance: Labs.LabInstance) {
var activityComponent = <Labs.Components.ActivityComponentInstance> this._labInstance.components[0];
this._links.get(activityComponent.component.data.id).done((item) => {
var attemptsDeferred = $.Deferred();
activityComponent.getAttempts(createCallback(attemptsDeferred));
var attemptP = attemptsDeferred.promise().then((attempts) => {
var currentAttemptDeferred = $.Deferred();
if (attempts.length > 0) {
currentAttemptDeferred.resolve(attempts[attempts.length - 1]);
} else {
activityComponent.createAttempt(createCallback(currentAttemptDeferred));
}
return currentAttemptDeferred.then((currentAttempt: Labs.Components.ActivityComponentAttempt) => {
var resumeDeferred = $.Deferred();
currentAttempt.resume(createCallback(resumeDeferred));
return resumeDeferred.promise().then(() => {
return currentAttempt;
});
});
});
return attemptP.then((attempt: Labs.Components.ActivityComponentAttempt) => {
var completeDeferred = $.Deferred();
if (attempt.getState() !== Labs.ProblemState.Completed) {
attempt.complete(createCallback(completeDeferred));
} else {
completeDeferred.resolve();
}
this._appView("view");
this.item(item);
return completeDeferred.promise();
});
});
}
//
// Action invoked when the user clicks on the insert button on the details page
//
onInsertClick() {
var configuration = this._links.buildConfiguration(this.item());
if (this._labEditor) {
this._labEditor.setConfiguration(configuration, (err, unused) => {
this._userView("view");
});
}
}
//
// Method invoked when the user clicks on a selection and wants to move to the details page
//
moveToDetailPage(content: AppContent) {
this._links.get(content.id).then((item: AppContent) => {
this.item(item);
});
}
//
// Moves back to the select page
//
moveToSelectPage() {
this.item(null);
}
//
// Callback inoked when a search occurs
//
search() {
this.setActiveDomain(this.activeDomain());
}
}
export function initialize(driver: AppDataDriver) {
$(document).ready(() => {
// Initialize Labs.JS
Labs.connect((err, connectionResponse) => {
var viewModel = new MixAppBrowserViewModel(driver, connectionResponse.mode);
ko.applyBindings(viewModel);
});
});
}
}