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 |
/**
|
|
|
18 |
* @author Andreas Grabs <moodle@grabs-edv.de>
|
|
|
19 |
* @copyright 2024 Andreas Grabs
|
|
|
20 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
21 |
*/
|
|
|
22 |
|
|
|
23 |
import log from 'core/log';
|
|
|
24 |
import config from 'core/config';
|
|
|
25 |
|
|
|
26 |
let _formid;
|
|
|
27 |
let _type;
|
|
|
28 |
let _useDragdrop;
|
|
|
29 |
let _contextid;
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Find the items in our mform we want to be draggable
|
|
|
33 |
*
|
|
|
34 |
* @param {string} formid The id of the mform the draggable items are related toHtml
|
|
|
35 |
* @returns {array}
|
|
|
36 |
*/
|
|
|
37 |
const getDraggableItems = (formid) => {
|
|
|
38 |
let fieldsets = document.querySelectorAll('#' + formid + ' fieldset');
|
|
|
39 |
const items = [];
|
|
|
40 |
fieldsets.forEach(fieldset => {
|
|
|
41 |
if (fieldset.id.startsWith('id_singleelementheader_')) {
|
|
|
42 |
items.push(fieldset);
|
|
|
43 |
}
|
|
|
44 |
});
|
|
|
45 |
return items;
|
|
|
46 |
};
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Initialize a draggable item to be ready for drag and drop
|
|
|
50 |
*
|
|
|
51 |
* @param {Element} item The draggable item
|
|
|
52 |
* @param {Integer} index The index of the draggable item
|
|
|
53 |
*/
|
|
|
54 |
const initDragElement = (item, index) => {
|
|
|
55 |
// Add the class "dragging" a little later to get the dragging image visible.
|
|
|
56 |
if (_useDragdrop) {
|
|
|
57 |
item.classList.add('draggable');
|
|
|
58 |
}
|
|
|
59 |
item.dataset.index = index;
|
|
|
60 |
};
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* Set the new sortorder values dependig on the current list order.
|
|
|
64 |
*/
|
|
|
65 |
const resortList = () => {
|
|
|
66 |
// Set the new sortorder;
|
|
|
67 |
let i = 0;
|
|
|
68 |
log.debug('Changed sortorder');
|
|
|
69 |
document.querySelectorAll('#' + _formid + ' fieldset.draggable').forEach(sortitem => {
|
|
|
70 |
let elementindex = sortitem.dataset.index;
|
|
|
71 |
log.debug('Set sortorder in element: ' + 'unilabeltype_' + _type + '_sortorder[' + elementindex + ']');
|
|
|
72 |
let hiddenelement = document.forms[_formid].elements['unilabeltype_' + _type + '_sortorder[' + elementindex + ']'];
|
|
|
73 |
let oldvalue = hiddenelement.value;
|
|
|
74 |
hiddenelement.value = i + 1;
|
|
|
75 |
log.debug('Element: ' + elementindex + ' - old value: ' + oldvalue + ', new value: ' + hiddenelement.value);
|
|
|
76 |
i++;
|
|
|
77 |
});
|
|
|
78 |
};
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Export our init method.
|
|
|
82 |
*
|
|
|
83 |
* @param {string} type The type of unilabeltype e.g.: grid
|
|
|
84 |
* @param {string} formid The id of the mform the draggable elements are related to
|
|
|
85 |
* @param {boolean} useDragdrop If false or not set drag drop is deactivated.
|
|
|
86 |
*/
|
|
|
87 |
export const init = async(type, formid, useDragdrop) => {
|
|
|
88 |
_type = type;
|
|
|
89 |
_formid = formid;
|
|
|
90 |
_useDragdrop = useDragdrop;
|
|
|
91 |
|
|
|
92 |
// Initialize drag and drop.
|
|
|
93 |
const items = getDraggableItems(formid);
|
|
|
94 |
let index = 0;
|
|
|
95 |
items.forEach(item => {
|
|
|
96 |
initDragElement(item, index);
|
|
|
97 |
index++;
|
|
|
98 |
});
|
|
|
99 |
|
|
|
100 |
// Add event listener for new items.
|
|
|
101 |
document.querySelector('#' + formid).addEventListener('itemadded', (e) => {
|
|
|
102 |
log.debug('New element created with index: ' + e.detail);
|
|
|
103 |
var newitem = document.querySelector('#id_singleelementheader_' + e.detail);
|
|
|
104 |
initDragElement(newitem, e.detail);
|
|
|
105 |
if (_useDragdrop) {
|
|
|
106 |
resortList();
|
|
|
107 |
}
|
|
|
108 |
});
|
|
|
109 |
// Add event listener if item is removed.
|
|
|
110 |
document.querySelector('#' + formid).addEventListener('itemremoved', (e) => {
|
|
|
111 |
log.debug('Element has been deleted: ' + e.detail);
|
|
|
112 |
if (_useDragdrop) {
|
|
|
113 |
resortList();
|
|
|
114 |
}
|
|
|
115 |
});
|
|
|
116 |
|
|
|
117 |
// Import Sortable from 'js/Sortable.js';
|
|
|
118 |
const Sortable = await import(config.wwwroot + '/mod/unilabel/js/Sortable.min.js');
|
|
|
119 |
_contextid = config.contextid;
|
|
|
120 |
const mysortablelist = document.querySelector('#' + formid);
|
|
|
121 |
var sortable = Sortable.create(
|
|
|
122 |
mysortablelist,
|
|
|
123 |
{
|
|
|
124 |
draggable: '.draggable',
|
|
|
125 |
handle: '.draghandle',
|
|
|
126 |
animation: 150,
|
|
|
127 |
swapThreshold: 0.5,
|
|
|
128 |
onEnd: async(e) => {
|
|
|
129 |
log.debug(e.item);
|
|
|
130 |
if (globalThis.tinymce !== undefined) {
|
|
|
131 |
var fieldsetselector = '#' + e.item.id;
|
|
|
132 |
log.debug(fieldsetselector);
|
|
|
133 |
var repeatindex = parseInt(document.querySelector(fieldsetselector).dataset.index);
|
|
|
134 |
let tinyeditor = await import('editor_tiny/editor');
|
|
|
135 |
let tinyconfig = await import('mod_unilabel/tinyconfig');
|
|
|
136 |
let editor = globalThis.tinymce;
|
|
|
137 |
editor.remove('#' + e.item.id + ' textarea');
|
|
|
138 |
document.querySelectorAll('#id_singleelementheader_' + repeatindex + ' [data-fieldtype="editor"]').forEach(
|
|
|
139 |
async(editorcontainer) => {
|
|
|
140 |
let target = editorcontainer.querySelector('textarea');
|
|
|
141 |
let targetid = target.getAttribute('id');
|
|
|
142 |
let targetname = target.getAttribute('name');
|
|
|
143 |
// Find the current draftitemid;
|
|
|
144 |
let draftitemidselector = targetname.replace('[text]', '[itemid]');
|
|
|
145 |
let draftitemid = document.forms[_formid][draftitemidselector].value;
|
|
|
146 |
let config = await tinyconfig.getTinyConfig(_contextid, targetid, targetname, draftitemid, repeatindex);
|
|
|
147 |
await tinyeditor.setupForTarget(target, config);
|
|
|
148 |
}
|
|
|
149 |
);
|
|
|
150 |
}
|
|
|
151 |
resortList();
|
|
|
152 |
return true;
|
|
|
153 |
}
|
|
|
154 |
}
|
|
|
155 |
);
|
|
|
156 |
log.debug('Initialized sortable list');
|
|
|
157 |
return sortable;
|
|
|
158 |
|
|
|
159 |
};
|