Skip to content

Commit

Permalink
table poc
Browse files Browse the repository at this point in the history
  • Loading branch information
Dosant committed Oct 1, 2020
1 parent d8ded4d commit e18ebbe
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,19 @@ export class UrlDrilldown implements Drilldown<Config, UrlTrigger, ActionFactory
config: Config,
context: ActionFactoryContext
): config is Config => {
const { isValid } = urlDrilldownValidateUrlTemplate(config.url, this.buildEditorScope(context));
const { isValid } = urlDrilldownValidateUrlTemplate(
config.url,
this.buildEditorScope(context),
{ validateVariables: false }
);
return isValid;
};

public readonly isCompatible = async (config: Config, context: ActionContext) => {
const { isValid, error } = urlDrilldownValidateUrlTemplate(
config.url,
await this.buildRuntimeScope(context)
await this.buildRuntimeScope(context),
{ validateVariables: true }
);

if (!isValid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,25 @@ describe('VALUE_CLICK_TRIGGER', () => {
]) as ValueClickTriggerEventScope;
expect(mockEventScope.points.length).toBeGreaterThan(3);
expect(mockEventScope.points).toMatchInlineSnapshot(`
Array [
Object {
"key": "event.points.0.key",
"value": "event.points.0.value",
},
Object {
"key": "event.points.1.key",
"value": "event.points.1.value",
},
Object {
"key": "event.points.2.key",
"value": "event.points.2.value",
},
Object {
"key": "event.points.3.key",
"value": "event.points.3.value",
},
]
`);
Array [
Object {
"key": "event.points.[0].key",
"value": "event.points.[0].value",
},
Object {
"key": "event.points.[1].key",
"value": "event.points.[1].value",
},
Object {
"key": "event.points.[2].key",
"value": "event.points.[2].value",
},
Object {
"key": "event.points.[3].key",
"value": "event.points.[3].value",
},
]
`);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export interface ValueClickTriggerEventScope {
value: Primitive;
negate: boolean;
points: Array<{ key?: string; value: Primitive }>;
row: Primitive[];
}
export interface RangeSelectTriggerEventScope {
key: string;
Expand All @@ -131,7 +132,7 @@ function getEventScopeFromRangeSelectTriggerContext(
const { table, column: columnIndex, range } = eventScopeInput.data;
const column = table.columns[columnIndex];
return cleanEmptyKeys({
key: toPrimitiveOrUndefined(column?.meta?.aggConfigParams?.field) as string,
key: toPrimitiveOrUndefined(column?.meta?.aggConfigParams?.field ?? column?.name) as string,
from: toPrimitiveOrUndefined(range[0]) as string | number | undefined,
to: toPrimitiveOrUndefined(range[range.length - 1]) as string | number | undefined,
});
Expand All @@ -141,19 +142,29 @@ function getEventScopeFromValueClickTriggerContext(
eventScopeInput: ValueClickContext
): ValueClickTriggerEventScope {
const negate = eventScopeInput.data.negate ?? false;

const points = eventScopeInput.data.data.map(({ table, value, column: columnIndex }) => {
const column = table.columns[columnIndex];
return {
value: toPrimitiveOrUndefined(value) as Primitive,
key: toPrimitiveOrUndefined(column?.meta?.aggConfigParams?.field) as string | undefined,
key: toPrimitiveOrUndefined(column?.meta?.aggConfigParams?.field ?? column?.name) as
| string
| undefined,
};
});

const dataPoint = eventScopeInput.data.data[0];

const row = dataPoint.table.columns.map(
({ id }) => dataPoint.table.rows[dataPoint.row][id]
) as Primitive[];

return cleanEmptyKeys({
key: points[0]?.key,
value: points[0]?.value,
negate,
points,
row,
});
}

Expand All @@ -174,14 +185,19 @@ export function getMockEventScope([trigger]: UrlTrigger[]): UrlDrilldownEventSco
// should be larger or equal of any possible data points length emitted by VALUE_CLICK_TRIGGER
const nPoints = 4;
const points = new Array(nPoints).fill(0).map((_, index) => ({
key: `event.points.${index}.key`,
value: `event.points.${index}.value`,
key: `event.points.[${index}].key`,
value: `event.points.[${index}].value`,
}));

// number of mock columns to generate
const nColumns = 4;
const row = new Array(nColumns).fill(0).map((_, index) => `event.row.[${index}]`);
return {
key: `event.key`,
value: `event.value`,
key: points[0].key,
value: points[0].value,
negate: false,
points,
row,
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ handlebars.registerHelper('date', (...args) => {
return format ? momentDate.format(format) : momentDate.toISOString();
});

export function compile(url: string, context: object): string {
const template = handlebars.compile(url, { strict: true, noEscape: true });
export function compile(
url: string,
context: object,
{ strict = true }: { strict: boolean } = { strict: true }
): string {
const template = handlebars.compile(url, { strict, noEscape: true });
return encodeURI(template(context));
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ export function validateUrl(url: string): { isValid: boolean; error?: string } {

export function validateUrlTemplate(
urlTemplate: UrlDrilldownConfig['url'],
scope: UrlDrilldownScope
scope: UrlDrilldownScope,
{ validateVariables = true }: { validateVariables: boolean } = { validateVariables: true }
): { isValid: boolean; error?: string } {
if (!urlTemplate.template)
return {
Expand All @@ -60,7 +61,7 @@ export function validateUrlTemplate(
};

try {
const compiledUrl = compile(urlTemplate.template, scope);
const compiledUrl = compile(urlTemplate.template, scope, { strict: validateVariables });
return validateUrl(compiledUrl);
} catch (e) {
return {
Expand Down

0 comments on commit e18ebbe

Please sign in to comment.