]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
add preventDefault support for all inital event types (show, close, hide, etc.) ...
authorJacob Thornton <jacobthornton@gmail.com>
Sun, 25 Mar 2012 01:20:09 +0000 (18:20 -0700)
committerJacob Thornton <jacobthornton@gmail.com>
Sun, 25 Mar 2012 01:20:09 +0000 (18:20 -0700)
16 files changed:
docs/assets/bootstrap.zip
docs/assets/js/bootstrap-alert.js
docs/assets/js/bootstrap-carousel.js
docs/assets/js/bootstrap-collapse.js
docs/assets/js/bootstrap-scrollspy.js
docs/assets/js/bootstrap-tab.js
js/bootstrap-alert.js
js/bootstrap-carousel.js
js/bootstrap-collapse.js
js/bootstrap-scrollspy.js
js/bootstrap-tab.js
js/tests/index.html
js/tests/unit/bootstrap-alert.js
js/tests/unit/bootstrap-carousel.js [new file with mode: 0644]
js/tests/unit/bootstrap-collapse.js
js/tests/unit/bootstrap-tab.js

index 8ad9a0695f35dbc9fc933318dd661d1ed85f5cac..594fb24db8cf2d9287ba4f0559891cbf0e78c526 100644 (file)
Binary files a/docs/assets/bootstrap.zip and b/docs/assets/bootstrap.zip differ
index d17f44e150238fd403b3ff4e5f6e2753b821bf8f..7a1607da152e0c89ffb1f544c6ca784d44fd42ec 100644 (file)
       }
 
       $parent = $(selector)
-      $parent.trigger('close')
 
       e && e.preventDefault()
 
       $parent.length || ($parent = $this.hasClass('alert') ? $this : $this.parent())
 
-      $parent
-        .trigger('close')
-        .removeClass('in')
+      $parent.trigger(e = $.Event('close'))
+
+      if (e.isDefaultPrevented()) return
+
+      $parent.removeClass('in')
 
       function removeElement() {
         $parent
index 7db5bea3331fb171878683c4a670d2089ccd40f0..8ce7418d221d7ae0fe35f9df6512e61d48e3adf7 100644 (file)
@@ -86,6 +86,7 @@
         , direction = type == 'next' ? 'left' : 'right'
         , fallback  = type == 'next' ? 'first' : 'last'
         , that = this
+        , e = $.Event('slide')
 
       this.sliding = true
 
       if ($next.hasClass('active')) return
 
       if ($.support.transition && this.$element.hasClass('slide')) {
+        this.$element.trigger(e)
+        if (e.isDefaultPrevented()) return
         $next.addClass(type)
         $next[0].offsetWidth // force reflow
         $active.addClass(direction)
         $next.addClass(direction)
-        this.$element.trigger('slide')
         this.$element.one($.support.transition.end, function () {
           $next.removeClass([type, direction].join(' ')).addClass('active')
           $active.removeClass(['active', direction].join(' '))
           setTimeout(function () { that.$element.trigger('slid') }, 0)
         })
       } else {
-        this.$element.trigger('slide')
+        this.$element.trigger(e)
+        if (e.isDefaultPrevented()) return
         $active.removeClass('active')
         $next.addClass('active')
         this.sliding = false
index 16b91eb2441eb56be3b98cfa4139a37356d7f11d..478eea39121c7087fcee5db783cb439fff119320 100644 (file)
@@ -61,7 +61,7 @@
       }
 
       this.$element[dimension](0)
-      this.transition('addClass', 'show', 'shown')
+      this.transition('addClass', $.Event('show'), 'shown')
       this.$element[dimension](this.$element[0][scroll])
     }
 
@@ -70,7 +70,7 @@
       if (this.transitioning) return
       dimension = this.dimension()
       this.reset(this.$element[dimension]())
-      this.transition('removeClass', 'hide', 'hidden')
+      this.transition('removeClass', $.Event('hide'), 'hidden')
       this.$element[dimension](0)
     }
 
             that.$element.trigger(completeEvent)
           }
 
+      this.$element.trigger(startEvent)
+
+      if (startEvent.isDefaultPrevented()) return
+
       this.transitioning = 1
 
-      this.$element
-        .trigger(startEvent)
-        [method]('in')
+      this.$element[method]('in')
 
       $.support.transition && this.$element.hasClass('collapse') ?
         this.$element.one($.support.transition.end, complete) :
index bd3f106044f46e1eeb005f41615bc9b2ae31757e..c02e263b0e9dc38e824bdd723ceda56d78b3f6b8 100644 (file)
@@ -46,8 +46,8 @@
         var self = this
           , $targets
 
-        this.offsets = []
-        this.targets = []
+        this.offsets = $([])
+        this.targets = $([])
 
         $targets = this.$body
           .find(this.selector)
index b3938f6714994ef4dbc2572f17bbac08346cb388..974cc0aca21cc884656a2b0008abce67b33663cf 100644 (file)
@@ -39,6 +39,7 @@
         , selector = $this.attr('data-target')
         , previous
         , $target
+        , e
 
       if (!selector) {
         selector = $this.attr('href')
 
       previous = $ul.find('.active a').last()[0]
 
-      $this.trigger({
-        type: 'show'
-      , relatedTarget: previous
+      e = $.Event('show', {
+        relatedTarget: previous
       })
 
+      $this.trigger(e)
+
+      if (e.isDefaultPrevented()) return
+
       $target = $(selector)
 
       this.activate($this.parent('li'), $ul)
index d17f44e150238fd403b3ff4e5f6e2753b821bf8f..7a1607da152e0c89ffb1f544c6ca784d44fd42ec 100644 (file)
       }
 
       $parent = $(selector)
-      $parent.trigger('close')
 
       e && e.preventDefault()
 
       $parent.length || ($parent = $this.hasClass('alert') ? $this : $this.parent())
 
-      $parent
-        .trigger('close')
-        .removeClass('in')
+      $parent.trigger(e = $.Event('close'))
+
+      if (e.isDefaultPrevented()) return
+
+      $parent.removeClass('in')
 
       function removeElement() {
         $parent
index 7db5bea3331fb171878683c4a670d2089ccd40f0..8ce7418d221d7ae0fe35f9df6512e61d48e3adf7 100644 (file)
@@ -86,6 +86,7 @@
         , direction = type == 'next' ? 'left' : 'right'
         , fallback  = type == 'next' ? 'first' : 'last'
         , that = this
+        , e = $.Event('slide')
 
       this.sliding = true
 
       if ($next.hasClass('active')) return
 
       if ($.support.transition && this.$element.hasClass('slide')) {
+        this.$element.trigger(e)
+        if (e.isDefaultPrevented()) return
         $next.addClass(type)
         $next[0].offsetWidth // force reflow
         $active.addClass(direction)
         $next.addClass(direction)
-        this.$element.trigger('slide')
         this.$element.one($.support.transition.end, function () {
           $next.removeClass([type, direction].join(' ')).addClass('active')
           $active.removeClass(['active', direction].join(' '))
           setTimeout(function () { that.$element.trigger('slid') }, 0)
         })
       } else {
-        this.$element.trigger('slide')
+        this.$element.trigger(e)
+        if (e.isDefaultPrevented()) return
         $active.removeClass('active')
         $next.addClass('active')
         this.sliding = false
index 16b91eb2441eb56be3b98cfa4139a37356d7f11d..478eea39121c7087fcee5db783cb439fff119320 100644 (file)
@@ -61,7 +61,7 @@
       }
 
       this.$element[dimension](0)
-      this.transition('addClass', 'show', 'shown')
+      this.transition('addClass', $.Event('show'), 'shown')
       this.$element[dimension](this.$element[0][scroll])
     }
 
@@ -70,7 +70,7 @@
       if (this.transitioning) return
       dimension = this.dimension()
       this.reset(this.$element[dimension]())
-      this.transition('removeClass', 'hide', 'hidden')
+      this.transition('removeClass', $.Event('hide'), 'hidden')
       this.$element[dimension](0)
     }
 
             that.$element.trigger(completeEvent)
           }
 
+      this.$element.trigger(startEvent)
+
+      if (startEvent.isDefaultPrevented()) return
+
       this.transitioning = 1
 
-      this.$element
-        .trigger(startEvent)
-        [method]('in')
+      this.$element[method]('in')
 
       $.support.transition && this.$element.hasClass('collapse') ?
         this.$element.one($.support.transition.end, complete) :
index bd3f106044f46e1eeb005f41615bc9b2ae31757e..c02e263b0e9dc38e824bdd723ceda56d78b3f6b8 100644 (file)
@@ -46,8 +46,8 @@
         var self = this
           , $targets
 
-        this.offsets = []
-        this.targets = []
+        this.offsets = $([])
+        this.targets = $([])
 
         $targets = this.$body
           .find(this.selector)
index b3938f6714994ef4dbc2572f17bbac08346cb388..974cc0aca21cc884656a2b0008abce67b33663cf 100644 (file)
@@ -39,6 +39,7 @@
         , selector = $this.attr('data-target')
         , previous
         , $target
+        , e
 
       if (!selector) {
         selector = $this.attr('href')
 
       previous = $ul.find('.active a').last()[0]
 
-      $this.trigger({
-        type: 'show'
-      , relatedTarget: previous
+      e = $.Event('show', {
+        relatedTarget: previous
       })
 
+      $this.trigger(e)
+
+      if (e.isDefaultPrevented()) return
+
       $target = $(selector)
 
       this.activate($this.parent('li'), $ul)
index 2d59ab032108bba254aa214a862415a838fb3b3c..3e6cb9777c643ab9817b6f7bea26aca811d51667 100644 (file)
@@ -15,6 +15,7 @@
   <script src="../../js/bootstrap-transition.js"></script>
   <script src="../../js/bootstrap-alert.js"></script>
   <script src="../../js/bootstrap-button.js"></script>
+  <script src="../../js/bootstrap-carousel.js"></script>
   <script src="../../js/bootstrap-collapse.js"></script>
   <script src="../../js/bootstrap-dropdown.js"></script>
   <script src="../../js/bootstrap-modal.js"></script>
@@ -28,6 +29,7 @@
   <script src="unit/bootstrap-transition.js"></script>
   <script src="unit/bootstrap-alert.js"></script>
   <script src="unit/bootstrap-button.js"></script>
+  <script src="unit/bootstrap-carousel.js"></script>
   <script src="unit/bootstrap-collapse.js"></script>
   <script src="unit/bootstrap-dropdown.js"></script>
   <script src="unit/bootstrap-modal.js"></script>
index e607f43402fe48aa32351a5ff59da27ab6b0bc57..7f24e0e6bec83d22be915733c4a4a46ecc5dc677 100644 (file)
@@ -38,4 +38,19 @@ $(function () {
         ok(!$('#qunit-fixture').find('.alert-message').length, 'element removed from dom')
       })
 
+      test("should not fire closed when close is prevented", function () {
+        $.support.transition = false
+        stop();
+        $('<div class="alert"/>')
+          .bind('close', function (e) {
+            e.preventDefault();
+            ok(true);
+            start();
+          })
+          .bind('closed', function () {
+            ok(false);
+          })
+          .alert('close')
+      })
+
 })
\ No newline at end of file
diff --git a/js/tests/unit/bootstrap-carousel.js b/js/tests/unit/bootstrap-carousel.js
new file mode 100644 (file)
index 0000000..92c23e2
--- /dev/null
@@ -0,0 +1,28 @@
+$(function () {
+
+    module("bootstrap-carousel")
+
+      test("should be defined on jquery object", function () {
+        ok($(document.body).carousel, 'carousel method is defined')
+      })
+
+      test("should return element", function () {
+        ok($(document.body).carousel()[0] == document.body, 'document.body returned')
+      })
+
+      test("should not fire sliden when slide is prevented", function () {
+        $.support.transition = false
+        stop();
+        $('<div class="carousel"/>')
+          .bind('slide', function (e) {
+            e.preventDefault();
+            ok(true);
+            start();
+          })
+          .bind('slid', function () {
+            ok(false);
+          })
+          .carousel('next')
+      })
+
+})
\ No newline at end of file
index 698238d96b0718ae0553b33f10861f2a1204a43c..8e52898b5d7d1c86a0a918b84129116d26811399 100644 (file)
@@ -22,4 +22,19 @@ $(function () {
         ok(/height/.test(el.attr('style')), 'has height set')
       })
 
+      test("should not fire shown when show is prevented", function () {
+        $.support.transition = false
+        stop();
+        $('<div class="collapse"/>')
+          .bind('show', function (e) {
+            e.preventDefault();
+            ok(true);
+            start();
+          })
+          .bind('shown', function () {
+            ok(false);
+          })
+          .collapse('show')
+      })
+
 })
\ No newline at end of file
index 18f490fa5e454500b1137dc934ddb720c5250e31..9878047810dd0be1a8595e112d3aa20a8b0895cc 100644 (file)
@@ -42,4 +42,20 @@ $(function () {
         equals($("#qunit-fixture").find('.active').attr('id'), "home")
       })
 
+
+      test("should not fire closed when close is prevented", function () {
+        $.support.transition = false
+        stop();
+        $('<div class="tab"/>')
+          .bind('show', function (e) {
+            e.preventDefault();
+            ok(true);
+            start();
+          })
+          .bind('shown', function () {
+            ok(false);
+          })
+          .tab('show')
+      })
+
 })
\ No newline at end of file