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