github.com/kayoticsully/syncthing@v0.8.9-0.20140724133906-c45a2fdc03f8/assets/bootstrap-3.1.1/js/scrollspy.js (about) 1 /* ======================================================================== 2 * Bootstrap: scrollspy.js v3.1.1 3 * http://getbootstrap.com/javascript/#scrollspy 4 * ======================================================================== 5 * Copyright 2011-2014 Twitter, Inc. 6 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) 7 * ======================================================================== */ 8 9 10 +function ($) { 11 'use strict'; 12 13 // SCROLLSPY CLASS DEFINITION 14 // ========================== 15 16 function ScrollSpy(element, options) { 17 var href 18 var process = $.proxy(this.process, this) 19 20 this.$element = $(element).is('body') ? $(window) : $(element) 21 this.$body = $('body') 22 this.$scrollElement = this.$element.on('scroll.bs.scroll-spy.data-api', process) 23 this.options = $.extend({}, ScrollSpy.DEFAULTS, options) 24 this.selector = (this.options.target 25 || ((href = $(element).attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) //strip for ie7 26 || '') + ' .nav li > a' 27 this.offsets = $([]) 28 this.targets = $([]) 29 this.activeTarget = null 30 31 this.refresh() 32 this.process() 33 } 34 35 ScrollSpy.DEFAULTS = { 36 offset: 10 37 } 38 39 ScrollSpy.prototype.refresh = function () { 40 var offsetMethod = this.$element[0] == window ? 'offset' : 'position' 41 42 this.offsets = $([]) 43 this.targets = $([]) 44 45 var self = this 46 var $targets = this.$body 47 .find(this.selector) 48 .map(function () { 49 var $el = $(this) 50 var href = $el.data('target') || $el.attr('href') 51 var $href = /^#./.test(href) && $(href) 52 53 return ($href 54 && $href.length 55 && $href.is(':visible') 56 && [[ $href[offsetMethod]().top + (!$.isWindow(self.$scrollElement.get(0)) && self.$scrollElement.scrollTop()), href ]]) || null 57 }) 58 .sort(function (a, b) { return a[0] - b[0] }) 59 .each(function () { 60 self.offsets.push(this[0]) 61 self.targets.push(this[1]) 62 }) 63 } 64 65 ScrollSpy.prototype.process = function () { 66 var scrollTop = this.$scrollElement.scrollTop() + this.options.offset 67 var scrollHeight = this.$scrollElement[0].scrollHeight || this.$body[0].scrollHeight 68 var maxScroll = scrollHeight - this.$scrollElement.height() 69 var offsets = this.offsets 70 var targets = this.targets 71 var activeTarget = this.activeTarget 72 var i 73 74 if (scrollTop >= maxScroll) { 75 return activeTarget != (i = targets.last()[0]) && this.activate(i) 76 } 77 78 if (activeTarget && scrollTop <= offsets[0]) { 79 return activeTarget != (i = targets[0]) && this.activate(i) 80 } 81 82 for (i = offsets.length; i--;) { 83 activeTarget != targets[i] 84 && scrollTop >= offsets[i] 85 && (!offsets[i + 1] || scrollTop <= offsets[i + 1]) 86 && this.activate( targets[i] ) 87 } 88 } 89 90 ScrollSpy.prototype.activate = function (target) { 91 this.activeTarget = target 92 93 $(this.selector) 94 .parentsUntil(this.options.target, '.active') 95 .removeClass('active') 96 97 var selector = this.selector + 98 '[data-target="' + target + '"],' + 99 this.selector + '[href="' + target + '"]' 100 101 var active = $(selector) 102 .parents('li') 103 .addClass('active') 104 105 if (active.parent('.dropdown-menu').length) { 106 active = active 107 .closest('li.dropdown') 108 .addClass('active') 109 } 110 111 active.trigger('activate.bs.scrollspy') 112 } 113 114 115 // SCROLLSPY PLUGIN DEFINITION 116 // =========================== 117 118 var old = $.fn.scrollspy 119 120 $.fn.scrollspy = function (option) { 121 return this.each(function () { 122 var $this = $(this) 123 var data = $this.data('bs.scrollspy') 124 var options = typeof option == 'object' && option 125 126 if (!data) $this.data('bs.scrollspy', (data = new ScrollSpy(this, options))) 127 if (typeof option == 'string') data[option]() 128 }) 129 } 130 131 $.fn.scrollspy.Constructor = ScrollSpy 132 133 134 // SCROLLSPY NO CONFLICT 135 // ===================== 136 137 $.fn.scrollspy.noConflict = function () { 138 $.fn.scrollspy = old 139 return this 140 } 141 142 143 // SCROLLSPY DATA-API 144 // ================== 145 146 $(window).on('load', function () { 147 $('[data-spy="scroll"]').each(function () { 148 var $spy = $(this) 149 $spy.scrollspy($spy.data()) 150 }) 151 }) 152 153 }(jQuery);