scrollspy.js 11.6 KB
Newer Older
XhmikosR's avatar
Dist    
XhmikosR committed
1
2
3
4
5
/*!
  * Bootstrap scrollspy.js v4.1.3 (https://getbootstrap.com/)
  * Copyright 2011-2018 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
  * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  */
Mark Otto's avatar
dist    
Mark Otto committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
(function (global, factory) {
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('jquery'), require('./util.js')) :
  typeof define === 'function' && define.amd ? define(['jquery', './util.js'], factory) :
  (global.ScrollSpy = factory(global.jQuery,global.Util));
}(this, (function ($,Util) { 'use strict';

  $ = $ && $.hasOwnProperty('default') ? $['default'] : $;
  Util = Util && Util.hasOwnProperty('default') ? Util['default'] : Util;

  function _defineProperties(target, props) {
    for (var i = 0; i < props.length; i++) {
      var descriptor = props[i];
      descriptor.enumerable = descriptor.enumerable || false;
      descriptor.configurable = true;
      if ("value" in descriptor) descriptor.writable = true;
      Object.defineProperty(target, descriptor.key, descriptor);
    }
  }

  function _createClass(Constructor, protoProps, staticProps) {
    if (protoProps) _defineProperties(Constructor.prototype, protoProps);
    if (staticProps) _defineProperties(Constructor, staticProps);
    return Constructor;
  }

  function _defineProperty(obj, key, value) {
    if (key in obj) {
      Object.defineProperty(obj, key, {
        value: value,
        enumerable: true,
        configurable: true,
        writable: true
      });
    } else {
      obj[key] = value;
    }

    return obj;
  }

  function _objectSpread(target) {
    for (var i = 1; i < arguments.length; i++) {
      var source = arguments[i] != null ? arguments[i] : {};
      var ownKeys = Object.keys(source);
Mark Otto's avatar
dist    
Mark Otto committed
50

Mark Otto's avatar
dist    
Mark Otto committed
51
52
53
54
55
      if (typeof Object.getOwnPropertySymbols === 'function') {
        ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) {
          return Object.getOwnPropertyDescriptor(source, sym).enumerable;
        }));
      }
Mark Otto's avatar
dist    
Mark Otto committed
56

Mark Otto's avatar
dist    
Mark Otto committed
57
58
59
60
      ownKeys.forEach(function (key) {
        _defineProperty(target, key, source[key]);
      });
    }
Mark Otto's avatar
dist    
Mark Otto committed
61

Mark Otto's avatar
dist    
Mark Otto committed
62
63
    return target;
  }
fat's avatar
fat committed
64
65

  /**
Mark Otto's avatar
dist    
Mark Otto committed
66
   * --------------------------------------------------------------------------
Mark Otto's avatar
Mark Otto committed
67
   * Bootstrap (v4.1.3): scrollspy.js
Mark Otto's avatar
dist    
Mark Otto committed
68
69
   * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
   * --------------------------------------------------------------------------
fat's avatar
fat committed
70
   */
Mark Otto's avatar
dist    
Mark Otto committed
71

XhmikosR's avatar
Dist    
XhmikosR committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  /**
   * ------------------------------------------------------------------------
   * Constants
   * ------------------------------------------------------------------------
   */

  var NAME = 'scrollspy';
  var VERSION = '4.1.3';
  var DATA_KEY = 'bs.scrollspy';
  var EVENT_KEY = "." + DATA_KEY;
  var DATA_API_KEY = '.data-api';
  var JQUERY_NO_CONFLICT = $.fn[NAME];
  var Default = {
    offset: 10,
    method: 'auto',
    target: ''
  };
  var DefaultType = {
    offset: 'number',
    method: 'string',
    target: '(string|element)'
  };
  var Event = {
    ACTIVATE: "activate" + EVENT_KEY,
    SCROLL: "scroll" + EVENT_KEY,
    LOAD_DATA_API: "load" + EVENT_KEY + DATA_API_KEY
  };
  var ClassName = {
    DROPDOWN_ITEM: 'dropdown-item',
    DROPDOWN_MENU: 'dropdown-menu',
    ACTIVE: 'active'
  };
  var Selector = {
    DATA_SPY: '[data-spy="scroll"]',
    ACTIVE: '.active',
    NAV_LIST_GROUP: '.nav, .list-group',
    NAV_LINKS: '.nav-link',
    NAV_ITEMS: '.nav-item',
    LIST_ITEMS: '.list-group-item',
    DROPDOWN: '.dropdown',
    DROPDOWN_ITEMS: '.dropdown-item',
    DROPDOWN_TOGGLE: '.dropdown-toggle'
  };
  var OffsetMethod = {
    OFFSET: 'offset',
    POSITION: 'position'
Mark Otto's avatar
dist    
Mark Otto committed
118
119
    /**
     * ------------------------------------------------------------------------
XhmikosR's avatar
Dist    
XhmikosR committed
120
     * Class Definition
Mark Otto's avatar
dist    
Mark Otto committed
121
122
     * ------------------------------------------------------------------------
     */
fat's avatar
fat committed
123

XhmikosR's avatar
Dist    
XhmikosR committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
  };

  var ScrollSpy =
  /*#__PURE__*/
  function () {
    function ScrollSpy(element, config) {
      var _this = this;

      this._element = element;
      this._scrollElement = element.tagName === 'BODY' ? window : element;
      this._config = this._getConfig(config);
      this._selector = this._config.target + " " + Selector.NAV_LINKS + "," + (this._config.target + " " + Selector.LIST_ITEMS + ",") + (this._config.target + " " + Selector.DROPDOWN_ITEMS);
      this._offsets = [];
      this._targets = [];
      this._activeTarget = null;
      this._scrollHeight = 0;
      $(this._scrollElement).on(Event.SCROLL, function (event) {
        return _this._process(event);
      });
      this.refresh();
Jacob Thornton's avatar
Jacob Thornton committed
144

XhmikosR's avatar
Dist    
XhmikosR committed
145
146
      this._process();
    } // Getters
fat's avatar
fat committed
147
148


XhmikosR's avatar
Dist    
XhmikosR committed
149
    var _proto = ScrollSpy.prototype;
fat's avatar
fat committed
150

XhmikosR's avatar
Dist    
XhmikosR committed
151
152
153
    // Public
    _proto.refresh = function refresh() {
      var _this2 = this;
Mark Otto's avatar
dist    
Mark Otto committed
154

XhmikosR's avatar
Dist    
XhmikosR committed
155
156
157
158
159
160
161
162
163
164
      var autoMethod = this._scrollElement === this._scrollElement.window ? OffsetMethod.OFFSET : OffsetMethod.POSITION;
      var offsetMethod = this._config.method === 'auto' ? autoMethod : this._config.method;
      var offsetBase = offsetMethod === OffsetMethod.POSITION ? this._getScrollTop() : 0;
      this._offsets = [];
      this._targets = [];
      this._scrollHeight = this._getScrollHeight();
      var targets = [].slice.call(document.querySelectorAll(this._selector));
      targets.map(function (element) {
        var target;
        var targetSelector = Util.getSelectorFromElement(element);
Mark Otto's avatar
dist    
Mark Otto committed
165

XhmikosR's avatar
Dist    
XhmikosR committed
166
167
168
        if (targetSelector) {
          target = document.querySelector(targetSelector);
        }
169

XhmikosR's avatar
Dist    
XhmikosR committed
170
171
        if (target) {
          var targetBCR = target.getBoundingClientRect();
fat's avatar
fat committed
172

XhmikosR's avatar
Dist    
XhmikosR committed
173
174
175
          if (targetBCR.width || targetBCR.height) {
            // TODO (fat): remove sketch reliance on jQuery position/offset
            return [$(target)[offsetMethod]().top + offsetBase, targetSelector];
Mark Otto's avatar
dist    
Mark Otto committed
176
          }
XhmikosR's avatar
Dist    
XhmikosR committed
177
        }
fat's avatar
fat committed
178

XhmikosR's avatar
Dist    
XhmikosR committed
179
180
181
182
183
184
185
        return null;
      }).filter(function (item) {
        return item;
      }).sort(function (a, b) {
        return a[0] - b[0];
      }).forEach(function (item) {
        _this2._offsets.push(item[0]);
fat's avatar
fat committed
186

XhmikosR's avatar
Dist    
XhmikosR committed
187
188
189
        _this2._targets.push(item[1]);
      });
    };
Mark Otto's avatar
grunt    
Mark Otto committed
190

XhmikosR's avatar
Dist    
XhmikosR committed
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
    _proto.dispose = function dispose() {
      $.removeData(this._element, DATA_KEY);
      $(this._scrollElement).off(EVENT_KEY);
      this._element = null;
      this._scrollElement = null;
      this._config = null;
      this._selector = null;
      this._offsets = null;
      this._targets = null;
      this._activeTarget = null;
      this._scrollHeight = null;
    }; // Private


    _proto._getConfig = function _getConfig(config) {
      config = _objectSpread({}, Default, typeof config === 'object' && config ? config : {});

      if (typeof config.target !== 'string') {
        var id = $(config.target).attr('id');

        if (!id) {
          id = Util.getUID(NAME);
          $(config.target).attr('id', id);
Mark Otto's avatar
dist    
Mark Otto committed
214
        }
matus's avatar
matus committed
215

XhmikosR's avatar
Dist    
XhmikosR committed
216
217
        config.target = "#" + id;
      }
Mark Otto's avatar
grunt    
Mark Otto committed
218

XhmikosR's avatar
Dist    
XhmikosR committed
219
220
221
      Util.typeCheckConfig(NAME, config, DefaultType);
      return config;
    };
fat's avatar
fat committed
222

XhmikosR's avatar
Dist    
XhmikosR committed
223
224
225
    _proto._getScrollTop = function _getScrollTop() {
      return this._scrollElement === window ? this._scrollElement.pageYOffset : this._scrollElement.scrollTop;
    };
fat's avatar
fat committed
226

XhmikosR's avatar
Dist    
XhmikosR committed
227
228
229
    _proto._getScrollHeight = function _getScrollHeight() {
      return this._scrollElement.scrollHeight || Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
    };
fat's avatar
fat committed
230

XhmikosR's avatar
Dist    
XhmikosR committed
231
232
233
    _proto._getOffsetHeight = function _getOffsetHeight() {
      return this._scrollElement === window ? window.innerHeight : this._scrollElement.getBoundingClientRect().height;
    };
fat's avatar
fat committed
234

XhmikosR's avatar
Dist    
XhmikosR committed
235
236
    _proto._process = function _process() {
      var scrollTop = this._getScrollTop() + this._config.offset;
fat's avatar
fat committed
237

XhmikosR's avatar
Dist    
XhmikosR committed
238
      var scrollHeight = this._getScrollHeight();
Mark Otto's avatar
dist    
Mark Otto committed
239

XhmikosR's avatar
Dist    
XhmikosR committed
240
      var maxScroll = this._config.offset + scrollHeight - this._getOffsetHeight();
Mark Otto's avatar
dist    
Mark Otto committed
241

XhmikosR's avatar
Dist    
XhmikosR committed
242
243
244
      if (this._scrollHeight !== scrollHeight) {
        this.refresh();
      }
Mark Otto's avatar
dist    
Mark Otto committed
245

XhmikosR's avatar
Dist    
XhmikosR committed
246
247
      if (scrollTop >= maxScroll) {
        var target = this._targets[this._targets.length - 1];
fat's avatar
fat committed
248

XhmikosR's avatar
Dist    
XhmikosR committed
249
250
        if (this._activeTarget !== target) {
          this._activate(target);
Mark Otto's avatar
dist    
Mark Otto committed
251
        }
fat's avatar
fat committed
252

XhmikosR's avatar
Dist    
XhmikosR committed
253
254
        return;
      }
Mark Otto's avatar
dist    
Mark Otto committed
255

XhmikosR's avatar
Dist    
XhmikosR committed
256
257
      if (this._activeTarget && scrollTop < this._offsets[0] && this._offsets[0] > 0) {
        this._activeTarget = null;
Mark Otto's avatar
dist    
Mark Otto committed
258

XhmikosR's avatar
Dist    
XhmikosR committed
259
        this._clear();
Mark Otto's avatar
dist    
Mark Otto committed
260

XhmikosR's avatar
Dist    
XhmikosR committed
261
262
        return;
      }
fat's avatar
fat committed
263

XhmikosR's avatar
Dist    
XhmikosR committed
264
      var offsetLength = this._offsets.length;
Mark Otto's avatar
dist    
Mark Otto committed
265

XhmikosR's avatar
Dist    
XhmikosR committed
266
267
      for (var i = offsetLength; i--;) {
        var isActiveTarget = this._activeTarget !== this._targets[i] && scrollTop >= this._offsets[i] && (typeof this._offsets[i + 1] === 'undefined' || scrollTop < this._offsets[i + 1]);
Mark Otto's avatar
dist    
Mark Otto committed
268

XhmikosR's avatar
Dist    
XhmikosR committed
269
270
271
272
273
        if (isActiveTarget) {
          this._activate(this._targets[i]);
        }
      }
    };
Mark Otto's avatar
dist    
Mark Otto committed
274

XhmikosR's avatar
Dist    
XhmikosR committed
275
276
    _proto._activate = function _activate(target) {
      this._activeTarget = target;
Mark Otto's avatar
dist    
Mark Otto committed
277

XhmikosR's avatar
Dist    
XhmikosR committed
278
      this._clear();
Mark Otto's avatar
dist    
Mark Otto committed
279

XhmikosR's avatar
Dist    
XhmikosR committed
280
      var queries = this._selector.split(','); // eslint-disable-next-line arrow-body-style
Mark Otto's avatar
dist    
Mark Otto committed
281
282


XhmikosR's avatar
Dist    
XhmikosR committed
283
284
285
286
      queries = queries.map(function (selector) {
        return selector + "[data-target=\"" + target + "\"]," + (selector + "[href=\"" + target + "\"]");
      });
      var $link = $([].slice.call(document.querySelectorAll(queries.join(','))));
Mark Otto's avatar
dist    
Mark Otto committed
287

XhmikosR's avatar
Dist    
XhmikosR committed
288
289
290
291
292
293
294
      if ($link.hasClass(ClassName.DROPDOWN_ITEM)) {
        $link.closest(Selector.DROPDOWN).find(Selector.DROPDOWN_TOGGLE).addClass(ClassName.ACTIVE);
        $link.addClass(ClassName.ACTIVE);
      } else {
        // Set triggered link as active
        $link.addClass(ClassName.ACTIVE); // Set triggered links parents as active
        // With both <ul> and <nav> markup a parent is the previous sibling of any nav ancestor
Mark Otto's avatar
dist    
Mark Otto committed
295

XhmikosR's avatar
Dist    
XhmikosR committed
296
        $link.parents(Selector.NAV_LIST_GROUP).prev(Selector.NAV_LINKS + ", " + Selector.LIST_ITEMS).addClass(ClassName.ACTIVE); // Handle special case when .nav-link is inside .nav-item
fat's avatar
fat committed
297

XhmikosR's avatar
Dist    
XhmikosR committed
298
299
        $link.parents(Selector.NAV_LIST_GROUP).prev(Selector.NAV_ITEMS).children(Selector.NAV_LINKS).addClass(ClassName.ACTIVE);
      }
Mark Otto's avatar
grunt    
Mark Otto committed
300

XhmikosR's avatar
Dist    
XhmikosR committed
301
302
303
304
      $(this._scrollElement).trigger(Event.ACTIVATE, {
        relatedTarget: target
      });
    };
Mark Otto's avatar
grunt    
Mark Otto committed
305

XhmikosR's avatar
Dist    
XhmikosR committed
306
307
308
309
    _proto._clear = function _clear() {
      var nodes = [].slice.call(document.querySelectorAll(this._selector));
      $(nodes).filter(Selector.ACTIVE).removeClass(ClassName.ACTIVE);
    }; // Static
Mark Otto's avatar
grunt    
Mark Otto committed
310

Mark Otto's avatar
dist    
Mark Otto committed
311

XhmikosR's avatar
Dist    
XhmikosR committed
312
313
314
    ScrollSpy._jQueryInterface = function _jQueryInterface(config) {
      return this.each(function () {
        var data = $(this).data(DATA_KEY);
Mark Otto's avatar
dist    
Mark Otto committed
315

XhmikosR's avatar
Dist    
XhmikosR committed
316
        var _config = typeof config === 'object' && config;
Mark Otto's avatar
dist    
Mark Otto committed
317

XhmikosR's avatar
Dist    
XhmikosR committed
318
319
320
321
        if (!data) {
          data = new ScrollSpy(this, _config);
          $(this).data(DATA_KEY, data);
        }
Mark Otto's avatar
dist    
Mark Otto committed
322

XhmikosR's avatar
Dist    
XhmikosR committed
323
324
325
        if (typeof config === 'string') {
          if (typeof data[config] === 'undefined') {
            throw new TypeError("No method named \"" + config + "\"");
Mark Otto's avatar
dist    
Mark Otto committed
326
          }
Mark Otto's avatar
dist    
Mark Otto committed
327

XhmikosR's avatar
Dist    
XhmikosR committed
328
          data[config]();
Mark Otto's avatar
dist    
Mark Otto committed
329
        }
XhmikosR's avatar
Dist    
XhmikosR committed
330
331
      });
    };
Mark Otto's avatar
dist    
Mark Otto committed
332

XhmikosR's avatar
Dist    
XhmikosR committed
333
334
335
336
337
338
339
340
341
342
343
    _createClass(ScrollSpy, null, [{
      key: "VERSION",
      get: function get() {
        return VERSION;
      }
    }, {
      key: "Default",
      get: function get() {
        return Default;
      }
    }]);
fat's avatar
fat committed
344

XhmikosR's avatar
Dist    
XhmikosR committed
345
346
347
348
349
350
351
    return ScrollSpy;
  }();
  /**
   * ------------------------------------------------------------------------
   * Data Api implementation
   * ------------------------------------------------------------------------
   */
fat's avatar
fat committed
352

Mark Otto's avatar
dist    
Mark Otto committed
353

XhmikosR's avatar
Dist    
XhmikosR committed
354
355
356
  $(window).on(Event.LOAD_DATA_API, function () {
    var scrollSpys = [].slice.call(document.querySelectorAll(Selector.DATA_SPY));
    var scrollSpysLength = scrollSpys.length;
fat's avatar
fat committed
357

XhmikosR's avatar
Dist    
XhmikosR committed
358
359
    for (var i = scrollSpysLength; i--;) {
      var $spy = $(scrollSpys[i]);
Mark Otto's avatar
dist    
Mark Otto committed
360

XhmikosR's avatar
Dist    
XhmikosR committed
361
362
363
364
365
366
367
368
      ScrollSpy._jQueryInterface.call($spy, $spy.data());
    }
  });
  /**
   * ------------------------------------------------------------------------
   * jQuery
   * ------------------------------------------------------------------------
   */
fat's avatar
fat committed
369

XhmikosR's avatar
Dist    
XhmikosR committed
370
371
  $.fn[NAME] = ScrollSpy._jQueryInterface;
  $.fn[NAME].Constructor = ScrollSpy;
Mark Otto's avatar
dist    
Mark Otto committed
372

XhmikosR's avatar
Dist    
XhmikosR committed
373
374
375
376
  $.fn[NAME].noConflict = function () {
    $.fn[NAME] = JQUERY_NO_CONFLICT;
    return ScrollSpy._jQueryInterface;
  };
fat's avatar
fat committed
377
378

  return ScrollSpy;
Mark Otto's avatar
dist    
Mark Otto committed
379
380
381

})));
//# sourceMappingURL=scrollspy.js.map