@@ -10,10 +10,17 @@ class RDTATabs {
1010 * Class constructor.
1111 *
1212 * @private Do not access this method directly, it was called via `init()` method.
13- * @param {object } options
14- * @returns {RDTATabs }
13+ * @param {object } options The options.
14+ * @param {int } options.activeTabs The index number of active tab to be manually set.
15+ * @param {boolean } options.rememberLastTab Set to `true` to remember last tab opened. Default is `false` or not remember.
16+ * @param {string } options.selector The JS selector.
17+ * @returns {undefined }
1518 */
16- constructor ( options ) {
19+ constructor ( options = { } ) {
20+ if ( typeof ( options ) !== 'object' ) {
21+ throw new Error ( 'The argument options must be an object.' ) ;
22+ }
23+
1724 this . selector = ( options . selector ? options . selector : '' ) ;
1825 this . options = ( options . options ? options . options : { } ) ;
1926 } // constructor
@@ -22,12 +29,14 @@ class RDTATabs {
2229 /**
2330 * Activate tab content.
2431 *
25- * @private This method was called from `addRequiredClasses()`, `listenOnTabNav()`.
32+ * This method was called from `#addRequiredClasses()`, `#listenOnTabNav()`.
33+ *
34+ * @since 2.4.1 Renamed from `activateTabContent()`.
2635 * @param {object } selector Object of each tab main element.
2736 * @param {string } targetTabContent
2837 * @returns {undefined }
2938 */
30- activateTabContent ( selector , targetTabContent ) {
39+ # activateTabContent( selector , targetTabContent ) {
3140 if ( ! targetTabContent || ! selector ) {
3241 return false ;
3342 }
@@ -81,17 +90,19 @@ class RDTATabs {
8190 let event = new CustomEvent ( 'rdta.tabs.activeTab' , { 'detail' : eventDetail } ) ;
8291 document . querySelector ( thisClass . selector ) . dispatchEvent ( event ) ;
8392 }
84- } // activateTabContent
93+ } // # activateTabContent
8594
8695
8796 /**
8897 * Add required CSS classes.
8998 *
90- * @private Do not call this, just call `init()`.
99+ * This method was called from `init()`.
100+ *
101+ * @since 2.4.1 Renamed from `addRequiredClasses()`.
91102 * @param {object } options
92103 * @returns {undefined }
93104 */
94- addRequiredClasses ( options ) {
105+ # addRequiredClasses( options ) {
95106 let thisClass = this ;
96107
97108 document . querySelectorAll ( thisClass . selector ) . forEach ( function ( item , index ) {
@@ -126,26 +137,28 @@ class RDTATabs {
126137 targetTab = tabNavElement . children [ i ] . querySelector ( 'a' ) . hash ;
127138 }
128139 if ( targetTab ) {
129- thisClass . activateTabContent ( item , targetTab ) ;
140+ thisClass . # activateTabContent( item , targetTab ) ;
130141 }
131142 break ;
132143 }
133144 } // endfor;
134145 }
135146 } ) ;
136- } // addRequiredClasses
147+ } // # addRequiredClasses
137148
138149
139150 /**
140151 * Ajax and set content to target. Did not activate the tab nav.
141152 *
142- * @private Do not call this, just call `init()`.
153+ * This method was called from `#listenOnTabNav()`.
154+ *
155+ * @since 2.4.1 Renamed from `ajaxTabContent()`.
143156 * @param {string } url
144157 * @param {object } selector Object of each tab main element.
145158 * @param {string } targetTabContent
146159 * @returns {undefined }
147160 */
148- ajaxTabContent ( url , selector , targetTabContent ) {
161+ # ajaxTabContent( url , selector , targetTabContent ) {
149162 let xhr = new XMLHttpRequest ( ) ;
150163 let thisClass = this ;
151164
@@ -164,44 +177,7 @@ class RDTATabs {
164177
165178 xhr . open ( 'GET' , url ) ;
166179 xhr . send ( ) ;
167- } // ajaxTabContent
168-
169-
170- /**
171- * Initialize RDTA tabs.
172- *
173- * @param {string } selector
174- * @param {object } options
175- * @returns {undefined }
176- */
177- static init ( selector , options ) {
178- let defaultOptions = {
179- 'activeTabs' : 0 ,
180- 'rememberLastTab' : false ,
181- } ;
182- if ( typeof ( options ) === 'object' ) {
183- options = Object . assign ( defaultOptions , options ) ;
184- } else {
185- options = defaultOptions ;
186- }
187-
188- if ( options . rememberLastTab === true && window . localStorage ) {
189- // if option was set to remember last tab.
190- let lastTab = window . localStorage . getItem ( 'rdtaTabsLast-' + selector ) ;
191- if ( ! isNaN ( lastTab ) && lastTab !== '' && lastTab !== null ) {
192- options . activeTabs = parseInt ( lastTab ) ;
193- }
194- }
195-
196- let thisClass = new this ( { 'selector' : selector , 'options' : options } ) ;
197- thisClass . addRequiredClasses ( options ) ;
198- thisClass . listenOnTabNav ( options ) ;
199-
200- // set tab navbar horizontal scroll if overflow. this must run after set activate tab content.
201- thisClass . setTabNavHorizontalScroll ( ) ;
202- thisClass . listenWindowResize ( ) ;
203- thisClass . listenClickOnTabScroll ( ) ;
204- } // init
180+ } // #ajaxTabContent
205181
206182
207183 /**
@@ -210,10 +186,11 @@ class RDTATabs {
210186 * Tabs nav scroll will be visible on tabs size is overflow visible width.
211187 *
212188 * @since 2.2.1
189+ * @since 2.4.1 Renamed from `listenClickOnTabScroll()`.
213190 * @private This method was called from `init()`.
214191 * @returns {undefined }
215192 */
216- listenClickOnTabScroll ( ) {
193+ # listenClickOnTabScroll( ) {
217194 let thisClass = this ;
218195
219196 document . addEventListener ( 'click' , ( event ) => {
@@ -292,18 +269,19 @@ class RDTATabs {
292269 } // endif user clicked on < or >.
293270 } // endif; user is really click on tabs nav scroll.
294271 } ) ;
295- } // listenClickOnTabScroll
272+ } // # listenClickOnTabScroll
296273
297274
298275 /**
299276 * Listen on tab nav click and activate tab content.
300277 *
301- * @link https://stackoverflow.com/a/25248515/128761 Method 1 for delegation selection.
302- * @private Do not call this, just call `init()`.
278+ * This method was called from `init()`.
279+ *
280+ * @since 2.4.1 Renamed from `listenOnTabNav()`.
303281 * @param {object } options
304282 * @returns {undefined }
305283 */
306- listenOnTabNav ( options ) {
284+ # listenOnTabNav( options ) {
307285 let thisClass = this ;
308286 let tabElement = document . querySelector ( this . selector ) ;
309287
@@ -338,38 +316,42 @@ class RDTATabs {
338316 }
339317
340318 if ( target . getAttribute ( 'href' ) && target . getAttribute ( 'href' ) . charAt ( 0 ) !== '#' && ! target . hash ) {
341- thisClass . ajaxTabContent ( target . getAttribute ( 'href' ) , target . closest ( thisClass . selector ) , targetTab )
319+ thisClass . # ajaxTabContent( target . getAttribute ( 'href' ) , target . closest ( thisClass . selector ) , targetTab )
342320 }
343321
344- thisClass . activateTabContent ( target . closest ( thisClass . selector ) , targetTab ) ;
322+ thisClass . # activateTabContent( target . closest ( thisClass . selector ) , targetTab ) ;
345323 }
346324 } ) ;
347- } // listenOnTabNav
325+ } // # listenOnTabNav
348326
349327
350328 /**
351329 * Listen on window resize and do the task.
352330 *
331+ * This method was called from `init()`.
332+ *
353333 * @since 2.2.1
354- * @private This method was called from `init ()`.
334+ * @since 2.4.1 Renamed from `listenWindowResize ()`.
355335 * @returns {undefined }
356336 */
357- listenWindowResize ( ) {
337+ # listenWindowResize( ) {
358338 let thisClass = this ;
359339 window . addEventListener ( 'resize' , ( event ) => {
360- thisClass . setTabNavHorizontalScroll ( ) ;
340+ thisClass . # setTabNavHorizontalScroll( ) ;
361341 } ) ;
362- } // listenWindowResize
342+ } // # listenWindowResize
363343
364344
365345 /**
366346 * Set tabs nav horizontal scroll.
367347 *
348+ * This method was called from `init()`, `#listenWindowResize()`.
349+ *
368350 * @since 2.2.1
369- * @private This method was called from `init()`, `listenWindowResize ()`.
351+ * @since 2.4.1 Renamed from `setTabNavHorizontalScroll ()`.
370352 * @returns {undefined }
371353 */
372- setTabNavHorizontalScroll ( ) {
354+ # setTabNavHorizontalScroll( ) {
373355 let thisClass = this ;
374356 const navContainerClass = 'rd-tabs-nav-container' ;
375357 const navWrapperClass = 'rd-tabs-nav-wrapper' ;
@@ -471,7 +453,54 @@ class RDTATabs {
471453 // if this main tab element is vertical tabs.
472454 } // endif .rd-tabs not contain vertical class.
473455 } ) ;
474- } // setTabNavHorizontalScroll
456+ } // #setTabNavHorizontalScroll
457+
458+
459+ /**
460+ * Initialize RDTA tabs.
461+ *
462+ * @param {string } selector The JS selector. It can be CSS class or HTML ID.
463+ * @param {object } options The options.
464+ * @param {int } options.activeTabs The index number of active tab to be manually set.
465+ * @param {boolean } options.rememberLastTab Set to `true` to remember last tab opened. Default is `false` or not remember.
466+ * @returns {undefined }
467+ */
468+ static init ( selector , options = { } ) {
469+ if ( typeof ( selector ) !== 'string' ) {
470+ throw new Error ( 'The argument selector must be string.' ) ;
471+ }
472+ if ( typeof ( options ) !== 'object' ) {
473+ throw new Error ( 'The argument options must be an object.' ) ;
474+ }
475+
476+ let defaultOptions = {
477+ 'activeTabs' : 0 ,
478+ 'rememberLastTab' : false ,
479+ } ;
480+ if ( typeof ( options ) === 'object' ) {
481+ options = Object . assign ( defaultOptions , options ) ;
482+ } else {
483+ options = defaultOptions ;
484+ }
485+ defaultOptions = undefined ;
486+
487+ if ( options . rememberLastTab === true && window . localStorage ) {
488+ // if option was set to remember last tab.
489+ let lastTab = window . localStorage . getItem ( 'rdtaTabsLast-' + selector ) ;
490+ if ( ! isNaN ( lastTab ) && lastTab !== '' && lastTab !== null ) {
491+ options . activeTabs = parseInt ( lastTab ) ;
492+ }
493+ }
494+
495+ let thisClass = new this ( { 'selector' : selector , 'options' : options } ) ;
496+ thisClass . #addRequiredClasses( options ) ;
497+ thisClass . #listenOnTabNav( options ) ;
498+
499+ // set tab navbar horizontal scroll if overflow. this must run after set activate tab content.
500+ thisClass . #setTabNavHorizontalScroll( ) ;
501+ thisClass . #listenWindowResize( ) ;
502+ thisClass . #listenClickOnTabScroll( ) ;
503+ } // init
475504
476505
477506}
0 commit comments