Proyectos de Subversion Moodle

Rev

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

{"version":3,"file":"usedfiles.min.js","sources":["../src/usedfiles.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 * Tiny Media Manager usedfiles.\n *\n * @module      tiny_media/usedfiles\n * @copyright   2022, Stevani Andolo <stevani@hotmail.com.au>\n * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport * as Templates from 'core/templates';\nimport Config from 'core/config';\n\nclass UsedFileManager {\n    constructor(userContext, itemId, elementId) {\n        this.files = this.getFiles();\n        this.userContext = userContext;\n        this.itemId = itemId;\n        this.elementId = elementId;\n    }\n\n    getElementId() {\n        return this.elementId;\n    }\n\n    getUsedFiles() {\n        const editor = window.parent.tinymce.EditorManager.get(this.getElementId());\n        if (!editor) {\n            window.console.error(`Editor not found for ${this.getElementId()}`);\n            return [];\n        }\n        const content = editor.getContent();\n        const baseUrl = `${Config.wwwroot}/draftfile.php/${this.userContext}/user/draft/${this.itemId}/`;\n        const pattern = new RegExp(\"[\\\"']\" + baseUrl.replace(/[-/\\\\^$*+?.()|[\\]{}]/g, '\\\\$&') + \"(?<filename>.+?)[\\\\?\\\"']\", 'gm');\n\n        const usedFiles = [...content.matchAll(pattern)].map((match) => decodeURIComponent(match.groups.filename));\n\n        return usedFiles;\n    }\n\n    // Return an array of unused files.\n    findUnusedFiles(usedFiles) {\n        return Object.entries(this.files)\n            .filter(([filename]) => !usedFiles.includes(filename))\n            .map(([filename]) => filename);\n    }\n\n    // Return an array of missing files.\n    findMissingFiles(usedFiles) {\n        return usedFiles.filter((filename) => !this.files.hasOwnProperty(filename));\n    }\n\n    updateFiles() {\n        const form = document.querySelector('form');\n        const usedFiles = this.getUsedFiles();\n        const unusedFiles = this.findUnusedFiles(usedFiles);\n        const missingFiles = this.findMissingFiles(usedFiles);\n\n        form.querySelectorAll('input[type=checkbox][name^=\"deletefile\"]').forEach((checkbox) => {\n            if (!unusedFiles.includes(checkbox.dataset.filename)) {\n                checkbox.closest('.fitem').remove();\n            }\n        });\n\n        form.classList.toggle('has-missing-files', !!missingFiles.length);\n        form.classList.toggle('has-unused-files', !!unusedFiles.length);\n\n        return Templates.renderForPromise('tiny_media/missingfiles', {\n            missingFiles,\n        }).then(({html, js}) => {\n            Templates.replaceNodeContents(form.querySelector('.missing-files'), html, js);\n            return;\n        });\n    }\n\n    /**\n     * Retrieves a list of existing files selected for deletion.\n     *\n     * @returns {Object} An object where the keys are filenames and the values are file hashes.\n     *\n     */\n     getFiles() {\n        const files = {};\n        document.querySelectorAll('input[type=checkbox][name^=\"deletefile\"]').forEach(input => {\n            files[input.dataset.filename] = input.dataset.filehash;\n        });\n        return files;\n    }\n}\n\nexport const init = (files, usercontext, itemid, elementid) => {\n    const manager = new UsedFileManager(files, usercontext, itemid, elementid);\n    manager.updateFiles();\n\n    return manager;\n};\n"],"names":["UsedFileManager","constructor","userContext","itemId","elementId","files","this","getFiles","getElementId","getUsedFiles","editor","window","parent","tinymce","EditorManager","get","console","error","content","getContent","baseUrl","Config","wwwroot","pattern","RegExp","replace","matchAll","map","match","decodeURIComponent","groups","filename","findUnusedFiles","usedFiles","Object","entries","filter","_ref","includes","_ref2","findMissingFiles","hasOwnProperty","updateFiles","form","document","querySelector","unusedFiles","missingFiles","querySelectorAll","forEach","checkbox","dataset","closest","remove","classList","toggle","length","Templates","renderForPromise","then","_ref3","html","js","replaceNodeContents","input","filehash","usercontext","itemid","elementid","manager"],"mappings":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