vendor/assets/javascripts/twitter/bootstrap/scrollspy.js in less-rails-bootstrap-2.0.10 vs vendor/assets/javascripts/twitter/bootstrap/scrollspy.js in less-rails-bootstrap-2.0.11

- old
+ new

@@ -1,7 +1,7 @@ /* ============================================================= - * bootstrap-scrollspy.js v2.0.2 + * bootstrap-scrollspy.js v2.0.3 * http://twitter.github.com/bootstrap/javascript.html#scrollspy * ============================================================= * Copyright 2012 Twitter, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,14 +15,16 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================== */ -!function ( $ ) { - "use strict" +!function ($) { + "use strict"; // jshint ;_; + + /* SCROLLSPY CLASS DEFINITION * ========================== */ function ScrollSpy( element, options) { var process = $.proxy(this.process, this) @@ -41,54 +43,78 @@ ScrollSpy.prototype = { constructor: ScrollSpy , refresh: function () { - this.targets = this.$body + var self = this + , $targets + + this.offsets = $([]) + this.targets = $([]) + + $targets = this.$body .find(this.selector) .map(function () { - var href = $(this).attr('href') - return /^#\w/.test(href) && $(href).length ? href : null + var $el = $(this) + , href = $el.data('target') || $el.attr('href') + , $href = /^#\w/.test(href) && $(href) + return ( $href + && href.length + && [[ $href.position().top, href ]] ) || null }) - - this.offsets = $.map(this.targets, function (id) { - return $(id).position().top - }) + .sort(function (a, b) { return a[0] - b[0] }) + .each(function () { + self.offsets.push(this[0]) + self.targets.push(this[1]) + }) } , process: function () { var scrollTop = this.$scrollElement.scrollTop() + this.options.offset + , scrollHeight = this.$scrollElement[0].scrollHeight || this.$body[0].scrollHeight + , maxScroll = scrollHeight - this.$scrollElement.height() , offsets = this.offsets , targets = this.targets , activeTarget = this.activeTarget , i + if (scrollTop >= maxScroll) { + return activeTarget != (i = targets.last()[0]) + && this.activate ( i ) + } + for (i = offsets.length; i--;) { activeTarget != targets[i] && scrollTop >= offsets[i] && (!offsets[i + 1] || scrollTop <= offsets[i + 1]) && this.activate( targets[i] ) } } , activate: function (target) { var active + , selector this.activeTarget = target - this.$body - .find(this.selector).parent('.active') + $(this.selector) + .parent('.active') .removeClass('active') - active = this.$body - .find(this.selector + '[href="' + target + '"]') + selector = this.selector + + '[data-target="' + target + '"],' + + this.selector + '[href="' + target + '"]' + + active = $(selector) .parent('li') .addClass('active') - if ( active.parent('.dropdown-menu') ) { - active.closest('li.dropdown').addClass('active') + if (active.parent('.dropdown-menu')) { + active = active.closest('li.dropdown').addClass('active') } + + active.trigger('activate') } } @@ -120,6 +146,6 @@ var $spy = $(this) $spy.scrollspy($spy.data()) }) }) -}( window.jQuery ); +}(window.jQuery); \ No newline at end of file