import { HashType, History } from 'history'; import { NodeDefinition, StatefulGraphNode, StatefulNodeType } from '../../types/graph'; import { LocationParamsEncoder } from './location-common'; export { LocationParamsEncoder } from './location-common'; /** * An instance of the [[location]] node. * See the [[location]] documentation to find out more. */ export interface LocationNode extends StatefulGraphNode<'location', LocationNodeProperties, LocationNodeState, LocationNodeData> { } /** * A definition of the [[location]] node. * See the [[location]] documentation to find out more. */ export interface LocationNodeDefinition extends NodeDefinition<'location', LocationNodeProperties, LocationNodeState, LocationNodeData> { } export interface LocationNodeProperties { hash: HashType | undefined; paramsEncoder: LocationParamsEncoder; update: boolean | undefined; } export declare type HistoryWithId = History & { id: string; }; export interface LocationNodeState { currentValue: NodeDefinition; history: HistoryWithId; } export interface LocationNodeData { unsubscribeEvent: (() => void) | undefined; unsubscribeHistory: (() => void) | undefined; } /** * The implementation of the [[location]] node. * See the [[location]] documentation to learn more. */ export declare const LocationNodeType: StatefulNodeType<'location', LocationNodeProperties, LocationNodeState>; export declare type LocationParamsEncoding = 'base64' | 'json'; export interface LocationOptions { encoding?: LocationParamsEncoding | LocationParamsEncoder; hash?: HashType; update?: boolean; } /** * Creates a new instance of a [[location]] node, which is a type of a [[NodeDefinition]] used when accessing browser location. * This node allows for reading/writing to the address bar, and can be used to implement custom routing mechanism. * The path can be encoded using following formats: * - slash: #/home * - noslash: #home * - hashbang: #!/home * * Additionally, the node allows storing parameters both as URL encoded values, and as JSON serialized objects. * @returns {LocationNodeDefinition} * * * @example **Get current location** * ```js * import muster, { location, ref } from '@dws/muster'; * * const app = muster({ * navigation: location(), * }); * * // Given a URL: #/ * await app.resolve(ref('navigation')); * // === { path: '/', params: {} } * * // Given a URL: #/home?showWelcome=true * await app.resolve(ref('navigation')); * // === { path: '/home', params: { showWelcome: 'true' } } * ``` * This example shows how to get current path with parameters as a combined object. * * * @example **Set current location** * ```js * import muster, { location, ref, set } from '@dws/muster'; * * const app = muster({ * navigation: location(), * }); * * // Given a URL: #/home * await app.resolve(set(ref('navigation'), { path: '/user', params: { id: 10 } })); * // URL after set: #/user?id=10 * ``` * This example shows how to set the current location to a new value. * * * @example **Get current path** * ```js * import muster, { location, ref } from '@dws/muster'; * * const app = muster({ * navigation: location(), * }); * * // Given a URL: #/home?test=value * await app.resolve(ref('navigation', 'path')); * // === '/home' * ``` * This example shows how to get only the path part of the URL. Internally the 'path' is handled by the * [[locationPath]] node. * * * @example **Set current path** * ```js * import muster, { location, ref, set } from '@dws/muster'; * * const app = muster({ * navigation: location(), * }); * * // Given a URL: #/home?id=12 * await app.resolve(set(ref('navigation', 'path'), '/user')); * // URL after set: #/user?id=12 * ``` * This example shows how to set only the path without overwriting path params. Internally the 'params' is handled * by the [[locationPath]] node. * * * @example **Get current params** * ```js * import muster, { location, ref } from '@dws/muster'; * * const app = muster({ * navigation: location(), * }); * * // Given a URL: #/home?id=12 * await app.resolve(ref('navigation', 'params')); * // === { id: '12' } * ``` * This example shows how to get the current path parameters. Internally the 'params' is handled by the * [[locationData]] node. * * * @example **Set current params** * ```js * import muster, { location, ref, set } from '@dws/muster'; * * const app = muster({ * navigation: location(), * }); * * // Given a URL: #/home?id=12 * await app.resolve(set(ref('navigation', 'params'), { test: 'value' })); * // URL after set: #/home?test=value * ``` * This example shows how to set the path parameters without overwriting the path. Internally the 'params' is handled * by the [[locationData]] node. */ export declare function location(options?: LocationOptions): NodeDefinition; export declare function isLocationNodeDefinition(value: NodeDefinition): value is LocationNodeDefinition;