-
Notifications
You must be signed in to change notification settings - Fork 635
/
LearnImmersiveLayout.vue
523 lines (497 loc) · 15.9 KB
/
LearnImmersiveLayout.vue
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
<template>
<div
v-if="!loading"
ref="mainWrapper"
class="main-wrapper"
>
<div v-if="blockDoubleClicks" class="click-mask"></div>
<SkipNavigationLink />
<LearningActivityBar
ref="activityBar"
:resourceTitle="resourceTitle"
:learningActivities="content.learning_activities"
:isLessonContext="lessonContext"
:isBookmarked="bookmark ? true : bookmark"
:isCoachContent="isCoachContent"
:contentProgress="contentProgress"
:allowMarkComplete="allowMarkComplete"
:contentKind="content.kind"
:showBookmark="isUserLoggedIn"
data-test="learningActivityBar"
@navigateBack="navigateBack"
@toggleBookmark="toggleBookmark"
@viewResourceList="toggleResourceList"
@viewInfo="openSidePanel"
@completionModal="openCompletionModal"
/>
<KLinearLoader
v-if="loading"
class="loader"
type="indeterminate"
:delay="false"
/>
<KPageContainer v-if="notAuthorized">
<AuthMessage
:authorizedRole="authorizedRole"
:header="authorizationErrorHeader"
:details="authorizationErrorDetails"
/>
</KPageContainer>
<KPageContainer v-else-if="error">
<AppError />
</KPageContainer>
<div
v-else
id="main"
role="main"
tabindex="-1"
class="main"
>
<ContentPage
ref="contentPage"
class="content"
data-test="contentPage"
:content="content"
:lessonId="lessonId"
:allowMarkComplete="allowMarkComplete"
@finished="$refs.activityBar && $refs.activityBar.animateNextSteps()"
/>
</div>
<GlobalSnackbar />
<!-- Side Panel for content metadata -->
<FullScreenSidePanel
v-if="sidePanelContent"
ref="resourcePanel"
alignment="right"
@closePanel="sidePanelContent = null"
@shouldFocusFirstEl="findFirstEl()"
>
<template #header>
<!-- Flex styles tested in ie11 and look good. Ensures good spacing between
multiple chips - not a common thing but just in case -->
<div
v-for="activity in sidePanelContent.learning_activities"
:key="activity"
class="side-panel-chips"
:class="$computedClass({ '::after': {
content: '',
flex: 'auto'
} })"
>
<LearningActivityChip
class="chip"
style="margin-left: 8px; margin-bottom: 8px;"
:kind="activity"
/>
</div>
</template>
<CurrentlyViewedResourceMetadata
:content="sidePanelContent"
:canDownloadContent="canDownload"
/>
</FullScreenSidePanel>
<!-- Side Panel for "view resources" or "lesson resources" -->
<FullScreenSidePanel
v-if="showViewResourcesSidePanel"
ref="resourcePanel"
class="also-in-this-side-panel"
alignment="right"
@closePanel="showViewResourcesSidePanel = false"
@shouldFocusFirstEl="findFirstEl()"
>
<template #header>
<h2 style="margin: 0;">
{{ viewResourcesTitle }}
</h2>
</template>
<AlsoInThis
style="margin-top: 8px"
:contentNodes="viewResourcesContents"
:nextContent="nextContent"
:isLesson="lessonContext"
:loading="resourcesSidePanelLoading"
/>
</FullScreenSidePanel>
</div>
</template>
<script>
import { mapGetters, mapState } from 'vuex';
import get from 'lodash/get';
import responsiveWindowMixin from 'kolibri.coreVue.mixins.responsiveWindowMixin';
import { crossComponentTranslator } from 'kolibri.utils.i18n';
import AuthMessage from 'kolibri.coreVue.components.AuthMessage';
import FullScreenSidePanel from 'kolibri.coreVue.components.FullScreenSidePanel';
import { ContentNodeResource } from 'kolibri.resources';
import commonCoreStrings from 'kolibri.coreVue.mixins.commonCoreStrings';
import client from 'kolibri.client';
import urls from 'kolibri.urls';
import GlobalSnackbar from '../../../../../../kolibri/core/assets/src/views/GlobalSnackbar';
import SkipNavigationLink from '../../../../../../kolibri/core/assets/src/views/SkipNavigationLink';
import AppError from '../../../../../../kolibri/core/assets/src/views/AppError';
import useCoreLearn from '../composables/useCoreLearn';
import useContentNodeProgress from '../composables/useContentNodeProgress';
import useLearnerResources from '../composables/useLearnerResources';
import LearningActivityChip from './LearningActivityChip';
import LessonResourceViewer from './classes/LessonResourceViewer';
import CurrentlyViewedResourceMetadata from './CurrentlyViewedResourceMetadata';
import ContentPage from './ContentPage';
import LearningActivityBar from './LearningActivityBar';
import AlsoInThis from './AlsoInThis';
const lessonStrings = crossComponentTranslator(LessonResourceViewer);
export default {
name: 'LearnImmersiveLayout',
metaInfo() {
return {
// Use arrow function to bind $tr to this component
titleTemplate: title => {
if (this.error) {
return this.$tr('kolibriTitleMessage', { title: this.$tr('errorPageTitle') });
}
if (!title) {
// If no child component sets title, it reads 'Kolibri'
return this.coreString('kolibriLabel');
}
// If child component sets title, it reads 'Child Title - Kolibri'
return this.$tr('kolibriTitleMessage', { title });
},
title: this.pageTitle,
};
},
components: {
AlsoInThis,
AppError,
AuthMessage,
ContentPage,
FullScreenSidePanel,
GlobalSnackbar,
LearningActivityBar,
LearningActivityChip,
CurrentlyViewedResourceMetadata,
SkipNavigationLink,
},
mixins: [responsiveWindowMixin, commonCoreStrings],
setup() {
const { canDownload } = useCoreLearn();
const {
fetchContentNodeProgress,
fetchContentNodeTreeProgress,
contentNodeProgressMap,
} = useContentNodeProgress();
const { fetchLesson } = useLearnerResources();
return {
canDownload,
contentNodeProgressMap,
fetchContentNodeProgress,
fetchContentNodeTreeProgress,
fetchLesson,
};
},
props: {
content: {
type: Object,
required: false,
default: null,
},
// AUTHORIZATION SPECIFIC
authorized: {
type: Boolean,
required: false,
default: true,
},
authorizedRole: {
type: String,
default: null,
},
// link to where the 'back' button should go
back: {
type: Object,
required: true,
default: null,
},
},
data() {
return {
bookmark: null,
sidePanelContent: null,
showViewResourcesSidePanel: false,
nextContent: null,
viewResourcesContents: [],
resourcesSidePanelFetched: false,
resourcesSidePanelLoading: false,
};
},
computed: {
...mapGetters(['currentUserId', 'isUserLoggedIn']),
...mapState({
error: state => state.core.error,
loading: state => state.core.loading,
blockDoubleClicks: state => state.core.blockDoubleClicks,
}),
...mapState('topicsTree', {
isCoachContent: state => (state.content.coach_content ? 1 : 0),
}),
contentProgress() {
return this.contentNodeProgressMap[this.content.content_id];
},
notAuthorized() {
// catch "not authorized" error, display AuthMessage
if (
this.error &&
this.error.response &&
this.error.response.status &&
this.error.response.status == 403
) {
return true;
}
return !this.authorized;
},
resourceTitle() {
return this.content ? this.content.title : '';
},
allowMarkComplete() {
return get(this, ['content', 'options', 'completion_criteria', 'learner_managed'], false);
},
lessonContext() {
return Boolean(this.lessonId);
},
lessonId() {
return get(this.back, 'params.lessonId', null);
},
viewResourcesTitle() {
/* eslint-disable kolibri/vue-no-undefined-string-uses */
return this.lessonContext
? lessonStrings.$tr('nextInLesson')
: this.content && this.content.ancestors.slice(-1)[0].title;
/* eslint-enable */
},
},
watch: {
content(newContent, oldContent) {
if ((newContent && !oldContent) || newContent.id !== oldContent.id) {
this.initializeState();
}
},
showViewResourcesSidePanel(newVal, oldVal) {
if (newVal && !oldVal) {
this.getSidebarInfo();
}
},
},
created() {
this.initializeState();
},
methods: {
initializeState() {
this.bookmark = null;
this.showViewResourcesSidePanel = false;
this.nextContent = null;
this.viewResourcesContents = [];
this.resourcesSidePanelFetched = false;
this.resourcesSidePanelLoading = false;
if (this.content) {
const id = this.content.id;
client({
method: 'get',
url: urls['kolibri:core:bookmarks-list'](),
params: { contentnode_id: this.content.id },
}).then(response => {
// As the component never gets fully torn down
// this request could be stale. Only set bookmark
// data in the case that the ids still match.
if (this.content && this.content.id === id) {
this.bookmark = response.data[0] || false;
}
});
}
},
getSidebarInfo() {
if (!this.resourcesSidePanelFetched && !this.resourcesSidePanelLoading) {
this.resourcesSidePanelLoading = true;
let promise = Promise.resolve();
if (this.lessonId) {
promise = this.fetchLessonSiblings();
} else if (this.content && this.content.id) {
promise = this.fetchSiblings();
}
if (promise) {
promise.then(() => {
this.resourcesSidePanelLoading = false;
this.resourcesSidePanelFetched = true;
});
}
}
},
/**
* When a lessonId is given, this method will fetch the lesson and then fetch its
* content nodes. The user is guaranteed to be logged in if there is a lessonId.
*
* The nodes' progresses are mapped via the useContentNodeProgress composable
*
* @modifies this.viewResourcesContents - Assigned the content nodes retrieved
* @modifies useContentNodeProgress.contentNodeProgressMap (indirectly)
*/
fetchLessonSiblings() {
// Get the lesson and then assign its resources to this.viewResourcesContents
// fetchLesson also handles fetching the progress data for this lesson and
// the content node data for the resources
this.fetchLesson({ lessonId: this.lessonId }).then(lesson => {
// Filter out this.content
this.viewResourcesContents = lesson.resources
.filter(n => n.contentnode && n.contentnode_id !== this.content.id)
.map(n => n.contentnode);
});
},
/**
* Prepares a list of content nodes which are children of this.content.parent without
* this.content and calls fetchContentNodeProgress when the user is logged in.
*
* Then it will fetch the "next folder" - which is the next content for this.content that
* is a topic.
*
* @modifies this.viewResourcesContents - Sets it to the progress-mapped nodes
* @modifies this.nextContent - Sets the value with this.content's parents next sibling folder
* if found
* @modifies useContentNodeProgress.contentNodeProgressMap (indirectly) if the user
* is logged in
*/
fetchSiblings() {
// Fetch the next content
const nextPromise = ContentNodeResource.fetchNextContent(this.content.parent, {
topicOnly: true,
}).then(nextContent => {
// This may return the immediate parent if nothing else is found so let's be sure
// not to assign that
if (nextContent && this.content.parent !== nextContent.id) {
this.nextContent = nextContent;
}
});
const treeParams = {
id: this.content.parent,
params: {
include_coach_content:
this.$store.getters.isAdmin ||
this.$store.getters.isCoach ||
this.$store.getters.isSuperuser,
depth: 1,
},
};
// Fetch and map the progress for the nodes if logged in
if (this.$store.getters.isUserLoggedIn) {
this.fetchContentNodeTreeProgress(treeParams);
}
const treePromise = ContentNodeResource.fetchTree(treeParams).then(parent => {
// Filter out this.content
this.viewResourcesContents = parent.children.results.filter(
n => n.id !== this.content.id
);
});
return Promise.all([nextPromise, treePromise]);
},
navigateBack() {
this.$router.push(this.back);
},
openSidePanel() {
this.sidePanelContent = this.content;
},
toggleBookmark() {
if (this.bookmark) {
client({
method: 'delete',
url: urls['kolibri:core:bookmarks_detail'](this.bookmark.id),
}).then(() => {
this.bookmark = false;
});
} else if (this.bookmark === false) {
const id = this.content.id;
client({
method: 'post',
url: urls['kolibri:core:bookmarks-list'](),
data: {
contentnode_id: id,
user: this.currentUserId,
},
}).then(response => {
if (this.content && this.content.id === id) {
// Don't set a stale response if a user
// navigated away before the bookmark finished.
this.bookmark = response.data;
}
});
}
},
toggleResourceList() {
this.showViewResourcesSidePanel = true;
},
findFirstEl() {
if (this.$refs.embeddedPanel) {
this.$refs.embeddedPanel.focusFirstEl();
} else {
this.$refs.resourcePanel.focusFirstEl();
}
},
openCompletionModal() {
if (this.$refs.contentPage) {
this.$refs.contentPage.displayCompletionModal();
}
},
},
$trs: {
kolibriTitleMessage: {
message: '{ title } - Kolibri',
context: 'DO NOT TRANSLATE\nCopy the source string.',
},
errorPageTitle: {
message: 'Error',
context:
"When Kolibri throws an error, this is the text that's used as the title of the error page. The description of the error follows below.",
},
},
};
</script>
<style lang="scss" scoped>
@import '~kolibri-design-system/lib/styles/definitions';
.main-wrapper {
display: inline-block;
width: 100%;
@media print {
/* Without this, things won't print correctly
* - Firefox: Tables will get cutoff
* - Chrome: Table header won't repeat correctly on each page
*/
display: block;
}
}
// When focused by SkipNavigationLink, don't outline non-buttons/links
/deep/ [tabindex='-1'] {
outline-style: none !important;
}
.click-mask {
position: fixed;
top: 0;
left: 0;
z-index: 24;
width: 100%;
height: 100%;
}
.loader {
position: fixed;
right: 0;
left: 0;
}
.also-in-this-side-panel {
/deep/ .side-panel {
padding-bottom: 0;
}
}
.side-panel-chips {
display: flex;
flex-wrap: wrap;
justify-content: flex-start;
margin-bottom: -8px;
margin-left: -8px;
}
.chip {
margin-bottom: 8px;
margin-left: 8px;
}
</style>