| 1 |
import { Alpine } from '@elementor/alpinejs'; |
| 2 |
import { getTabId } from './utils'; |
| 3 |
|
| 4 |
/** |
| 5 |
* @typedef {Record<string, number>} TabsState - Maps tabsId to the selected tab index. |
| 6 |
*/ |
| 7 |
const STORE_NAME = 'editor-atomic-tabs-state'; |
| 8 |
|
| 9 |
function ensureStore() { |
| 10 |
if ( ! Alpine.store( STORE_NAME ) ) { |
| 11 |
Alpine.store( STORE_NAME, /** @type {TabsState} */ ( {} ) ); |
| 12 |
} |
| 13 |
|
| 14 |
return /** @type {TabsState} */ ( Alpine.store( STORE_NAME ) ); |
| 15 |
} |
| 16 |
|
| 17 |
export function getActiveTabId( tabsId, fallback ) { |
| 18 |
const store = ensureStore(); |
| 19 |
const storedIndex = store[ tabsId ]; |
| 20 |
|
| 21 |
if ( storedIndex === undefined ) { |
| 22 |
return fallback; |
| 23 |
} |
| 24 |
|
| 25 |
return getTabId( tabsId, storedIndex ); |
| 26 |
} |
| 27 |
|
| 28 |
export function setActiveTabIndex( tabsId, index ) { |
| 29 |
const store = ensureStore(); |
| 30 |
|
| 31 |
store[ tabsId ] = index; |
| 32 |
} |
| 33 |
|
| 34 |
export function validateActiveTab( tabsId, tabCount ) { |
| 35 |
const store = ensureStore(); |
| 36 |
const storedIndex = store[ tabsId ]; |
| 37 |
|
| 38 |
if ( storedIndex === undefined ) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
if ( storedIndex >= tabCount ) { |
| 43 |
delete store[ tabsId ]; |
| 44 |
} |
| 45 |
} |
| 46 |
|