]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
Backport #28777.
authorJohann-S <johann.servoire@gmail.com>
Wed, 22 May 2019 07:55:14 +0000 (09:55 +0200)
committerXhmikosR <xhmikosr@gmail.com>
Thu, 30 May 2019 08:58:34 +0000 (11:58 +0300)
Toast should allow prevent default for hide and show events

js/src/toast.js
js/tests/unit/toast.js

index 9657048469d0bc441308b3f89c744ec72c003926..fd023e7021dcf4cd9d5b6f9caa495232b1f2e473 100644 (file)
@@ -82,7 +82,12 @@ class Toast {
   // Public
 
   show() {
-    $(this._element).trigger(Event.SHOW)
+    const showEvent = $.Event(Event.SHOW)
+
+    $(this._element).trigger(showEvent)
+    if (showEvent.isDefaultPrevented()) {
+      return
+    }
 
     if (this._config.animation) {
       this._element.classList.add(ClassName.FADE)
@@ -119,7 +124,13 @@ class Toast {
       return
     }
 
-    $(this._element).trigger(Event.HIDE)
+    const hideEvent = $.Event(Event.HIDE)
+
+    $(this._element).trigger(hideEvent)
+    if (hideEvent.isDefaultPrevented()) {
+      return
+    }
+
     this._close()
   }
 
index 2081693ebce6aa83a1e17f9693bbbb3d541b02d7..3b5da05f3f1c7b74e4f02f072994ee3e8fe791b7 100644 (file)
@@ -256,4 +256,75 @@ $(function () {
     var toast = $toast.data('bs.toast')
     assert.strictEqual(toast._config.delay, defaultDelay)
   })
+
+  QUnit.test('should not trigger shown if show is prevented', function (assert) {
+    assert.expect(1)
+    var done = assert.async()
+
+    var toastHtml =
+      '<div class="toast" data-delay="1" data-autohide="false">' +
+        '<div class="toast-body">' +
+          'a simple toast' +
+        '</div>' +
+      '</div>'
+
+    var $toast = $(toastHtml)
+      .bootstrapToast()
+      .appendTo($('#qunit-fixture'))
+
+    var shownCalled = false
+    function assertDone() {
+      setTimeout(function () {
+        assert.strictEqual(shownCalled, false)
+        done()
+      }, 20)
+    }
+
+    $toast
+      .on('show.bs.toast', function (event) {
+        event.preventDefault()
+        assertDone()
+      })
+      .on('shown.bs.toast', function () {
+        shownCalled = true
+      })
+      .bootstrapToast('show')
+  })
+
+  QUnit.test('should not trigger hidden if hide is prevented', function (assert) {
+    assert.expect(1)
+    var done = assert.async()
+
+    var toastHtml =
+      '<div class="toast" data-delay="1" data-autohide="false">' +
+        '<div class="toast-body">' +
+          'a simple toast' +
+        '</div>' +
+      '</div>'
+
+    var $toast = $(toastHtml)
+      .bootstrapToast()
+      .appendTo($('#qunit-fixture'))
+
+    var hiddenCalled = false
+    function assertDone() {
+      setTimeout(function () {
+        assert.strictEqual(hiddenCalled, false)
+        done()
+      }, 20)
+    }
+
+    $toast
+      .on('shown.bs.toast', function () {
+        $toast.bootstrapToast('hide')
+      })
+      .on('hide.bs.toast', function (event) {
+        event.preventDefault()
+        assertDone()
+      })
+      .on('hidden.bs.toast', function () {
+        hiddenCalled = true
+      })
+      .bootstrapToast('show')
+  })
 })