PageRenderTime 30ms CodeModel.GetById 17ms app.highlight 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/honeynet_web/honeywall/static/twitter-bootstrap/js/bootstrap-scrollspy.js

https://bitbucket.org/cpdean/pig
JavaScript | 125 lines | 76 code | 25 blank | 24 comment | 16 complexity | 7b94a7cb923fcb9d3006abeaffd9593e MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1
  1/* =============================================================
  2 * bootstrap-scrollspy.js v2.0.0
  3 * http://twitter.github.com/bootstrap/javascript.html#scrollspy
  4 * =============================================================
  5 * Copyright 2012 Twitter, Inc.
  6 *
  7 * Licensed under the Apache License, Version 2.0 (the "License");
  8 * you may not use this file except in compliance with the License.
  9 * You may obtain a copy of the License at
 10 *
 11 * http://www.apache.org/licenses/LICENSE-2.0
 12 *
 13 * Unless required by applicable law or agreed to in writing, software
 14 * distributed under the License is distributed on an "AS IS" BASIS,
 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 16 * See the License for the specific language governing permissions and
 17 * limitations under the License.
 18 * ============================================================== */
 19
 20!function ( $ ) {
 21
 22  "use strict"
 23
 24  /* SCROLLSPY CLASS DEFINITION
 25   * ========================== */
 26
 27  function ScrollSpy( element, options) {
 28    var process = $.proxy(this.process, this)
 29      , $element = $(element).is('body') ? $(window) : $(element)
 30      , href
 31    this.options = $.extend({}, $.fn.scrollspy.defaults, options)
 32    this.$scrollElement = $element.on('scroll.scroll.data-api', process)
 33    this.selector = (this.options.target
 34      || ((href = $(element).attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) //strip for ie7
 35      || '') + ' .nav li > a'
 36    this.$body = $('body').on('click.scroll.data-api', this.selector, process)
 37    this.refresh()
 38    this.process()
 39  }
 40
 41  ScrollSpy.prototype = {
 42
 43      constructor: ScrollSpy
 44
 45    , refresh: function () {
 46        this.targets = this.$body
 47          .find(this.selector)
 48          .map(function () {
 49            var href = $(this).attr('href')
 50            return /^#\w/.test(href) && $(href).length ? href : null
 51          })
 52
 53        this.offsets = $.map(this.targets, function (id) {
 54          return $(id).position().top
 55        })
 56      }
 57
 58    , process: function () {
 59        var scrollTop = this.$scrollElement.scrollTop() + this.options.offset
 60          , offsets = this.offsets
 61          , targets = this.targets
 62          , activeTarget = this.activeTarget
 63          , i
 64
 65        for (i = offsets.length; i--;) {
 66          activeTarget != targets[i]
 67            && scrollTop >= offsets[i]
 68            && (!offsets[i + 1] || scrollTop <= offsets[i + 1])
 69            && this.activate( targets[i] )
 70        }
 71      }
 72
 73    , activate: function (target) {
 74        var active
 75
 76        this.activeTarget = target
 77
 78        this.$body
 79          .find(this.selector).parent('.active')
 80          .removeClass('active')
 81
 82        active = this.$body
 83          .find(this.selector + '[href="' + target + '"]')
 84          .parent('li')
 85          .addClass('active')
 86
 87        if ( active.parent('.dropdown-menu') )  {
 88          active.closest('li.dropdown').addClass('active')
 89        }
 90      }
 91
 92  }
 93
 94
 95 /* SCROLLSPY PLUGIN DEFINITION
 96  * =========================== */
 97
 98  $.fn.scrollspy = function ( option ) {
 99    return this.each(function () {
100      var $this = $(this)
101        , data = $this.data('scrollspy')
102        , options = typeof option == 'object' && option
103      if (!data) $this.data('scrollspy', (data = new ScrollSpy(this, options)))
104      if (typeof option == 'string') data[option]()
105    })
106  }
107
108  $.fn.scrollspy.Constructor = ScrollSpy
109
110  $.fn.scrollspy.defaults = {
111    offset: 10
112  }
113
114
115 /* SCROLLSPY DATA-API
116  * ================== */
117
118  $(function () {
119    $('[data-spy="scroll"]').each(function () {
120      var $spy = $(this)
121      $spy.scrollspy($spy.data())
122    })
123  })
124
125}( window.jQuery )