2015-01-03 16:58:44 -05:00
|
|
|
/** =======================================================================
|
|
|
|
* Bootstrap: scrollspy.js v4.0.0
|
2013-10-29 13:10:47 -04:00
|
|
|
* http://getbootstrap.com/javascript/#scrollspy
|
2013-05-21 22:30:33 -04:00
|
|
|
* ========================================================================
|
2014-12-31 19:23:48 -05:00
|
|
|
* Copyright 2011-2015 Twitter, Inc.
|
2013-12-18 18:28:08 -05:00
|
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
|
2015-01-03 16:58:44 -05:00
|
|
|
* ========================================================================
|
|
|
|
* @fileoverview - Bootstrap's scrollspy plugin.
|
|
|
|
*
|
|
|
|
* Public Methods & Properties:
|
|
|
|
*
|
|
|
|
* + $.scrollspy
|
|
|
|
* + $.scrollspy.noConflict
|
|
|
|
* + $.scrollspy.Constructor
|
|
|
|
* + $.scrollspy.Constructor.VERSION
|
|
|
|
* + $.scrollspy.Constructor.Defaults
|
|
|
|
* + $.scrollspy.Constructor.Defaults.offset
|
|
|
|
* + $.scrollspy.Constructor.prototype.refresh
|
|
|
|
*
|
|
|
|
* ========================================================================
|
|
|
|
*/
|
2011-09-11 01:24:31 -04:00
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
'use strict';
|
2012-03-24 21:59:04 -04:00
|
|
|
|
2014-06-23 14:07:18 -04:00
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
/**
|
|
|
|
* Our scrollspy class.
|
|
|
|
* @param {Element!} element
|
|
|
|
* @param {Object=} opt_config
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function ScrollSpy(element, opt_config) {
|
2014-06-23 14:07:18 -04:00
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
/** @private {Element|Window} */
|
|
|
|
this._scrollElement = element.tagName == 'BODY' ? window : element
|
2014-06-23 14:07:18 -04:00
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
/** @private {Object} */
|
|
|
|
this._config = $.extend({}, ScrollSpy['Defaults'], opt_config)
|
2014-06-23 14:07:18 -04:00
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
/** @private {string} */
|
|
|
|
this._selector = (this._config.target || '') + ' .nav li > a'
|
2014-06-23 14:07:18 -04:00
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
/** @private {Array} */
|
|
|
|
this._offsets = []
|
2014-06-23 14:07:18 -04:00
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
/** @private {Array} */
|
|
|
|
this._targets = []
|
2014-06-23 14:07:18 -04:00
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
/** @private {Element} */
|
|
|
|
this._activeTarget = null
|
2014-06-23 14:07:18 -04:00
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
/** @private {number} */
|
|
|
|
this._scrollHeight = 0
|
2014-06-23 14:07:18 -04:00
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
$(this._scrollElement).on('scroll.bs.scrollspy', this._process.bind(this))
|
2014-05-29 03:24:41 -04:00
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
this['refresh']()
|
2014-06-23 14:07:18 -04:00
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
this._process()
|
|
|
|
}
|
2014-06-10 20:37:40 -04:00
|
|
|
|
2013-05-16 20:18:15 -04:00
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
/**
|
|
|
|
* @const
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
ScrollSpy['VERSION'] = '4.0.0'
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @const
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
|
|
|
ScrollSpy['Defaults'] = {
|
|
|
|
'offset': 10
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @const
|
|
|
|
* @type {string}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ScrollSpy._NAME = 'scrollspy'
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @const
|
|
|
|
* @type {string}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ScrollSpy._DATA_KEY = 'bs.scrollspy'
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @const
|
|
|
|
* @type {Function}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ScrollSpy._JQUERY_NO_CONFLICT = $.fn[ScrollSpy._NAME]
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @const
|
|
|
|
* @enum {string}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ScrollSpy._Event = {
|
|
|
|
ACTIVATE: 'activate.bs.scrollspy'
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @const
|
|
|
|
* @enum {string}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ScrollSpy._ClassName = {
|
|
|
|
DROPDOWN_MENU : 'dropdown-menu',
|
|
|
|
ACTIVE : 'active'
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @const
|
|
|
|
* @enum {string}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ScrollSpy._Selector = {
|
|
|
|
DATA_SPY : '[data-spy="scroll"]',
|
|
|
|
ACTIVE : '.active',
|
|
|
|
LI_DROPDOWN : 'li.dropdown',
|
|
|
|
LI : 'li'
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Object=} opt_config
|
|
|
|
* @this {jQuery}
|
|
|
|
* @return {jQuery}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ScrollSpy._jQueryInterface = function (opt_config) {
|
|
|
|
return this.each(function () {
|
|
|
|
var data = $(this).data(ScrollSpy._DATA_KEY)
|
|
|
|
var config = typeof opt_config === 'object' && opt_config || null
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
data = new ScrollSpy(this, config)
|
|
|
|
$(this).data(ScrollSpy._DATA_KEY, data)
|
2013-11-04 12:09:48 -05:00
|
|
|
}
|
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
if (typeof opt_config === 'string') {
|
|
|
|
data[opt_config]()
|
2013-05-16 20:18:15 -04:00
|
|
|
}
|
2015-01-03 16:58:44 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Refresh the scrollspy target cache
|
|
|
|
*/
|
|
|
|
ScrollSpy.prototype['refresh'] = function () {
|
|
|
|
var offsetMethod = 'offset'
|
|
|
|
var offsetBase = 0
|
|
|
|
|
|
|
|
if (this._scrollElement !== this._scrollElement.window) {
|
|
|
|
offsetMethod = 'position'
|
|
|
|
offsetBase = this._getScrollTop()
|
2014-06-23 14:07:18 -04:00
|
|
|
}
|
2011-09-11 01:14:57 -04:00
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
this._offsets = []
|
|
|
|
this._targets = []
|
2013-05-16 20:18:15 -04:00
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
this._scrollHeight = this._getScrollHeight()
|
2013-05-16 20:18:15 -04:00
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
var targets = /** @type {Array.<Element>} */ ($.makeArray($(this._selector)))
|
2011-11-24 23:27:18 -05:00
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
targets
|
|
|
|
.map(function (element, index) {
|
|
|
|
var target
|
|
|
|
var targetSelector = Bootstrap.getSelectorFromElement(element)
|
2013-05-16 20:18:15 -04:00
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
if (targetSelector) {
|
|
|
|
target = $(targetSelector)[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
if (target && (target.offsetWidth || target.offsetHeight)) {
|
|
|
|
// todo (fat): remove sketch reliance on jQuery position/offset
|
|
|
|
return [$(target)[offsetMethod]().top + offsetBase, targetSelector]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.filter(function (item) { return item })
|
|
|
|
.sort(function (a, b) { return a[0] - b[0] })
|
|
|
|
.forEach(function (item, index) {
|
|
|
|
this._offsets.push(item[0])
|
|
|
|
this._targets.push(item[1])
|
|
|
|
}.bind(this))
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ScrollSpy.prototype._getScrollTop = function () {
|
|
|
|
return this._scrollElement === window ?
|
|
|
|
this._scrollElement.scrollY : this._scrollElement.scrollTop
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ScrollSpy.prototype._getScrollHeight = function () {
|
|
|
|
return this._scrollElement.scrollHeight
|
|
|
|
|| Math.max(document.body.scrollHeight, document.documentElement.scrollHeight)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ScrollSpy.prototype._process = function () {
|
|
|
|
var scrollTop = this._getScrollTop() + this._config.offset
|
|
|
|
var scrollHeight = this._getScrollHeight()
|
|
|
|
var maxScroll = this._config.offset + scrollHeight - this._scrollElement.offsetHeight
|
|
|
|
|
|
|
|
if (this._scrollHeight != scrollHeight) {
|
|
|
|
this['refresh']()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scrollTop >= maxScroll) {
|
|
|
|
var target = this._targets[this._targets.length - 1]
|
|
|
|
|
|
|
|
if (this._activeTarget != target) {
|
|
|
|
this._activate(target)
|
2014-06-12 14:11:04 -04:00
|
|
|
}
|
2015-01-03 16:58:44 -05:00
|
|
|
}
|
2013-05-16 20:18:15 -04:00
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
if (this._activeTarget && scrollTop < this._offsets[0]) {
|
|
|
|
this._activeTarget = null
|
|
|
|
this._clear()
|
|
|
|
return
|
2014-06-23 14:07:18 -04:00
|
|
|
}
|
2011-11-24 23:27:18 -05:00
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
for (var i = this._offsets.length; i--;) {
|
|
|
|
var isActiveTarget = this._activeTarget != this._targets[i]
|
|
|
|
&& scrollTop >= this._offsets[i]
|
|
|
|
&& (!this._offsets[i + 1] || scrollTop < this._offsets[i + 1])
|
|
|
|
|
|
|
|
if (isActiveTarget) {
|
|
|
|
this._activate(this._targets[i])
|
|
|
|
}
|
2014-05-23 18:27:05 -04:00
|
|
|
}
|
2015-01-03 16:58:44 -05:00
|
|
|
}
|
2014-05-23 18:27:05 -04:00
|
|
|
|
2013-05-16 20:18:15 -04:00
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
/**
|
|
|
|
* @param {Element} target
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ScrollSpy.prototype._activate = function (target) {
|
|
|
|
this._activeTarget = target
|
2011-11-24 23:27:18 -05:00
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
this._clear()
|
2014-04-22 01:03:33 -04:00
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
var selector = this._selector
|
|
|
|
+ '[data-target="' + target + '"],'
|
|
|
|
+ this._selector + '[href="' + target + '"]'
|
|
|
|
|
|
|
|
// todo (fat): this seems horribly wrong… getting all raw li elements up the tree ,_,
|
|
|
|
var parentListItems = $(selector).parents(ScrollSpy._Selector.LI)
|
|
|
|
|
|
|
|
for (var i = parentListItems.length; i--;) {
|
|
|
|
$(parentListItems[i]).addClass(ScrollSpy._ClassName.ACTIVE)
|
|
|
|
|
|
|
|
var itemParent = parentListItems[i].parentNode
|
2011-11-24 23:27:18 -05:00
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
if (itemParent && $(itemParent).hasClass(ScrollSpy._ClassName.DROPDOWN_MENU)) {
|
|
|
|
var closestDropdown = $(itemParent).closest(ScrollSpy._Selector.LI_DROPDOWN)[0]
|
|
|
|
$(closestDropdown).addClass(ScrollSpy._ClassName.ACTIVE)
|
|
|
|
}
|
|
|
|
}
|
2011-11-24 23:27:18 -05:00
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
$(this._scrollElement).trigger(ScrollSpy._Event.ACTIVATE, {
|
|
|
|
relatedTarget: target
|
|
|
|
})
|
|
|
|
}
|
2012-12-07 17:06:01 -05:00
|
|
|
|
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ScrollSpy.prototype._clear = function () {
|
|
|
|
var activeParents = $(this._selector).parentsUntil(this._config.target, ScrollSpy._Selector.ACTIVE)
|
2012-12-07 17:06:01 -05:00
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
for (var i = activeParents.length; i--;) {
|
|
|
|
$(activeParents[i]).removeClass(ScrollSpy._ClassName.ACTIVE)
|
2014-06-23 14:07:18 -04:00
|
|
|
}
|
2015-01-03 16:58:44 -05:00
|
|
|
}
|
2011-11-24 23:27:18 -05:00
|
|
|
|
2014-06-12 14:11:04 -04:00
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* jQuery Interface + noConflict implementaiton
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
2014-06-12 14:11:04 -04:00
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
/**
|
|
|
|
* @const
|
|
|
|
* @type {Function}
|
|
|
|
*/
|
|
|
|
$.fn[ScrollSpy._NAME] = ScrollSpy._jQueryInterface
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @const
|
|
|
|
* @type {Function}
|
|
|
|
*/
|
|
|
|
$.fn[ScrollSpy._NAME]['Constructor'] = ScrollSpy
|
2011-09-11 23:08:43 -04:00
|
|
|
|
2015-01-03 16:58:44 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @const
|
|
|
|
* @type {Function}
|
|
|
|
*/
|
|
|
|
$.fn[ScrollSpy._NAME]['noConflict'] = function () {
|
|
|
|
$.fn[ScrollSpy._NAME] = ScrollSpy._JQUERY_NO_CONFLICT
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Data Api implementation
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
$(window).on('load.bs.scrollspy.data-api', function () {
|
|
|
|
var scrollSpys = /** @type {Array.<Element>} */ ($.makeArray($(ScrollSpy._Selector.DATA_SPY)))
|
|
|
|
|
|
|
|
for (var i = scrollSpys.length; i--;) {
|
|
|
|
var $spy = $(scrollSpys[i])
|
|
|
|
ScrollSpy._jQueryInterface.call($spy, /** @type {Object|null} */ ($spy.data()))
|
|
|
|
}
|
|
|
|
})
|