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

- old
+ new

@@ -55,13 +55,52 @@ }); $content.attr({'role': 'tabpanel', 'aria-labelledby': linkId, 'aria-hidden': true, 'id': id}); }); var $initActive = this.$element.find('.is-active').children('[data-tab-content]'); + this.firstTimeInit = true; if($initActive.length){ - this.down($initActive, true); + this.down($initActive, this.firstTimeInit); + this.firstTimeInit = false; } + + 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+'"]'), + $anchor = $(anchor); + + if ($link.length && $anchor) { + if (!$link.parent('[data-accordion-item]').hasClass('is-active')) { + this.down($anchor, this.firstTimeInit); + this.firstTimeInit = false; + }; + + //roll up a little to show the titles + if (this.options.deepLinkSmudge) { + var _this = this; + $(window).load(function() { + var offset = _this.$element.offset(); + $('html, body').animate({ scrollTop: offset.top }, _this.options.deepLinkSmudgeDelay); + }); + } + + /** + * Fires when the zplugin has deeplinked at pageload + * @event Accordion#deeplink + */ + this.$element.trigger('deeplink.zf.accordion', [$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 accordion. @@ -101,10 +140,13 @@ } }); }); } }); + if(this.options.deepLink) { + $(window).on('popstate', this._checkDeepLink); + } } /** * Toggles the selected content pane's open/close state. * @param {jQuery} $target - jQuery object of the pane to toggle (`.accordion-content`). @@ -114,10 +156,20 @@ if($target.parent().hasClass('is-active')) { this.up($target); } else { this.down($target); } + //either replace or update browser history + if (this.options.deepLink) { + var anchor = $target.prev('a').attr('href'); + + if (this.options.updateHistory) { + history.pushState({}, '', anchor); + } else { + history.replaceState({}, '', anchor); + } + } } /** * Opens the accordion tab defined by `$target`. * @param {jQuery} $target - Accordion pane to open (`.accordion-content`). @@ -192,33 +244,70 @@ * @function */ destroy() { this.$element.find('[data-tab-content]').stop(true).slideUp(0).css('display', ''); this.$element.find('a').off('.zf.accordion'); + if(this.options.deepLink) { + $(window).off('popstate', this._checkDeepLink); + } Foundation.unregisterPlugin(this); } } Accordion.defaults = { /** * Amount of time to animate the opening of an accordion pane. * @option - * @example 250 + * @type {number} + * @default 250 */ slideSpeed: 250, /** * Allow the accordion to have multiple open panes. * @option - * @example false + * @type {boolean} + * @default false */ multiExpand: false, /** * Allow the accordion to close all panes. * @option - * @example false + * @type {boolean} + * @default false */ - allowAllClosed: false + allowAllClosed: false, + /** + * Allows the window to scroll to content of pane specified by hash anchor + * @option + * @type {boolean} + * @default false + */ + deepLink: false, + + /** + * Adjust the deep link scroll to make sure the top of the accordion panel is visible + * @option + * @type {boolean} + * @default false + */ + deepLinkSmudge: false, + + /** + * Animation time (ms) for the deep link adjustment + * @option + * @type {number} + * @default 300 + */ + deepLinkSmudgeDelay: 300, + + /** + * Update the browser history with the open accordion + * @option + * @type {boolean} + * @default false + */ + updateHistory: false }; // Window exports Foundation.plugin(Accordion, 'Accordion');