Proyectos de Subversion Moodle

Rev

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

{"version":3,"file":"mediabase.min.js","sources":["../src/mediabase.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 plugin class helpers for image and embed.\n *\n * @module      tiny_media/mediabase\n * @copyright   2024 Stevani Andolo <stevani@hotmail.com.au>\n * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\nimport {\n    isPercentageValue,\n    hideElements,\n    showElements,\n} from './helpers';\nimport Selectors from './selectors';\n\nexport class MediaBase {\n\n    /**\n     * Handles the selection of media size options and updates the form inputs accordingly.\n     *\n     * @param {string} option - The selected media size option (\"original\" or \"custom\").\n     */\n    sizeChecked = async(option) => {\n        const widthInput = this.root.querySelector(Selectors[this.selectorType].elements.width);\n        const heightInput = this.root.querySelector(Selectors[this.selectorType].elements.height);\n        if (option === \"original\") {\n            this.sizeOriginalChecked();\n            widthInput.value = this.mediaDimensions.width;\n            heightInput.value = this.mediaDimensions.height;\n        } else if (option === \"custom\") {\n            this.sizeCustomChecked();\n            widthInput.value = this.currentWidth;\n            heightInput.value = this.currentHeight;\n\n            // If the current size is equal to the original size and selectorType = IMAGE,\n            // then check the Keep proportion checkbox.\n            if (\n                this.selectorType === Selectors.IMAGE.type &&\n                this.currentWidth === this.mediaDimensions.width &&\n                this.currentHeight === this.mediaDimensions.height\n            ) {\n                const constrainField = this.root.querySelector(Selectors[this.selectorType].elements.constrain);\n                constrainField.checked = true;\n            }\n        }\n        this.autoAdjustSize();\n    };\n\n    /**\n     * Handles the selection of the \"Original Size\" option and updates the form elements accordingly.\n     */\n    sizeOriginalChecked() {\n        this.root.querySelector(Selectors[this.selectorType].elements.sizeOriginal).checked = true;\n        this.root.querySelector(Selectors[this.selectorType].elements.sizeCustom).checked = false;\n        hideElements(Selectors[this.selectorType].elements.properties, this.root);\n    }\n\n    /**\n     * Handles the selection of the \"Custom Size\" option and updates the form elements accordingly.\n     */\n    sizeCustomChecked() {\n        this.root.querySelector(Selectors[this.selectorType].elements.sizeOriginal).checked = false;\n        this.root.querySelector(Selectors[this.selectorType].elements.sizeCustom).checked = true;\n        showElements(Selectors[this.selectorType].elements.properties, this.root);\n    }\n\n    /**\n     * Auto adjust the media width/height.\n     * It is put here so image.js and/or friends can extend this class and call this for media proportion.\n     *\n     * @param {boolean} forceHeight Whether set by height or not\n     */\n    autoAdjustSize = (forceHeight = false) => {\n        // If we do not know the media size, do not do anything.\n        if (!this.mediaDimensions) {\n            return;\n        }\n\n        const widthField = this.root.querySelector(Selectors[this.selectorType].elements.width);\n        const heightField = this.root.querySelector(Selectors[this.selectorType].elements.height);\n\n        const normalizeFieldData = (fieldData) => {\n            fieldData.isPercentageValue = isPercentageValue(fieldData.field.value);\n            if (fieldData.isPercentageValue) {\n                fieldData.percentValue = parseInt(fieldData.field.value, 10);\n                fieldData.pixelSize = this.mediaDimensions[fieldData.type] / 100 * fieldData.percentValue;\n            } else {\n                fieldData.pixelSize = parseInt(fieldData.field.value, 10);\n                fieldData.percentValue = fieldData.pixelSize / this.mediaDimensions[fieldData.type] * 100;\n            }\n\n            return fieldData;\n        };\n\n        const getKeyField = () => {\n            const getValue = () => {\n                if (forceHeight) {\n                    return {\n                        field: heightField,\n                        type: 'height',\n                    };\n                } else {\n                    return {\n                        field: widthField,\n                        type: 'width',\n                    };\n                }\n            };\n\n            const currentValue = getValue();\n            if (currentValue.field.value === '') {\n                currentValue.field.value = this.mediaDimensions[currentValue.type];\n            }\n\n            return normalizeFieldData(currentValue);\n        };\n\n        const getRelativeField = () => {\n            if (forceHeight) {\n                return normalizeFieldData({\n                    field: widthField,\n                    type: 'width',\n                });\n            } else {\n                return normalizeFieldData({\n                    field: heightField,\n                    type: 'height',\n                });\n            }\n        };\n\n        // Now update with the new values.\n        const constrainField = this.root.querySelector(Selectors[this.selectorType].elements.constrain); // Only image.\n        if ((constrainField && constrainField.checked) || this.mediaType === 'video') {\n            const keyField = getKeyField();\n            const relativeField = getRelativeField();\n            // We are keeping the media in proportion.\n            // Calculate the size for the relative field.\n            if (keyField.isPercentageValue) {\n                // In proportion, so the percentages are the same.\n                relativeField.field.value = keyField.field.value;\n                relativeField.percentValue = keyField.percentValue;\n            } else {\n                relativeField.pixelSize = Math.round(\n                    keyField.pixelSize / this.mediaDimensions[keyField.type] * this.mediaDimensions[relativeField.type]\n                );\n                relativeField.field.value = relativeField.pixelSize;\n            }\n        }\n\n        if (this.selectorType === Selectors.IMAGE.type) {\n            // Store the custom width and height to reuse.\n            this.currentWidth = Number(widthField.value) !== this.mediaDimensions.width ? widthField.value : this.currentWidth;\n            this.currentHeight = Number(heightField.value) !== this.mediaDimensions.height ? heightField.value : this.currentHeight;\n        }\n    };\n}\n"],"names":["async","widthInput","this","root","querySelector","Selectors","selectorType","elements","width","heightInput","height","option","sizeOriginalChecked","value","mediaDimensions","sizeCustomChecked","currentWidth","currentHeight","IMAGE","type","constrain","checked","autoAdjustSize","forceHeight","_this","widthField","heightField","normalizeFieldData","fieldData","isPercentageValue","field","percentValue","parseInt","pixelSize","getKeyField","currentValue","getRelativeField","constrainField","mediaType","keyField","relativeField","Math","round","Number","sizeOriginal","sizeCustom","properties"],"mappings":"kgBAoCkBA,MAAAA,eACJC,WAAaC,KAAKC,KAAKC,cAAcC,mBAAUH,KAAKI,cAAcC,SAASC,OAC3EC,YAAcP,KAAKC,KAAKC,cAAcC,mBAAUH,KAAKI,cAAcC,SAASG,WACnE,aAAXC,YACKC,sBACLX,WAAWY,MAAQX,KAAKY,gBAAgB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