Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 66 additions & 35 deletions jquery.stickytabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,69 @@
*/
(function ( $ ) {
$.fn.stickyTabs = function( options ) {
var context = this

var settings = $.extend({
getHashCallback: function(hash, btn) { return hash },
let context = this;
let settings = $.extend({
getHashCallback: (hash, btn) => hash,
selectorAttribute: "href",
backToTop: false,
initialTab: $('li.active > a', context)
}, options );
}, options );

// Merge keys and values to object
let keysValuesToObject = function (keys, values) {
values = values || keys.map(function (v) { return true; });
let some;
keys.map(function (v) { return [v, this.shift()] }, values)
.map(function (v) { this[v[0]] = v[1]; }, some = {});
return some;
};

// Transpose keys and values on given object
let transposeObject = (object) => {
let keys = Object.keys(object);
let values = Object.values(object);
return keysValuesToObject(values, keys);
};

// Get back from our custom route name to selector
let resolveSelector = (hash) => {
let realSelector;
hash = hash.split('#')[1];

// if there is route object given, get route name, else take hash as route name
realSelector = (settings.routeObject ? transposeObject(settings.routeObject)[hash] : hash);
return realSelector ? `a[${settings.selectorAttribute}='#${realSelector}']` : settings.initialTab;
};

// Show the tab corresponding with the hash in the URL, or the first tab.
var showTabFromHash = function() {
var hash = settings.selectorAttribute == "href" ? window.location.hash : window.location.hash.substring(1);
if (hash != '') {
var selector = hash ? 'a[' + settings.selectorAttribute +'="' + hash + '"]' : settings.initialTab;
$(selector, context).tab('show');
setTimeout(backToTop, 1);
}
}
let showTabFromHash = () => {
let hash = settings.selectorAttribute == "href" ? window.location.hash : window.location.hash.substring(1);
if (hash !== '') {
let selector = resolveSelector(hash);
$(selector, context).tab('show');
setTimeout(backToTop, 1);
}
};

// We use pushState if it's available so the page won't jump, otherwise a shim.
var changeHash = function(hash) {
if (history && history.pushState) {
history.pushState(null, null, window.location.pathname + window.location.search + '#' + hash);
} else {
scrollV = document.body.scrollTop;
scrollH = document.body.scrollLeft;
window.location.hash = hash;
document.body.scrollTop = scrollV;
document.body.scrollLeft = scrollH;
}
}
let changeHash = (hash) => {
if (history && history.pushState) {
history.pushState(null, null, window.location.pathname + window.location.search + '#' + hash);
} else {
scrollV = document.body.scrollTop;
scrollH = document.body.scrollLeft;
window.location.hash = hash;
document.body.scrollTop = scrollV;
document.body.scrollLeft = scrollH;
}
};

var backToTop = function() {
if (settings.backToTop === true) {
window.scrollTo(0, 0);
}
}
let backToTop = () => {
if (settings.backToTop === true) {
window.scrollTo(0, 0);
}
};

// Set the correct tab when the page loads
showTabFromHash();
Expand All @@ -52,14 +78,19 @@

// Change the URL when tabs are clicked
$('a', context).on('click', function(e) {
var hash = this.href.split('#')[1];
if (typeof hash != 'undefined' && hash != '') {
var adjustedhash = settings.getHashCallback(hash, this);
changeHash(adjustedhash);
setTimeout(backToTop, 1);
}
let hash = this.href.split('#')[1];

if (settings.routeObject) {
hash = settings.routeObject[hash];
}

if (typeof hash != 'undefined' && hash != '') {
let adjustedhash = settings.getHashCallback(hash, this);
changeHash(adjustedhash);
setTimeout(backToTop, 1);
}
});

return this;
};
}( jQuery ));
}( jQuery ));