Proyectos de Subversion Moodle

Rev

Autoría | Ultima modificación | Ver Log |

{"version":3,"file":"user_picker.min.js","sources":["../../../../../src/local/grades/local/grader/user_picker.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * This module will tie together all of the different calls the gradable module will make.\n *\n * @module     mod_forum/local/grades/local/grader/user_picker\n * @copyright  2019 Mathew May <mathew.solutions>\n * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport Templates from 'core/templates';\nimport Selectors from './user_picker/selectors';\nimport {getString} from 'core/str';\n\nconst templatePath = 'mod_forum/local/grades/local/grader';\n\n/**\n * The Grader User Picker.\n *\n * @class mod_forum/local/grades/local/grader/user_picker\n */\nclass UserPicker {\n\n    /**\n     * Constructor for the User Picker.\n     *\n     * @constructor mod_forum/local/grades/local/grader/user_picker\n     * @param {Array} userList List of users\n     * @param {Function} showUserCallback The callback used to display the user\n     * @param {Function} preChangeUserCallback The callback to use before changing user\n     */\n    constructor(userList, showUserCallback, preChangeUserCallback) {\n        this.userList = userList;\n        this.showUserCallback = showUserCallback;\n        this.preChangeUserCallback = preChangeUserCallback;\n        this.currentUserIndex = 0;\n\n        // Ensure that render is bound correctly.\n        this.render = this.render.bind(this);\n        this.setUserId = this.setUserId.bind(this);\n    }\n\n    /**\n     * Set the current userid without rendering the change.\n     * To show the user, call showUser too.\n     *\n     * @param {Number} userId\n     */\n    setUserId(userId) {\n        // Determine the current index based on the user ID.\n        const userIndex = this.userList.findIndex(user => {\n            return user.id === parseInt(userId);\n        });\n\n        if (userIndex === -1) {\n            throw Error(`User with id ${userId} not found`);\n        }\n\n        this.currentUserIndex = userIndex;\n    }\n\n    /**\n     * Render the user picker.\n     */\n    async render() {\n        // Create the root node.\n        this.root = document.createElement('div');\n\n        const {html, js} = await this.renderNavigator();\n        Templates.replaceNodeContents(this.root, html, js);\n\n        // Call the showUser function to show the first user immediately.\n        await this.showUser(this.currentUser);\n\n        // Ensure that the event listeners are all bound.\n        this.registerEventListeners();\n    }\n\n    /**\n     * Render the navigator itself.\n     *\n     * @returns {Promise}\n     */\n    renderNavigator() {\n        return Templates.renderForPromise(`${templatePath}/user_picker`, {});\n    }\n\n    /**\n     * Render the current user details for the picker.\n     *\n     * @param {Object} context The data used to render the user picker.\n     * @returns {Promise}\n     */\n    renderUserChange(context) {\n        return Templates.renderForPromise(`${templatePath}/user_picker/user`, context);\n    }\n\n    /**\n     * Show the specified user in the picker.\n     *\n     * @param {Object} user\n     */\n    async showUser(user) {\n        const [{html, js}] = await Promise.all([this.renderUserChange(user), this.showUserCallback(user)]);\n        const userRegion = this.root.querySelector(Selectors.regions.userRegion);\n        Templates.replaceNodeContents(userRegion, html, js);\n\n        // Update the hidden now-grading region so screen readers can announce the user that's currently being graded.\n        const currentUserRegion = this.root.querySelector(Selectors.regions.currentUser);\n        currentUserRegion.textContent = await getString('nowgradinguser', 'mod_forum', user.fullname);\n    }\n\n    /**\n     * Register the event listeners for the user picker.\n     */\n    registerEventListeners() {\n        this.root.addEventListener('click', async(e) => {\n            const button = e.target.closest(Selectors.actions.changeUser);\n\n            if (button) {\n                const result = await this.preChangeUserCallback(this.currentUser);\n\n                if (!result.failed) {\n                    this.updateIndex(parseInt(button.dataset.direction));\n                    await this.showUser(this.currentUser);\n                }\n            }\n        });\n    }\n\n    /**\n     * Update the current user index.\n     *\n     * @param {Number} direction\n     * @returns {Number}}\n     */\n    updateIndex(direction) {\n        this.currentUserIndex += direction;\n\n        // Loop around the edges.\n        if (this.currentUserIndex < 0) {\n            this.currentUserIndex = this.userList.length - 1;\n        } else if (this.currentUserIndex > this.userList.length - 1) {\n            this.currentUserIndex = 0;\n        }\n\n        return this.currentUserIndex;\n    }\n\n    /**\n     * Get the details of the user currently shown with the total number of users, and the 1-indexed count of the\n     * current user.\n     *\n     * @returns {Object}\n     */\n    get currentUser() {\n        return {\n            ...this.userList[this.currentUserIndex],\n            total: this.userList.length,\n            displayIndex: this.currentUserIndex + 1,\n        };\n    }\n\n    /**\n     * Get the root node for the User Picker.\n     *\n     * @returns {HTMLElement}\n     */\n    get rootNode() {\n        return this.root;\n    }\n}\n\n/**\n * Create a new user picker.\n *\n * @param {Array} users The list of users\n * @param {Function} showUserCallback The function to call to show a specific user\n * @param {Function} preChangeUserCallback The fucntion to call to save the grade for the current user\n * @param {Number} [currentUserID] The userid of the current user\n * @returns {UserPicker}\n */\nexport default async(\n    users,\n    showUserCallback,\n    preChangeUserCallback,\n    {\n        initialUserId = null,\n    } = {}\n) => {\n    const userPicker = new UserPicker(users, showUserCallback, preChangeUserCallback);\n    if (initialUserId) {\n        userPicker.setUserId(initialUserId);\n    }\n    await userPicker.render();\n\n    return userPicker;\n};\n"],"names":["UserPicker","constructor","userList","showUserCallback","preChangeUserCallback","currentUserIndex","render","this","bind","setUserId","userId","userIndex","findIndex","user","id","parseInt","Error","root","document","createElement","html","js","renderNavigator","replaceNodeContents","showUser","currentUser","registerEventListeners","Templates","renderForPromise","renderUserChange","context","Promise","all","userRegion","querySelector","Selectors","regions","textContent","fullname","addEventListener","async","button","e","target","closest","actions","changeUser","failed","updateIndex","dataset","direction","length","total","displayIndex","rootNode","users","initialUserId","userPicker"],"mappings":";;;;;;;uLAkCMA,WAUFC,YAAYC,SAAUC,iBAAkBC,4BAC/BF,SAAWA,cACXC,iBAAmBA,sBACnBC,sBAAwBA,2BACxBC,iBAAmB,OAGnBC,OAASC,KAAKD,OAAOE,KAAKD,WAC1BE,UAAYF,KAAKE,UAAUD,KAAKD,MASzCE,UAAUC,cAE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