@@ -57,9 +57,48 @@ class Accordion {
5757 $content . attr ( { 'role' : 'tabpanel' , 'aria-labelledby' : linkId , 'aria-hidden' : true , 'id' : id } ) ;
5858 } ) ;
5959 var $initActive = this . $element . find ( '.is-active' ) . children ( '[data-tab-content]' ) ;
60+ this . firstTimeInit = true ;
6061 if ( $initActive . length ) {
61- this . down ( $initActive , true ) ;
62+ this . down ( $initActive , this . firstTimeInit ) ;
63+ this . firstTimeInit = false ;
6264 }
65+
66+ this . _checkDeepLink = ( ) => {
67+ var anchor = window . location . hash ;
68+ //need a hash and a relevant anchor in this tabset
69+ if ( anchor . length ) {
70+ var $link = this . $element . find ( '[href$="' + anchor + '"]' ) ,
71+ $anchor = $ ( anchor ) ;
72+
73+ if ( $link . length && $anchor ) {
74+ if ( ! $link . parent ( '[data-accordion-item]' ) . hasClass ( 'is-active' ) ) {
75+ this . down ( $anchor , this . firstTimeInit ) ;
76+ this . firstTimeInit = false ;
77+ } ;
78+
79+ //roll up a little to show the titles
80+ if ( this . options . deepLinkSmudge ) {
81+ var _this = this ;
82+ $ ( window ) . load ( function ( ) {
83+ var offset = _this . $element . offset ( ) ;
84+ $ ( 'html, body' ) . animate ( { scrollTop : offset . top } , _this . options . deepLinkSmudgeDelay ) ;
85+ } ) ;
86+ }
87+
88+ /**
89+ * Fires when the zplugin has deeplinked at pageload
90+ * @event Accordion#deeplink
91+ */
92+ this . $element . trigger ( 'deeplink.zf.accordion' , [ $link , $anchor ] ) ;
93+ }
94+ }
95+ }
96+
97+ //use browser to open a tab, if it exists in this tabset
98+ if ( this . options . deepLink ) {
99+ this . _checkDeepLink ( ) ;
100+ }
101+
63102 this . _events ( ) ;
64103 }
65104
@@ -103,6 +142,9 @@ class Accordion {
103142 } ) ;
104143 }
105144 } ) ;
145+ if ( this . options . deepLink ) {
146+ $ ( window ) . on ( 'popstate' , this . _checkDeepLink ) ;
147+ }
106148 }
107149
108150 /**
@@ -116,6 +158,16 @@ class Accordion {
116158 } else {
117159 this . down ( $target ) ;
118160 }
161+ //either replace or update browser history
162+ if ( this . options . deepLink ) {
163+ var anchor = $target . prev ( 'a' ) . attr ( 'href' ) ;
164+
165+ if ( this . options . updateHistory ) {
166+ history . pushState ( { } , '' , anchor ) ;
167+ } else {
168+ history . replaceState ( { } , '' , anchor ) ;
169+ }
170+ }
119171 }
120172
121173 /**
@@ -194,6 +246,9 @@ class Accordion {
194246 destroy ( ) {
195247 this . $element . find ( '[data-tab-content]' ) . stop ( true ) . slideUp ( 0 ) . css ( 'display' , '' ) ;
196248 this . $element . find ( 'a' ) . off ( '.zf.accordion' ) ;
249+ if ( this . options . deepLink ) {
250+ $ ( window ) . off ( 'popstate' , this . _checkDeepLink ) ;
251+ }
197252
198253 Foundation . unregisterPlugin ( this ) ;
199254 }
@@ -203,21 +258,55 @@ Accordion.defaults = {
203258 /**
204259 * Amount of time to animate the opening of an accordion pane.
205260 * @option
206- * @example 250
261+ * @type {number }
262+ * @default 250
207263 */
208264 slideSpeed : 250 ,
209265 /**
210266 * Allow the accordion to have multiple open panes.
211267 * @option
212- * @example false
268+ * @type {boolean }
269+ * @default false
213270 */
214271 multiExpand : false ,
215272 /**
216273 * Allow the accordion to close all panes.
217274 * @option
218- * @example false
275+ * @type {boolean }
276+ * @default false
277+ */
278+ allowAllClosed : false ,
279+ /**
280+ * Allows the window to scroll to content of pane specified by hash anchor
281+ * @option
282+ * @type {boolean }
283+ * @default false
284+ */
285+ deepLink : false ,
286+
287+ /**
288+ * Adjust the deep link scroll to make sure the top of the accordion panel is visible
289+ * @option
290+ * @type {boolean }
291+ * @default false
292+ */
293+ deepLinkSmudge : false ,
294+
295+ /**
296+ * Animation time (ms) for the deep link adjustment
297+ * @option
298+ * @type {number }
299+ * @default 300
300+ */
301+ deepLinkSmudgeDelay : 300 ,
302+
303+ /**
304+ * Update the browser history with the open accordion
305+ * @option
306+ * @type {boolean }
307+ * @default false
219308 */
220- allowAllClosed : false
309+ updateHistory : false
221310} ;
222311
223312// Window exports
0 commit comments