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 embed thumbnail preview class.
|
|
|
18 |
*
|
|
|
19 |
* This handles:
|
|
|
20 |
* - Embed thumbnail preview.
|
|
|
21 |
* - Delete thumbnail preview.
|
|
|
22 |
* - Update the embed preview details with new thumbnail.
|
|
|
23 |
*
|
|
|
24 |
* @module tiny_media/embed/mediathumbnail
|
|
|
25 |
* @copyright 2024 Stevani Andolo <stevani@hotmail.com.au>
|
|
|
26 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
27 |
*/
|
|
|
28 |
|
|
|
29 |
import Selectors from '../selectors';
|
|
|
30 |
import {getString} from 'core/str';
|
|
|
31 |
import {
|
|
|
32 |
sourceTypeChecked,
|
|
|
33 |
setPropertiesFromData,
|
|
|
34 |
showElements,
|
|
|
35 |
stopMediaLoading,
|
|
|
36 |
} from '../helpers';
|
|
|
37 |
import Notification from 'core/notification';
|
|
|
38 |
import {EmbedPreview} from './embedpreview';
|
|
|
39 |
import {mediaDetailsTemplateContext} from './embedhelpers';
|
|
|
40 |
import {EmbedHandler} from './embedhandler';
|
|
|
41 |
import {component} from '../common';
|
|
|
42 |
|
|
|
43 |
export class EmbedThumbnailPreview {
|
|
|
44 |
|
|
|
45 |
constructor(data) {
|
|
|
46 |
setPropertiesFromData(this, data); // Creates dynamic properties based on "data" param.
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Init the media thumbnail preview.
|
|
|
51 |
*
|
|
|
52 |
* @param {object} mediaData Object of selected media data
|
|
|
53 |
*/
|
|
|
54 |
init = (mediaData) => {
|
|
|
55 |
this.mediaData = mediaData;
|
|
|
56 |
this.currentModal.uploadThumbnailModal.setTitle(getString('thumbnail', component));
|
|
|
57 |
sourceTypeChecked({
|
|
|
58 |
source: this.media.poster,
|
|
|
59 |
root: this.thumbnailModalRoot,
|
|
|
60 |
fileNameSelector: Selectors.EMBED.elements.fileNameLabel,
|
|
|
61 |
});
|
|
|
62 |
this.setThumbnailSource();
|
|
|
63 |
this.registerMediaThumbnailEventListeners();
|
|
|
64 |
};
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Sets media thumbnail source.
|
|
|
68 |
*/
|
|
|
69 |
setThumbnailSource = () => {
|
|
|
70 |
const thumbnailPreview = this.thumbnailModalRoot.querySelector(Selectors.EMBED.elements.thumbnailPreview);
|
|
|
71 |
thumbnailPreview.src = this.media.poster;
|
|
|
72 |
|
|
|
73 |
thumbnailPreview.addEventListener('error', async() => {
|
|
|
74 |
// Show warning notification.
|
|
|
75 |
const urlWarningLabelEle = this.thumbnailModalRoot.querySelector(Selectors.EMBED.elements.urlWarning);
|
|
|
76 |
urlWarningLabelEle.innerHTML = await getString('imageurlrequired', component);
|
|
|
77 |
showElements(Selectors.EMBED.elements.urlWarning, this.thumbnailModalRoot);
|
|
|
78 |
|
|
|
79 |
// Stop the spinner.
|
|
|
80 |
stopMediaLoading(this.thumbnailModalRoot, Selectors.EMBED.type);
|
|
|
81 |
|
|
|
82 |
// Reset the upload form.
|
|
|
83 |
(new EmbedHandler(this)).resetUploadForm(false);
|
|
|
84 |
});
|
|
|
85 |
|
|
|
86 |
thumbnailPreview.addEventListener('load', () => {
|
|
|
87 |
this.mediaData.media.poster = this.media.poster;
|
|
|
88 |
this.media = this.mediaData.media;
|
|
|
89 |
stopMediaLoading(this.thumbnailModalRoot, Selectors.EMBED.type);
|
|
|
90 |
});
|
|
|
91 |
};
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Deletes the media after confirming with the user and loads the insert media page.
|
|
|
95 |
*/
|
|
|
96 |
deleteMedia = () => {
|
|
|
97 |
Notification.deleteCancelPromise(
|
|
|
98 |
getString('deletemediathumbnail', component),
|
|
|
99 |
getString('deletemediathumbnailwarning', component)
|
|
|
100 |
).then(() => {
|
|
|
101 |
(new EmbedHandler(this)).resetUploadForm(false);
|
|
|
102 |
return;
|
|
|
103 |
}).catch(error => {
|
|
|
104 |
window.console.log(error);
|
|
|
105 |
});
|
|
|
106 |
};
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
* Loads and displays a media preview with thumbnail.
|
|
|
110 |
*/
|
|
|
111 |
loadPreviewMediaThumbnail = async() => {
|
|
|
112 |
(new EmbedHandler(this)).loadMediaDetails(new EmbedPreview(this), await mediaDetailsTemplateContext(this))
|
|
|
113 |
.then(() => {
|
|
|
114 |
// Close the thumbnail upload modal once media details have been loaded.
|
|
|
115 |
this.currentModal.uploadThumbnailModal.destroy();
|
|
|
116 |
const currentModal = this.currentModal.insertMediaModal;
|
|
|
117 |
this.currentModal = currentModal.insertMediaModal;
|
|
|
118 |
delete this.mediaData;
|
|
|
119 |
return;
|
|
|
120 |
}).catch(error => {
|
|
|
121 |
window.console.log(error);
|
|
|
122 |
});
|
|
|
123 |
};
|
|
|
124 |
|
|
|
125 |
/**
|
|
|
126 |
* Only registers event listeners for new loaded elements in mediaThumbnail.
|
|
|
127 |
*/
|
|
|
128 |
registerMediaThumbnailEventListeners = () => {
|
|
|
129 |
// Handles delete thumbnail.
|
|
|
130 |
const deleteMedia = this.thumbnailModalRoot.querySelector(Selectors.EMBED.actions.deleteThumbnail);
|
|
|
131 |
if (deleteMedia) {
|
|
|
132 |
deleteMedia.addEventListener('click', (e) => {
|
|
|
133 |
e.preventDefault();
|
|
|
134 |
this.deleteMedia();
|
|
|
135 |
});
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
// Handles setting the media poster.
|
|
|
139 |
const setPoster = this.thumbnailModalRoot.querySelector(Selectors.EMBED.actions.setPoster);
|
|
|
140 |
if (setPoster) {
|
|
|
141 |
setPoster.addEventListener('click', () => {
|
|
|
142 |
this.loadPreviewMediaThumbnail();
|
|
|
143 |
});
|
|
|
144 |
}
|
|
|
145 |
};
|
|
|
146 |
}
|