Skip to content

Commit

Permalink
Un-hardcode 'baseline', use a store instead
Browse files Browse the repository at this point in the history
Closes #63
  • Loading branch information
yongrenjie committed Nov 2, 2023
1 parent e16036f commit 1aaddf0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
2 changes: 2 additions & 0 deletions web/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { type LayerName } from "src/types";
import {
allScenarios,
referenceScenarioName,
scenarioName,
compareScenarioName,
scaleFactors,
Expand Down Expand Up @@ -38,6 +39,7 @@
$scaleFactors = setupScaleFactors(referenceScenarioUnscaled);
$validAreaNames = setupAreaNames(referenceScenarioUnscaled);
$allScenarios = setupScenarioMap($scaleFactors, $validAreaNames);
$referenceScenarioName = referenceScenarioUnscaled.metadata.name;
// Set the initial scenario name to the reference scenario, and the
// scenario being compared against to nothing
$scenarioName = referenceScenarioUnscaled.metadata.name;
Expand Down
33 changes: 17 additions & 16 deletions web/src/lib/leftSidebar/create/ModifyOutputAreas.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { onMount, onDestroy } from "svelte";
import {
allScenarios,
referenceScenarioName,
customScenarioInProgress,
clickedOAs,
} from "src/stores";
Expand All @@ -31,9 +32,9 @@
$customScenarioInProgress = false;
});
function getBaselineSig(oaName: string): number {
function getReferenceSig(oaName: string): number {
return $allScenarios
.get("baseline")
.get($referenceScenarioName)
.values.get(oaName)
.get("signature_type");
}
Expand All @@ -42,7 +43,7 @@
// editable layers
function getSingleOAChanges(
oaName: string
): Map<MacroVar | "baseline_sig", number | null> {
): Map<MacroVar | "reference_sig", number | null> {
const hasChanges = changes.has(oaName);
const thisChanges = changes.get(oaName);
return new Map([
Expand All @@ -53,7 +54,7 @@
["job_types", hasChanges ? thisChanges.get("job_types") : null],
["use", hasChanges ? thisChanges.get("use") : null],
["greenspace", hasChanges ? thisChanges.get("greenspace") : null],
["baseline_sig", getBaselineSig(oaName)],
["reference_sig", getReferenceSig(oaName)],
]);
}
Expand Down Expand Up @@ -88,7 +89,7 @@
thisOAChanges.set(macroVar, macroVarValue);
}
});
thisOAChanges.delete("baseline_sig");
thisOAChanges.delete("reference_sig");
changes.set(oa.name, thisOAChanges as Map<MacroVar, number>);
});
}
Expand Down Expand Up @@ -137,23 +138,23 @@
useModified = use !== null;
green = oaChanges.get("greenspace");
greenModified = green !== null;
baselineSig = oaChanges.get("baseline_sig");
referenceSig = oaChanges.get("reference_sig");
} else {
// More than one OA selected
const allSigs: Array<number | null> = oas.map((oa) =>
getSingleOAChanges(oa.name).get("signature_type")
);
const allBaselineSigs: Array<number> = oas.map((oa) =>
getSingleOAChanges(oa.name).get("baseline_sig")
const allReferenceSigs: Array<number> = oas.map((oa) =>
getSingleOAChanges(oa.name).get("reference_sig")
);
// If none of the values were changed, check if the baseline values
// If none of the values were changed, check if the reference values
// are all the same. If so, display that
if (allSigs.every((s) => s === null)) {
sigModified = false;
if (allBaselineSigs.every((s) => s === allBaselineSigs[0])) {
baselineSig = allBaselineSigs[0];
if (allReferenceSigs.every((s) => s === allReferenceSigs[0])) {
referenceSig = allReferenceSigs[0];
} else {
baselineSig = null;
referenceSig = null;
}
}
// If some of the values were changed, but not all, display an unchecked checkbox
Expand All @@ -163,7 +164,7 @@
) {
sig = null;
sigModified = false;
baselineSig = null;
referenceSig = null;
}
// If we reached here, then that means that all of the values were
// changed to something else.
Expand Down Expand Up @@ -216,7 +217,7 @@
// Variables for OA modifiers
let sig: number | null = null;
let sigModified: boolean;
let baselineSig: null | number;
let referenceSig: null | number;
let job: number | null = null;
let jobModified: boolean;
let use: number | null = null;
Expand Down Expand Up @@ -267,7 +268,7 @@
bind:checked={sigModified}
on:change={() => {
if (sigModified) {
sig = baselineSig;
sig = referenceSig;
}
if ($clickedOAs.length > 1 && !sigModified) {
sig = null;
Expand All @@ -288,7 +289,7 @@
{/each}
</select>
{:else}
<select id="sig-dropdown" value={baselineSig} disabled>
<select id="sig-dropdown" value={referenceSig} disabled>
{#each [...config.signatures.entries()] as [signatureId, signature]}
<option value={signatureId}
>{signatureId}: {signature.name}</option
Expand Down
1 change: 1 addition & 0 deletions web/src/stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { type Scenario, type LayerName } from 'src/types';
// Create stores, but don't initialise their values yet; this will be done at
// the top of App.svelte.
export const allScenarios: Writable<Map<string, Scenario>> = writable(undefined);
export const referenceScenarioName: Writable<string> = writable(undefined);
export const scenarioName: Writable<string> = writable(undefined);
export const compareScenarioName: Writable<string | null> = writable(undefined);
export const scaleFactors: Writable<Map<LayerName, { min: number, max: number }>> = writable(undefined);
Expand Down

0 comments on commit 1aaddf0

Please sign in to comment.