Proyectos de Subversion Moodle

Rev

Rev 1 | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

{"version":3,"file":"edit.min.js","sources":["../src/edit.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 * Edit items in feedback module\n *\n * @module     mod_feedback/edit\n * @copyright  2016 Marina Glancy\n * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\n\"use strict\";\n\nimport {addIconToContainerRemoveOnCompletion} from 'core/loadingicon';\nimport Notification from 'core/notification';\nimport Pending from 'core/pending';\nimport {prefetchStrings} from 'core/prefetch';\nimport SortableList from 'core/sortable_list';\nimport {getString, getStrings} from 'core/str';\nimport {add as addToast} from 'core/toast';\nimport {reorderQuestions} from 'mod_feedback/local/repository';\nimport Templates from 'core/templates';\n\nconst Selectors = {\n    deleteQuestionButton: '[data-action=\"delete\"]',\n    sortableListRegion: '[data-region=\"questions-sortable-list\"]',\n    sortableElement: '[data-region=\"questions-sortable-list\"] .feedback_itemlist[id]',\n    sortableElementTitle: '[data-region=\"item-title\"] span',\n    questionLabel: '[data-region=\"questions-sortable-list\"] .col-form-label',\n    actionsMenuData: '[data-item-actions-menu]',\n};\n\n/**\n * Returns the Feedback question item id from the DOM id of an item.\n *\n * @param {String} id The dom id, f.g.: feedback_item_22\n * @return int\n */\nconst getItemId = (id) => {\n    return Number(id.replace(/^.*feedback_item_/i, ''));\n};\n\n/**\n * Returns the order of the items in the sortable list.\n *\n * @param {Element} element The element to get the order from.\n * @return string\n */\nconst getItemOrder = (element) => {\n    const sortableList = element.closest(Selectors.sortableListRegion);\n    let itemOrder = [];\n    sortableList.querySelectorAll(Selectors.sortableElement).forEach((item) => {\n        itemOrder.push(getItemId(item.id));\n    });\n    return itemOrder.toString();\n};\n\nlet initialized = false;\nlet moduleId = null;\n\n/**\n * Initialise editor and all it's modules\n *\n * @param {Integer} cmId\n */\nexport const init = async(cmId) => {\n\n    moduleId = cmId;\n\n    // Ensure we only add our listeners once (can be called multiple times).\n    if (initialized) {\n        return;\n    }\n\n    prefetchStrings('core', [\n        'yes',\n        'no',\n    ]);\n    prefetchStrings('admin', [\n        'confirmation',\n    ]);\n    prefetchStrings('mod_feedback', [\n        'confirmdeleteitem',\n        'questionmoved',\n        'move_item',\n    ]);\n\n    await enhanceEditForm();\n\n    // Add event listeners.\n    document.addEventListener('click', async event => {\n\n        // Delete question.\n        const deleteButton = event.target.closest(Selectors.deleteQuestionButton);\n        if (deleteButton) {\n            event.preventDefault();\n            const confirmationStrings = await getStrings([\n                {key: 'confirmation', component: 'admin'},\n                {key: 'confirmdeleteitem', component: 'mod_feedback'},\n                {key: 'yes', component: 'core'},\n                {key: 'no', component: 'core'},\n            ]);\n            Notification.confirm(...confirmationStrings, () => {\n                window.location = deleteButton.getAttribute('href');\n            });\n            return;\n        }\n    });\n\n    // Initialize sortable list to handle active conditions moving.\n    const sortableList = new SortableList(document.querySelector(Selectors.sortableListRegion));\n    sortableList.getElementName = element => Promise.resolve(element[0].querySelector(Selectors.sortableElementTitle)?.textContent);\n\n    document.addEventListener(SortableList.EVENTS.elementDrop, event => {\n        if (!event.detail.positionChanged) {\n            return;\n        }\n        const pendingPromise = new Pending('mod_feedback/questions:reorder');\n        const itemOrder = getItemOrder(event.detail.element[0]);\n        addIconToContainerRemoveOnCompletion(event.detail.element[0], pendingPromise);\n        reorderQuestions(moduleId, itemOrder)\n            .then(() => getString('questionmoved', 'mod_feedback'))\n            .then(addToast)\n            .then(() => pendingPromise.resolve())\n            .catch(Notification.exception);\n    });\n\n    initialized = true;\n};\n\n/**\n * Enhance the edit form by adding a move item button and an action menu to each question.\n *\n * @returns {Promise<void>}\n */\nconst enhanceEditForm = async() => {\n    const questionLabels = document.querySelectorAll(Selectors.questionLabel);\n    const movetitle = await getString('move_item', 'mod_feedback');\n\n    const updates = Array.from(questionLabels).map(async(container) => {\n        const label = container.querySelector(Selectors.actionsMenuData);\n        if (!label) {\n            return;\n        }\n\n        try {\n            const contextData = {\n                movetitle,\n                label: label.parentElement.outerHTML,\n                actionsmenu: JSON.parse(label.dataset.itemActionsMenu || '{}'),\n            };\n            container.innerHTML = await Templates.render('mod_feedback/item_edit_enhanced_title', contextData);\n        } catch (error) {\n            await Notification.exception(error);\n        }\n    });\n    await Promise.all(updates);\n};\n"],"names":["Selectors","getItemOrder","element","sortableList","closest","itemOrder","querySelectorAll","forEach","item","id","push","Number","replace","toString","initialized","moduleId","async","cmId","enhanceEditForm","document","addEventListener","deleteButton","event","target","preventDefault","confirmationStrings","key","component","confirm","window","location","getAttribute","SortableList","querySelector","getElementName","Promise","resolve","_element$0$querySelec","textContent","EVENTS","elementDrop","detail","positionChanged","pendingPromise","Pending","then","addToast","catch","Notification","exception","questionLabels","movetitle","updates","Array","from","map","label","container","contextData","parentElement","outerHTML","actionsmenu","JSON","parse","dataset","itemActionsMenu","innerHTML","Templates","render","error","all"],"mappings":"sqBAmCMA,+BACoB,yBADpBA,6BAEkB,0CAFlBA,0BAGe,iEAHfA,+BAIoB,kCAJpBA,wBAKa,0DALbA,0BAMe,2BAmBfC,aAAgBC,gBACZC,aAAeD,QAAQE,QAAQJ,kCACjCK,UAAY,UAChBF,aAAaG,iBAAiBN,2BAA2BO,SAASC,OAbnDC,IAAAA,GAcXJ,UAAUK,MAdCD,GAccD,KAAKC,GAb3BE,OAAOF,GAAGG,QAAQ,qBAAsB,UAexCP,UAAUQ,gBAGjBC,aAAc,EACdC,SAAW,mBAOKC,MAAAA,UAEhBD,SAAWE,KAGPH,iDAIY,OAAQ,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