Proyectos de Subversion Moodle

Rev

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

{"version":3,"file":"categorymanager.min.js","sources":["../src/categorymanager.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 * Reactive module for category manager\n *\n * @module qbank_managecategories/categorymanager\n */\n\nimport {Reactive} from 'core/reactive';\nimport {get_string as getString} from 'core/str';\nimport {mutations} from 'qbank_managecategories/mutations';\nimport {eventTypes, notifyQbankManagecategoriesStateUpdated} from 'qbank_managecategories/events';\nimport Ajax from \"core/ajax\";\nimport Notification from \"core/notification\";\nimport ModalForm from 'core_form/modalform';\n\nconst SELECTORS = {\n    CATEGORY_LIST: '.qbank_managecategories-categorylist',\n    CONTEXT: '.qbank_managecategories-categorylist[data-contextid]',\n    CATEGORY_ITEM: '.qbank_managecategories-item[data-categoryid]',\n    CATEGORY_ROOT: '#categoryroot',\n    SHOWDESCRIPTIONS_TOGGLE: '#showdescriptions-toggle',\n    ADD_EDIT_BUTTON: '[data-action=\"addeditcategory\"]',\n};\n\nconst CLASSES = {\n    DRAGHANDLE: 'draghandle',\n    DANGER: 'alert-danger',\n};\n\n/**\n * Load the initial state.\n *\n * This iterates over the initial tree of category items, and captures the data required for the state from each category.\n * It also captures a count of the number of children in each list.\n *\n * @param {Reactive} reactive\n * @return {Promise<void>}\n */\nconst loadState = async(reactive) => {\n    const rootElement = document.querySelector(SELECTORS.CATEGORY_ROOT);\n    const stateData = {\n        page: {\n            contextid: rootElement.dataset.contextid,\n            showdescriptions: document.querySelector(SELECTORS.SHOWDESCRIPTIONS_TOGGLE).checked,\n        },\n        categories: [],\n        categoryLists: [],\n    };\n    const listItems = document.querySelectorAll(SELECTORS.CATEGORY_ITEM);\n    listItems.forEach(item => {\n        stateData.categories.push({\n            id: item.dataset.categoryid,\n            name: item.dataset.categoryname,\n            parent: item.dataset.parent,\n            contextid: item.dataset.contextid,\n            sortorder: item.dataset.sortorder,\n            draghandle: item.classList.contains(CLASSES.DRAGHANDLE),\n        });\n    });\n    const categoryLists = document.querySelectorAll(SELECTORS.CATEGORY_LIST);\n    categoryLists.forEach(categoryList => {\n        stateData.categoryLists.push({\n            id: categoryList.dataset.categoryid,\n            childCount: categoryList.querySelectorAll(SELECTORS.CATEGORY_ITEM).length,\n        });\n    });\n    reactive.setInitialState(stateData);\n};\n\n/**\n * Reactive instance for the category manager.\n */\nclass CategoryManager extends Reactive {\n    /**\n     * Move a category to a new position within the given parent.\n     *\n     * This will call the move_category web service function to re-order the categories, then update\n     * the state with the returned updates.\n     *\n     * @param {Number} categoryId The ID of the category being moved.\n     * @param {Number} targetParentId The ID of the destination parent category (this may not have changed).\n     * @param {Number} precedingSiblingId The ID of the category to put the moved category after.\n     *     This may be null if moving to the top of a list.\n     */\n    moveCategory(\n        categoryId,\n        targetParentId,\n        precedingSiblingId = null,\n    ) {\n        const call = {\n            methodname: 'qbank_managecategories_move_category',\n            args: {\n                pagecontextid: this.state.page.contextid,\n                categoryid: categoryId,\n                targetparentid: targetParentId,\n                precedingsiblingid: precedingSiblingId,\n            }\n        };\n        Ajax.call([call])[0]\n            .then((stateUpdates) => {\n                this.stateManager.processUpdates(stateUpdates);\n                return stateUpdates;\n            })\n            .catch(error => {\n                Notification.addNotification({\n                    message: error.message,\n                    type: 'error',\n                });\n                document.getElementsByClassName(CLASSES.DANGER)[0]?.scrollIntoView();\n            });\n    }\n\n    /**\n     * Return title for the add/edit modal.\n     *\n     * @param {boolean} isEdit is 'add' or 'edit' form\n     * @returns {String} title string\n     */\n    getTitle(isEdit) {\n        return getString(isEdit ? 'editcategory' : 'addcategory', 'question');\n    }\n\n    /**\n     * Return save button label for the add/edit modal.\n     *\n     * @param {boolean} isEdit is 'add' or 'edit' form\n     * @returns {String} save string\n     */\n    getSave(isEdit) {\n        return isEdit ? getString('savechanges', 'core') : getString('addcategory', 'question');\n    }\n\n    /**\n     * Function handling display of modal form.\n     *\n     * @param {Event} e The click event triggering the modal.\n     */\n    showEditModal(e) {\n        const addEditButton = e.target.closest(SELECTORS.ADD_EDIT_BUTTON);\n\n        // Return if it is not 'addeditcategory' button.\n        if (!addEditButton) {\n            return;\n        }\n\n        // Return if the action type is not specified.\n        if (!addEditButton.dataset.actiontype) {\n            return;\n        }\n\n        e.preventDefault();\n        // Data for the modal.\n        const title = categorymanager.getTitle(addEditButton.dataset.actiontype === 'edit');\n        const save = categorymanager.getSave(addEditButton.dataset.actiontype === 'edit');\n        const cmid = addEditButton.dataset.cmid;\n        const courseid = addEditButton.dataset.courseid;\n        const questioncount = addEditButton.dataset.questioncount;\n        let contextid = addEditButton.dataset.contextid;\n        let categoryid = null;\n        let sortorder = null;\n        let parent = null;\n        const categoryItem = e.target.closest(SELECTORS.CATEGORY_ITEM);\n        if (categoryItem) {\n            contextid = categoryItem.dataset.contextid;\n            categoryid = categoryItem.dataset.categoryid;\n            sortorder = categoryItem.dataset.sortorder;\n            const parentContext = categoryItem.closest(SELECTORS.CONTEXT);\n            parent = categoryItem.dataset.parent + ',' + parentContext.dataset.contextid;\n        }\n\n        // Call the modal.\n        const modalForm = new ModalForm({\n            formClass: \"qbank_managecategories\\\\form\\\\question_category_edit_form\",\n            args: {\n                cmid,\n                courseid,\n                questioncount,\n                contextid,\n                categoryid,\n                sortorder,\n                parent,\n            },\n            modalConfig: {\n                title: title,\n                large: true,\n            },\n            saveButtonText: save,\n            returnFocus: addEditButton,\n        });\n        // Once the form has been submitted via the web service, update the state with the new or updated\n        // category based on the web service response.\n        modalForm.addEventListener(modalForm.events.FORM_SUBMITTED, (response) => {\n            categorymanager.stateManager.processUpdates(response.detail);\n        });\n        // Show the form.\n        modalForm.show();\n    }\n}\n\nexport const categorymanager = new CategoryManager({\n    name: 'qtype_managecategories_categorymanager',\n    eventName: eventTypes.qbankManagecategoriesStateUpdated,\n    eventDispatch: notifyQbankManagecategoriesStateUpdated,\n    mutations,\n});\n\n/**\n * Load the initial state.\n */\nexport const init = () => {\n    loadState(categorymanager);\n};\n"],"names":["SELECTORS","CLASSES","CategoryManager","Reactive","moveCategory","categoryId","targetParentId","precedingSiblingId","call","methodname","args","pagecontextid","this","state","page","contextid","categoryid","targetparentid","precedingsiblingid","then","stateUpdates","stateManager","processUpdates","catch","error","addNotification","message","type","document","getElementsByClassName","scrollIntoView","getTitle","isEdit","getSave","showEditModal","e","addEditButton","target","closest","dataset","actiontype","preventDefault","title","categorymanager","save","cmid","courseid","questioncount","sortorder","parent","categoryItem","parentContext","modalForm","ModalForm","formClass","modalConfig","large","saveButtonText","returnFocus","addEventListener","events","FORM_SUBMITTED","response","detail","show","name","eventName","eventTypes","qbankManagecategoriesStateUpdated","eventDispatch","notifyQbankManagecategoriesStateUpdated","mutations","async","stateData","querySelector","showdescriptions","checked","categories","categoryLists","querySelectorAll","forEach","item","push","id","categoryname","draghandle","classList","contains","categoryList","childCount","length","reactive","setInitialState","loadState"],"mappings":"ymBA6BMA,wBACa,uCADbA,kBAEO,uDAFPA,wBAGa,gDAHbA,wBAIa,gBAJbA,kCAKuB,2BALvBA,0BAMe,kCAGfC,mBACU,aADVA,eAEM,qBA8CNC,wBAAwBC,mBAY1BC,aACIC,WACAC,oBACAC,0EAAqB,WAEfC,KAAO,CACTC,WAAY,uCACZC,KAAM,CACFC,cAAeC,KAAKC,MAAMC,KAAKC,UAC/BC,WAAYX,WACZY,eAAgBX,eAChBY,mBAAoBX,mCAGvBC,KAAK,CAACA,OAAO,GACbW,MAAMC,oBACEC,aAAaC,eAAeF,cAC1BA,gBAEVG,OAAMC,wDACUC,gBAAgB,CACzBC,QAASF,MAAME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