UNPKG

@hisptz/react-ui

Version:

A collection of reusable complex DHIS2 react ui components.

186 lines (150 loc) 6.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateLegends = generateLegends; exports.getColorFromLegendSet = getColorFromLegendSet; exports.getLegendCount = getLegendCount; exports.getOrgUnitsSelection = getOrgUnitsSelection; exports.highlightFeature = highlightFeature; exports.resetHighlight = resetHighlight; exports.sanitizeDate = sanitizeDate; exports.sanitizeOrgUnits = sanitizeOrgUnits; exports.toGeoJson = toGeoJson; var _lodash = require("lodash"); var _colors = require("./colors"); function highlightFeature(e, style) { const layer = e.target; layer.setStyle(style); // layer.bringToFront(); } function resetHighlight(e, defaultStyle) { const layer = e.target; layer.setStyle(defaultStyle); // layer.bringToBack(); } function getColorFromLegendSet(legends, value) { var _find; if (!value) { return ""; } const legend = (_find = (0, _lodash.find)(legends !== null && legends !== void 0 ? legends : [], legend => (legend === null || legend === void 0 ? void 0 : legend.startValue) <= value && (legend === null || legend === void 0 ? void 0 : legend.endValue) >= value)) !== null && _find !== void 0 ? _find : {}; return legend.color ? legend.color : "transparent"; } function getLegendCount(legend, data) { const { startValue, endValue } = legend; return (0, _lodash.filter)(data, d => d.data >= startValue && d.data <= endValue).length; } function getOrgUnitsSelection(orgUnitSelection) { var _orgUnitSelection$org, _orgUnitSelection$org2; const orgUnits = []; if (orgUnitSelection.userOrgUnit) { orgUnits.push("USER_ORGUNIT"); } if (orgUnitSelection.userSubUnit) { orgUnits.push("USER_ORGUNIT_CHILDREN"); } if (orgUnitSelection.userSubX2Unit) { orgUnits.push("USER_ORGUNIT_GRANDCHILDREN"); } if (!(0, _lodash.isEmpty)(orgUnitSelection.levels)) { (0, _lodash.forEach)(orgUnitSelection.levels, level => orgUnits.push("LEVEL-".concat(level))); } return [...orgUnits, ...((_orgUnitSelection$org = orgUnitSelection === null || orgUnitSelection === void 0 ? void 0 : (_orgUnitSelection$org2 = orgUnitSelection.orgUnits) === null || _orgUnitSelection$org2 === void 0 ? void 0 : _orgUnitSelection$org2.map(ou => "".concat(ou.id))) !== null && _orgUnitSelection$org !== void 0 ? _orgUnitSelection$org : [])]; } function sanitizeOrgUnits(metaData) { if (metaData) { var _metaData$dimensions, _metaData$dimensions$; return metaData === null || metaData === void 0 ? void 0 : (_metaData$dimensions = metaData.dimensions) === null || _metaData$dimensions === void 0 ? void 0 : (_metaData$dimensions$ = _metaData$dimensions.ou) === null || _metaData$dimensions$ === void 0 ? void 0 : _metaData$dimensions$.map(ouId => { var _metaData$items$ouId, _metaData$ouHierarchy; return { id: ouId, name: metaData === null || metaData === void 0 ? void 0 : (_metaData$items$ouId = metaData.items[ouId]) === null || _metaData$items$ouId === void 0 ? void 0 : _metaData$items$ouId.name, path: metaData === null || metaData === void 0 ? void 0 : (_metaData$ouHierarchy = metaData.ouHierarchy) === null || _metaData$ouHierarchy === void 0 ? void 0 : _metaData$ouHierarchy[ouId] }; }); } return []; } function toGeoJson(organisationUnits) { return (0, _lodash.sortBy)(organisationUnits, "le").map(ou => { try { const coord = JSON.parse(ou.co); let gpid = ""; let gppg = ""; let type = "Point"; if (ou.ty === 2) { type = "Polygon"; if (ou.co.substring(0, 4) === "[[[[") { type = "MultiPolygon"; } } // Grand parent if ((0, _lodash.isString)(ou.pg) && ou.pg.length) { const ids = (0, _lodash.compact)(ou.pg.split("/")); // Grand parent id if (ids.length >= 2) { gpid = ids[ids.length - 2]; } // Grand parent parent graph if (ids.length > 2) { gppg = "/" + ids.slice(0, ids.length - 2).join("/"); } } return { type: "Feature", id: ou.id, geometry: { type, coordinates: coord }, properties: { type, id: ou.id, name: ou.na, hasCoordinatesDown: ou.hcd, hasCoordinatesUp: ou.hcu, level: ou.le, grandParentParentGraph: gppg, grandParentId: gpid, parentGraph: ou.pg, parentId: ou.pi, parentName: ou.pn, dimensions: ou.dimensions } }; } catch (e) { return {}; } }); } function sanitizeDate(startDate) { var _startDate$split, _startDate$split$; if ((startDate === null || startDate === void 0 ? void 0 : (_startDate$split = startDate.split("-")) === null || _startDate$split === void 0 ? void 0 : (_startDate$split$ = _startDate$split[0]) === null || _startDate$split$ === void 0 ? void 0 : _startDate$split$.length) < 4) { var _startDate$split2, _startDate$split2$rev; return startDate === null || startDate === void 0 ? void 0 : (_startDate$split2 = startDate.split("-")) === null || _startDate$split2 === void 0 ? void 0 : (_startDate$split2$rev = _startDate$split2.reverse()) === null || _startDate$split2$rev === void 0 ? void 0 : _startDate$split2$rev.join("-"); } return startDate; } function generateLegends(maxValue, minValue, _ref) { let { classesCount, colorClass } = _ref; const count = classesCount !== null && classesCount !== void 0 ? classesCount : _colors.defaultClasses; const color = colorClass !== null && colorClass !== void 0 ? colorClass : _colors.defaultColorScaleName; const colorScale = [...(0, _colors.getColorPalette)(color, count)].reverse(); const maxLegendValue = 5 * Math.ceil(maxValue / 5); const range = maxLegendValue / count; const values = []; let legendColorsIterator = colorScale.length - 1; for (let i = 0; i < maxLegendValue; i += range) { const id = colorScale[legendColorsIterator]; values.push({ startValue: Math.floor(i), endValue: Math.floor(i + range), id, color: id }); legendColorsIterator--; } return values.reverse(); }