Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON metadata -- set language #1211

Closed
wants to merge 9 commits into from
3 changes: 2 additions & 1 deletion examples/minimal_v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"map"
],
"display_defaults": {
"color_by": "country"
"color_by": "country",
"language": "ja"
},
"geo_resolutions": [
{
Expand Down
41 changes: 40 additions & 1 deletion src/actions/recomputeReduxState.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { strainNameToIdx, getIdxMatchingLabel, calculateVisiblityAndBranchThickn
import { constructVisibleTipLookupBetweenTrees } from "../util/treeTangleHelpers";
import { calcTipRadii } from "../util/tipRadiusHelpers";
import { getDefaultControlsState, shouldDisplayTemporalConfidence } from "../reducers/controls";
import { getDefaultGeneralState } from "../reducers/general";
import { countTraitsAcrossTree, calcTotalTipsInTree } from "../util/treeCountingHelpers";
import { calcEntropyInView } from "../util/entropy";
import { treeJsonToState } from "../util/treeJsonProcessing";
Expand Down Expand Up @@ -170,7 +171,23 @@ const restoreQueryableStateToDefaults = (state) => {
// console.log("state now", state);
return state;
};
const modifyGeneralStateViaMetadata = (state, metadata) => {
if (metadata.displayDefaults) {
const keysToCheckFor = ["language"];
const expectedTypes = ["string"]; // eslint-disable-line

for (let i = 0; i < keysToCheckFor.length; i += 1) {
if (Object.hasOwnProperty.call(metadata.displayDefaults, keysToCheckFor[i])) {
if (typeof metadata.displayDefaults[keysToCheckFor[i]] === expectedTypes[i]) { // eslint-disable-line valid-typeof
state[keysToCheckFor[i]] = metadata.displayDefaults[keysToCheckFor[i]];
state.defaults[keysToCheckFor[i]] = metadata.displayDefaults[keysToCheckFor[i]];
} else {
console.error("Skipping 'display_default' for ", keysToCheckFor[i], "as it is not of type ", expectedTypes[i]);
}
}
}
}
};
const modifyStateViaMetadata = (state, metadata) => {
if (metadata.date_range) {
/* this may be useful if, e.g., one were to want to display an outbreak
Expand Down Expand Up @@ -602,6 +619,25 @@ const convertColoringsListToDict = (coloringsList) => {
*
* A lot of this is simply changing augur's snake_case to auspice's camelCase
*/
const createMetadataGeneralStateFromJSON = (json) => {
const metadata = {};
if (json.meta.display_defaults) {
metadata.displayDefaults = {};
const jsonKeyToAuspiceKey = {
language: "language"
};
for (const [jsonKey, auspiceKey] of Object.entries(jsonKeyToAuspiceKey)) {
if (Object.prototype.hasOwnProperty.call(json.meta.display_defaults, jsonKey)) {
metadata.displayDefaults[auspiceKey] = json.meta.display_defaults[jsonKey];
}
}
}
if (Object.prototype.hasOwnProperty.call(metadata, "loaded")) {
console.error("Metadata JSON must not contain the key \"loaded\". Ignoring.");
}
metadata.loaded = true;
return metadata;
};
const createMetadataStateFromJSON = (json) => {
const metadata = {};
if (json.meta.colorings) {
Expand Down Expand Up @@ -707,6 +743,9 @@ export const createStateFromQueryOrJSONs = ({
controls = modifyStateViaMetadata(controls, metadata);
controls["absoluteZoomMin"] = 0;
controls["absoluteZoomMax"] = entropy.lengthSequence;
generalMetaData = createMetadataGeneralStateFromJSON();
general_state = getDefaultGeneralState();
general_state = modifyGeneralStateViaMetadata(general_state, generalMetaData);
} else if (oldState) {
/* creating deep copies avoids references to (nested) objects remaining the same which
can affect props comparisons. Due to the size of some of the state, we only do this selectively */
Expand Down Expand Up @@ -809,7 +848,7 @@ export const createStateFromQueryOrJSONs = ({
/* if narratives then switch the query back to ?n=<SLIDE> for display */
if (narrativeBlocks) query = {n: narrativeSlideIdx}; // eslint-disable-line no-param-reassign

return {tree, treeToo, metadata, entropy, controls, narrative, frequencies, query};
return {tree, treeToo, metadata, entropy, controls, narrative, frequencies, query, general};
};

export const createTreeTooState = ({
Expand Down
28 changes: 16 additions & 12 deletions src/reducers/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ import { hasExtension, getExtension } from "../util/extensions";
not limited to <App>
*/

const query = queryString.parse(window.location.search);

const defaults = {
language: "en"
};

const getFirstPageToDisplay = () => {
if (hasExtension("entryPage")) {
Expand All @@ -20,15 +15,24 @@ const getFirstPageToDisplay = () => {
return chooseDisplayComponentFromURL(window.location.pathname);
};

export const getDefaultGeneralState = () => {
const query = queryString.parse(window.location.search);
const defaults = {
language: "en"
};
return {
defaults,
displayComponent: getFirstPageToDisplay(),
errorMessage: undefined,
pathname: window.location.pathname, // keep a copy of what the app "thinks" the pathname is
language: query.lang ? query.lang : defaults.language
}
}

const general = (state = {
defaults,
displayComponent: getFirstPageToDisplay(),
errorMessage: undefined,
pathname: window.location.pathname, // keep a copy of what the app "thinks" the pathname is
language: query.lang ? query.lang : defaults.language
}, action) => {
const general = (state = getDefaultGeneralState(), action) => {
switch (action.type) {
case types.CLEAN_START:
return action.general
case types.PAGE_CHANGE:
const stateUpdate = {
displayComponent: action.displayComponent,
Expand Down