-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add default-units functionality and testing * change and API * PR Fixes * PR fixes * renaming * PR fix * style fix * PR fixes * NextVersion and minor fixes * PR fixes * minor fix * PR fixes * minor fix
- Loading branch information
Showing
12 changed files
with
1,404 additions
and
1,186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...on/changes/@bentley/presentation-backend/presentation-default-units_2021-01-26-13-40.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"changes": [ | ||
{ | ||
"packageName": "@bentley/presentation-backend", | ||
"comment": "Added a way to set default property formatting", | ||
"type": "none" | ||
} | ||
], | ||
"packageName": "@bentley/presentation-backend", | ||
"email": "30312645+aurislt7@users.noreply.github.com" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,4 +90,4 @@ | |
], | ||
"extends": "plugin:@bentley/imodeljs-recommended" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Bentley Systems, Incorporated. All rights reserved. | ||
* See LICENSE.md in the project root for license terms and full copyright notice. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { Field } from "@bentley/presentation-common"; | ||
|
||
/** Returns field by given label. | ||
* @internal | ||
*/ | ||
export function findFieldByLabel(fields: Field[], label: string, allFields?: Field[]): Field | undefined { | ||
const isTopLevel = (undefined === allFields); | ||
if (!allFields) | ||
allFields = new Array<Field>(); | ||
for (const field of fields) { | ||
if (field.label === label) | ||
return field; | ||
|
||
if (field.isNestedContentField()) { | ||
const nestedMatchingField = findFieldByLabel(field.nestedFields, label, allFields); | ||
if (nestedMatchingField) | ||
return nestedMatchingField; | ||
} | ||
|
||
allFields.push(field); | ||
} | ||
if (isTopLevel) { | ||
// eslint-disable-next-line no-console | ||
console.error(`Field '${label}' not found. Available fields: [${allFields.map((f) => `"${f.label}"`).join(", ")}]`); | ||
} | ||
return undefined; | ||
} |
110 changes: 110 additions & 0 deletions
110
full-stack-tests/presentation/src/backend/PresentationManager.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Bentley Systems, Incorporated. All rights reserved. | ||
* See LICENSE.md in the project root for license terms and full copyright notice. | ||
*--------------------------------------------------------------------------------------------*/ | ||
import { expect } from "chai"; | ||
import { ClientRequestContext, Guid } from "@bentley/bentleyjs-core"; | ||
import { IModelDb, SnapshotDb } from "@bentley/imodeljs-backend"; | ||
import { PresentationManager } from "@bentley/presentation-backend"; | ||
import { UnitSystemFormat } from "@bentley/presentation-backend/lib/presentation-backend/PresentationManager"; | ||
import { | ||
ContentSpecificationTypes, DisplayValue, DisplayValuesArray, DisplayValuesMap, KeySet, PresentationUnitSystem, Ruleset, RuleTypes, | ||
} from "@bentley/presentation-common"; | ||
import { initialize, terminate } from "../IntegrationTests"; | ||
import { findFieldByLabel } from "../Utils"; | ||
|
||
describe("PresentationManager", () => { | ||
|
||
let imodel: IModelDb; | ||
before(async () => { | ||
await initialize(); | ||
imodel = SnapshotDb.openFile("assets/datasets/Properties_60InstancesWithUrl2.ibim"); | ||
expect(imodel).is.not.null; | ||
}); | ||
|
||
after(async () => { | ||
imodel.close(); | ||
await terminate(); | ||
}); | ||
|
||
describe("Property value formatting", () => { | ||
|
||
const ruleset: Ruleset = { | ||
id: Guid.createValue(), | ||
rules: [{ | ||
ruleType: RuleTypes.Content, | ||
specifications: [{ specType: ContentSpecificationTypes.SelectedNodeInstances }], | ||
}], | ||
}; | ||
const keys = KeySet.fromJSON({ instanceKeys: [["Generic:PhysicalObject", ["0x74"]]], nodeKeys: [] }); | ||
|
||
it("formats property with default kind of quantity format when it doesn't have format for requested unit system", async () => { | ||
expect(await getAreaDisplayValue(PresentationUnitSystem.BritishImperial)).to.eq("150.1235 cm²"); | ||
|
||
}); | ||
|
||
it("formats property using default format when it doesn't have format for requested unit system", async () => { | ||
const formatProps = { | ||
composite: { | ||
includeZero: true, | ||
spacer: " ", | ||
units: [ | ||
{ label: "ft²", name: "SQ_FT" }, | ||
], | ||
}, | ||
formatTraits: "KeepSingleZero|KeepDecimalPoint|ShowUnitLabel", | ||
precision: 4, | ||
type: "Decimal", | ||
uomSeparator: "", | ||
}; | ||
|
||
const defaultFormats = { | ||
area: { unitSystems: [PresentationUnitSystem.BritishImperial], format: formatProps }, | ||
}; | ||
|
||
expect(await getAreaDisplayValue(PresentationUnitSystem.BritishImperial, defaultFormats)).to.eq("0.1616 ft²"); | ||
}); | ||
|
||
it("formats property using provided format when it has provided format and default format for requested unit system", async () => { | ||
const formatProps = { | ||
composite: { | ||
includeZero: true, | ||
spacer: " ", | ||
units: [ | ||
{ label: "ft²", name: "SQ_FT" }, | ||
], | ||
}, | ||
formatTraits: "KeepSingleZero|KeepDecimalPoint|ShowUnitLabel", | ||
precision: 4, | ||
type: "Decimal", | ||
uomSeparator: "", | ||
}; | ||
|
||
const defaultFormats = { | ||
area: { unitSystems: [PresentationUnitSystem.Metric], format: formatProps }, | ||
}; | ||
|
||
expect(await getAreaDisplayValue(PresentationUnitSystem.Metric, defaultFormats)).to.eq("150.1235 cm²"); | ||
}); | ||
|
||
async function getAreaDisplayValue(unitSystem: PresentationUnitSystem, defaultFormats?: { [phenomenon: string]: UnitSystemFormat }): Promise<DisplayValue> { | ||
const manager: PresentationManager = new PresentationManager({ defaultFormats }); | ||
const descriptor = await manager.getContentDescriptor({ | ||
imodel, | ||
rulesetOrId: ruleset, | ||
keys, | ||
displayType: "Grid", | ||
requestContext: new ClientRequestContext(), | ||
unitSystem, | ||
}); | ||
expect(descriptor).to.not.be.undefined; | ||
const field = findFieldByLabel(descriptor!.fields, "cm2")!; | ||
expect(field).not.to.be.undefined; | ||
const content = await manager.getContent({ imodel, rulesetOrId: ruleset, keys, descriptor: descriptor!, requestContext: new ClientRequestContext(), unitSystem }); | ||
const displayValues = content!.contentSet[0].displayValues.rc_generic_PhysicalObject_ncc_MyProp_areaElementAspect as DisplayValuesArray; | ||
expect(displayValues.length).is.eq(1); | ||
return ((displayValues[0] as DisplayValuesMap).displayValues as DisplayValuesMap)[field.name]!; | ||
} | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.