esri-map-view
Version:
Display an Esri map view as a web component.
133 lines (132 loc) • 4 kB
JavaScript
/**
* Utility functions and helpers.
*/
;
export function parseViewpoint(viewpoint) {
const defaultLOD = 2;
if (viewpoint === undefined || viewpoint === null || viewpoint == "") {
viewpoint = "0,0," + defaultLOD;
}
let values = viewpoint.split(",");
if (values.length < 3) {
values[2] = defaultLOD;
}
return ({
longitude: parseFloat(values[0]) || 0,
latitude: parseFloat(values[1]) || 0,
levelOfDetail: parseInt(values[2]) || defaultLOD,
scale: 0
});
}
;
export function parseMinMax(minMax, defaultMin, defaultMax) {
let min;
let max;
if (minMax === undefined || minMax === null || minMax == "") {
minMax = `${defaultMin},${defaultMax}`;
}
let values = minMax.split(",");
if (values.length < 1) {
values[0] = defaultMin;
values[1] = defaultMax;
}
else if (values.length < 2) {
// if only 1 value provided then min and max should be the same value.
values[1] = values[0];
}
min = parseInt(values[0]);
if (Number.isNaN(min)) {
min = defaultMin;
}
max = parseInt(values[1]);
if (Number.isNaN(max)) {
max = defaultMax;
}
if (max < min) {
// if numbers are swapped then reverse them.
const hold = max;
max = min;
min = hold;
}
if (min < defaultMin) {
min = defaultMin;
}
if (max > defaultMax) {
max = defaultMax;
}
return ({
min: min,
max: max,
});
}
;
export function parseOffset(offset) {
if (offset === undefined || offset === null) {
offset = "0,0";
}
let values = offset.split(",");
return ({
x: parseFloat(values[0]) || 0,
y: parseFloat(values[1]) || 0
});
}
;
export function parseCameraPosition(cameraPosition) {
const defaultZ = 50000;
const defaultHeading = 90;
const defaultTilt = 0;
if (cameraPosition === undefined || cameraPosition === null) {
cameraPosition = `0,0,${defaultZ},${defaultHeading},${defaultTilt}`;
}
let values = cameraPosition.split(",");
if (values.length < 5) {
const defaultFill = [0, 0, defaultZ, defaultHeading, defaultTilt];
for (let i = values.length; i < 5; i++) {
values[i] = defaultFill[i];
}
}
return ({
x: parseFloat(values[0]) || 0,
y: parseFloat(values[1]) || 0,
z: parseFloat(values[2]) || defaultZ,
heading: parseFloat(values[3]) || defaultHeading,
tilt: parseFloat(values[4]) || defaultTilt,
});
}
/**
* Helper function to test a string to see if it is a valid ArcGIS Online item ID.
* @param itemID {string} A proposed ArcGIS Online item ID.
* @returns {boolean} `true` if the input appears to be a valid item ID, otherwise `false`.
*/
export function isValidItemID(itemID) {
return /^[a-f0-9]{32}$/.test(itemID);
}
/**
* Helper function to test a string to see if it is a valid URL.
* @param itemID {string} A proposed URL.
* @returns {boolean} `true` if the input appears to be a valid URL, otherwise `false`.
*/
export function isValidURL(url) {
return /^http(s)?:\/\//.test(url);
}
/**
* Verify a proposed search widget position is an expected value.
* @param position {string} A search position specification.
* @returns {boolean} `true` when OK, `false` when bad.
*/
export function isValidSearchPosition(position) {
return ["top-left", "top-right", "bottom-left", "bottom-right", "off"].indexOf(position) >= 0;
}
/**
* Given a user attribute setting for the UI show setting, return either true to show the UI
* or false to hide it.
* @param uiSetting {string} The string to check.
* @returns {boolean} Either true or false.
*/
export function showUI(uiSetting) {
const allLower = uiSetting ? uiSetting.toLocaleLowerCase() : "";
if (["false", "off", "hide", "disable", "0"].indexOf(allLower) >= 0) {
return false;
}
return true;
}