github.com/kayoticsully/syncthing@v0.8.9-0.20140724133906-c45a2fdc03f8/assets/bootstrap-3.1.1/js/affix.js (about)

     1  /* ========================================================================
     2   * Bootstrap: affix.js v3.1.1
     3   * http://getbootstrap.com/javascript/#affix
     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    // AFFIX CLASS DEFINITION
    14    // ======================
    15  
    16    var Affix = function (element, options) {
    17      this.options = $.extend({}, Affix.DEFAULTS, options)
    18      this.$window = $(window)
    19        .on('scroll.bs.affix.data-api', $.proxy(this.checkPosition, this))
    20        .on('click.bs.affix.data-api',  $.proxy(this.checkPositionWithEventLoop, this))
    21  
    22      this.$element     = $(element)
    23      this.affixed      =
    24      this.unpin        =
    25      this.pinnedOffset = null
    26  
    27      this.checkPosition()
    28    }
    29  
    30    Affix.RESET = 'affix affix-top affix-bottom'
    31  
    32    Affix.DEFAULTS = {
    33      offset: 0
    34    }
    35  
    36    Affix.prototype.getPinnedOffset = function () {
    37      if (this.pinnedOffset) return this.pinnedOffset
    38      this.$element.removeClass(Affix.RESET).addClass('affix')
    39      var scrollTop = this.$window.scrollTop()
    40      var position  = this.$element.offset()
    41      return (this.pinnedOffset = position.top - scrollTop)
    42    }
    43  
    44    Affix.prototype.checkPositionWithEventLoop = function () {
    45      setTimeout($.proxy(this.checkPosition, this), 1)
    46    }
    47  
    48    Affix.prototype.checkPosition = function () {
    49      if (!this.$element.is(':visible')) return
    50  
    51      var scrollHeight = $(document).height()
    52      var scrollTop    = this.$window.scrollTop()
    53      var position     = this.$element.offset()
    54      var offset       = this.options.offset
    55      var offsetTop    = offset.top
    56      var offsetBottom = offset.bottom
    57  
    58      if (this.affixed == 'top') position.top += scrollTop
    59  
    60      if (typeof offset != 'object')         offsetBottom = offsetTop = offset
    61      if (typeof offsetTop == 'function')    offsetTop    = offset.top(this.$element)
    62      if (typeof offsetBottom == 'function') offsetBottom = offset.bottom(this.$element)
    63  
    64      var affix = this.unpin   != null && (scrollTop + this.unpin <= position.top) ? false :
    65                  offsetBottom != null && (position.top + this.$element.height() >= scrollHeight - offsetBottom) ? 'bottom' :
    66                  offsetTop    != null && (scrollTop <= offsetTop) ? 'top' : false
    67  
    68      if (this.affixed === affix) return
    69      if (this.unpin) this.$element.css('top', '')
    70  
    71      var affixType = 'affix' + (affix ? '-' + affix : '')
    72      var e         = $.Event(affixType + '.bs.affix')
    73  
    74      this.$element.trigger(e)
    75  
    76      if (e.isDefaultPrevented()) return
    77  
    78      this.affixed = affix
    79      this.unpin = affix == 'bottom' ? this.getPinnedOffset() : null
    80  
    81      this.$element
    82        .removeClass(Affix.RESET)
    83        .addClass(affixType)
    84        .trigger($.Event(affixType.replace('affix', 'affixed')))
    85  
    86      if (affix == 'bottom') {
    87        this.$element.offset({ top: scrollHeight - offsetBottom - this.$element.height() })
    88      }
    89    }
    90  
    91  
    92    // AFFIX PLUGIN DEFINITION
    93    // =======================
    94  
    95    var old = $.fn.affix
    96  
    97    $.fn.affix = function (option) {
    98      return this.each(function () {
    99        var $this   = $(this)
   100        var data    = $this.data('bs.affix')
   101        var options = typeof option == 'object' && option
   102  
   103        if (!data) $this.data('bs.affix', (data = new Affix(this, options)))
   104        if (typeof option == 'string') data[option]()
   105      })
   106    }
   107  
   108    $.fn.affix.Constructor = Affix
   109  
   110  
   111    // AFFIX NO CONFLICT
   112    // =================
   113  
   114    $.fn.affix.noConflict = function () {
   115      $.fn.affix = old
   116      return this
   117    }
   118  
   119  
   120    // AFFIX DATA-API
   121    // ==============
   122  
   123    $(window).on('load', function () {
   124      $('[data-spy="affix"]').each(function () {
   125        var $spy = $(this)
   126        var data = $spy.data()
   127  
   128        data.offset = data.offset || {}
   129  
   130        if (data.offsetBottom) data.offset.bottom = data.offsetBottom
   131        if (data.offsetTop)    data.offset.top    = data.offsetTop
   132  
   133        $spy.affix(data)
   134      })
   135    })
   136  
   137  }(jQuery);