forked from foundation/foundation-sites
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoundation.magellan.js
291 lines (260 loc) · 8.14 KB
/
foundation.magellan.js
1
2
3
4
5
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
'use strict';
import $ from 'jquery';
import { Plugin } from './foundation.core.plugin';
import { onLoad, GetYoDigits } from './foundation.core.utils';
import { SmoothScroll } from './foundation.smoothScroll';
/**
* Magellan module.
* @module foundation.magellan
* @requires foundation.smoothScroll
*/
class Magellan extends Plugin {
/**
* Creates a new instance of Magellan.
* @class
* @name Magellan
* @fires Magellan#init
* @param {Object} element - jQuery object to add the trigger to.
* @param {Object} options - Overrides to the default plugin settings.
*/
_setup(element, options) {
this.$element = element;
this.options = $.extend({}, Magellan.defaults, this.$element.data(), options);
this.className = 'Magellan'; // ie9 back compat
this._init();
this.calcPoints();
}
/**
* Initializes the Magellan plugin and calls functions to get equalizer functioning on load.
* @private
*/
_init() {
var id = this.$element[0].id || GetYoDigits(6, 'magellan');
var _this = this;
this.$targets = $('[data-magellan-target]');
this.$links = this.$element.find('a');
this.$element.attr({
'data-resize': id,
'data-scroll': id,
'id': id
});
this.$active = $();
this.scrollPos = parseInt(window.pageYOffset, 10);
this._events();
}
/**
* Calculates an array of pixel values that are the demarcation lines between locations on the page.
* Can be invoked if new elements are added or the size of a location changes.
* @function
*/
calcPoints() {
var _this = this,
body = document.body,
html = document.documentElement;
this.points = [];
this.winHeight = Math.round(Math.max(window.innerHeight, html.clientHeight));
this.docHeight = Math.round(Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight));
this.$targets.each(function(){
var $tar = $(this),
pt = Math.round($tar.offset().top - _this.options.threshold);
$tar.targetPoint = pt;
_this.points.push(pt);
});
}
/**
* Initializes events for Magellan.
* @private
*/
_events() {
var _this = this,
$body = $('html, body'),
opts = {
duration: _this.options.animationDuration,
easing: _this.options.animationEasing
};
$(window).one('load', function(){
if(_this.options.deepLinking){
if(location.hash){
_this.scrollToLoc(location.hash);
}
}
_this.calcPoints();
_this._updateActive();
});
_this.onLoadListener = onLoad($(window), function () {
_this.$element
.on({
'resizeme.zf.trigger': _this.reflow.bind(_this),
'scrollme.zf.trigger': _this._updateActive.bind(_this)
})
.on('click.zf.magellan', 'a[href^="#"]', function (e) {
e.preventDefault();
var arrival = this.getAttribute('href');
_this.scrollToLoc(arrival);
});
});
this._deepLinkScroll = function(e) {
if(_this.options.deepLinking) {
_this.scrollToLoc(window.location.hash);
}
};
$(window).on('hashchange', this._deepLinkScroll);
}
/**
* Function to scroll to a given location on the page.
* @param {String} loc - a properly formatted jQuery id selector. Example: '#foo'
* @function
*/
scrollToLoc(loc) {
this._inTransition = true;
var _this = this;
var options = {
animationEasing: this.options.animationEasing,
animationDuration: this.options.animationDuration,
threshold: this.options.threshold,
offset: this.options.offset
};
SmoothScroll.scrollToLoc(loc, options, function() {
_this._inTransition = false;
})
}
/**
* Calls necessary functions to update Magellan upon DOM change
* @function
*/
reflow() {
this.calcPoints();
this._updateActive();
}
/**
* Updates the visibility of an active location link, and updates the url hash for the page, if deepLinking enabled.
* @private
* @function
* @fires Magellan#update
*/
_updateActive(/*evt, elem, scrollPos*/) {
if(this._inTransition) return;
const newScrollPos = parseInt(window.pageYOffset, 10);
const isScrollingUp = this.scrollPos > newScrollPos;
this.scrollPos = newScrollPos;
let activeIdx;
// Before the first point: no link
if(newScrollPos < this.points[0]){ /* do nothing */ }
// At the bottom of the page: last link
else if(newScrollPos + this.winHeight === this.docHeight){ activeIdx = this.points.length - 1; }
// Otherwhise, use the last visible link
else{
const visibleLinks = this.points.filter((p, i) => {
return (p - this.options.offset - (isScrollingUp ? this.options.threshold : 0)) <= newScrollPos;
});
activeIdx = visibleLinks.length ? visibleLinks.length - 1 : 0;
}
// Get the new active link
const $oldActive = this.$active;
let activeHash = '';
if(activeIdx !== undefined){
this.$active = this.$links.filter('[href="#' + this.$targets.eq(activeIdx).data('magellan-target') + '"]');
if (this.$active.length) activeHash = this.$active[0].getAttribute('href');
}else{
this.$active = $();
}
const isNewActive = !(!this.$active.length && !$oldActive.length) && !this.$active.is($oldActive);
const isNewHash = activeHash !== window.location.hash;
// Update the active link element
if(isNewActive) {
$oldActive.removeClass(this.options.activeClass);
this.$active.addClass(this.options.activeClass);
}
// Update the hash (it may have changed with the same active link)
if(this.options.deepLinking && isNewHash){
if(window.history.pushState){
// Set or remove the hash (see: https://stackoverflow.com/a/5298684/4317384
const url = activeHash ? activeHash : window.location.pathname + window.location.search;
if(this.options.updateHistory){
window.history.pushState({}, '', url);
}else{
window.history.replaceState({}, '', url);
}
}else{
window.location.hash = activeHash;
}
}
if (isNewActive) {
/**
* Fires when magellan is finished updating to the new active element.
* @event Magellan#update
*/
this.$element.trigger('update.zf.magellan', [this.$active]);
}
}
/**
* Destroys an instance of Magellan and resets the url of the window.
* @function
*/
_destroy() {
this.$element.off('.zf.trigger .zf.magellan')
.find(`.${this.options.activeClass}`).removeClass(this.options.activeClass);
if(this.options.deepLinking){
var hash = this.$active[0].getAttribute('href');
window.location.hash.replace(hash, '');
}
$(window).off('hashchange', this._deepLinkScroll)
if (this.onLoadListener) $(window).off(this.onLoadListener);
}
}
/**
* Default settings for plugin
*/
Magellan.defaults = {
/**
* Amount of time, in ms, the animated scrolling should take between locations.
* @option
* @type {number}
* @default 500
*/
animationDuration: 500,
/**
* Animation style to use when scrolling between locations. Can be `'swing'` or `'linear'`.
* @option
* @type {string}
* @default 'linear'
* @see {@link https://api.jquery.com/animate|Jquery animate}
*/
animationEasing: 'linear',
/**
* Number of pixels to use as a marker for location changes.
* @option
* @type {number}
* @default 50
*/
threshold: 50,
/**
* Class applied to the active locations link on the magellan container.
* @option
* @type {string}
* @default 'is-active'
*/
activeClass: 'is-active',
/**
* Allows the script to manipulate the url of the current page, and if supported, alter the history.
* @option
* @type {boolean}
* @default false
*/
deepLinking: false,
/**
* Update the browser history with the active link, if deep linking is enabled.
* @option
* @type {boolean}
* @default false
*/
updateHistory: false,
/**
* Number of pixels to offset the scroll of the page on item click if using a sticky nav bar.
* @option
* @type {number}
* @default 0
*/
offset: 0
}
export {Magellan};