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 |
* Show an add block modal instead of doing it on a separate page.
|
|
|
18 |
*
|
|
|
19 |
* @module core/addblockmodal
|
|
|
20 |
* @deprecated since Moodle 4.2 - please use core_block/add_modal instead.
|
|
|
21 |
* @copyright 2016 Damyon Wiese <damyon@moodle.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
import CancelModal from './modal_cancel';
|
|
|
26 |
import Templates from 'core/templates';
|
|
|
27 |
import {getString} from 'core/str';
|
|
|
28 |
import Ajax from 'core/ajax';
|
|
|
29 |
|
|
|
30 |
const SELECTORS = {
|
|
|
31 |
ADD_BLOCK: '[data-key="addblock"]'
|
|
|
32 |
};
|
|
|
33 |
|
|
|
34 |
// Ensure we only add our listeners once.
|
|
|
35 |
let listenerEventsRegistered = false;
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Register related event listeners.
|
|
|
39 |
*
|
|
|
40 |
* @method registerListenerEvents
|
|
|
41 |
* @param {String} pageType The type of the page
|
|
|
42 |
* @param {String} pageLayout The layout of the page
|
|
|
43 |
* @param {String|null} addBlockUrl The add block URL
|
|
|
44 |
* @param {String} subPage The subpage identifier
|
|
|
45 |
*/
|
|
|
46 |
const registerListenerEvents = (pageType, pageLayout, addBlockUrl, subPage) => {
|
|
|
47 |
document.addEventListener('click', e => {
|
|
|
48 |
|
|
|
49 |
const addBlock = e.target.closest(SELECTORS.ADD_BLOCK);
|
|
|
50 |
if (addBlock) {
|
|
|
51 |
e.preventDefault();
|
|
|
52 |
|
|
|
53 |
let addBlockModal = null;
|
|
|
54 |
let addBlockModalUrl = addBlockUrl ?? addBlock.dataset.url;
|
|
|
55 |
|
|
|
56 |
buildAddBlockModal()
|
|
|
57 |
.then(modal => {
|
|
|
58 |
addBlockModal = modal;
|
|
|
59 |
const modalBody = renderBlocks(addBlockModalUrl, pageType, pageLayout, subPage);
|
|
|
60 |
modal.setBody(modalBody);
|
|
|
61 |
modal.show();
|
|
|
62 |
|
|
|
63 |
return modalBody;
|
|
|
64 |
})
|
|
|
65 |
.catch(() => {
|
|
|
66 |
addBlockModal.destroy();
|
|
|
67 |
});
|
|
|
68 |
}
|
|
|
69 |
});
|
|
|
70 |
};
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* Method that creates the 'add block' modal.
|
|
|
74 |
*
|
|
|
75 |
* @method buildAddBlockModal
|
|
|
76 |
* @returns {Promise} The modal promise (modal's body will be rendered later).
|
|
|
77 |
*/
|
|
|
78 |
const buildAddBlockModal = () => {
|
|
|
79 |
return CancelModal.create({
|
|
|
80 |
title: getString('addblock')
|
|
|
81 |
});
|
|
|
82 |
};
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* Method that renders the list of available blocks.
|
|
|
86 |
*
|
|
|
87 |
* @method renderBlocks
|
|
|
88 |
* @param {String} addBlockUrl The add block URL
|
|
|
89 |
* @param {String} pageType The type of the page
|
|
|
90 |
* @param {String} pageLayout The layout of the page
|
|
|
91 |
* @param {String} subPage The subpage identifier
|
|
|
92 |
* @return {Promise}
|
|
|
93 |
*/
|
|
|
94 |
const renderBlocks = async(addBlockUrl, pageType, pageLayout, subPage) => {
|
|
|
95 |
// Fetch all addable blocks in the given page.
|
|
|
96 |
const blocks = await getAddableBlocks(pageType, pageLayout, subPage);
|
|
|
97 |
|
|
|
98 |
return Templates.render('core/add_block_body', {
|
|
|
99 |
blocks: blocks,
|
|
|
100 |
url: addBlockUrl
|
|
|
101 |
});
|
|
|
102 |
};
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Method that fetches all addable blocks in a given page.
|
|
|
106 |
*
|
|
|
107 |
* @method getAddableBlocks
|
|
|
108 |
* @param {String} pageType The type of the page
|
|
|
109 |
* @param {String} pageLayout The layout of the page
|
|
|
110 |
* @param {String} subPage The subpage identifier
|
|
|
111 |
* @return {Promise}
|
|
|
112 |
*/
|
|
|
113 |
const getAddableBlocks = async(pageType, pageLayout, subPage) => {
|
|
|
114 |
const request = {
|
|
|
115 |
methodname: 'core_block_fetch_addable_blocks',
|
|
|
116 |
args: {
|
|
|
117 |
pagecontextid: M.cfg.contextid,
|
|
|
118 |
pagetype: pageType,
|
|
|
119 |
pagelayout: pageLayout,
|
|
|
120 |
subpage: subPage,
|
|
|
121 |
},
|
|
|
122 |
};
|
|
|
123 |
|
|
|
124 |
return Ajax.call([request])[0];
|
|
|
125 |
};
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* Set up the actions.
|
|
|
129 |
*
|
|
|
130 |
* @method init
|
|
|
131 |
* @param {String} pageType The type of the page
|
|
|
132 |
* @param {String} pageLayout The layout of the page
|
|
|
133 |
* @param {String|null} addBlockUrl The add block URL
|
|
|
134 |
* @param {String} subPage The subpage identifier
|
|
|
135 |
*/
|
|
|
136 |
export const init = (pageType, pageLayout, addBlockUrl = null, subPage = '') => {
|
|
|
137 |
if (!listenerEventsRegistered) {
|
|
|
138 |
registerListenerEvents(pageType, pageLayout, addBlockUrl, subPage);
|
|
|
139 |
listenerEventsRegistered = true;
|
|
|
140 |
}
|
|
|
141 |
};
|