mobx-route 0.30.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.cjs +65 -47
- package/index.cjs.map +1 -1
- package/index.d.ts +6 -4
- package/index.js +65 -47
- package/index.js.map +1 -1
- package/package.json +9 -7
- package/react.cjs +1 -16
- package/react.cjs.map +1 -1
- package/react.d.ts +1 -7
- package/react.js +2 -17
- package/react.js.map +1 -1
- package/view-model.cjs +19 -0
- package/view-model.cjs.map +1 -1
- package/view-model.d.ts +24 -0
- package/view-model.js +19 -0
- package/view-model.js.map +1 -1
package/react.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react.js","sources":["../src/react/components/link.tsx","../src/react/components/route-view.tsx","../src/react/components/route-view-group.tsx"],"sourcesContent":["import { observer } from 'mobx-react-lite';\nimport {\n type AnyRoute,\n buildSearchString,\n type InputPathParams,\n parseSearchString,\n type RouteNavigateParams,\n routeConfig,\n} from 'mobx-route';\nimport {\n type AnchorHTMLAttributes,\n cloneElement,\n forwardRef,\n isValidElement,\n type MouseEvent,\n useMemo,\n useRef,\n} from 'react';\nimport { isShallowEqual } from 'yummies/data';\nimport type { IsPartial, Maybe } from 'yummies/types';\n\ninterface LinkAnchorProps\n extends Omit<AnchorHTMLAttributes<HTMLAnchorElement>, 'href'> {\n asChild?: boolean;\n}\n\ntype LinkPathRouteProps<TRoute extends AnyRoute> = {\n to: TRoute;\n} & (IsPartial<InputPathParams<TRoute['pathDeclaration']>> extends true\n ? {\n params?: InputPathParams<TRoute['pathDeclaration']> | null | undefined;\n }\n : { params: InputPathParams<TRoute['pathDeclaration']> });\n\ntype LinkSimpleRouteProps =\n | {\n to: string;\n }\n | {\n href: Maybe<string>;\n };\n\nexport type LinkProps<TRoute extends AnyRoute> = LinkAnchorProps &\n RouteNavigateParams &\n (LinkPathRouteProps<TRoute> | LinkSimpleRouteProps);\n\ntype LinkComponentType = <TRoute extends AnyRoute>(\n props: LinkProps<TRoute>,\n) => React.ReactNode;\n\nexport const Link = observer(\n forwardRef<\n HTMLAnchorElement,\n LinkAnchorProps &\n RouteNavigateParams & {\n params?: any;\n to: string | AnyRoute;\n href: string;\n }\n >((props, ref) => {\n const {\n to,\n href: outerHref,\n mergeQuery,\n asChild,\n children,\n params,\n // route navigate params\n query,\n replace,\n state,\n //\n ...outerAnchorProps\n } = props;\n\n const isExternalNavigation =\n outerAnchorProps.target === '_blank' ||\n outerAnchorProps.target === 'blank';\n const queryDataRef = useRef<RouteNavigateParams['query']>(query);\n\n if (!isShallowEqual(queryDataRef.current, query)) {\n queryDataRef.current = query;\n }\n\n const { href, navigateParams } = useMemo(() => {\n const navigateParams: RouteNavigateParams = {\n mergeQuery,\n query,\n replace,\n state,\n };\n\n const cfg = routeConfig.get();\n\n let href: string = '';\n\n if (outerHref) {\n href = outerHref;\n } else if (to) {\n if (typeof to === 'string') {\n const isNeedToMergeQuery =\n navigateParams.mergeQuery ?? cfg.mergeQuery;\n\n const [path, ...querySegments] = to.split('?');\n\n const existedQuery = parseSearchString(querySegments.join('?'));\n\n const query = {\n ...(isNeedToMergeQuery ? cfg.queryParams.data : {}),\n ...existedQuery,\n ...navigateParams.query,\n };\n\n href = `${path}${buildSearchString(query)}`;\n } else {\n href = to.createUrl(\n params,\n navigateParams.query,\n navigateParams.mergeQuery,\n );\n }\n }\n\n return {\n href: cfg.formatLinkHref?.(href) ?? href,\n navigateParams,\n };\n }, [mergeQuery, replace, state, outerHref, to, queryDataRef.current]);\n\n const handleClick = (event: MouseEvent<HTMLAnchorElement>) => {\n if (\n isExternalNavigation ||\n event.ctrlKey ||\n event.metaKey ||\n event.altKey ||\n event.shiftKey ||\n event.button !== 0\n )\n return;\n\n outerAnchorProps.onClick?.(event);\n\n if (\n !event.defaultPrevented &&\n !href.startsWith('https://') &&\n !href.startsWith('http://')\n ) {\n event.preventDefault();\n\n if (navigateParams.replace) {\n routeConfig.get().history.replace(href, navigateParams.state);\n } else {\n routeConfig.get().history.push(href, navigateParams.state);\n }\n }\n };\n\n const anchorProps = {\n ...outerAnchorProps,\n href,\n onClick: handleClick,\n rel:\n outerAnchorProps.rel ??\n (isExternalNavigation ? 'noopener noreferrer' : undefined),\n };\n\n return asChild && isValidElement(children) ? (\n cloneElement(children, anchorProps)\n ) : (\n <a {...anchorProps} ref={ref}>\n {children}\n </a>\n );\n }),\n) as unknown as LinkComponentType;\n","import { observer } from 'mobx-react-lite';\nimport type {\n AnyAbstractRouteEntity,\n AnyRoute,\n AnyVirtualRoute,\n} from 'mobx-route';\nimport { type LoadableConfig, loadable } from 'react-simple-loadable';\n\nexport type RouteViewComponent<TRoute extends AnyAbstractRouteEntity> =\n React.ComponentType<RouteViewProps<TRoute>>;\n\ninterface RouteViewConfigWithoutRoute {\n children?: React.ReactNode | (() => React.ReactNode);\n}\n\ntype LoadViewFn<TRoute extends AnyAbstractRouteEntity> = (\n route: TRoute,\n) => Promise<RouteViewComponent<TRoute>>;\n\nexport interface RouteViewConfigWithRoute<TRoute extends AnyAbstractRouteEntity>\n extends Pick<LoadableConfig, 'loading' | 'preload' | 'throwOnError'> {\n route: TRoute;\n view?: RouteViewComponent<TRoute>;\n /**\n * @deprecated use your own load fn for lazy load view\n */\n loadView?: LoadViewFn<TRoute>;\n /**\n * Case when route is not opened\n */\n fallback?: React.ReactNode;\n children?:\n | React.ReactNode\n | ((\n params: RouteViewProps<TRoute>['params'],\n route: TRoute,\n ) => React.ReactNode);\n}\n\nexport type RouteViewConfig<TRoute extends AnyAbstractRouteEntity> =\n | RouteViewConfigWithRoute<TRoute>\n | RouteViewConfigWithoutRoute;\n\nexport type RouteViewProps<TRoute extends AnyAbstractRouteEntity> = {\n children?: React.ReactNode;\n params: TRoute extends AnyRoute\n ? Exclude<TRoute['params'], null | undefined>\n : TRoute extends AnyVirtualRoute\n ? TRoute['params']\n : never;\n};\n\ntype RouteViewBaseComponent = <TRoute extends AnyAbstractRouteEntity>(\n props: RouteViewConfig<TRoute>,\n) => React.ReactNode;\n\nfunction RouteViewBase<TRoute extends AnyAbstractRouteEntity>(\n props: Readonly<RouteViewConfig<TRoute>>,\n): React.ReactNode {\n let Component: React.ComponentType<any> | undefined;\n\n if (!('route' in props)) {\n return typeof props.children === 'function'\n ? props.children()\n : props.children;\n }\n\n if (!props.route.isOpened) {\n return props.fallback ?? null;\n }\n\n const loadViewFn = props.loadView as\n | (LoadViewFn<TRoute> & { _loadableComponent: any })\n | undefined;\n\n if (loadViewFn) {\n if (!loadViewFn._loadableComponent) {\n loadViewFn._loadableComponent = loadable({\n load: () => props.loadView!(props.route),\n loading: props.loading,\n preload: props.preload,\n throwOnError: props.throwOnError,\n cache: false,\n });\n }\n Component = loadViewFn._loadableComponent;\n } else {\n Component = props.view;\n }\n\n const params: any = 'params' in props.route ? props.route.params : {};\n\n if (Component) {\n return <Component params={params}>{props.children}</Component>;\n }\n\n if (typeof props.children === 'function') {\n return props.children(params, props.route);\n }\n\n return props.children;\n}\n\nexport const RouteView = observer(RouteViewBase) as RouteViewBaseComponent;\n","import { observer } from 'mobx-react-lite';\nimport {\n type AnyRoute,\n type AnyRouteEntity,\n buildSearchString,\n isRouteEntity,\n type RouteNavigateParams,\n type RouteParams,\n routeConfig,\n} from 'mobx-route';\nimport { isValidElement, useEffect } from 'react';\nimport type { IsPartial, Maybe } from 'yummies/types';\n\ntype LayoutComponent =\n | React.ComponentType<{ children?: React.ReactNode }>\n | React.ComponentType<{ children: React.ReactNode }>;\n\ninterface BaseProps extends RouteNavigateParams {\n children: React.ReactNode;\n layout?: LayoutComponent;\n useLastOpened?: boolean;\n}\n\ntype PropsWithDefaultRoute<TRoute extends AnyRouteEntity> = BaseProps & {\n otherwise?: TRoute;\n} & (IsPartial<RouteParams<TRoute>> extends true\n ? {\n params?: Maybe<RouteParams<TRoute>>;\n }\n : {\n params: RouteParams<TRoute>;\n });\n\ntype PropsWithDefaultUrl = BaseProps & {\n otherwise?: string;\n};\n\nexport type RouteViewGroupProps<TRoute extends AnyRouteEntity> =\n | PropsWithDefaultRoute<TRoute>\n | PropsWithDefaultUrl;\n\ntype RouteViewGroupComponent = <TRoute extends AnyRouteEntity>(\n props: RouteViewGroupProps<TRoute>,\n) => React.ReactNode;\n\nexport const RouteViewGroup = observer(\n <TRoute extends AnyRouteEntity>({\n children,\n layout: Layout,\n otherwise: otherwiseNavigation,\n useLastOpened,\n // @ts-expect-error\n params,\n ...navigateParams\n }: RouteViewGroupProps<TRoute>) => {\n let activeChildRouteNode: React.ReactNode = null;\n let lastInactiveChildNode: React.ReactNode = null;\n let hasRoutesInOpening = false;\n\n const childNodes: React.ReactNode[] = Array.isArray(children)\n ? children\n : [children];\n\n for (const childNode of childNodes) {\n const isRouteChild =\n isValidElement(childNode) &&\n // @ts-expect-error redundand checks better to wrap in this directive\n isRouteEntity(childNode.props?.route);\n\n if (isRouteChild) {\n const route = (childNode.props as any).route as AnyRoute;\n\n if (route.isOpened) {\n activeChildRouteNode = childNode;\n if (!useLastOpened) {\n break;\n }\n } else {\n if (route.isOpening) {\n hasRoutesInOpening = true;\n }\n lastInactiveChildNode = childNode;\n }\n } else {\n lastInactiveChildNode = childNode;\n }\n }\n\n const hasActiveChildNode = !!activeChildRouteNode;\n\n useEffect(() => {\n if (!hasActiveChildNode && !hasRoutesInOpening && otherwiseNavigation) {\n if (typeof otherwiseNavigation === 'string') {\n const history = routeConfig.get().history;\n const url = `${otherwiseNavigation}${buildSearchString(navigateParams.query || {})}`;\n\n if (navigateParams.replace) {\n history.replace(url, navigateParams.state);\n } else {\n history.push(url, navigateParams.state);\n }\n } else if (!otherwiseNavigation.isOpened) {\n otherwiseNavigation.open(params, navigateParams);\n }\n }\n }, [hasActiveChildNode, hasRoutesInOpening, otherwiseNavigation]);\n\n if (otherwiseNavigation && !activeChildRouteNode) {\n return null;\n }\n\n const resultNodeToRender =\n activeChildRouteNode ?? lastInactiveChildNode ?? null;\n\n if (Layout) {\n return <Layout>{resultNodeToRender}</Layout>;\n }\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YAAY,IAAA,EAAM;AAClC,gBAAM,MAAM,GAAG,mBAAmB,GAAG,kBAAkB,eAAe,SAAS,CAAA,CAAE,CAAC;AAElF,cAAI,eAAe,SAAS;AAC1B,oBAAQ,QAAQ,KAAK,eAAe,KAAK;AAAA,UAC3C,OAAO;AACL,oBAAQ,KAAK,KAAK,eAAe,KAAK;AAAA,UACxC;AAAA,QACF,WAAW,CAAC,oBAAoB,UAAU;AACxC,8BAAoB,KAAK,QAAQ,cAAc;AAAA,QACjD;AAAA,MACF;AAAA,IACF,GAAG,CAAC,oBAAoB,oBAAoB,mBAAmB,CAAC;AAEhE,QAAI,uBAAuB,CAAC,sBAAsB;AAChD,aAAO;AAAA,IACT;AAEA,UAAM,qBACJ,wBAAwB,yBAAyB;AAEnD,QAAI,QAAQ;AACV,aAAO,oBAAC,UAAQ,UAAA,mBAAA,CAAmB;AAAA,IACrC;AAEA,WAAO;AAAA,EACT;AACF;"}
|
|
1
|
+
{"version":3,"file":"react.js","sources":["../src/react/components/link.tsx","../src/react/components/route-view.tsx","../src/react/components/route-view-group.tsx"],"sourcesContent":["import { observer } from 'mobx-react-lite';\nimport {\n type AnyRoute,\n buildSearchString,\n type InputPathParams,\n parseSearchString,\n type RouteNavigateParams,\n routeConfig,\n} from 'mobx-route';\nimport {\n type AnchorHTMLAttributes,\n cloneElement,\n forwardRef,\n isValidElement,\n type MouseEvent,\n useMemo,\n useRef,\n} from 'react';\nimport { isShallowEqual } from 'yummies/data';\nimport type { IsPartial, Maybe } from 'yummies/types';\n\ninterface LinkAnchorProps\n extends Omit<AnchorHTMLAttributes<HTMLAnchorElement>, 'href'> {\n asChild?: boolean;\n}\n\ntype LinkPathRouteProps<TRoute extends AnyRoute> = {\n to: TRoute;\n} & (IsPartial<InputPathParams<TRoute['pathDeclaration']>> extends true\n ? {\n params?: InputPathParams<TRoute['pathDeclaration']> | null | undefined;\n }\n : { params: InputPathParams<TRoute['pathDeclaration']> });\n\ntype LinkSimpleRouteProps =\n | {\n to: string;\n }\n | {\n href: Maybe<string>;\n };\n\nexport type LinkProps<TRoute extends AnyRoute> = LinkAnchorProps &\n RouteNavigateParams &\n (LinkPathRouteProps<TRoute> | LinkSimpleRouteProps);\n\ntype LinkComponentType = <TRoute extends AnyRoute>(\n props: LinkProps<TRoute>,\n) => React.ReactNode;\n\nexport const Link = observer(\n forwardRef<\n HTMLAnchorElement,\n LinkAnchorProps &\n RouteNavigateParams & {\n params?: any;\n to: string | AnyRoute;\n href: string;\n }\n >((props, ref) => {\n const {\n to,\n href: outerHref,\n mergeQuery,\n asChild,\n children,\n params,\n // route navigate params\n query,\n replace,\n state,\n //\n ...outerAnchorProps\n } = props;\n\n const isExternalNavigation =\n outerAnchorProps.target === '_blank' ||\n outerAnchorProps.target === 'blank';\n const queryDataRef = useRef<RouteNavigateParams['query']>(query);\n\n if (!isShallowEqual(queryDataRef.current, query)) {\n queryDataRef.current = query;\n }\n\n const { href, navigateParams } = useMemo(() => {\n const navigateParams: RouteNavigateParams = {\n mergeQuery,\n query,\n replace,\n state,\n };\n\n const cfg = routeConfig.get();\n\n let href: string = '';\n\n if (outerHref) {\n href = outerHref;\n } else if (to) {\n if (typeof to === 'string') {\n const isNeedToMergeQuery =\n navigateParams.mergeQuery ?? cfg.mergeQuery;\n\n const [path, ...querySegments] = to.split('?');\n\n const existedQuery = parseSearchString(querySegments.join('?'));\n\n const query = {\n ...(isNeedToMergeQuery ? cfg.queryParams.data : {}),\n ...existedQuery,\n ...navigateParams.query,\n };\n\n href = `${path}${buildSearchString(query)}`;\n } else {\n href = to.createUrl(\n params,\n navigateParams.query,\n navigateParams.mergeQuery,\n );\n }\n }\n\n return {\n href: cfg.formatLinkHref?.(href) ?? href,\n navigateParams,\n };\n }, [mergeQuery, replace, state, outerHref, to, queryDataRef.current]);\n\n const handleClick = (event: MouseEvent<HTMLAnchorElement>) => {\n if (\n isExternalNavigation ||\n event.ctrlKey ||\n event.metaKey ||\n event.altKey ||\n event.shiftKey ||\n event.button !== 0\n )\n return;\n\n outerAnchorProps.onClick?.(event);\n\n if (\n !event.defaultPrevented &&\n !href.startsWith('https://') &&\n !href.startsWith('http://')\n ) {\n event.preventDefault();\n\n if (navigateParams.replace) {\n routeConfig.get().history.replace(href, navigateParams.state);\n } else {\n routeConfig.get().history.push(href, navigateParams.state);\n }\n }\n };\n\n const anchorProps = {\n ...outerAnchorProps,\n href,\n onClick: handleClick,\n rel:\n outerAnchorProps.rel ??\n (isExternalNavigation ? 'noopener noreferrer' : undefined),\n };\n\n return asChild && isValidElement(children) ? (\n cloneElement(children, anchorProps)\n ) : (\n <a {...anchorProps} ref={ref}>\n {children}\n </a>\n );\n }),\n) as unknown as LinkComponentType;\n","import { observer } from 'mobx-react-lite';\nimport type {\n AnyAbstractRouteEntity,\n AnyRoute,\n AnyVirtualRoute,\n} from 'mobx-route';\n\nexport type RouteViewComponent<TRoute extends AnyAbstractRouteEntity> =\n React.ComponentType<RouteViewProps<TRoute>>;\n\ninterface RouteViewConfigWithoutRoute {\n children?: React.ReactNode | (() => React.ReactNode);\n}\n\nexport interface RouteViewConfigWithRoute<\n TRoute extends AnyAbstractRouteEntity,\n> {\n route: TRoute;\n view?: RouteViewComponent<TRoute>;\n /**\n * Case when route is not opened\n */\n fallback?: React.ReactNode;\n children?:\n | React.ReactNode\n | ((\n params: RouteViewProps<TRoute>['params'],\n route: TRoute,\n ) => React.ReactNode);\n}\n\nexport type RouteViewConfig<TRoute extends AnyAbstractRouteEntity> =\n | RouteViewConfigWithRoute<TRoute>\n | RouteViewConfigWithoutRoute;\n\nexport type RouteViewProps<TRoute extends AnyAbstractRouteEntity> = {\n children?: React.ReactNode;\n params: TRoute extends AnyRoute\n ? Exclude<TRoute['params'], null | undefined>\n : TRoute extends AnyVirtualRoute\n ? TRoute['params']\n : never;\n};\n\ntype RouteViewBaseComponent = <TRoute extends AnyAbstractRouteEntity>(\n props: RouteViewConfig<TRoute>,\n) => React.ReactNode;\n\nfunction RouteViewBase<TRoute extends AnyAbstractRouteEntity>(\n props: Readonly<RouteViewConfig<TRoute>>,\n): React.ReactNode {\n let Component: React.ComponentType<any> | undefined;\n\n if (!('route' in props)) {\n return typeof props.children === 'function'\n ? props.children()\n : props.children;\n }\n\n if (!props.route.isOpened) {\n return props.fallback ?? null;\n }\n\n Component = props.view;\n\n const params: any = 'params' in props.route ? props.route.params : {};\n\n if (Component) {\n return <Component params={params}>{props.children}</Component>;\n }\n\n if (typeof props.children === 'function') {\n return props.children(params, props.route);\n }\n\n return props.children;\n}\n\nexport const RouteView = observer(RouteViewBase) as RouteViewBaseComponent;\n","import { observer } from 'mobx-react-lite';\nimport {\n type AnyRoute,\n type AnyRouteEntity,\n buildSearchString,\n isRouteEntity,\n type RouteNavigateParams,\n type RouteParams,\n routeConfig,\n} from 'mobx-route';\nimport { isValidElement, useEffect } from 'react';\nimport type { IsPartial, Maybe } from 'yummies/types';\n\ntype LayoutComponent =\n | React.ComponentType<{ children?: React.ReactNode }>\n | React.ComponentType<{ children: React.ReactNode }>;\n\ninterface BaseProps extends RouteNavigateParams {\n children: React.ReactNode;\n layout?: LayoutComponent;\n useLastOpened?: boolean;\n}\n\ntype PropsWithDefaultRoute<TRoute extends AnyRouteEntity> = BaseProps & {\n otherwise?: TRoute;\n} & (IsPartial<RouteParams<TRoute>> extends true\n ? {\n params?: Maybe<RouteParams<TRoute>>;\n }\n : {\n params: RouteParams<TRoute>;\n });\n\ntype PropsWithDefaultUrl = BaseProps & {\n otherwise?: string;\n};\n\nexport type RouteViewGroupProps<TRoute extends AnyRouteEntity> =\n | PropsWithDefaultRoute<TRoute>\n | PropsWithDefaultUrl;\n\ntype RouteViewGroupComponent = <TRoute extends AnyRouteEntity>(\n props: RouteViewGroupProps<TRoute>,\n) => React.ReactNode;\n\nexport const RouteViewGroup = observer(\n <TRoute extends AnyRouteEntity>({\n children,\n layout: Layout,\n otherwise: otherwiseNavigation,\n useLastOpened,\n // @ts-expect-error\n params,\n ...navigateParams\n }: RouteViewGroupProps<TRoute>) => {\n let activeChildRouteNode: React.ReactNode = null;\n let lastInactiveChildNode: React.ReactNode = null;\n let hasRoutesInOpening = false;\n\n const childNodes: React.ReactNode[] = Array.isArray(children)\n ? children\n : [children];\n\n for (const childNode of childNodes) {\n const isRouteChild =\n isValidElement(childNode) &&\n // @ts-expect-error redundand checks better to wrap in this directive\n isRouteEntity(childNode.props?.route);\n\n if (isRouteChild) {\n const route = (childNode.props as any).route as AnyRoute;\n\n if (route.isOpened) {\n activeChildRouteNode = childNode;\n if (!useLastOpened) {\n break;\n }\n } else {\n if (route.isOpening) {\n hasRoutesInOpening = true;\n }\n lastInactiveChildNode = childNode;\n }\n } else {\n lastInactiveChildNode = childNode;\n }\n }\n\n const hasActiveChildNode = !!activeChildRouteNode;\n\n useEffect(() => {\n if (!hasActiveChildNode && !hasRoutesInOpening && otherwiseNavigation) {\n if (typeof otherwiseNavigation === 'string') {\n const history = routeConfig.get().history;\n const url = `${otherwiseNavigation}${buildSearchString(navigateParams.query || {})}`;\n\n if (navigateParams.replace) {\n history.replace(url, navigateParams.state);\n } else {\n history.push(url, navigateParams.state);\n }\n } else if (!otherwiseNavigation.isOpened) {\n otherwiseNavigation.open(params, navigateParams);\n }\n }\n }, [hasActiveChildNode, hasRoutesInOpening, otherwiseNavigation]);\n\n if (otherwiseNavigation && !activeChildRouteNode) {\n return null;\n }\n\n const resultNodeToRender =\n activeChildRouteNode ?? lastInactiveChildNode ?? null;\n\n if (Layout) {\n return <Layout>{resultNodeToRender}</Layout>;\n }\n\n return resultNodeToRender;\n },\n) as unknown as RouteViewGroupComponent;\n"],"names":["navigateParams","href","query"],"mappings":";;;;;AAkDO,MAAM,OAAO;AAAA,EAClB,WAQE,CAAC,OAAO,QAAQ;AAChB,UAAM;AAAA,MACJ;AAAA,MACA,MAAM;AAAA,MACN;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA;AAAA,MAEA;AAAA,MACA;AAAA,MACA;AAAA;AAAA,MAEA,GAAG;AAAA,IAAA,IACD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26 +8,45 @@ const annotations = [
|
|
|
8
8
|
[mobx.computed, "query"]
|
|
9
9
|
];
|
|
10
10
|
class RouteViewModel extends mobxViewModel.ViewModelBase {
|
|
11
|
+
/**
|
|
12
|
+
* Caches the latest known route params.
|
|
13
|
+
*/
|
|
11
14
|
lastPayload = {};
|
|
12
15
|
constructor(params) {
|
|
13
16
|
super(params);
|
|
14
17
|
mobxViewModel.applyObservable(this, annotations, this.vmConfig.observable.viewModels);
|
|
15
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* Current route params with fallback to the last cached value.
|
|
21
|
+
*/
|
|
16
22
|
get payload() {
|
|
17
23
|
if ("params" in this.route && this.route.params != null) {
|
|
18
24
|
this.lastPayload = this.route.params;
|
|
19
25
|
}
|
|
20
26
|
return this.lastPayload;
|
|
21
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Current query params from route or global route config.
|
|
30
|
+
*
|
|
31
|
+
* [**Documentation**](https://js2me.github.io/mobx-route/view-model/RouteViewModel#query)
|
|
32
|
+
*/
|
|
22
33
|
get query() {
|
|
23
34
|
if ("query" in this.route) {
|
|
24
35
|
return this.route.query;
|
|
25
36
|
}
|
|
26
37
|
return mobxRoute.routeConfig.get().queryParams;
|
|
27
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Alias for `payload`.
|
|
41
|
+
*
|
|
42
|
+
* [**Documentation**](https://js2me.github.io/mobx-route/view-model/RouteViewModel#pathparams)
|
|
43
|
+
*/
|
|
28
44
|
get pathParams() {
|
|
29
45
|
return this.payload;
|
|
30
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Mounted state including the route opened status.
|
|
49
|
+
*/
|
|
31
50
|
get isMounted() {
|
|
32
51
|
return super.isMounted && this.route.isOpened;
|
|
33
52
|
}
|
package/view-model.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"view-model.cjs","sources":["../src/view-model/route-view-model.ts"],"sourcesContent":["import { computed } from 'mobx';\nimport {\n type AnyAbstractRouteEntity,\n type IQueryParams,\n type RouteParams,\n routeConfig,\n} from 'mobx-route';\nimport {\n applyObservable,\n ViewModelBase,\n type ViewModelParams,\n} from 'mobx-view-model';\nimport type { ObservableAnnotationsArray } from 'yummies/mobx';\nimport type { EmptyObject } from 'yummies/types';\n\nconst annotations: ObservableAnnotationsArray<RouteViewModel<any>> = [\n [computed.struct, 'pathParams'],\n [computed, 'query'],\n];\n\nexport abstract class RouteViewModel<\n TRoute extends AnyAbstractRouteEntity = AnyAbstractRouteEntity,\n> extends ViewModelBase<EmptyObject> {\n abstract readonly route: TRoute;\n protected lastPayload: RouteParams<TRoute> = {} as any;\n\n constructor(params: ViewModelParams<EmptyObject, any>) {\n super(params);\n\n applyObservable(this, annotations, this.vmConfig.observable.viewModels);\n }\n\n override get payload(): RouteParams<TRoute> {\n if ('params' in this.route && this.route.params != null) {\n this.lastPayload = this.route.params as RouteParams<TRoute>;\n }\n\n return this.lastPayload;\n }\n\n get query(): IQueryParams {\n if ('query' in this.route) {\n return this.route.query as IQueryParams;\n }\n\n return routeConfig.get().queryParams;\n }\n\n get pathParams() {\n return this.payload;\n }\n\n get isMounted() {\n return super.isMounted && this.route.isOpened;\n }\n}\n"],"names":["computed","ViewModelBase","applyObservable","routeConfig"],"mappings":";;;;;AAeA,MAAM,cAA+D;AAAA,EACnE,CAACA,KAAAA,SAAS,QAAQ,YAAY;AAAA,EAC9B,CAACA,KAAAA,UAAU,OAAO;AACpB;AAEO,MAAe,uBAEZC,cAAAA,cAA2B;AAAA,
|
|
1
|
+
{"version":3,"file":"view-model.cjs","sources":["../src/view-model/route-view-model.ts"],"sourcesContent":["import { computed } from 'mobx';\nimport {\n type AnyAbstractRouteEntity,\n type IQueryParams,\n type RouteParams,\n routeConfig,\n} from 'mobx-route';\nimport {\n applyObservable,\n ViewModelBase,\n type ViewModelParams,\n} from 'mobx-view-model';\nimport type { ObservableAnnotationsArray } from 'yummies/mobx';\nimport type { EmptyObject } from 'yummies/types';\n\nconst annotations: ObservableAnnotationsArray<RouteViewModel<any>> = [\n [computed.struct, 'pathParams'],\n [computed, 'query'],\n];\n\nexport abstract class RouteViewModel<\n TRoute extends AnyAbstractRouteEntity = AnyAbstractRouteEntity,\n> extends ViewModelBase<EmptyObject> {\n /**\n * Route entity bound to this view model.\n *\n * [**Documentation**](https://js2me.github.io/mobx-route/view-model/RouteViewModel#route)\n */\n abstract readonly route: TRoute;\n /**\n * Caches the latest known route params.\n */\n protected lastPayload: RouteParams<TRoute> = {} as any;\n\n constructor(params: ViewModelParams<EmptyObject, any>) {\n super(params);\n\n applyObservable(this, annotations, this.vmConfig.observable.viewModels);\n }\n\n /**\n * Current route params with fallback to the last cached value.\n */\n override get payload(): RouteParams<TRoute> {\n if ('params' in this.route && this.route.params != null) {\n this.lastPayload = this.route.params as RouteParams<TRoute>;\n }\n\n return this.lastPayload;\n }\n\n /**\n * Current query params from route or global route config.\n *\n * [**Documentation**](https://js2me.github.io/mobx-route/view-model/RouteViewModel#query)\n */\n get query(): IQueryParams {\n if ('query' in this.route) {\n return this.route.query as IQueryParams;\n }\n\n return routeConfig.get().queryParams;\n }\n\n /**\n * Alias for `payload`.\n *\n * [**Documentation**](https://js2me.github.io/mobx-route/view-model/RouteViewModel#pathparams)\n */\n get pathParams() {\n return this.payload;\n }\n\n /**\n * Mounted state including the route opened status.\n */\n get isMounted() {\n return super.isMounted && this.route.isOpened;\n }\n}\n"],"names":["computed","ViewModelBase","applyObservable","routeConfig"],"mappings":";;;;;AAeA,MAAM,cAA+D;AAAA,EACnE,CAACA,KAAAA,SAAS,QAAQ,YAAY;AAAA,EAC9B,CAACA,KAAAA,UAAU,OAAO;AACpB;AAEO,MAAe,uBAEZC,cAAAA,cAA2B;AAAA;AAAA;AAAA;AAAA,EAUzB,cAAmC,CAAA;AAAA,EAE7C,YAAY,QAA2C;AACrD,UAAM,MAAM;AAEZC,kBAAAA,gBAAgB,MAAM,aAAa,KAAK,SAAS,WAAW,UAAU;AAAA,EACxE;AAAA;AAAA;AAAA;AAAA,EAKA,IAAa,UAA+B;AAC1C,QAAI,YAAY,KAAK,SAAS,KAAK,MAAM,UAAU,MAAM;AACvD,WAAK,cAAc,KAAK,MAAM;AAAA,IAChC;AAEA,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,IAAI,QAAsB;AACxB,QAAI,WAAW,KAAK,OAAO;AACzB,aAAO,KAAK,MAAM;AAAA,IACpB;AAEA,WAAOC,UAAAA,YAAY,MAAM;AAAA,EAC3B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,IAAI,aAAa;AACf,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,YAAY;AACd,WAAO,MAAM,aAAa,KAAK,MAAM;AAAA,EACvC;AACF;;"}
|
package/view-model.d.ts
CHANGED
|
@@ -3,12 +3,36 @@ import { ViewModelBase, ViewModelParams } from 'mobx-view-model';
|
|
|
3
3
|
import { EmptyObject } from 'yummies/types';
|
|
4
4
|
|
|
5
5
|
declare abstract class RouteViewModel<TRoute extends AnyAbstractRouteEntity = AnyAbstractRouteEntity> extends ViewModelBase<EmptyObject> {
|
|
6
|
+
/**
|
|
7
|
+
* Route entity bound to this view model.
|
|
8
|
+
*
|
|
9
|
+
* [**Documentation**](https://js2me.github.io/mobx-route/view-model/RouteViewModel#route)
|
|
10
|
+
*/
|
|
6
11
|
abstract readonly route: TRoute;
|
|
12
|
+
/**
|
|
13
|
+
* Caches the latest known route params.
|
|
14
|
+
*/
|
|
7
15
|
protected lastPayload: RouteParams<TRoute>;
|
|
8
16
|
constructor(params: ViewModelParams<EmptyObject, any>);
|
|
17
|
+
/**
|
|
18
|
+
* Current route params with fallback to the last cached value.
|
|
19
|
+
*/
|
|
9
20
|
get payload(): RouteParams<TRoute>;
|
|
21
|
+
/**
|
|
22
|
+
* Current query params from route or global route config.
|
|
23
|
+
*
|
|
24
|
+
* [**Documentation**](https://js2me.github.io/mobx-route/view-model/RouteViewModel#query)
|
|
25
|
+
*/
|
|
10
26
|
get query(): IQueryParams;
|
|
27
|
+
/**
|
|
28
|
+
* Alias for `payload`.
|
|
29
|
+
*
|
|
30
|
+
* [**Documentation**](https://js2me.github.io/mobx-route/view-model/RouteViewModel#pathparams)
|
|
31
|
+
*/
|
|
11
32
|
get pathParams(): RouteParams<TRoute>;
|
|
33
|
+
/**
|
|
34
|
+
* Mounted state including the route opened status.
|
|
35
|
+
*/
|
|
12
36
|
get isMounted(): boolean;
|
|
13
37
|
}
|
|
14
38
|
|
package/view-model.js
CHANGED
|
@@ -6,26 +6,45 @@ const annotations = [
|
|
|
6
6
|
[computed, "query"]
|
|
7
7
|
];
|
|
8
8
|
class RouteViewModel extends ViewModelBase {
|
|
9
|
+
/**
|
|
10
|
+
* Caches the latest known route params.
|
|
11
|
+
*/
|
|
9
12
|
lastPayload = {};
|
|
10
13
|
constructor(params) {
|
|
11
14
|
super(params);
|
|
12
15
|
applyObservable(this, annotations, this.vmConfig.observable.viewModels);
|
|
13
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Current route params with fallback to the last cached value.
|
|
19
|
+
*/
|
|
14
20
|
get payload() {
|
|
15
21
|
if ("params" in this.route && this.route.params != null) {
|
|
16
22
|
this.lastPayload = this.route.params;
|
|
17
23
|
}
|
|
18
24
|
return this.lastPayload;
|
|
19
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Current query params from route or global route config.
|
|
28
|
+
*
|
|
29
|
+
* [**Documentation**](https://js2me.github.io/mobx-route/view-model/RouteViewModel#query)
|
|
30
|
+
*/
|
|
20
31
|
get query() {
|
|
21
32
|
if ("query" in this.route) {
|
|
22
33
|
return this.route.query;
|
|
23
34
|
}
|
|
24
35
|
return routeConfig.get().queryParams;
|
|
25
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Alias for `payload`.
|
|
39
|
+
*
|
|
40
|
+
* [**Documentation**](https://js2me.github.io/mobx-route/view-model/RouteViewModel#pathparams)
|
|
41
|
+
*/
|
|
26
42
|
get pathParams() {
|
|
27
43
|
return this.payload;
|
|
28
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* Mounted state including the route opened status.
|
|
47
|
+
*/
|
|
29
48
|
get isMounted() {
|
|
30
49
|
return super.isMounted && this.route.isOpened;
|
|
31
50
|
}
|
package/view-model.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"view-model.js","sources":["../src/view-model/route-view-model.ts"],"sourcesContent":["import { computed } from 'mobx';\nimport {\n type AnyAbstractRouteEntity,\n type IQueryParams,\n type RouteParams,\n routeConfig,\n} from 'mobx-route';\nimport {\n applyObservable,\n ViewModelBase,\n type ViewModelParams,\n} from 'mobx-view-model';\nimport type { ObservableAnnotationsArray } from 'yummies/mobx';\nimport type { EmptyObject } from 'yummies/types';\n\nconst annotations: ObservableAnnotationsArray<RouteViewModel<any>> = [\n [computed.struct, 'pathParams'],\n [computed, 'query'],\n];\n\nexport abstract class RouteViewModel<\n TRoute extends AnyAbstractRouteEntity = AnyAbstractRouteEntity,\n> extends ViewModelBase<EmptyObject> {\n abstract readonly route: TRoute;\n protected lastPayload: RouteParams<TRoute> = {} as any;\n\n constructor(params: ViewModelParams<EmptyObject, any>) {\n super(params);\n\n applyObservable(this, annotations, this.vmConfig.observable.viewModels);\n }\n\n override get payload(): RouteParams<TRoute> {\n if ('params' in this.route && this.route.params != null) {\n this.lastPayload = this.route.params as RouteParams<TRoute>;\n }\n\n return this.lastPayload;\n }\n\n get query(): IQueryParams {\n if ('query' in this.route) {\n return this.route.query as IQueryParams;\n }\n\n return routeConfig.get().queryParams;\n }\n\n get pathParams() {\n return this.payload;\n }\n\n get isMounted() {\n return super.isMounted && this.route.isOpened;\n }\n}\n"],"names":[],"mappings":";;;AAeA,MAAM,cAA+D;AAAA,EACnE,CAAC,SAAS,QAAQ,YAAY;AAAA,EAC9B,CAAC,UAAU,OAAO;AACpB;AAEO,MAAe,uBAEZ,cAA2B;AAAA,
|
|
1
|
+
{"version":3,"file":"view-model.js","sources":["../src/view-model/route-view-model.ts"],"sourcesContent":["import { computed } from 'mobx';\nimport {\n type AnyAbstractRouteEntity,\n type IQueryParams,\n type RouteParams,\n routeConfig,\n} from 'mobx-route';\nimport {\n applyObservable,\n ViewModelBase,\n type ViewModelParams,\n} from 'mobx-view-model';\nimport type { ObservableAnnotationsArray } from 'yummies/mobx';\nimport type { EmptyObject } from 'yummies/types';\n\nconst annotations: ObservableAnnotationsArray<RouteViewModel<any>> = [\n [computed.struct, 'pathParams'],\n [computed, 'query'],\n];\n\nexport abstract class RouteViewModel<\n TRoute extends AnyAbstractRouteEntity = AnyAbstractRouteEntity,\n> extends ViewModelBase<EmptyObject> {\n /**\n * Route entity bound to this view model.\n *\n * [**Documentation**](https://js2me.github.io/mobx-route/view-model/RouteViewModel#route)\n */\n abstract readonly route: TRoute;\n /**\n * Caches the latest known route params.\n */\n protected lastPayload: RouteParams<TRoute> = {} as any;\n\n constructor(params: ViewModelParams<EmptyObject, any>) {\n super(params);\n\n applyObservable(this, annotations, this.vmConfig.observable.viewModels);\n }\n\n /**\n * Current route params with fallback to the last cached value.\n */\n override get payload(): RouteParams<TRoute> {\n if ('params' in this.route && this.route.params != null) {\n this.lastPayload = this.route.params as RouteParams<TRoute>;\n }\n\n return this.lastPayload;\n }\n\n /**\n * Current query params from route or global route config.\n *\n * [**Documentation**](https://js2me.github.io/mobx-route/view-model/RouteViewModel#query)\n */\n get query(): IQueryParams {\n if ('query' in this.route) {\n return this.route.query as IQueryParams;\n }\n\n return routeConfig.get().queryParams;\n }\n\n /**\n * Alias for `payload`.\n *\n * [**Documentation**](https://js2me.github.io/mobx-route/view-model/RouteViewModel#pathparams)\n */\n get pathParams() {\n return this.payload;\n }\n\n /**\n * Mounted state including the route opened status.\n */\n get isMounted() {\n return super.isMounted && this.route.isOpened;\n }\n}\n"],"names":[],"mappings":";;;AAeA,MAAM,cAA+D;AAAA,EACnE,CAAC,SAAS,QAAQ,YAAY;AAAA,EAC9B,CAAC,UAAU,OAAO;AACpB;AAEO,MAAe,uBAEZ,cAA2B;AAAA;AAAA;AAAA;AAAA,EAUzB,cAAmC,CAAA;AAAA,EAE7C,YAAY,QAA2C;AACrD,UAAM,MAAM;AAEZ,oBAAgB,MAAM,aAAa,KAAK,SAAS,WAAW,UAAU;AAAA,EACxE;AAAA;AAAA;AAAA;AAAA,EAKA,IAAa,UAA+B;AAC1C,QAAI,YAAY,KAAK,SAAS,KAAK,MAAM,UAAU,MAAM;AACvD,WAAK,cAAc,KAAK,MAAM;AAAA,IAChC;AAEA,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,IAAI,QAAsB;AACxB,QAAI,WAAW,KAAK,OAAO;AACzB,aAAO,KAAK,MAAM;AAAA,IACpB;AAEA,WAAO,YAAY,MAAM;AAAA,EAC3B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,IAAI,aAAa;AACf,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,YAAY;AACd,WAAO,MAAM,aAAa,KAAK,MAAM;AAAA,EACvC;AACF;"}
|