Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add filter option to show() and showMap() #12

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
### Added
- `filter` option for `CadenzaClient#show` and `CadenzaClient#showMap`
jkissel marked this conversation as resolved.
Show resolved Hide resolved

## 1.6.0 - 2023-11-17
### Changed
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,17 @@ const cadenza = window.cadenza.noConflict();

<small>API: [CadenzaClient#show](./classes/CadenzaClient.html#show)</small>

Show an embedding target in an iframe and hide Cadenza's main header and footer as well as the workbook toolbar. Additionally, enable simplified operation mode and disable the designer.
Show an embedding target in an iframe and hide Cadenza's main header and footer as well as the workbook toolbar. Additionally, enable simplified operation mode, disable the designer and set the filter variable "var1" to "foo".

```javascript
cadenzaClient.show('{embeddingTargetId}', {
hideMainHeaderAndFooter: true,
hideWorkbookToolBar: true,
operationMode: 'simplified',
disabledUiFeatures: ['workbook-design']
disabledUiFeatures: ['workbook-design'],
filter: {
var1: 'foo'
}
});
```

Expand Down
22 changes: 20 additions & 2 deletions sandbox.html
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@
minScale,
parts,
simplifiedOperationMode,
useMapSrs
useMapSrs,
filter
}) {
return {
disabledUiFeatures: disabledUiFeatures && disabledUiFeatures.split(','),
Expand All @@ -145,9 +146,22 @@
minScale,
parts: parts && parts.split(','),
...(simplifiedOperationMode === 'on' && { operationMode: 'simplified' }),
useMapSrs: useMapSrs === 'on'
useMapSrs: useMapSrs === 'on',
filter: filter && parseFilterVariables(filter)
};
}

function parseFilterVariables (filterString) {
return JSON.parse(filterString, (_, value) => {
if (value === '{{currentDate}}') { // convenience for current date
return new Date();
}
if (value.match?.(/....-..-..T..:..:..\....Z/)) {
return new Date(value);
}
return value;
});
}
jkissel marked this conversation as resolved.
Show resolved Hide resolved
</script>
</head>
<body>
Expand Down Expand Up @@ -212,6 +226,10 @@
<label for="disabledUiFeatures">Disabled UI features</label>
<input name="disabledUiFeatures" id="disabledUiFeatures" placeholder="feature1,feature2...">
</div>
<div>
<label for="filter">Filter</label>
<input name="filter" id="filter" placeholder='{ "textvar": "foo", "numbervar": 42.0, "datevar1": "2000-01-01T00.000Z", "datevar2": "{{currentDate}}" }'>
</div>
</template>

<template id="common-map">
Expand Down
35 changes: 32 additions & 3 deletions src/cadenza.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ globalThis.cadenza = Object.assign(
*/
/** @typedef {'columns' | 'values' | 'totals'} TablePart - A part of a table to export. */

/**
* @typedef {Record<string, string | number | Date>} FilterVariables - Filter variable values
*/
/**
* _Notes:_
* * Most public methods can be aborted using an [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal).
Expand Down Expand Up @@ -182,6 +185,8 @@ export class CadenzaClient {
* @param {object} [options]
* @param {UiFeature[]} [options.disabledUiFeatures] - Cadenza UI features to disable
* @param {boolean} [options.expandNavigator] - Indicates if the navigator should be expanded.
* @param {FilterVariables} [options.filter] - Filter variables. The keys should contain the
* filter variable names and the values the filter variable values.
* @param {boolean} [options.hideMainHeaderAndFooter] - Whether to hide the main Cadenza header and footer
* @param {boolean} [options.hideWorkbookToolBar] - Whether to hide the workbook toolbar
* @param {GlobalId} [options.highlightGlobalId] - The ID of an item to highlight / expand in the navigator
Expand All @@ -198,6 +203,7 @@ export class CadenzaClient {
{
disabledUiFeatures,
expandNavigator,
filter,
hideMainHeaderAndFooter,
hideWorkbookToolBar,
highlightGlobalId,
Expand All @@ -222,6 +228,7 @@ export class CadenzaClient {
const params = createParams({
disabledUiFeatures,
expandNavigator,
filter,
hideMainHeaderAndFooter,
hideWorkbookToolBar,
highlightGlobalId,
Expand All @@ -240,6 +247,8 @@ export class CadenzaClient {
* @param {object} [options] - Options
* @param {UiFeature[]} [options.disabledUiFeatures] - Cadenza UI features to disable
* @param {boolean} [options.expandNavigator] - Indicates if the navigator should be expanded.
* @param {FilterVariables} [options.filter] - Filter variables. The keys should contain the
* filter variable names and the values the filter variable values.
* @param {Geometry} [options.geometry] - A geometry to show on the map
* @param {boolean} [options.hideMainHeaderAndFooter] - Whether to hide the main Cadenza header and footer
* @param {boolean} [options.hideWorkbookToolBar] - Whether to hide the workbook toolbar
Expand All @@ -257,6 +266,7 @@ export class CadenzaClient {
{
disabledUiFeatures,
expandNavigator,
filter,
geometry,
hideMainHeaderAndFooter,
hideWorkbookToolBar,
Expand All @@ -275,6 +285,7 @@ export class CadenzaClient {
const params = createParams({
disabledUiFeatures,
expandNavigator,
filter,
hideMainHeaderAndFooter,
hideWorkbookToolBar,
highlightGlobalId,
Expand Down Expand Up @@ -584,7 +595,7 @@ function resolvePath(
) {
if (typeof source === 'string') {
assert(
validEmbeddingTargetId(source),
validKebabCaseString(source),
`Invalid embedding target ID: ${source}`,
);
return `/w/${source}`;
Expand Down Expand Up @@ -636,7 +647,7 @@ function validPageName(/** @type string */ value) {
return ['welcome'].includes(value);
}

function validEmbeddingTargetId(/** @type string */ value) {
function validKebabCaseString(/** @type string */ value) {
return /^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(value);
}

Expand Down Expand Up @@ -709,6 +720,8 @@ function assertSupportedMediaType(
* @param {UiFeature[]} [params.disabledUiFeatures]
* @param {boolean} [params.expandNavigator]
* @param {string} [params.fileName]
* @param {FilterVariables} [params.filter] - Filter variables. The keys should contain the filter
* variable names and the values the filter variable values.
* @param {GeometryType} [params.geometryType]
* @param {boolean} [params.hideMainHeaderAndFooter]
* @param {boolean} [params.hideWorkbookToolBar]
Expand All @@ -726,8 +739,10 @@ function assertSupportedMediaType(
*/
function createParams({
action,
disabledUiFeatures,
expandNavigator,
fileName,
filter,
geometryType,
hideMainHeaderAndFooter,
hideWorkbookToolBar,
Expand All @@ -741,7 +756,6 @@ function createParams({
useMapSrs,
webApplication,
operationMode,
disabledUiFeatures,
}) {
if (disabledUiFeatures) {
disabledUiFeatures.forEach((feature) =>
Expand All @@ -762,6 +776,14 @@ function createParams({
assert(validTablePart(part), `Invalid table part: ${part}`),
);
}
if (filter) {
Object.keys(filter).forEach((varName) =>
assert(
validKebabCaseString(varName),
`Invalid filter variable name: ${varName}`,
),
);
}
return new URLSearchParams({
...(action && { action }),
...(disabledUiFeatures && {
Expand All @@ -785,6 +807,13 @@ function createParams({
webApplicationLink: webApplication.externalLinkId,
webApplicationLinkRepository: webApplication.repositoryName,
}),
...(filter &&
Object.fromEntries(
Object.entries(filter).map(([variable, value]) => [
`filter.${variable}`,
JSON.stringify(value instanceof Date ? value.toISOString() : value),
]),
)),
});
}

Expand Down