Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
// This file is part of Moodle - http://moodle.org/
2
//
3
// Moodle is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, either version 3 of the License, or
6
// (at your option) any later version.
7
//
8
// Moodle is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
// GNU General Public License for more details.
12
//
13
// You should have received a copy of the GNU General Public License
14
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
 
16
/**
17
 * Tiny media plugin class helpers for image and embed.
18
 *
19
 * @module      tiny_media/mediabase
20
 * @copyright   2024 Stevani Andolo <stevani@hotmail.com.au>
21
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
import {
24
    isPercentageValue,
25
    hideElements,
26
    showElements,
27
} from './helpers';
28
import Selectors from './selectors';
29
 
30
export class MediaBase {
31
 
32
    /**
33
     * Handles the selection of media size options and updates the form inputs accordingly.
34
     *
35
     * @param {string} option - The selected media size option ("original" or "custom").
36
     */
37
    sizeChecked = async(option) => {
38
        const widthInput = this.root.querySelector(Selectors[this.selectorType].elements.width);
39
        const heightInput = this.root.querySelector(Selectors[this.selectorType].elements.height);
40
        if (option === "original") {
41
            this.sizeOriginalChecked();
42
            widthInput.value = this.mediaDimensions.width;
43
            heightInput.value = this.mediaDimensions.height;
44
        } else if (option === "custom") {
45
            this.sizeCustomChecked();
46
            widthInput.value = this.currentWidth;
47
            heightInput.value = this.currentHeight;
48
 
49
            // If the current size is equal to the original size and selectorType = IMAGE,
50
            // then check the Keep proportion checkbox.
51
            if (
52
                this.selectorType === Selectors.IMAGE.type &&
53
                this.currentWidth === this.mediaDimensions.width &&
54
                this.currentHeight === this.mediaDimensions.height
55
            ) {
56
                const constrainField = this.root.querySelector(Selectors[this.selectorType].elements.constrain);
57
                constrainField.checked = true;
58
            }
59
        }
60
        this.autoAdjustSize();
61
    };
62
 
63
    /**
64
     * Handles the selection of the "Original Size" option and updates the form elements accordingly.
65
     */
66
    sizeOriginalChecked() {
67
        this.root.querySelector(Selectors[this.selectorType].elements.sizeOriginal).checked = true;
68
        this.root.querySelector(Selectors[this.selectorType].elements.sizeCustom).checked = false;
69
        hideElements(Selectors[this.selectorType].elements.properties, this.root);
70
    }
71
 
72
    /**
73
     * Handles the selection of the "Custom Size" option and updates the form elements accordingly.
74
     */
75
    sizeCustomChecked() {
76
        this.root.querySelector(Selectors[this.selectorType].elements.sizeOriginal).checked = false;
77
        this.root.querySelector(Selectors[this.selectorType].elements.sizeCustom).checked = true;
78
        showElements(Selectors[this.selectorType].elements.properties, this.root);
79
    }
80
 
81
    /**
82
     * Auto adjust the media width/height.
83
     * It is put here so image.js and/or friends can extend this class and call this for media proportion.
84
     *
85
     * @param {boolean} forceHeight Whether set by height or not
86
     */
87
    autoAdjustSize = (forceHeight = false) => {
88
        // If we do not know the media size, do not do anything.
89
        if (!this.mediaDimensions) {
90
            return;
91
        }
92
 
93
        const widthField = this.root.querySelector(Selectors[this.selectorType].elements.width);
94
        const heightField = this.root.querySelector(Selectors[this.selectorType].elements.height);
95
 
96
        const normalizeFieldData = (fieldData) => {
97
            fieldData.isPercentageValue = isPercentageValue(fieldData.field.value);
98
            if (fieldData.isPercentageValue) {
99
                fieldData.percentValue = parseInt(fieldData.field.value, 10);
100
                fieldData.pixelSize = this.mediaDimensions[fieldData.type] / 100 * fieldData.percentValue;
101
            } else {
102
                fieldData.pixelSize = parseInt(fieldData.field.value, 10);
103
                fieldData.percentValue = fieldData.pixelSize / this.mediaDimensions[fieldData.type] * 100;
104
            }
105
 
106
            return fieldData;
107
        };
108
 
109
        const getKeyField = () => {
110
            const getValue = () => {
111
                if (forceHeight) {
112
                    return {
113
                        field: heightField,
114
                        type: 'height',
115
                    };
116
                } else {
117
                    return {
118
                        field: widthField,
119
                        type: 'width',
120
                    };
121
                }
122
            };
123
 
124
            const currentValue = getValue();
125
            if (currentValue.field.value === '') {
126
                currentValue.field.value = this.mediaDimensions[currentValue.type];
127
            }
128
 
129
            return normalizeFieldData(currentValue);
130
        };
131
 
132
        const getRelativeField = () => {
133
            if (forceHeight) {
134
                return normalizeFieldData({
135
                    field: widthField,
136
                    type: 'width',
137
                });
138
            } else {
139
                return normalizeFieldData({
140
                    field: heightField,
141
                    type: 'height',
142
                });
143
            }
144
        };
145
 
146
        // Now update with the new values.
147
        const constrainField = this.root.querySelector(Selectors[this.selectorType].elements.constrain); // Only image.
148
        if ((constrainField && constrainField.checked) || this.mediaType === 'video') {
149
            const keyField = getKeyField();
150
            const relativeField = getRelativeField();
151
            // We are keeping the media in proportion.
152
            // Calculate the size for the relative field.
153
            if (keyField.isPercentageValue) {
154
                // In proportion, so the percentages are the same.
155
                relativeField.field.value = keyField.field.value;
156
                relativeField.percentValue = keyField.percentValue;
157
            } else {
158
                relativeField.pixelSize = Math.round(
159
                    keyField.pixelSize / this.mediaDimensions[keyField.type] * this.mediaDimensions[relativeField.type]
160
                );
161
                relativeField.field.value = relativeField.pixelSize;
162
            }
163
        }
164
 
165
        if (this.selectorType === Selectors.IMAGE.type) {
166
            // Store the custom width and height to reuse.
167
            this.currentWidth = Number(widthField.value) !== this.mediaDimensions.width ? widthField.value : this.currentWidth;
168
            this.currentHeight = Number(heightField.value) !== this.mediaDimensions.height ? heightField.value : this.currentHeight;
169
        }
170
    };
171
}