MDL-60207 javascript: close modal when user touch/click outside it
authorTreu Quan <quanht2795@gmail.com>
Mon, 22 Jan 2018 06:24:52 +0000 (13:24 +0700)
committerRyan Wyllie <ryan@ryanwyllie.com>
Tue, 29 May 2018 02:04:37 +0000 (10:04 +0800)
lib/amd/build/modal.min.js
lib/amd/src/modal.js
lib/tests/behat/action_modal.feature [new file with mode: 0644]
lib/tests/behat/behat_general.php
lib/yui/build/moodle-core-notification-dialogue/moodle-core-notification-dialogue-debug.js
lib/yui/build/moodle-core-notification-dialogue/moodle-core-notification-dialogue-min.js
lib/yui/build/moodle-core-notification-dialogue/moodle-core-notification-dialogue.js
lib/yui/src/notification/js/dialogue.js

index bef1918..b60885a 100644 (file)
Binary files a/lib/amd/build/modal.min.js and b/lib/amd/build/modal.min.js differ
index 8b6e1ec..91466e3 100644 (file)
@@ -722,6 +722,15 @@ define(['jquery', 'core/templates', 'core/notification', 'core/key_codes',
             }
         }.bind(this));
 
+        // Listen for clicks on the modal container.
+        this.getRoot().click(function(e) {
+            // If the click wasn't inside the modal element then we should
+            // hide the modal.
+            if (!$(e.target).closest(SELECTORS.MODAL).length) {
+                this.hide();
+            }
+        }.bind(this));
+
         CustomEvents.define(this.getModal(), [CustomEvents.events.activate]);
         this.getModal().on(CustomEvents.events.activate, SELECTORS.HIDE, function(e, data) {
             this.hide();
diff --git a/lib/tests/behat/action_modal.feature b/lib/tests/behat/action_modal.feature
new file mode 100644 (file)
index 0000000..8a85aa6
--- /dev/null
@@ -0,0 +1,50 @@
+@core
+Feature: Close modals by clicking outside them
+  In order to easily close the currently open pop-up
+  As a user
+  Clicking outside the modal should close it.
+
+  @javascript
+  Scenario: The popup closes when clicked on dead space - YUI
+    Given the following "courses" exist:
+      | fullname | shortname |
+      | Course 1 | C1        |
+    And the following "activities" exist:
+      | activity | name           | intro                 | course | idnumber |
+      | quiz     | Test quiz name | Test quiz description | C1     | quiz1    |
+    And I log in as "admin"
+    And I am on "Course 1" course homepage
+    And I follow "Test quiz name"
+    And I click on "Edit quiz" "button"
+    And I click on "Add" "link"
+    And I click on "a new question" "link"
+    # Cannot use the normal ‘I click on’ here, because the pop-up gets in the way.
+    And I click on ".moodle-dialogue-lightbox" "css_element" skipping visibility check
+    Then I should not see "Choose a question type to add"
+
+  @javascript
+  Scenario: The popup closes when clicked on dead space - Modal
+    And I log in as "admin"
+    And I click on "Site home" "link"
+    And I turn editing mode on
+    And I click on "Add a block" "link"
+    And I click on ".modal" "css_element"
+    Then ".modal-backdrop" "css_element" should not be visible
+    And ".modal-content" "css_element" should not be visible
+
+  @javascript
+  Scenario: The popup help closes when clicked
+    Given the following "courses" exist:
+      | fullname | shortname |
+      | Course 1 | C1        |
+    And the following "activities" exist:
+      | activity | name           | intro                 | course | idnumber |
+      | quiz     | Test quiz name | Test quiz description | C1     | quiz1    |
+    And I log in as "admin"
+    And I am on "Course 1" course homepage
+    And I follow "Test quiz name"
+    And I click on "Edit quiz" "button"
+    And I click on "Help with Editing quiz" "icon"
+    And I should see "More help"
+    And I click on "body" "css_element"
+    Then I should not see "More help"
index 4128402..6951b15 100644 (file)
@@ -1722,4 +1722,19 @@ class behat_general extends behat_base {
         $value = ($shift == ' shift') ? [\WebDriver\Key::SHIFT . \WebDriver\Key::TAB] : [\WebDriver\Key::TAB];
         $this->getSession()->getDriver()->getWebDriverSession()->activeElement()->postValue(['value' => $value]);
     }
+
+    /**
+     * Trigger click on node via javascript instead of actually clicking on it via pointer.
+     * This function resolves the issue of nested elements.
+     *
+     * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" skipping visibility check$/
+     * @param string $element
+     * @param string $selectortype
+     */
+    public function i_click_on_skipping_visibility_check($element, $selectortype) {
+
+        // Gets the node based on the requested selector type and locator.
+        $node = $this->get_selected_node($selectortype, $element);
+        $this->js_trigger_click($node);
+    }
 }
index 6b17799..ad65070 100644 (file)
Binary files a/lib/yui/build/moodle-core-notification-dialogue/moodle-core-notification-dialogue-debug.js and b/lib/yui/build/moodle-core-notification-dialogue/moodle-core-notification-dialogue-debug.js differ
index cb1767c..5adb3f3 100644 (file)
Binary files a/lib/yui/build/moodle-core-notification-dialogue/moodle-core-notification-dialogue-min.js and b/lib/yui/build/moodle-core-notification-dialogue/moodle-core-notification-dialogue-min.js differ
index 896d1c7..649c32e 100644 (file)
Binary files a/lib/yui/build/moodle-core-notification-dialogue/moodle-core-notification-dialogue.js and b/lib/yui/build/moodle-core-notification-dialogue/moodle-core-notification-dialogue.js differ
index f31ab9c..d9581b3 100644 (file)
@@ -51,7 +51,8 @@ Y.extend(DIALOGUE, Y.Panel, {
     // Orientation change event listener.
     _orientationevent: null,
     _calculatedzindex: false,
-
+    // Current maskNode id
+    _currentMaskNodeId: null,
     /**
      * The original position of the dialogue before it was reposition to
      * avoid browser jumping.
@@ -101,6 +102,10 @@ Y.extend(DIALOGUE, Y.Panel, {
             // hidden by default. ARIA visibility is managed for modal dialogues.
             this.get(BASE).set('aria-hidden', 'true');
             this.plug(Y.M.core.LockScroll);
+
+            var maskNode = this.get('maskNode');
+            maskNode.on('click', this.hide, this);
+            this._currentMaskNodeId = maskNode.get('_yuid');
         }
 
         // Workaround upstream YUI bug http://yuilibrary.com/projects/yui3/ticket/2532507
@@ -127,6 +132,13 @@ Y.extend(DIALOGUE, Y.Panel, {
                 this._originalPosition = bb.getXY();
             }
 
+            // Check if maskNode already init click event.
+            var maskNode = this.get('maskNode');
+            if (this._currentMaskNodeId !== maskNode.get('_yuid')) {
+                this._currentMaskNodeId = maskNode.get('_yuid');
+                maskNode.on('click', this.hide, this);
+            }
+
             if (bb.getStyle('position') !== 'fixed') {
                 // If the boundingBox has been positioned in a fixed manner, then it will not position correctly to scrollTop.
                 bb.setStyles({