Skip to content

Commit 012db10

Browse files
committed
refactor: clean up the Tab initialization
1 parent 7307c43 commit 012db10

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

js/foundation.tabs.js

+25-22
Original file line numberDiff line numberDiff line change
@@ -101,36 +101,38 @@ class Tabs extends Plugin {
101101
}
102102
}
103103

104-
//current context-bound function to open tabs on page load or history hashchange
104+
// Current context-bound function to open tabs on page load or history hashchange
105105
this._checkDeepLink = () => {
106106
var anchor = window.location.hash;
107107

108-
// if there is no anchor, return to the initial tab
108+
// If there is no anchor, return to the initial panel
109109
if (!anchor.length && this._initialAnchor) {
110110
anchor = this._initialAnchor;
111111
}
112112

113-
//need a hash and a relevant anchor in this tabset
114-
if (anchor.length) {
115-
var anchorNoHash = (anchor.indexOf('#') >= 0 ? anchor.slice(1) : anchor);
116-
var $link = this.$element.find(`[href$="${anchor}"],[data-tabs-target="${anchorNoHash}"]`).first();
117-
if ($link.length) {
118-
this.selectTab($(anchor), true);
119-
120-
//roll up a little to show the titles
121-
if (this.options.deepLinkSmudge) {
122-
var offset = this.$element.offset();
123-
$('html, body').animate({ scrollTop: offset.top }, this.options.deepLinkSmudgeDelay);
124-
}
113+
var anchorNoHash = anchor.indexOf('#') >= 0 ? anchor.slice(1) : anchor;
114+
var $anchor = anchorNoHash && $(`#${anchorNoHash}`);
115+
var $link = anchor && this.$element.find(`[href$="${anchor}"],[data-tabs-target="${anchorNoHash}"]`).first();
116+
117+
// If there is an anchor for the hash, select it
118+
if ($anchor && $anchor.length && $link && $link.length) {
119+
this.selectTab($anchor, true);
120+
}
121+
122+
// Roll up a little to show the titles
123+
if (this.options.deepLinkSmudge) {
124+
var offset = this.$element.offset();
125+
$('html, body').animate({ scrollTop: offset.top }, this.options.deepLinkSmudgeDelay);
126+
}
125127

126-
/**
127-
* Fires when the zplugin has deeplinked at pageload
128-
* @event Tabs#deeplink
129-
*/
130-
this.$element.trigger('deeplink.zf.tabs', [$link, $(anchor)]);
131-
}
132-
}
133-
}
128+
if ($anchor && $link) {
129+
/**
130+
* Fires when the zplugin has deeplinked at pageload
131+
* @event Tabs#deeplink
132+
*/
133+
this.$element.trigger('deeplink.zf.tabs', [$link, $anchor]);
134+
}
135+
}
134136

135137
//use browser to open a tab, if it exists in this tabset
136138
if (this.options.deepLink) {
@@ -360,6 +362,7 @@ class Tabs extends Plugin {
360362

361363
this._handleTabChange($target, historyHandled);
362364
};
365+
363366
/**
364367
* Sets the height of each panel to the height of the tallest panel.
365368
* If enabled in options, gets called on media query change.

0 commit comments

Comments
 (0)