MDL-71963 Core: Convert block deletion to a modal

Co-authored-by: Mathew May <mathewm@hotmail.co.nz>
This commit is contained in:
Andrew Nicols
2021-06-30 09:37:34 +08:00
committed by Mathew May
parent 214adb7984
commit 529c5c2ece
8 changed files with 165 additions and 2 deletions
+32
View File
@@ -0,0 +1,32 @@
@core @core_block
Feature: Block removal via modal
In order to remove blocks
As a teacher
I need to use a modal to confirm the block to delete
Background:
Given the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And I log in as "admin"
And I am on "Course 1" course homepage with editing mode on
And I add the "Search forums" block
And I should see "Search forums"
@javascript
Scenario: Removing a block via modal should remove the block on the page
Given I open the "Search forums" blocks action menu
When I click on "Delete Search forums block" "link" in the "Search forums" "block"
Then "Delete block?" "dialogue" should exist
And I click on "Delete" "button" in the "Delete block?" "dialogue"
And I wait to be redirected
And I should not see "Search forums"
@javascript
Scenario: Cancel removing a block via modal should retain the block on the page
Given I open the "Search forums" blocks action menu
When I click on "Delete Search forums block" "link" in the "Search forums" "block"
Then "Delete block?" "dialogue" should exist
And I click on "Cancel" "button" in the "Delete block?" "dialogue"
And I should not see "Delete block?"
And I should see "Search forums"
+1 -1
View File
@@ -33,7 +33,7 @@ Feature: Course activity controls works as expected
And I add the "Recent activity" block
And I open the action menu in "Recent activity" "block"
And I click on "Delete Recent activity block" "link"
And I press "Yes"
And I click on "Delete" "button" in the "Delete block?" "dialogue"
And <belowpage> "section" <should_see_other_sections> exist
And I add a "Forum" to section "1" and I fill the form with:
| Forum name | Test forum name 1 |
+1
View File
@@ -39,6 +39,7 @@ $string['defaultregion_help'] = 'Themes may define one or more named block regio
$string['defaultweight'] = 'Default weight';
$string['defaultweight_help'] = 'The default weight allows you to choose roughly where you want the block to appear in the chosen region, either at the top or the bottom. The final location is calculated from all the blocks in that region (for example, only one block can actually be at the top). This value can be overridden on specific pages if required.';
$string['deletecheck'] = 'Delete {$a} block?';
$string['deletecheck_modal'] = 'Delete block?';
$string['deleteblock'] = 'Delete {$a} block';
$string['deleteblockcheck'] = 'Are you sure that you want to delete this block titled {$a}?';
$string['deleteblockwarning'] = '<p>You are about to delete a block that appears elsewhere.</p><p>Original block location: {$a->location}<br />Display on page types: {$a->pagetype}</p><p>Are you sure you want to continue?</p>';
+2
View File
@@ -0,0 +1,2 @@
function _typeof(a){"@babel/helpers - typeof";if("function"==typeof Symbol&&"symbol"==typeof Symbol.iterator){_typeof=function(a){return typeof a}}else{_typeof=function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a}}return _typeof(a)}define ("core/confirm",["core/notification","core/str"],function(a,b){"use strict";b=d(b);function c(){if("function"!=typeof WeakMap)return null;var a=new WeakMap;c=function(){return a};return a}function d(a){if(a&&a.__esModule){return a}if(null===a||"object"!==_typeof(a)&&"function"!=typeof a){return{default:a}}var b=c();if(b&&b.has(a)){return b.get(a)}var d={},e=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var f in a){if(Object.prototype.hasOwnProperty.call(a,f)){var g=e?Object.getOwnPropertyDescriptor(a,f):null;if(g&&(g.get||g.set)){Object.defineProperty(d,f,g)}else{d[f]=a[f]}}}d.default=a;if(b){b.set(a,d)}return d}var e=!1,f=function(b,c,d){return new Promise(function(e,f){(0,a.saveCancel)(b,c,d,e,f)})},g=function(a,c){if(a["confirmation".concat(c,"Str")]){return b.get_string.apply(null,JSON.parse(a["confirmation".concat(c,"Str")]))}return a["confirmation".concat(c)]},h=function(){document.addEventListener("click",function(a){var b=a.target.closest("[data-confirmation=\"modal\"]");if(b){a.preventDefault();f(g(b.dataset,"Title"),g(b.dataset,"Question"),g(b.dataset,"YesButton")).then(function(){window.location.href=b.dataset.confirmationDestination}).catch(function(){})}})};if(!e){h();e=!0}});
//# sourceMappingURL=confirm.min.js.map
File diff suppressed because one or more lines are too long
+109
View File
@@ -0,0 +1,109 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Javascript events for the `core_confirm` modal.
*
* @module core/confirm
* @copyright 2021 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 4.0
*
* @example <caption>Calling the confirmation modal to delete a block</caption>
*
* // The following is an example of how to use this module via an indirect PHP call with a button.
*
* $controls[] = new action_menu_link_secondary(
* $deleteactionurl,
* new pix_icon('t/delete', $str, 'moodle', array('class' => 'iconsmall', 'title' => '')),
* $str,
* [
* 'class' => 'editing_delete',
* 'data-confirmation' => 'modal', // Needed so this module will pick it up in the click handler.
* 'data-confirmation-title-str' => json_encode(['deletecheck_modal', 'block']),
* 'data-confirmation-question-str' => json_encode(['deleteblockcheck', 'block', $blocktitle]),
* 'data-confirmation-yes-button-str' => json_encode(['delete', 'core']),
* 'data-confirmation-toast' => 'true', // Can be set to inform the user that their action was a success.
* 'data-confirmation-toast-confirmation-str' => json_encode(['deleteblockinprogress', 'block', $blocktitle]),
* 'data-confirmation-destination' => $deleteconfirmationurl->out(false), // Where do you want to direct the user?
* ]
* );
*/
import {saveCancel} from 'core/notification';
import * as Str from 'core/str';
// We want to ensure that we only initialize the listeners only once.
let registered = false;
/**
* Add all of the supplied notifications.
*
* @method confirmedPromise
* @protected
* @param {Promise|String} title The header of the modal
* @param {Promise|String} question What do we want the user to confirm
* @param {Promise|String} saveLabel The modal action link text
* @return {Promise}
*/
const confirmedPromise = (title, question, saveLabel) => new Promise((resolve, reject) => {
saveCancel(title, question, saveLabel, resolve, reject);
});
/**
* Either fetch the string or return it from the dom node.
*
* @method getConfirmationString
* @private
* @param {HTMLElement} dataset The page element to fetch dataset items in
* @param {String} field The dataset field name to fetch the contents of
* @return {Promise|String}
*
*/
const getConfirmationString = (dataset, field) => {
if (dataset[`confirmation${field}Str`]) {
return Str.get_string.apply(null, JSON.parse(dataset[`confirmation${field}Str`]));
}
return dataset[`confirmation${field}`];
};
/**
* Set up the listeners for the confirmation modal widget within the page.
*
* @method registerConfirmationListeners
* @private
*/
const registerConfirmationListeners = () => {
document.addEventListener('click', e => {
const confirmRequest = e.target.closest('[data-confirmation="modal"]');
if (confirmRequest) {
e.preventDefault();
confirmedPromise(
getConfirmationString(confirmRequest.dataset, 'Title'),
getConfirmationString(confirmRequest.dataset, 'Question'),
getConfirmationString(confirmRequest.dataset, 'YesButton'),
)
.then(() => {
window.location.href = confirmRequest.dataset.confirmationDestination;
return;
}).catch(() => {}); // Catch an empty function as the user cancelled rather than an actual error.
}
});
};
if (!registered) {
registerConfirmationListeners();
registered = true;
}
+18 -1
View File
@@ -1350,11 +1350,28 @@ class block_manager {
$str = new lang_string('deleteblock', 'block', $blocktitle);
$deleteactionurl = new moodle_url($actionurl, ['bui_deleteid' => $block->instance->id]);
$deleteactionurl->remove_params(['sesskey']);
$deleteconfirmationurl = new moodle_url($actionurl, [
'bui_deleteid' => $block->instance->id,
'bui_confirm' => 1,
'sesskey' => sesskey(),
]);
$blocktitle = $block->get_title();
$controls[] = new action_menu_link_secondary(
$deleteactionurl,
new pix_icon('t/delete', $str, 'moodle', array('class' => 'iconsmall', 'title' => '')),
$str,
array('class' => 'editing_delete')
[
'class' => 'editing_delete',
'data-confirmation' => 'modal',
'data-confirmation-title-str' => json_encode(['deletecheck_modal', 'block']),
'data-confirmation-question-str' => json_encode(['deleteblockcheck', 'block', $blocktitle]),
'data-confirmation-yes-button-str' => json_encode(['delete', 'core']),
'data-confirmation-toast' => 'true',
'data-confirmation-toast-confirmation-str' => json_encode(['deleteblockinprogress', 'block', $blocktitle]),
'data-confirmation-destination' => $deleteconfirmationurl->out(false),
]
);
}
+1
View File
@@ -1661,6 +1661,7 @@ EOF;
$this->js_call_amd('core/log', 'setConfig', array($logconfig));
// Add any global JS that needs to run on all pages.
$this->js_call_amd('core/page_global', 'init');
$this->js_call_amd('core/confirm');
// Call amd init functions.
$output .= $this->get_amd_footercode();