UNPKG

@esri/solution-common

Version:

Provides general helper functions for @esri/solution.js.

88 lines 3.1 kB
/** @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. */ import { getProp, cloneObject } from "@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 */ export function _upgradeTwoDotZero(model) { if (getProp(model, "item.properties.schemaVersion") >= 2) { return model; } // get the indicators from the .configurationSettings... const clone = cloneObject(model); const configSettings = 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; } /** * Given the Indicators entry from a CAS configurationSettings array, * convert to an indicators object in the new schema * * @private */ export 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(_convertIndicatorToDefinition); // now we need to create an object which has props for each def return defs; } /** * Convert a CAS formatted indicator to the .definition in the new schama * * @private */ export 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(_convertIndicatorField), }, }; return def; }; /** * Convert the CAS formatted "field" into the new schema * * @private */ export const _convertIndicatorField = function (field) { return { id: field.fieldName, name: field.label, optional: field.optional || false, description: field.tooltip, supportedTypes: [].concat(field.supportedTypes), }; }; //# sourceMappingURL=upgrade-two-dot-zero.js.map