Skip to content

Commit

Permalink
Merge branch 'MDL-71963-master' of https://github.com/Chocolate-light…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols authored and stronk7 committed Oct 5, 2021
2 parents 40e4f5f + 6dcb5eb commit 89457e9
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 3 deletions.
32 changes: 32 additions & 0 deletions blocks/tests/behat/delete_block.feature
@@ -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"
2 changes: 1 addition & 1 deletion course/tests/behat/course_controls.feature
Expand Up @@ -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 |
Expand Down
4 changes: 3 additions & 1 deletion lang/en/block.php
Expand Up @@ -39,8 +39,10 @@
$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['deleteblockinprogress'] = 'Block {$a} is currently being removed from this page';
$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>';
$string['hideblock'] = 'Hide {$a} block';
$string['hidepanel'] = 'Hide panel';
Expand Down Expand Up @@ -78,4 +80,4 @@
$string['hidedockpanel'] = 'Hide the dock panel';
$string['undockall'] = 'Undock all';
$string['undockblock'] = 'Undock {$a} block';
$string['undockitem'] = 'Undock this item';
$string['undockitem'] = 'Undock this item';
2 changes: 2 additions & 0 deletions lib/amd/build/confirm.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/amd/build/confirm.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

118 changes: 118 additions & 0 deletions lib/amd/src/confirm.js
@@ -0,0 +1,118 @@
// 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, exception} from 'core/notification';
import * as Str from 'core/str';
import {add as addToast} from 'core/toast';

// 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(() => {
if (confirmRequest.dataset[`confirmationToast`] === 'true') {
const stringForToast = getConfirmationString(confirmRequest.dataset, 'ToastConfirmation');
if (typeof stringForToast === "string") {
addToast(stringForToast);
} else {
stringForToast.then(str => addToast(str)).catch(e => exception(e));
}
}
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;
}
19 changes: 18 additions & 1 deletion lib/blocklib.php
Expand Up @@ -1350,11 +1350,28 @@ public function edit_controls($block) {
$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),
]
);
}

Expand Down
1 change: 1 addition & 0 deletions lib/outputrequirementslib.php
Expand Up @@ -1668,6 +1668,7 @@ public function get_end_code() {
$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();
Expand Down

0 comments on commit 89457e9

Please sign in to comment.