From 529c5c2ece110e2dc7b2b842dbe4f41e9c08c018 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Wed, 30 Jun 2021 09:37:34 +0800 Subject: [PATCH] MDL-71963 Core: Convert block deletion to a modal Co-authored-by: Mathew May --- blocks/tests/behat/delete_block.feature | 32 ++++++ course/tests/behat/course_controls.feature | 2 +- lang/en/block.php | 1 + lib/amd/build/confirm.min.js | Bin 0 -> 1560 bytes lib/amd/build/confirm.min.js.map | Bin 0 -> 5921 bytes lib/amd/src/confirm.js | 109 +++++++++++++++++++++ lib/blocklib.php | 19 +++- lib/outputrequirementslib.php | 1 + 8 files changed, 162 insertions(+), 2 deletions(-) create mode 100644 blocks/tests/behat/delete_block.feature create mode 100644 lib/amd/build/confirm.min.js create mode 100644 lib/amd/build/confirm.min.js.map create mode 100644 lib/amd/src/confirm.js diff --git a/blocks/tests/behat/delete_block.feature b/blocks/tests/behat/delete_block.feature new file mode 100644 index 00000000000..8fff9a65703 --- /dev/null +++ b/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" diff --git a/course/tests/behat/course_controls.feature b/course/tests/behat/course_controls.feature index a571bf6ad3d..b4b0fa4d4e5 100644 --- a/course/tests/behat/course_controls.feature +++ b/course/tests/behat/course_controls.feature @@ -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 "section" exist And I add a "Forum" to section "1" and I fill the form with: | Forum name | Test forum name 1 | diff --git a/lang/en/block.php b/lang/en/block.php index 4c4307fde2e..6d545a932f7 100644 --- a/lang/en/block.php +++ b/lang/en/block.php @@ -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'] = '

You are about to delete a block that appears elsewhere.

Original block location: {$a->location}
Display on page types: {$a->pagetype}

Are you sure you want to continue?

'; diff --git a/lib/amd/build/confirm.min.js b/lib/amd/build/confirm.min.js new file mode 100644 index 0000000000000000000000000000000000000000..87ed5fa0fe4c80b44747adda43f9e53f01b3d449 GIT binary patch literal 1560 zcmah}?{CyN5dF?yk@}=4mW<1N?aC@T;L8c<-4%pTSydBfvTN|#k>j+g(){m?^J|w1 z4r#S=#-4dI?>)QjoQ}r3>^43$=<5P_&t8^LqCMZEZ7{Ui^K9B<8&emoo->$IQ#iYR zIF#PzIcvwo(+z1N28cfJeg;uXjf563cEQaM84Ls37XR-g5YkfMDOR3?)ZVo*bea&A zQcH7iwyb3neDtaJ%cbLsc@jdO+d0xPR9G8_S;4dqcuv)86Anz=OyAB&QyUG5L?)&f z!LzMcdMrScL~7S!MjJ*)wkcIrlzg+C$aKN`AGkeRxc!9i?Hx3HZp(GnZtfstpd8+3 z^X^6;8C?m5VKXPeumV+yaY7;vLEheC`_5OL#gio3Od!M@JU%jCeno1SxH}SZO8?sD zDr8S|L1*IyahVz|6%Ch+Q|5Uo_s~)YD_kje7%4EGtW-Y;R97RUoLK)Sc(P!8$n!Z& z=dbSF5`K-X4yGZcH?H|MWBgx zd0y<&1doq9*#mXSlm)1{@iQEUQ)rypKNDt4b3i&=7H!cTGK8|{ST8X5kU$WF0UF|(JP5ya7 jvCXsd^FOlIcR?e)Dh(<3@xQlfE-eqn$*-+EK=bT7mn1Ob literal 0 HcmV?d00001 diff --git a/lib/amd/build/confirm.min.js.map b/lib/amd/build/confirm.min.js.map new file mode 100644 index 0000000000000000000000000000000000000000..51d059429008125e4a63535618eb35ef0389106b GIT binary patch literal 5921 zcmb_g3v=5x68t*L54lcRCp=D#9OKWk<Neh0xQieetvp!Y+59Ma=ekDv2ia0u!+*p8xY9s#rcD0)XU zr}vg0Gr_-U>Nnya+syzdqLUpO`anM`ws`r`$@m9jsp38nTr-c!2LDY7RlJ3t%>@IT zVi<2};a0MgZ?tH@&q?VTM*xP9tXQow9mqw)yQUKqGPYK zjDz=4G^SBVhgA%te-4IZB2Ntx*7;GlB^`rPR$U%Gf zewP;i#4HAcjNaofKnY(R(i|QqQ1ghMpY_OLKQQ$yCYMF5AO8?w(i-;JZD^g+!U3ak zw0(lNNu#5f*fD3-3;qr%1PhL%Uql{Rnh7~XoCBu42CMBLB~}9vTVBbr82`FYT%1Sa zQ+j3)@dpwIM`7TGHTj*MHwZxgS7!U4*1$l#W|P(ozHiNmU$e!EviSG0go}4bBpgs% z9oq?(VTU;TA7Gy23*q2kS3MqUsKen?^=3T^#{OqwIot^P(cyriYe)fqII2ZNv@eEi zNW9{&zrjYwQxyNqhT_#taP@75jW6|YrO+%}Vl%mvQyGV?D zDQlgS}%hpmdz% z;-I?BRVsE=DvMHz1Tbl&oXSjGI!3f)lTe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000..75d69624a82 --- /dev/null +++ b/lib/amd/src/confirm.js @@ -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 . + +/** + * Javascript events for the `core_confirm` modal. + * + * @module core/confirm + * @copyright 2021 Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since 4.0 + * + * @example Calling the confirmation modal to delete a block + * + * // 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; +} diff --git a/lib/blocklib.php b/lib/blocklib.php index af86926b025..54bbda0feb5 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -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), + ] ); } diff --git a/lib/outputrequirementslib.php b/lib/outputrequirementslib.php index f5e8d876a28..681c2d9afa4 100644 --- a/lib/outputrequirementslib.php +++ b/lib/outputrequirementslib.php @@ -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(); -- 2.43.0