]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
toast should allow prevent default for hide and show events
authorJohann-S <johann.servoire@gmail.com>
Thu, 16 May 2019 09:57:05 +0000 (11:57 +0200)
committerJohann-S <johann.servoire@gmail.com>
Thu, 16 May 2019 11:24:29 +0000 (13:24 +0200)
js/src/toast.js
js/tests/unit/toast.js

index 9956fd4064eefad0ba563034ba6a620343687853..b29b051ec36ea4f9913dc6a4bf9e5fd8a3c96dc5 100644 (file)
@@ -90,7 +90,11 @@ class Toast {
   // Public
 
   show() {
-    EventHandler.trigger(this._element, Event.SHOW)
+    const showEvent = EventHandler.trigger(this._element, Event.SHOW)
+
+    if (showEvent.defaultPrevented) {
+      return
+    }
 
     if (this._config.animation) {
       this._element.classList.add(ClassName.FADE)
@@ -126,7 +130,11 @@ class Toast {
       return
     }
 
-    EventHandler.trigger(this._element, Event.HIDE)
+    const hideEvent = EventHandler.trigger(this._element, Event.HIDE)
+
+    if (hideEvent.defaultPrevented) {
+      return
+    }
 
     const complete = () => {
       this._element.classList.add(ClassName.HIDE)
index 57f953bd17df59464e39a3cbd13336cf6effb848..234e5955c549e10a3c409f20538560f0eed8eb30 100644 (file)
@@ -255,4 +255,75 @@ $(function () {
     var toast = Toast._getInstance($toast[0])
     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')
+  })
 })