@esri/solution-common
Version:
Provides general helper functions for @esri/solution.js.
95 lines • 3.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports._convertIndicatorField = exports._convertIndicatorToDefinition = exports._convertIndicatorsToDefinitions = exports._upgradeTwoDotZero = void 0;
/** @license
* Copyright 2020 Esri
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const hub_common_1 = require("@esri/hub-common");
/**
* Convert indicator "definitions" from the CAS style to the Indicator schema
* see https://github.com/ArcGIS/Hub/blob/master/indicators.md
*
* @param model
* @private
*/
function _upgradeTwoDotZero(model) {
if ((0, hub_common_1.getProp)(model, "item.properties.schemaVersion") >= 2) {
return model;
}
// get the indicators from the .configurationSettings...
const clone = (0, hub_common_1.cloneObject)(model);
const configSettings = (0, hub_common_1.getProp)(clone, "data.configurationSettings") || [];
const indicatorsHash = configSettings.find((e) => e.category === "Indicators");
clone.data.indicators = _convertIndicatorsToDefinitions(indicatorsHash);
// remove CAS structure
delete clone.data.configurationSettings;
// set the schemaVersion...
clone.item.properties.schemaVersion = 2;
return clone;
}
exports._upgradeTwoDotZero = _upgradeTwoDotZero;
/**
* Given the Indicators entry from a CAS configurationSettings array,
* convert to an indicators object in the new schema
*
* @private
*/
function _convertIndicatorsToDefinitions(indicatorsHash = {}) {
// the incoming structure should have a .fields property, and what we want will be in there...
if (!indicatorsHash.fields || !Array.isArray(indicatorsHash.fields)) {
indicatorsHash.fields = [];
}
const defs = indicatorsHash.fields.map(exports._convertIndicatorToDefinition);
// now we need to create an object which has props for each def
return defs;
}
exports._convertIndicatorsToDefinitions = _convertIndicatorsToDefinitions;
/**
* Convert a CAS formatted indicator to the .definition in the new schama
*
* @private
*/
const _convertIndicatorToDefinition = function (ind) {
const def = {
id: ind.fieldName,
type: "Data",
name: ind.label || ind.fieldName,
optional: ind.optional || false,
definition: {
description: ind.label || ind.fieldName,
supportedTypes: [].concat(ind.layerOptions.supportedTypes),
geometryTypes: [].concat(ind.layerOptions.geometryTypes),
fields: ind.fields.map(exports._convertIndicatorField),
},
};
return def;
};
exports._convertIndicatorToDefinition = _convertIndicatorToDefinition;
/**
* Convert the CAS formatted "field" into the new schema
*
* @private
*/
const _convertIndicatorField = function (field) {
return {
id: field.fieldName,
name: field.label,
optional: field.optional || false,
description: field.tooltip,
supportedTypes: [].concat(field.supportedTypes),
};
};
exports._convertIndicatorField = _convertIndicatorField;
//# sourceMappingURL=upgrade-two-dot-zero.js.map