1 |
efrain |
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 |
* Contain the logic for the copy to clipboard modal, i.e. the modal contains a
|
|
|
18 |
* readonly input text field, that contains a value. Clicking on the single
|
|
|
19 |
* button "Copy to clipboard" in the footer, puts the content of the input
|
|
|
20 |
* text field into the clipboard and closes the modal.
|
|
|
21 |
*
|
|
|
22 |
* Usage:
|
|
|
23 |
* ModalCopyToClipboard.create(string:<stringToCopy>, string:<modalTitle>|null);
|
|
|
24 |
*
|
|
|
25 |
* @module core/modal_copy_to_clipboard
|
|
|
26 |
* @copyright 2023 Stephan Robotta <stephan.robotta@bfh.ch>
|
|
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
*/
|
|
|
29 |
|
|
|
30 |
import Modal from 'core/modal';
|
|
|
31 |
import 'core/copy_to_clipboard';
|
|
|
32 |
|
|
|
33 |
export default class CopyToClipboardModal extends Modal {
|
|
|
34 |
static TYPE = 'core/copytoclipboard';
|
|
|
35 |
static TEMPLATE = 'core/modal_copytoclipboard';
|
|
|
36 |
|
|
|
37 |
constructor(...config) {
|
|
|
38 |
// Override the constructor to set the removeOnClose property, and show the modal.
|
|
|
39 |
super(...config);
|
|
|
40 |
this.setRemoveOnClose(true);
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Set up all the event handling for the modal.
|
|
|
45 |
* This is an override of the parent method, adding an event listener to close upon the action.
|
|
|
46 |
*
|
|
|
47 |
* @param {array} args
|
|
|
48 |
*/
|
|
|
49 |
registerEventListeners(...args) {
|
|
|
50 |
super.registerEventListeners(...args);
|
|
|
51 |
|
|
|
52 |
this.getRoot().get(0).addEventListener('click', (e) => {
|
|
|
53 |
if (!e.target.closest('[data-action="copytoclipboard"]')) {
|
|
|
54 |
return;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
if (!this.getRoot().get(0).contains(e.target)) {
|
|
|
58 |
return;
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
// Note: We must call destroy() here, because the copy-to-clipboard action listens on the document,
|
|
|
62 |
// which will be processed after this event listener has been processed.
|
|
|
63 |
// By placing this in a setTimeout we move its processing to after the event loop has finished.
|
|
|
64 |
setTimeout(this.destroy.bind(this));
|
|
|
65 |
});
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* Create a new instance of the Modal. Set the text that is being copied. By default, the text is put into the
|
|
|
70 |
* value of an input readonly field. If useTextArea is set to true, the text is rendered in a textarea element.
|
|
|
71 |
* The optional title argument is for the modal title. If not set, the generic string "copy to clipboard" is used.
|
|
|
72 |
*
|
|
|
73 |
* @param {Object} data used in the template
|
|
|
74 |
* @param {string} data.text the text to copy to the clipboard
|
|
|
75 |
* @param {boolean} data.useTextArea when the text to copy is displayed in a textarea, default is input
|
|
|
76 |
* @param {string|null} title
|
|
|
77 |
* @returns {Promise<void>}
|
|
|
78 |
*/
|
|
|
79 |
static async create(
|
|
|
80 |
{
|
|
|
81 |
text,
|
|
|
82 |
useTextArea = false,
|
|
|
83 |
} = {},
|
|
|
84 |
title,
|
|
|
85 |
) {
|
|
|
86 |
const modalConfig = {
|
|
|
87 |
templateContext: {
|
|
|
88 |
text,
|
|
|
89 |
useTextArea,
|
|
|
90 |
},
|
|
|
91 |
};
|
|
|
92 |
if (title) {
|
|
|
93 |
modalConfig.title = title;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
return super.create(modalConfig);
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
configure(modalConfig) {
|
|
|
100 |
modalConfig.show = true;
|
|
|
101 |
modalConfig.removeOnClose = true;
|
|
|
102 |
|
|
|
103 |
super.configure(modalConfig);
|
|
|
104 |
}
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
CopyToClipboardModal.registerModalType();
|