Commit | Line | Data |
---|---|---|
bae67469 MM |
1 | // This file is part of Moodle - http://moodle.org/ |
2 | // | |
3 | // Moodle is free software: you can redistribute it and/or modify | |
4 | // it under the terms of the GNU General Public License as published by | |
5 | // the Free Software Foundation, either version 3 of the License, or | |
6 | // (at your option) any later version. | |
7 | // | |
8 | // Moodle is distributed in the hope that it will be useful, | |
9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | // GNU General Public License for more details. | |
12 | // | |
13 | // You should have received a copy of the GNU General Public License | |
14 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
15 | ||
16 | /** | |
17 | * This module will tie together all of the different calls the gradable module will make. | |
18 | * | |
19 | * @module mod_forum/local/grades/grader | |
20 | * @package mod_forum | |
21 | * @copyright 2019 Mathew May <mathew.solutions> | |
22 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
23 | */ | |
24 | import Templates from 'core/templates'; | |
25 | // TODO import Notification from 'core/notification'; | |
26 | import Selectors from './local/grader/selectors'; | |
27 | import * as UserPicker from './local/grader/user_picker'; | |
28 | import {createLayout as createFullScreenWindow} from 'mod_forum/local/layout/fullscreen'; | |
29 | ||
30 | const templateNames = { | |
31 | grader: { | |
32 | app: 'mod_forum/local/grades/grader', | |
33 | }, | |
34 | }; | |
35 | ||
36 | const displayUserPicker = (root, html) => { | |
37 | const pickerRegion = root.querySelector(Selectors.regions.pickerRegion); | |
38 | Templates.replaceNodeContents(pickerRegion, html, ''); | |
39 | }; | |
40 | ||
41 | const getUpdateUserContentFunction = (root, getContentForUser) => { | |
42 | return async(user) => { | |
43 | const [ | |
44 | {html, js}, | |
45 | ] = await Promise.all([ | |
46 | getContentForUser(user.id).then((html, js) => { | |
47 | return {html, js}; | |
48 | }), | |
49 | ]); | |
50 | Templates.replaceNodeContents(root.querySelector(Selectors.regions.moduleReplace), html, js); | |
51 | }; | |
52 | }; | |
53 | ||
54 | const registerEventListeners = (graderLayout) => { | |
55 | const graderContainer = graderLayout.getContainer(); | |
56 | graderContainer.addEventListener('click', (e) => { | |
57 | if (e.target.closest(Selectors.buttons.toggleFullscreen)) { | |
58 | e.stopImmediatePropagation(); | |
59 | e.preventDefault(); | |
60 | graderLayout.toggleFullscreen(); | |
61 | } else if (e.target.closest(Selectors.buttons.closeGrader)) { | |
62 | e.stopImmediatePropagation(); | |
63 | e.preventDefault(); | |
64 | ||
65 | graderLayout.close(); | |
66 | } | |
67 | }); | |
68 | }; | |
69 | ||
70 | // Make this explicit rather than object | |
71 | export const launch = async(getListOfUsers, getContentForUser, { | |
72 | initialUserId = 0, | |
73 | } = {}) => { | |
74 | ||
75 | const [ | |
76 | graderLayout, | |
77 | graderHTML, | |
78 | userList, | |
79 | ] = await Promise.all([ | |
80 | createFullScreenWindow({fullscreen: false, showLoader: false}), | |
81 | Templates.render(templateNames.grader.app, {}), | |
82 | getListOfUsers(), | |
83 | ]); | |
84 | const graderContainer = graderLayout.getContainer(); | |
85 | ||
86 | Templates.replaceNodeContents(graderContainer, graderHTML, ''); | |
87 | registerEventListeners(graderLayout); | |
88 | const updateUserContent = getUpdateUserContentFunction(graderContainer, getContentForUser); | |
89 | ||
90 | const pickerHTML = await UserPicker.buildPicker(userList, initialUserId, updateUserContent); | |
91 | displayUserPicker(graderContainer, pickerHTML); | |
92 | }; |