diff --git a/client-app/src/desktop/AppModel.ts b/client-app/src/desktop/AppModel.ts index c6f3add49..c036c6b7a 100755 --- a/client-app/src/desktop/AppModel.ts +++ b/client-app/src/desktop/AppModel.ts @@ -21,6 +21,7 @@ import {panelsTab} from './tabs/panels/PanelsTab'; import {fmtDateTimeSec} from '@xh/hoist/format'; import {span} from '@xh/hoist/cmp/layout'; import {BaseAppModel} from '../BaseAppModel'; +import {isEmpty} from 'lodash'; export class AppModel extends BaseAppModel { /** Singleton instance reference - installed by XH upon init. */ @@ -47,6 +48,8 @@ export class AppModel extends BaseAppModel { override async initAsync() { await super.initAsync(); await XH.installServicesAsync(GitHubService, PortfolioService); + // Set the queryParamsMode to 'loose' to allow for more flexible URL query parameters. + XH.router.setOption('queryParamsMode', 'loose'); // Demo app-specific handling of EnvironmentService.serverVersion observable. this.addReaction({ @@ -184,6 +187,11 @@ export class AppModel extends BaseAppModel { name: 'simpleRouting', path: '/simpleRouting', children: [{name: 'recordId', path: '/:recordId'}] + }, + { + name: 'advancedRouting', + path: '/advancedRouting', + ...routeParamEncoders } ] }, @@ -210,3 +218,15 @@ export class AppModel extends BaseAppModel { ]; } } + +// Encoding of json route params as base64 +export const routeParamEncoders = { + encodeParams: params => { + if (isEmpty(params)) return {}; + return {q: window.btoa(JSON.stringify(params))}; + }, + decodeParams: params => { + if (!params.q) return {}; + return JSON.parse(window.atob(params.q)); + } +}; diff --git a/client-app/src/desktop/tabs/other/OtherTab.ts b/client-app/src/desktop/tabs/other/OtherTab.ts index 873c5a7c0..bf1cac3e2 100644 --- a/client-app/src/desktop/tabs/other/OtherTab.ts +++ b/client-app/src/desktop/tabs/other/OtherTab.ts @@ -18,6 +18,7 @@ import {placeholderPanel} from './PlaceholderPanel'; import {popupsPanel} from './PopupsPanel'; import {relativeTimestampPanel} from './relativetimestamp/RelativeTimestampPanel'; import {simpleRoutingPanel} from './routing/SimpleRoutingPanel'; +import {advancedRoutingPanel} from './routing/AdvancedRoutingPanel'; export const otherTab = hoistCmp.factory(() => tabContainer({ @@ -45,8 +46,9 @@ export const otherTab = hoistCmp.factory(() => {id: 'pinPad', title: 'PIN Pad', content: pinPadPanel}, {id: 'placeholder', title: 'Placeholder', content: placeholderPanel}, {id: 'popups', content: popupsPanel}, - {id: 'timestamp', content: relativeTimestampPanel}, - {id: 'simpleRouting', content: simpleRoutingPanel} + {id: 'simpleRouting', title: 'Routing (Simple)', content: simpleRoutingPanel}, + {id: 'advancedRouting', title: 'Routing (Advanced)', content: advancedRoutingPanel}, + {id: 'timestamp', content: relativeTimestampPanel} ] }, className: 'toolbox-tab' diff --git a/client-app/src/desktop/tabs/other/routing/AdvancedRoutingPanel.tsx b/client-app/src/desktop/tabs/other/routing/AdvancedRoutingPanel.tsx new file mode 100644 index 000000000..1afa6c186 --- /dev/null +++ b/client-app/src/desktop/tabs/other/routing/AdvancedRoutingPanel.tsx @@ -0,0 +1,203 @@ +import {grid, GridModel} from '@xh/hoist/cmp/grid'; +import {span} from '@xh/hoist/cmp/layout'; +import {creates, hoistCmp, HoistModel, XH} from '@xh/hoist/core'; +import {select, switchInput} from '@xh/hoist/desktop/cmp/input'; +import {panel} from '@xh/hoist/desktop/cmp/panel'; +import {action, makeObservable, observable} from '@xh/hoist/mobx'; +import React from 'react'; +import {State} from 'router5'; +import {wrapper} from '../../../common'; + +export const advancedRoutingPanel = hoistCmp.factory({ + displayName: 'AdvancedRoutingPanel', + model: creates(() => new AdvancedRoutingPanelModel()), + + render({model}) { + return wrapper({ + description: [ +
+ This example demonstrates how to use URL route parameters to store and restore + the state of a component. The state of the grid (grouping, sorting, and selected + record) is stored in the URL, and the state is restored when the URL is + revisited. +
, +
+ Hoist applications are able to navigate to a specific URL and specify whether or
+ not to push onto the route history. In this example, selecting individual
+ records in the grid will not save the URL to the route history, but changing the{' '}
+ groupBy
or sortBy
fields will. Hoist also provides the
+ ability to prevent route deactivation, allowing the developer to present the
+ user with a pop-up before navigating away from the current route.
+
+ The state is encoded in the URL as a base64
string, which is then
+ decoded and parsed to restore the state.
+
+ The current state encoding is:
+
+ groupBy: {XH.routerState.params.groupBy || 'None'}
+
+ sortBy: {XH.routerState.params.sortBy || 'None'}
+
+ selectedId: {XH.routerState.params.selectedId || 'None'}
+
+