terriajs
Version:
Geospatial data visualization platform.
112 lines • 5.13 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { observer } from "mobx-react";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { applyTranslationIfExists, TRANSLATE_KEY_PREFIX } from "../../../../Language/languageHelpers";
import CatalogMemberMixin from "../../../../ModelMixins/CatalogMemberMixin";
import GroupMixin from "../../../../ModelMixins/GroupMixin";
import ReferenceMixin from "../../../../ModelMixins/ReferenceMixin";
import Loader from "../../../Loader";
import { useViewState } from "../../../Context";
import Styles from "./tools-panel.scss";
const CountDatasets = observer((props) => {
const [btnStringOrComponent, setBtnStringOrComponent] = useState(`${TRANSLATE_KEY_PREFIX}countDatasets.btnText`);
const { t, i18n } = useTranslation();
const viewState = useViewState();
const countDatasets = () => {
const totals = {
name: undefined,
groups: 0,
members: 0,
messages: [],
subTotals: [],
showResults: false
};
function counter(group, stats, path) {
stats.name = group.name;
const promises = group.memberModels.map(async (model) => {
// Not pure - updates stats object & path
let member = model;
if (ReferenceMixin.isMixedInto(member)) {
(await member.loadReference()).ignoreError();
if (!member.target) {
return;
}
member = member.target;
}
if (!CatalogMemberMixin.isMixedInto(member))
return;
// if (member.countValue === countValue) {
// continue;
// }
// member.countValue = countValue;
if (GroupMixin.isMixedInto(member)) {
const childStats = {
name: undefined,
groups: 0,
members: 0,
messages: [],
subTotals: []
};
path.push(member.name);
const loadPromise = member.loadMembers();
const countPromise = member.isLoading
? loadPromise
.then((result) => result.throwIfError())
.then(recurseAndUpdateTotals.bind(undefined, member, stats, childStats, path.slice()))
.catch(reportLoadError.bind(undefined, member, stats, path.slice()))
: recurseAndUpdateTotals(member, stats, childStats, path);
path.pop();
return countPromise;
}
else {
++stats.members;
}
});
return Promise.all(promises).then(() => { });
}
function recurseAndUpdateTotals(member, stats, childStats, path) {
const promise = counter(member, childStats, path).then(function () {
stats.groups += childStats.groups + 1;
stats.members += childStats.members;
stats.messages.push(...childStats.messages);
stats.subTotals.push(childStats);
});
return promise;
}
function reportLoadError(_member, stats, path) {
stats.messages.push(path.join(" -> ") + t("countDatasets.loadError"));
}
setBtnStringOrComponent(_jsx(Loader, { message: t("countDatasets.countingMessage") }));
// ++countValue;
const root = viewState.terria.catalog.group;
counter(root, totals, []).then(function () {
let info = t("countDatasets.totals", {
items: totals.members,
groups: totals.groups
});
props.updateResults(info);
let i;
const subTotals = totals.subTotals;
for (i = 0; i < subTotals.length; ++i) {
info += t("countDatasets.subTotals", {
name: subTotals[i].name,
items: subTotals[i].members,
groups: subTotals[i].groups
});
}
info += "<div> </div>";
const messages = totals.messages;
for (i = 0; i < messages.length; ++i) {
info += "<div>" + messages[i] + "</div>";
}
setBtnStringOrComponent(`${TRANSLATE_KEY_PREFIX}countDatasets.recount`);
props.updateResults(info);
});
};
return (_jsxs("form", { children: [t("countDatasets.title"), _jsx("button", { className: Styles.submit, onClick: countDatasets, type: "button", value: t("countDatasets.btnCount"), children: typeof btnStringOrComponent === "string"
? applyTranslationIfExists(btnStringOrComponent, i18n)
: btnStringOrComponent })] }));
});
export default CountDatasets;
//# sourceMappingURL=CountDatasets.js.map