1 line
8.3 KiB
Plaintext
1 line
8.3 KiB
Plaintext
{"version":3,"sources":["DrawerRouter.tsx"],"names":["nanoid","TabRouter","TabActions","DrawerActions","openDrawer","type","closeDrawer","toggleDrawer","isDrawerOpen","state","Boolean","history","some","it","filter","DrawerRouter","openByDefault","rest","router","getInitialState","routeNames","routeParamList","routeGetIdList","stale","key","getRehydratedState","partialState","getStateForRouteFocus","result","getStateForAction","action","options","actionCreators"],"mappings":"AAAA,SAASA,MAAT,QAAuB,mBAAvB;AAOA,OAAOC,SAAP,IACEC,UADF,QAMO,aANP;AAqDA,OAAO,MAAMC,aAAa,GAAG,EAC3B,GAAGD,UADwB;;AAE3BE,EAAAA,UAAU,GAAqB;AAC7B,WAAO;AAAEC,MAAAA,IAAI,EAAE;AAAR,KAAP;AACD,GAJ0B;;AAK3BC,EAAAA,WAAW,GAAqB;AAC9B,WAAO;AAAED,MAAAA,IAAI,EAAE;AAAR,KAAP;AACD,GAP0B;;AAQ3BE,EAAAA,YAAY,GAAqB;AAC/B,WAAO;AAAEF,MAAAA,IAAI,EAAE;AAAR,KAAP;AACD;;AAV0B,CAAtB;;AAaP,MAAMG,YAAY,GAChBC,KADmB;AAAA;;AAAA,SAIhBC,OAAO,mBAACD,KAAK,CAACE,OAAP,mDAAC,eAAeC,IAAf,CAAqBC,EAAD,IAAQA,EAAE,CAACR,IAAH,KAAY,QAAxC,CAAD,CAJS;AAAA,CAArB;;AAMA,MAAMD,UAAU,GACdK,KADiB,IAEwB;AACzC,MAAID,YAAY,CAACC,KAAD,CAAhB,EAAyB;AACvB,WAAOA,KAAP;AACD;;AAED,SAAO,EACL,GAAGA,KADE;AAELE,IAAAA,OAAO,EAAE,CAAC,GAAGF,KAAK,CAACE,OAAV,EAAmB;AAAEN,MAAAA,IAAI,EAAE;AAAR,KAAnB;AAFJ,GAAP;AAID,CAXD;;AAaA,MAAMC,WAAW,GACfG,KADkB,IAEuB;AACzC,MAAI,CAACD,YAAY,CAACC,KAAD,CAAjB,E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hC,EAAwCC,OAAxC,CAAP;;AAEF;AACE,iBAAOb,MAAM,CAACW,iBAAP,CAAyBpB,KAAzB,EAAgCqB,MAAhC,EAAwCC,OAAxC,CAAP;AA5BJ;AA8BD,KA1FI;;AA4FLC,IAAAA,cAAc,EAAE7B;AA5FX,GAAP;AA8FD","sourcesContent":["import { nanoid } from 'nanoid/non-secure';\nimport type {\n PartialState,\n CommonNavigationAction,\n Router,\n ParamListBase,\n} from './types';\nimport TabRouter, {\n TabActions,\n TabActionType,\n TabRouterOptions,\n TabNavigationState,\n TabActionHelpers,\n} from './TabRouter';\n\nexport type DrawerActionType =\n | TabActionType\n | {\n type: 'OPEN_DRAWER' | 'CLOSE_DRAWER' | 'TOGGLE_DRAWER';\n source?: string;\n target?: string;\n };\n\nexport type DrawerRouterOptions = TabRouterOptions & {\n openByDefault?: boolean;\n};\n\nexport type DrawerNavigationState<ParamList extends ParamListBase> = Omit<\n TabNavigationState<ParamList>,\n 'type' | 'history'\n> & {\n /**\n * Type of the router, in this case, it's drawer.\n */\n type: 'drawer';\n /**\n * List of previously visited route keys and drawer open status.\n */\n history: ({ type: 'route'; key: string } | { type: 'drawer' })[];\n};\n\nexport type DrawerActionHelpers<\n ParamList extends ParamListBase\n> = TabActionHelpers<ParamList> & {\n /**\n * Open the drawer sidebar.\n */\n openDrawer(): void;\n\n /**\n * Close the drawer sidebar.\n */\n closeDrawer(): void;\n\n /**\n * Open the drawer sidebar if closed, or close if opened.\n */\n toggleDrawer(): void;\n};\n\nexport const DrawerActions = {\n ...TabActions,\n openDrawer(): DrawerActionType {\n return { type: 'OPEN_DRAWER' };\n },\n closeDrawer(): DrawerActionType {\n return { type: 'CLOSE_DRAWER' };\n },\n toggleDrawer(): DrawerActionType {\n return { type: 'TOGGLE_DRAWER' };\n },\n};\n\nconst isDrawerOpen = (\n state:\n | DrawerNavigationState<ParamListBase>\n | PartialState<DrawerNavigationState<ParamListBase>>\n) => Boolean(state.history?.some((it) => it.type === 'drawer'));\n\nconst openDrawer = (\n state: DrawerNavigationState<ParamListBase>\n): DrawerNavigationState<ParamListBase> => {\n if (isDrawerOpen(state)) {\n return state;\n }\n\n return {\n ...state,\n history: [...state.history, { type: 'drawer' }],\n };\n};\n\nconst closeDrawer = (\n state: DrawerNavigationState<ParamListBase>\n): DrawerNavigationState<ParamListBase> => {\n if (!isDrawerOpen(state)) {\n return state;\n }\n\n return {\n ...state,\n history: state.history.filter((it) => it.type !== 'drawer'),\n };\n};\n\nexport default function DrawerRouter({\n openByDefault,\n ...rest\n}: DrawerRouterOptions): Router<\n DrawerNavigationState<ParamListBase>,\n DrawerActionType | CommonNavigationAction\n> {\n const router = (TabRouter(rest) as unknown) as Router<\n DrawerNavigationState<ParamListBase>,\n TabActionType | CommonNavigationAction\n >;\n\n return {\n ...router,\n\n type: 'drawer',\n\n getInitialState({ routeNames, routeParamList, routeGetIdList }) {\n let state = router.getInitialState({\n routeNames,\n routeParamList,\n routeGetIdList,\n });\n\n if (openByDefault) {\n state = openDrawer(state);\n }\n\n return {\n ...state,\n stale: false,\n type: 'drawer',\n key: `drawer-${nanoid()}`,\n };\n },\n\n getRehydratedState(\n partialState,\n { routeNames, routeParamList, routeGetIdList }\n ) {\n if (partialState.stale === false) {\n return partialState;\n }\n\n let state = router.getRehydratedState(partialState, {\n routeNames,\n routeParamList,\n routeGetIdList,\n });\n\n if (partialState.history ? isDrawerOpen(partialState) : openByDefault) {\n state = openDrawer(state);\n }\n\n return {\n ...state,\n type: 'drawer',\n key: `drawer-${nanoid()}`,\n };\n },\n\n getStateForRouteFocus(state, key) {\n const result = router.getStateForRouteFocus(state, key);\n\n if (openByDefault) {\n return openDrawer(result);\n }\n\n return closeDrawer(result);\n },\n\n getStateForAction(state, action, options) {\n switch (action.type) {\n case 'OPEN_DRAWER':\n return openDrawer(state);\n\n case 'CLOSE_DRAWER':\n return closeDrawer(state);\n\n case 'TOGGLE_DRAWER':\n if (isDrawerOpen(state)) {\n return closeDrawer(state);\n }\n\n return openDrawer(state);\n\n case 'GO_BACK':\n if (openByDefault) {\n if (!isDrawerOpen(state)) {\n return openDrawer(state);\n }\n } else {\n if (isDrawerOpen(state)) {\n return closeDrawer(state);\n }\n }\n\n return router.getStateForAction(state, action, options);\n\n default:\n return router.getStateForAction(state, action, options);\n }\n },\n\n actionCreators: DrawerActions,\n };\n}\n"]} |