mirrored from git://git.moodle.org/moodle.git
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'MDL-71963-master' of https://github.com/Chocolate-light…
- Loading branch information
Showing
8 changed files
with
176 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters