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
|
const getDefaultState = () => {
return {
children: [],
ordered: [],
};
};
const initialState = getDefaultState();
const state = { ...initialState };
const getters = {
children(state) {
return (id) => state.children[id] ?? [];
},
ordered(state) {
return state.ordered;
},
};
export const mutations = {
reset(state) {
state = getDefaultState();
},
setChildren(state, children) {
state.children = children;
},
setOrdered(state, ordered) {
state.ordered = ordered;
},
};
const actions = {
build({ commit, rootGetters }) {
const instance = rootGetters['courseware'];
if (!instance) {
throw new Error('Could not find current courseware');
}
const root = rootGetters['courseware-structural-elements/related']({
parent: { id: instance.id, type: instance.type },
relationship: 'root',
});
if (!root) {
commit('reset');
return;
}
const structuralElements = rootGetters['courseware-structural-elements/all'];
const children = structuralElements.reduce((memo, element) => {
const parent = element.relationships.parent?.data?.id ?? null;
if (parent) {
if (!memo[parent]) {
memo[parent] = [];
}
memo[parent].push([element.id, element.attributes.position]);
}
return memo;
}, {});
for (const key of Object.keys(children)) {
children[key].sort((childA, childB) => childA[1] - childB[1]);
children[key] = children[key].map(([id]) => id);
}
commit('setChildren', children);
const ordered = [...visitTree(children, root.id)];
commit('setOrdered', ordered);
},
invalidateCache({ rootGetters }) {
const courseware = rootGetters['courseware'];
if (!courseware) {
return;
}
const element = rootGetters['courseware-structural-elements/related']({
parent: { id: courseware.id, type: courseware.type },
relationship: 'root',
});
if (!element) {
return;
}
const cache = window.STUDIP.Cache.getInstance('courseware');
const cacheKey = `descendants/${element.id}/${rootGetters['userId']}`;
try {
cache.remove(cacheKey);
} catch (e) {
// nothing we can do
}
},
// load the structure of the current courseware
async load({ commit, dispatch, rootGetters }) {
const context = rootGetters['context'];
const instance = await dispatch('loadInstance', context);
commit('coursewareSet', instance, { root: true });
const root = rootGetters['courseware-structural-elements/related']({
parent: { id: instance.id, type: instance.type },
relationship: 'root',
});
if (!root) {
throw new Error(`Could not find root of courseware { id: ${instance.id}, type: ${instance.type}`);
}
dispatch('fetchDescendantsWithCaching', { root });
return instance;
},
// load the structure of a specified courseware
async loadAnotherCourseware({ commit, dispatch, rootGetters }, context) {
const instance = await dispatch('loadInstance', context);
const root = rootGetters['courseware-structural-elements/related']({
parent: { id: instance.id, type: instance.type },
relationship: 'root',
});
if (!root) {
throw new Error(`Could not find root of courseware { id: ${instance.id}, type: ${instance.type}`);
}
await dispatch('loadDescendants', { root });
return instance;
},
loadInstance({ commit, dispatch, rootGetters }, context) {
const parent = context;
const relationship = 'courseware';
const options = {
include: 'bookmarks,root',
};
return dispatch(
`courseware-instances/loadRelated`,
{
parent,
relationship,
options,
},
{ root: true }
).then(() => {
return rootGetters['courseware-instances/related']({ parent, relationship });
});
},
async fetchDescendantsWithCaching({ dispatch, rootGetters, commit }, { root }) {
const cache = window.STUDIP.Cache.getInstance('courseware');
const cacheKey = `descendants/${root.id}/${rootGetters['userId']}`;
await unpickleStaleDescendants();
return revalidateDescendants();
function unpickleStaleDescendants() {
try {
const descendants = cache.get(cacheKey);
const cacheHit = descendants !== undefined;
if (cacheHit) {
commit('courseware-structural-elements/REPLACE_ALL_RECORDS', descendants, { root: true });
}
} catch (e) {
return;
}
}
function revalidateDescendants() {
return dispatch('loadDescendants', { root }).then(removeStaleElements).then(pickleDescendants);
}
function pickleDescendants() {
try {
cache.set(cacheKey, rootGetters['courseware-structural-elements/all']);
} catch (e) {
// No action necessary
}
}
function removeStaleElements() {
const idsToKeep = [
root.id,
...rootGetters['courseware-structural-elements/related']({
parent: root,
relationship: 'descendants',
}).map(({ id }) => id),
];
rootGetters['courseware-structural-elements/all']
.map(({ id }) => id)
.filter((id) => !idsToKeep.includes(id))
.forEach((id) => commit('courseware-structural-elements/REMOVE_RECORD', { id }, { root: true }));
}
},
loadDescendants({ dispatch }, { root }) {
const parent = { id: root.id, type: root.type };
const relationship = 'descendants';
const options = {
'page[offset]': 0,
'page[limit]': 10000,
};
return dispatch(
'courseware-structural-elements/loadRelated',
{ parent, relationship, options },
{ root: true }
);
},
};
function* visitTree(tree, current) {
if (current) {
yield current;
const children = tree[current];
if (children) {
for (let index = 0; index < children.length; index++) {
yield* visitTree(tree, children[index]);
}
}
}
}
export default {
namespaced: true,
actions,
getters,
mutations,
state,
};
|