Proyectos de Subversion Moodle

Rev

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

{"version":3,"file":"dropzone.min.js","sources":["../src/dropzone.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 * JavaScript to handle dropzone.\n *\n * @module     core/dropzone\n * @copyright  2024 Huong Nguyen <huongnv13@gmail.com>\n * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n * @since      4.4\n */\n\nimport {getString} from 'core/str';\nimport Log from 'core/log';\nimport {prefetchString} from 'core/prefetch';\nimport Templates from 'core/templates';\n\n/**\n * A dropzone.\n *\n * @class core/dropzone\n */\nconst DropZone = class {\n\n    /**\n     * The element to render the dropzone.\n     * @type {Element}\n     */\n    dropZoneElement;\n\n    /**\n     * The file types that are allowed to be uploaded.\n     * @type {String}\n     */\n    fileTypes;\n\n    /**\n     * The function to call when a file is dropped.\n     * @type {CallableFunction}\n     */\n    callback;\n\n    /**\n     * The label to display in the dropzone.\n     * @type {string}\n     */\n    dropZoneLabel = '';\n\n    /**\n     * Constructor.\n     *\n     * @param {Element} dropZoneElement The element to render the dropzone.\n     * @param {String} fileTypes The file types that are allowed to be uploaded. Example: image/*\n     * @param {CallableFunction} callback The function to call when a file is dropped.\n     */\n    constructor(dropZoneElement, fileTypes, callback) {\n        prefetchString('core', 'addfilesdrop');\n        this.dropZoneElement = dropZoneElement;\n        this.fileTypes = fileTypes;\n        this.callback = callback;\n    }\n\n    /**\n     * Initialise the dropzone.\n     *\n     * @returns {DropZone}\n     */\n    init() {\n        this.dropZoneElement.addEventListener('dragover', (e) => {\n            const dropZone = this.getDropZoneFromEvent(e);\n            if (!dropZone) {\n                return;\n            }\n            e.preventDefault();\n            dropZone.classList.add('dragover');\n        });\n        this.dropZoneElement.addEventListener('dragleave', (e) => {\n            const dropZone = this.getDropZoneFromEvent(e);\n            if (!dropZone) {\n                return;\n            }\n            e.preventDefault();\n            dropZone.classList.remove('dragover');\n        });\n        this.dropZoneElement.addEventListener('drop', (e) => {\n            const dropZone = this.getDropZoneFromEvent(e);\n            if (!dropZone) {\n                return;\n            }\n            e.preventDefault();\n            dropZone.classList.remove('dragover');\n            this.callback(e.dataTransfer.files);\n        });\n        this.dropZoneElement.addEventListener('click', (e) => {\n            const dropZoneContainer = this.getDropZoneContainerFromEvent(e);\n            if (!dropZoneContainer) {\n                return;\n            }\n            this.getFileElementFromEvent(e).click();\n        });\n        this.dropZoneElement.addEventListener('click', (e) => {\n            const dropZoneLabel = e.target.closest('.dropzone-sr-only-focusable');\n            if (!dropZoneLabel) {\n                return;\n            }\n            this.getFileElementFromEvent(e).click();\n        });\n        this.dropZoneElement.addEventListener('change', (e) => {\n            const fileInput = this.getFileElementFromEvent(e);\n            if (fileInput) {\n                e.preventDefault();\n                this.callback(fileInput.files);\n            }\n        });\n        this.renderDropZone(this.dropZoneElement, this.fileTypes);\n        Log.info('Dropzone has been initialized!');\n        return this;\n    }\n\n    /**\n     * Get the dropzone.\n     *\n     * @param {Event} e The event.\n     * @returns {HTMLElement|bool}\n     */\n    getDropZoneFromEvent(e) {\n        return e.target.closest('.dropzone');\n    }\n\n    /**\n     * Get the dropzone container.\n     *\n     * @param {Event} e The event.\n     * @returns {HTMLElement|bool}\n     */\n    getDropZoneContainerFromEvent(e) {\n        return e.target.closest('.dropzone-container');\n    }\n\n    /**\n     * Get the file element.\n     *\n     * @param {Event} e The event.\n     * @returns {HTMLElement|bool}\n     */\n    getFileElementFromEvent(e) {\n        return e.target.closest('.dropzone-container').querySelector('.drop-zone-fileinput');\n    }\n\n    /**\n     * Set the label to display in the dropzone.\n     *\n     * @param {String} label The label to display in the dropzone.\n     */\n    setLabel(label) {\n        this.dropZoneLabel = label;\n    }\n\n    /**\n     * Get the label to display in the dropzone.\n     *\n     * @return {String} The label to display in the dropzone.\n     */\n    getLabel() {\n        return this.dropZoneLabel;\n    }\n\n    /**\n     * Render the dropzone.\n     *\n     * @param {Element} dropZoneElement The element to render the dropzone.\n     * @param {String} fileTypes The file types that are allowed to be uploaded.\n     * @returns {Promise}\n     */\n    async renderDropZone(dropZoneElement, fileTypes) {\n        if (!this.getLabel()) {\n            // Use the default one.\n            this.setLabel(await getString('addfilesdrop', 'core'));\n        }\n        const dropZoneLabel = this.getLabel();\n        dropZoneElement.innerHTML = await Templates.render('core/dropzone', {\n            label: dropZoneLabel,\n            filetypes: fileTypes,\n        });\n    }\n};\n\nexport default DropZone;\n"],"names":["constructor","dropZoneElement","fileTypes","callback","init","addEventListener","e","dropZone","this","getDropZoneFromEvent","preventDefault","classList","add","remove","dataTransfer","files","getDropZoneContainerFromEvent","getFileElementFromEvent","click","target","closest","fileInput","renderDropZone","info","querySelector","setLabel","label","dropZoneLabel","getLabel","innerHTML","Templates","render","filetypes"],"mappings":"gjBAkCiB,MAiCbA,YAAYC,gBAAiBC,UAAWC,+KATxB,iCAUG,OAAQ,qBAClBF,gBAAkBA,qBAClBC,UAAYA,eACZC,SAAWA,SAQpBC,mBACSH,gBAAgBI,iBAAiB,YAAaC,UACzCC,SAAWC,KAAKC,qBAAqBH,GACtCC,WAGLD,EAAEI,iBACFH,SAASI,UAAUC,IAAI,qBAEtBX,gBAAgBI,iBAAiB,aAAcC,UAC1CC,SAAWC,KAAKC,qBAAqBH,GACtCC,WAGLD,EAAEI,iBACFH,SAASI,UAAUE,OAAO,qBAEzBZ,gBAAgBI,iBAAiB,QAASC,UACrCC,SAAWC,KAAKC,qBAAqBH,GACtCC,WAGLD,EAAEI,iBACFH,SAASI,UAAUE,OAAO,iBACrBV,SAASG,EAAEQ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