Skip to content

Commit

Permalink
#90
Browse files Browse the repository at this point in the history
  • Loading branch information
jmhauck committed Sep 4, 2019
1 parent e0c5ccd commit 138f09e
Show file tree
Hide file tree
Showing 2 changed files with 289 additions and 5 deletions.
75 changes: 73 additions & 2 deletions packages/feature-layer/src/featureServiceHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ export function templatize(
return itemTemplate;
}

/**
* Delete key properties that are system managed
*
* @param layer The data layer instance with field name references within
*/
export function deleteViewProps(layer: any) {
const props: string[] = ["definitionQuery"];

props.forEach(prop => {
common.deleteProp(layer, prop);
});
}

/**
* Cache properties that contain field references
*
Expand All @@ -123,7 +136,8 @@ export function cacheFieldInfos(layer: any, fieldInfos: any): any {
"templates",
"relationships",
"drawingInfo",
"timeInfo"
"timeInfo",
"viewDefinitionQuery"
];

props.forEach(prop => {
Expand Down Expand Up @@ -473,6 +487,9 @@ export function updateFeatureServiceDefinition(
const item = toAdd.item;
const originalId = item.id;
fieldInfos = cacheFieldInfos(item, fieldInfos);
if (item.isView) {
deleteViewProps(item);
}
// when the item is a view we need to grab the supporting fieldInfos
if (itemTemplate.properties.service.isView) {
adminLayerInfos[originalId] = item.adminLayerInfo;
Expand Down Expand Up @@ -614,9 +631,17 @@ export function postProcessFields(
// visible true when added with the layer definition
// update the field visibility to match that of the source
if (item.isView) {
const fieldUpdates: any[] = _getFieldVisibilityUpdates(
let fieldUpdates: any[] = _getFieldVisibilityUpdates(
fieldInfos[item.id]
);

// view field domains can contain different values than the source field domains
// use the cached view domain when it differs from the source view domain
fieldUpdates = _validateDomains(
fieldInfos[item.id],
fieldUpdates
);

if (fieldUpdates.length > 0) {
fieldInfos[item.id].fields = fieldUpdates;
}
Expand Down Expand Up @@ -680,6 +705,52 @@ export function _getFieldVisibilityUpdates(fieldInfo: any): any[] {
return visibilityUpdates;
}

/**
* view field domains can contain different values than the source feature service field domains
* use the cached domain when it differs from the source view field domain
*
* @param fieldInfo current view layer or table fieldInfo
* @param fieldUpdates any existing field updates
* @return Array of fields to be updated
* @protected
*/
export function _validateDomains(fieldInfo: any, fieldUpdates: any[]) {
const domainFields: any[] = [];
const domainNames: string[] = [];

// loop through the cached fields from the source view we are cloning
fieldInfo.sourceFields.forEach((field: any) => {
if (field.hasOwnProperty("domain") && field.domain) {
domainFields.push(field.domain);
domainNames.push(String(field.name).toLocaleLowerCase());
}
});

// loop through the fields from the new view service
// add an update when the domains don't match
fieldInfo.newFields.forEach((field: any) => {
const i: number = domainNames.indexOf(
String(field.name).toLocaleLowerCase()
);
if (i > -1 && field.hasOwnProperty("domain") && field.domain) {
if (JSON.stringify(field.domain) !== JSON.stringify(domainFields[i])) {
// should mixin the update if the field already has some other update
let hasUpdate: boolean = false;
fieldUpdates.forEach((update: any) => {
if (update.name === field.name) {
hasUpdate = true;
update.domain = domainFields[i];
}
});
if (!hasUpdate) {
fieldUpdates.push({ name: field.name, domain: domainFields[i] });
}
}
}
});
return fieldUpdates;
}

/**
* Add popup info back to the layer item
*
Expand Down
219 changes: 216 additions & 3 deletions packages/feature-layer/test/featureServiceHelpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import {
templatize,
deleteViewProps,
cacheFieldInfos,
_cacheFieldInfo,
cachePopupInfos,
Expand All @@ -32,6 +33,7 @@ import {
addFeatureServiceLayersAndTables,
postProcessFields,
_getFieldVisibilityUpdates,
_validateDomains,
updatePopupInfo,
_templatize,
_templatizeProperty,
Expand Down Expand Up @@ -405,7 +407,8 @@ describe("Module `featureServiceHelpers`: utility functions for feature-service
type: "simple"
}
},
type: "layer"
type: "layer",
viewDefinitionQuery: "viewDefinitionQuery"
};

const expectedLayer: any = {
Expand All @@ -425,7 +428,8 @@ describe("Module `featureServiceHelpers`: utility functions for feature-service
templates: null,
relationships: null,
drawingInfo: null,
type: "layer"
type: "layer",
viewDefinitionQuery: null
};

const expectedFieldInfos: any = {
Expand Down Expand Up @@ -458,7 +462,8 @@ describe("Module `featureServiceHelpers`: utility functions for feature-service
type: "simple"
}
},
type: "layer"
type: "layer",
viewDefinitionQuery: "viewDefinitionQuery"
}
};

Expand Down Expand Up @@ -4720,4 +4725,212 @@ describe("Module `featureServiceHelpers`: utility functions for feature-service
done.fail();
});
});

describe("_validateDomains", () => {
it("should not update field when domains match", () => {
const fieldInfos: any = {
sourceFields: [
{
name: "A"
},
{
name: "B",
domain: null
},
{
name: "C",
domain: {
codedValues: [
{
name: "C_1",
value: "C_2"
}
]
}
}
],
newFields: [
{
name: "a"
},
{
name: "b",
domain: null
},
{
name: "c",
domain: {
codedValues: [
{
name: "C_1",
value: "C_2"
}
]
}
}
]
};

const fieldUpdates: any[] = [];
const expected: any[] = [];

const actual: any[] = _validateDomains(fieldInfos, fieldUpdates);

expect(actual).toEqual(expected);
});

it("should update field when domains don't match", () => {
const fieldInfos: any = {
sourceFields: [
{
name: "A"
},
{
name: "B",
domain: null
},
{
name: "C",
unrelatedProp: true,
domain: {
codedValues: [
{
name: "C_1",
value: "C_2"
}
]
}
},
{
name: "D",
domain: {
codedValues: [
{
name: "DDD_1",
value: "DDD_2"
}
]
}
},
{
name: "E",
domain: {
codedValues: [
{
name: "EEE_1",
value: "EEE_2"
}
]
}
}
],
newFields: [
{
name: "a"
},
{
name: "b",
domain: null
},
{
name: "c",
unrelatedProp: false,
domain: {
codedValues: [
{
name: "C_1",
value: "C_2"
}
]
}
},
{
name: "d",
domain: {
codedValues: [
{
name: "D_1",
value: "D_2"
}
]
}
},
{
name: "e",
domain: {
codedValues: [
{
name: "E_1",
value: "E_2"
}
]
}
}
]
};

const fieldUpdates: any[] = [
{
name: "d",
visible: true
}
];
const expected: any[] = [
{
name: "d",
visible: true,
domain: {
codedValues: [
{
name: "DDD_1",
value: "DDD_2"
}
]
}
},
{
name: "e",
domain: {
codedValues: [
{
name: "EEE_1",
value: "EEE_2"
}
]
}
}
];

const actual: any[] = _validateDomains(fieldInfos, fieldUpdates);

expect(actual).toEqual(expected);
});
});

describe("deleteViewProps", () => {
it("should remove key props from view layer", () => {
const layer: any = {
someProp: "A",
definitionQuery: "definitionQuery"
};
const expected: any = {
someProp: "A"
};
deleteViewProps(layer);

expect(layer).toEqual(expected);
});

it("should not fail when view does not contain key props", () => {
const layer: any = {
someProp: "A"
};
const expected: any = {
someProp: "A"
};
deleteViewProps(layer);

expect(layer).toEqual(expected);
});
});
});

0 comments on commit 138f09e

Please sign in to comment.