Skip to content

Commit

Permalink
Changes to Facet Slider to handle simpler config with non-automatic r…
Browse files Browse the repository at this point in the history
  • Loading branch information
olamothe committed May 5, 2017
1 parent b6a93bb commit 13ab55a
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 52 deletions.
89 changes: 46 additions & 43 deletions src/controllers/FacetSliderQueryController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,12 @@ export class FacetSliderQueryController {
this.putGroupByForSliderIntoQueryBuilder(queryBuilder);
}

public createBasicGroupByRequest(allowedValues?: string[], addComputedField: boolean = true) {
private createBasicGroupByRequest() {
let groupByQuery: IGroupByRequest = {
field: <string>this.facet.options.field,
completeFacetWithStandardValues: true
completeFacetWithStandardValues: true,
allowedValues: undefined
};
groupByQuery.allowedValues = undefined;
if (this.facet.options.graph) {
groupByQuery = this.buildGroupByQueryForSlider(groupByQuery);
} else {
groupByQuery = this.buildGroupByQueryForAutomaticRanges(groupByQuery);
}
return groupByQuery;
}

Expand Down Expand Up @@ -124,38 +119,17 @@ export class FacetSliderQueryController {
}
}

private buildGroupByQueryForSlider(groupByQuery: IGroupByRequest) {
if (this.facet.options.start != undefined && this.facet.options.end != undefined) {
let start = this.facet.options.start;
let end = this.facet.options.end;
if (this.facet.options.dateField || this.facet.options.dateField) {
start = this.getISOFormat(start);
end = this.getISOFormat(end);
}
groupByQuery.rangeValues = [{
start: start,
end: end,
endInclusive: true,
label: 'Slider'
}];
return groupByQuery;
} else {
return this.buildGroupByQueryForAutomaticRanges(groupByQuery);
}
}

private buildGroupByQueryForAutomaticRanges(groupByQuery: IGroupByRequest) {
groupByQuery.generateAutomaticRanges = true;
return groupByQuery;
}

private putGroupByForGraphIntoQueryBuilder(queryBuilder: QueryBuilder) {
this.graphGroupByQueriesIndex = queryBuilder.groupByRequests.length;
let basicGroupByRequestForGraph = this.createBasicGroupByRequest();
if (basicGroupByRequestForGraph.rangeValues) {
let basicRangeRequest = basicGroupByRequestForGraph.rangeValues[0];
basicGroupByRequestForGraph.rangeValues = this.createRangeValuesForGraph(basicRangeRequest);

if (this.facet.isSimpleSliderConfig()) {
basicGroupByRequestForGraph.rangeValues = this.createRangeValuesForGraphUsingStartAndEnd();
basicGroupByRequestForGraph.generateAutomaticRanges = false;
} else {
basicGroupByRequestForGraph.generateAutomaticRanges = true;
}

let filter = this.computeOurFilterExpression(this.facet.getSliderBoundaryForQuery());
if (filter != undefined) {
let queryOverrideObject = queryBuilder.computeCompleteExpressionPartsExcept(filter);
Expand All @@ -179,16 +153,45 @@ export class FacetSliderQueryController {

private putGroupByForSliderIntoQueryBuilder(queryBuilder: QueryBuilder) {
this.lastGroupByRequestIndex = queryBuilder.groupByRequests.length;
let basicGroupByRequestForSlider = this.createBasicGroupByRequest();
basicGroupByRequestForSlider.maximumNumberOfValues = this.facet.options.graph != null ? this.facet.options.graph.steps || 1 : 1;

let maximumNumberOfValues = 1;
if (this.facet.hasAGraph()) {
maximumNumberOfValues = this.facet.options.graph.steps;
}

let rangeValues = undefined;
if (this.facet.isSimpleSliderConfig()) {
rangeValues = [{
start: this.facet.options.start,
end: this.facet.options.end,
label: 'slider',
endInclusive: false
}];
}

const basicGroupByRequestForSlider = this.createBasicGroupByRequest();
basicGroupByRequestForSlider.maximumNumberOfValues = maximumNumberOfValues;
basicGroupByRequestForSlider.queryOverride = this.facet.options.queryOverride || '@uri';
basicGroupByRequestForSlider.sortCriteria = 'nosort';
basicGroupByRequestForSlider.generateAutomaticRanges = true;
basicGroupByRequestForSlider.rangeValues = undefined;
basicGroupByRequestForSlider.generateAutomaticRanges = !this.facet.isSimpleSliderConfig();
basicGroupByRequestForSlider.rangeValues = rangeValues;
queryBuilder.groupByRequests.push(basicGroupByRequestForSlider);
}

private createRangeValuesForGraph(basicRangeRequest: IRangeValue) {
private createRangeValuesForGraphUsingStartAndEnd() {
let start = this.facet.options.start;
let end = this.facet.options.end;
if (this.facet.options.dateField) {
start = this.getISOFormat(start);
end = this.getISOFormat(end);
}
let oneRange: IRangeValue = {
start: start,
end: end,
endInclusive: true,
label: 'Slider'
};

if (this.facet.options.graph.steps == undefined) {
this.facet.options.graph.steps = 10;
}
Expand All @@ -199,9 +202,9 @@ export class FacetSliderQueryController {
this.facet.options.end = new Date(this.facet.options.end).getTime();
}
if (this.rangeValuesForGraphToUse != undefined) {
return this.usePrebuiltRange(basicRangeRequest);
return this.usePrebuiltRange(oneRange);
} else {
return this.buildRange(basicRangeRequest);
return this.buildRange(oneRange);
}
}

Expand Down
24 changes: 16 additions & 8 deletions src/ui/FacetSlider/FacetSlider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { exportGlobally } from '../../GlobalExports';
import { ResponsiveFacetSlider } from '../ResponsiveComponents/ResponsiveFacetSlider';

import 'styling/_FacetSlider';
import { IGroupByResult } from '../../rest/GroupByResult';


export interface IFacetSliderOptions extends ISliderOptions {
Expand Down Expand Up @@ -524,6 +525,14 @@ export class FacetSlider extends Component {
}
}

public isSimpleSliderConfig() {
return this.options.start != null && this.options.end != null;
}

public hasAGraph() {
return this.options.graph != undefined;
}

private handleNoresults(): void {
this.isEmpty = true;
this.updateAppearanceDependingOnState();
Expand Down Expand Up @@ -671,9 +680,7 @@ export class FacetSlider extends Component {
this.ensureDom();
this.setupSliderIfNeeded(data);
let groupByResults = data.results.groupByResults[this.facetQueryController.lastGroupByRequestIndex];
if (groupByResults == undefined || groupByResults.values[0] == undefined) {
this.isEmpty = true;
}
this.isEmpty = this.isFacetEmpty(groupByResults);
this.updateAppearanceDependingOnState();
if (this.hasAGraph()) {
this.renderToSliderGraph(data);
Expand Down Expand Up @@ -769,7 +776,7 @@ export class FacetSlider extends Component {
});
}
if (totalGraphResults == 0) {
this.isEmpty = true;
this.isEmpty = !this.isSimpleSliderConfig();
this.updateAppearanceDependingOnState();
} else if (graphData != undefined && !this.isDropdownHidden()) {
this.slider.drawGraph(graphData);
Expand Down Expand Up @@ -929,10 +936,6 @@ export class FacetSlider extends Component {
}
}

private hasAGraph() {
return this.options.graph != undefined;
}

private updateAppearanceDependingOnState(sliding = false) {
if (this.isEmpty && !this.isActive() && !sliding) {
$$(this.element).addClass('coveo-disabled-empty');
Expand All @@ -954,5 +957,10 @@ export class FacetSlider extends Component {
private handleNuke() {
window.removeEventListener('resize', this.onResize);
}

private isFacetEmpty(groupByResults: IGroupByResult) {
return groupByResults == null || groupByResults.values[0] == null;
}
}

Initialization.registerAutoCreateComponent(FacetSlider);
3 changes: 3 additions & 0 deletions test/Test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,3 +455,6 @@ FormGroupTest();

import { MultiSelectTest } from './ui/MultiSelectTest';
MultiSelectTest();

import { FacetSliderQueryControllerTest } from './controllers/FacetSliderQueryControllerTest';
FacetSliderQueryControllerTest();
164 changes: 164 additions & 0 deletions test/controllers/FacetSliderQueryControllerTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import { FacetSliderQueryController } from '../../src/controllers/FacetSliderQueryController';
import * as Mock from '../MockEnvironment';
import { FacetSlider } from '../../src/ui/FacetSlider/FacetSlider';
import { IFieldOption } from '../../src/ui/Base/ComponentOptions';
import { QueryBuilder } from '../../src/ui/Base/QueryBuilder';

export function FacetSliderQueryControllerTest() {

describe('FacetSliderQueryController', () => {
let facet: FacetSlider;
let controller: FacetSliderQueryController;

beforeEach(() => {
facet = Mock.mockComponent<FacetSlider>(FacetSlider);
facet.options = {};
facet.bind = <any>{};
facet.bind.onRootElement = jasmine.createSpy('root');
facet.options.field = <IFieldOption>'@foo';
facet.options.start = 0;
facet.options.end = 100;
controller = new FacetSliderQueryController(facet);
});

afterEach(() => {
facet = null;
controller = null;
});

it('should allow to compute a filter expression', () => {
let filter = controller.computeOurFilterExpression([5, 99]);
expect(filter).toEqual('@foo==5..99');
});

it('should allow to compute a filter expression when the facet is inactive', () => {
let filter = controller.computeOurFilterExpression([0, 100]);
expect(filter).toBeUndefined();
});

it('should allow to compute a filter expression with outer bounds excluded at the end of the range', () => {
facet.options.excludeOuterBounds = true;
let filter = controller.computeOurFilterExpression([0, 99]);
expect(filter).toEqual('@foo<=99');
});

it('should allow to compute a filter expression with outer bounds excluded at the start of the range', () => {
facet.options.excludeOuterBounds = true;
let filter = controller.computeOurFilterExpression([1, 100]);
expect(filter).toEqual('@foo>=1');
});

it('should allow to compute a filter expression with outer bounds excluded, when both end are active', () => {
facet.options.excludeOuterBounds = true;
let filter = controller.computeOurFilterExpression([1, 99]);
expect(filter).toEqual('@foo==1..99');
});

it('should allow to compute a filter expression with outher bounds excluded as a date', () => {
facet.options.dateField = true;
facet.options.excludeOuterBounds = true;
let filter = controller.computeOurFilterExpression([1, 100]);
expect(filter).toContain(`@foo>="1970`);
});

it('should allow to put the group by into a query builder with simple slider config', () => {
facet.isSimpleSliderConfig = () => true;
const builder = new QueryBuilder();
controller.putGroupByIntoQueryBuilder(builder);
expect(builder.groupByRequests).toEqual(jasmine.arrayContaining([jasmine.objectContaining({
field: '@foo',
generateAutomaticRanges: false,
maximumNumberOfValues: 1,
rangeValues: jasmine.arrayContaining([
jasmine.objectContaining({
start: 0,
end: 100,
endInclusive: false
})
])
})]));
});

it('should add a group by for graph if needed if it\'s not a simple slider config', () => {
facet.isSimpleSliderConfig = () => false;
facet.options.graph = {};
facet.options.graph.steps = 10;
facet.getSliderBoundaryForQuery = () => [5, 99];

const builder = new QueryBuilder();
controller.putGroupByIntoQueryBuilder(builder);
expect(builder.groupByRequests[0]).toEqual(jasmine.objectContaining({
field: '@foo',
generateAutomaticRanges: true,
maximumNumberOfValues: 10,
queryOverride: '@uri',
sortCriteria: 'nosort'
}));
});

it('should add a group by for graph using the query override if specified', () => {
facet.isSimpleSliderConfig = () => false;
facet.options.graph = {};
facet.options.graph.steps = 10;
facet.options.queryOverride = 'my query override';
facet.getSliderBoundaryForQuery = () => [5, 99];

const builder = new QueryBuilder();
controller.putGroupByIntoQueryBuilder(builder);
expect(builder.groupByRequests[0]).toEqual(jasmine.objectContaining({
field: '@foo',
generateAutomaticRanges: true,
maximumNumberOfValues: 10,
queryOverride: 'my query override',
sortCriteria: 'nosort'
}));
});

it('should add a range request if needed for graph', () => {
facet.isSimpleSliderConfig = () => true;
facet.options.graph = {};
facet.options.graph.steps = 10;
facet.options.start = 0;
facet.options.end = 100;
facet.getSliderBoundaryForQuery = () => [5, 99];

const builder = new QueryBuilder();
controller.putGroupByIntoQueryBuilder(builder);
expect(builder.groupByRequests[0]).toEqual(jasmine.objectContaining({
rangeValues: jasmine.arrayContaining([jasmine.objectContaining({
'start': 0,
'end': 10,
endInclusive: true
}), jasmine.objectContaining({
'start': 10,
'end': 20,
endInclusive: true
})])
}));
});

it('should add a group by for graph if it\'s a date', () => {
facet.isSimpleSliderConfig = () => true;
facet.options.graph = {};
facet.options.graph.steps = 10;
facet.options.start = 1;
facet.options.end = 100;
facet.getSliderBoundaryForQuery = () => [5, 99];
facet.options.dateField = true;

const builder = new QueryBuilder();
controller.putGroupByIntoQueryBuilder(builder);
expect(builder.groupByRequests[0]).toEqual(jasmine.objectContaining({
field: '@foo',
maximumNumberOfValues: 10,
sortCriteria: 'nosort',
generateAutomaticRanges: false,
rangeValues: jasmine.arrayContaining([jasmine.objectContaining({
'start': jasmine.stringMatching('1970'),
'end': jasmine.stringMatching('1970'),
endInclusive: true
})])
}));
});
});
}
2 changes: 1 addition & 1 deletion test/ui/FacetSliderTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function FacetSliderTest() {
expect(simulation.queryBuilder.build().groupBy).toEqual(jasmine.arrayContaining([
jasmine.objectContaining({
field: '@foo',
generateAutomaticRanges: true
generateAutomaticRanges: false
})
]));
});
Expand Down

0 comments on commit 13ab55a

Please sign in to comment.