vendor/assets/js/foundation.tabs.js.es6 in foundation-rails-6.3.0.0 vs vendor/assets/js/foundation.tabs.js.es6 in foundation-rails-6.3.1.0

- old
+ new

@@ -74,48 +74,50 @@ $('html, body').animate({ scrollTop: $elem.offset().top }, _this.options.deepLinkSmudgeDelay, () => { $link.focus(); }); }); } - - //use browser to open a tab, if it exists in this tabset - if (_this.options.deepLink) { - var anchor = window.location.hash; - //need a hash and a relevant anchor in this tabset - if(anchor.length) { - var $link = $elem.find('[href="'+anchor+'"]'); - if ($link.length) { - _this.selectTab($(anchor)); - - //roll up a little to show the titles - if (_this.options.deepLinkSmudge) { - $(window).load(function() { - var offset = $elem.offset(); - $('html, body').animate({ scrollTop: offset.top }, _this.options.deepLinkSmudgeDelay); - }); - } - - /** - * Fires when the zplugin has deeplinked at pageload - * @event Tabs#deeplink - */ - $elem.trigger('deeplink.zf.tabs', [$link, $(anchor)]); - } - } - } }); - if(this.options.matchHeight) { var $images = this.$tabContent.find('img'); if ($images.length) { Foundation.onImagesLoaded($images, this._setHeight.bind(this)); } else { this._setHeight(); } } + //current context-bound function to open tabs on page load or history popstate + this._checkDeepLink = () => { + var anchor = window.location.hash; + //need a hash and a relevant anchor in this tabset + if(anchor.length) { + var $link = this.$element.find('[href$="'+anchor+'"]'); + if ($link.length) { + this.selectTab($(anchor), true); + + //roll up a little to show the titles + if (this.options.deepLinkSmudge) { + var offset = this.$element.offset(); + $('html, body').animate({ scrollTop: offset.top }, this.options.deepLinkSmudgeDelay); + } + + /** + * Fires when the zplugin has deeplinked at pageload + * @event Tabs#deeplink + */ + this.$element.trigger('deeplink.zf.tabs', [$link, $(anchor)]); + } + } + } + + //use browser to open a tab, if it exists in this tabset + if (this.options.deepLink) { + this._checkDeepLink(); + } + this._events(); } /** * Adds event handlers for items within the tabs. @@ -129,10 +131,14 @@ if (this.options.matchHeight) { this._setHeightMqHandler = this._setHeight.bind(this); $(window).on('changed.zf.mediaquery', this._setHeightMqHandler); } + + if(this.options.deepLink) { + $(window).on('popstate', this._checkDeepLink); + } } /** * Adds click handlers for items within the tabs. * @private @@ -201,14 +207,15 @@ } /** * Opens the tab `$targetContent` defined by `$target`. Collapses active tab. * @param {jQuery} $target - Tab to open. + * @param {boolean} historyHandled - browser has already handled a history update * @fires Tabs#change * @function */ - _handleTabChange($target) { + _handleTabChange($target, historyHandled) { /** * Check for active class on target. Collapse if exists. */ if ($target.hasClass(`${this.options.linkActiveClass}`)) { @@ -235,11 +242,11 @@ //open new tab this._openTab($target); //either replace or update browser history - if (this.options.deepLink) { + if (this.options.deepLink && !historyHandled) { var anchor = $target.find('a').attr('href'); if (this.options.updateHistory) { history.pushState({}, '', anchor); } else { @@ -293,13 +300,14 @@ } /** * Public method for selecting a content pane to display. * @param {jQuery | String} elem - jQuery object or string of the id of the pane to display. + * @param {boolean} historyHandled - browser has already handled a history update * @function */ - selectTab(elem) { + selectTab(elem, historyHandled) { var idStr; if (typeof elem === 'object') { idStr = elem[0].id; } else { @@ -308,29 +316,33 @@ if (idStr.indexOf('#') < 0) { idStr = `#${idStr}`; } - var $target = this.$tabTitles.find(`[href="${idStr}"]`).parent(`.${this.options.linkClass}`); + var $target = this.$tabTitles.find(`[href$="${idStr}"]`).parent(`.${this.options.linkClass}`); - this._handleTabChange($target); + this._handleTabChange($target, historyHandled); }; /** * Sets the height of each panel to the height of the tallest panel. * If enabled in options, gets called on media query change. * If loading content via external source, can be called directly or with _reflow. + * If enabled with `data-match-height="true"`, tabs sets to equal height * @function * @private */ _setHeight() { - var max = 0; + var max = 0, + _this = this; // Lock down the `this` value for the root tabs object + this.$tabContent .find(`.${this.options.panelClass}`) .css('height', '') .each(function() { + var panel = $(this), - isActive = panel.hasClass(`${this.options.panelActiveClass}`); + isActive = panel.hasClass(`${_this.options.panelActiveClass}`); // get the options from the parent instead of trying to get them from the child if (!isActive) { panel.css({'visibility': 'hidden', 'display': 'block'}); } @@ -363,96 +375,112 @@ if (this._setHeightMqHandler != null) { $(window).off('changed.zf.mediaquery', this._setHeightMqHandler); } } + if (this.options.deepLink) { + $(window).off('popstate', this._checkDeepLink); + } + Foundation.unregisterPlugin(this); } } Tabs.defaults = { /** * Allows the window to scroll to content of pane specified by hash anchor * @option - * @example false + * @type {boolean} + * @default false */ deepLink: false, /** * Adjust the deep link scroll to make sure the top of the tab panel is visible * @option - * @example false + * @type {boolean} + * @default false */ deepLinkSmudge: false, /** * Animation time (ms) for the deep link adjustment * @option - * @example 300 + * @type {number} + * @default 300 */ deepLinkSmudgeDelay: 300, /** * Update the browser history with the open tab * @option - * @example false + * @type {boolean} + * @default false */ updateHistory: false, /** * Allows the window to scroll to content of active pane on load if set to true. * Not recommended if more than one tab panel per page. * @option - * @example false + * @type {boolean} + * @default false */ autoFocus: false, /** * Allows keyboard input to 'wrap' around the tab links. * @option - * @example true + * @type {boolean} + * @default true */ wrapOnKeys: true, /** * Allows the tab content panes to match heights if set to true. * @option - * @example false + * @type {boolean} + * @default false */ matchHeight: false, /** * Allows active tabs to collapse when clicked. * @option - * @example false + * @type {boolean} + * @default false */ activeCollapse: false, /** * Class applied to `li`'s in tab link list. * @option - * @example 'tabs-title' + * @type {string} + * @default 'tabs-title' */ linkClass: 'tabs-title', /** * Class applied to the active `li` in tab link list. * @option - * @example 'is-active' + * @type {string} + * @default 'is-active' */ linkActiveClass: 'is-active', /** * Class applied to the content containers. * @option - * @example 'tabs-panel' + * @type {string} + * @default 'tabs-panel' */ panelClass: 'tabs-panel', /** * Class applied to the active content container. * @option - * @example 'is-active' + * @type {string} + * @default 'is-active' */ panelActiveClass: 'is-active' }; // Window exports