energy-visualization-sankey 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +62 -28
- package/dist/.tsbuildinfo +1 -1
- package/dist/sankey.esm.js +208 -40
- package/dist/sankey.esm.js.map +1 -1
- package/dist/sankey.umd.js +208 -40
- package/dist/sankey.umd.js.map +1 -1
- package/dist/types/core/Sankey.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/types/index.d.ts +29 -0
- package/dist/types/types/index.d.ts.map +1 -1
- package/dist/types/utils/header.d.ts +54 -0
- package/dist/types/utils/header.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/core/Sankey.ts +10 -41
- package/src/index.ts +2 -0
- package/src/types/index.ts +31 -0
- package/src/utils/header.ts +238 -0
- package/tests/header.test.ts +198 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Sankey.d.ts","sourceRoot":"","sources":["../../../src/core/Sankey.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EACR,mBAAmB,EACnB,iBAAiB,EACjB,qBAAqB,EAErB,iBAAiB,EACpB,MAAM,4BAA4B,CAAC;AAGpC,OAAO,KAAK,EAA6D,aAAa,EAAC,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"Sankey.d.ts","sourceRoot":"","sources":["../../../src/core/Sankey.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EACR,mBAAmB,EACnB,iBAAiB,EACjB,qBAAqB,EAErB,iBAAiB,EACpB,MAAM,4BAA4B,CAAC;AAGpC,OAAO,KAAK,EAA6D,aAAa,EAAC,MAAM,SAAS,CAAC;AAyEvG,MAAM,CAAC,OAAO,OAAO,MAAM;IAIvB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAW;IAKpC,OAAO,CAAC,QAAQ,CAoBT;IAMP,OAAO,CAAC,QAAQ,CAAC,SAAS,CA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
14
14
|
* @author Research Computing Center (RCC), University of Chicago
|
|
15
15
|
*/
|
|
16
|
-
export type { SankeyOptions, EnergyDataPoint, SummaryData, GraphData, YearTotals, BoxMaxes, BoxTops } from './types/index';
|
|
16
|
+
export type { SankeyOptions, SankeyHeaderOptions, SankeyHeaderLogo, EnergyDataPoint, SummaryData, GraphData, YearTotals, BoxMaxes, BoxTops } from './types/index';
|
|
17
17
|
export { SankeyError, DataValidationError } from './types/index';
|
|
18
18
|
export type { SankeyEventMap, SankeyPublicEventType, SankeyPublicEvent, SankeyEventListener, SankeyEventHandlers, SankeyUnsubscribe, SankeyElementType, SankeyMousePosition, SankeyReadyEventData, SankeyErrorEventData, SankeyYearChangedEventData, SankeyAnimationEventData, SankeySpeedChangedEventData, SankeyElementInteractionEventData, SankeyKeypressEventData, SankeySliderEventData, SankeyButtonEventData } from './core/types/public-events';
|
|
19
19
|
export { default as default } from './core/Sankey';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,YAAY,EACR,aAAa,EACb,eAAe,EACf,WAAW,EACX,SAAS,EACT,UAAU,EACV,QAAQ,EACR,OAAO,EACV,MAAM,eAAe,CAAC;AAGvB,OAAO,EACH,WAAW,EACX,mBAAmB,EACtB,MAAM,eAAe,CAAC;AAGvB,YAAY,EACR,cAAc,EACd,qBAAqB,EACrB,iBAAiB,EACjB,mBAAmB,EACnB,mBAAmB,EACnB,iBAAiB,EACjB,iBAAiB,EACjB,mBAAmB,EACnB,oBAAoB,EACpB,oBAAoB,EACpB,0BAA0B,EAC1B,wBAAwB,EACxB,2BAA2B,EAC3B,iCAAiC,EACjC,uBAAuB,EACvB,qBAAqB,EACrB,qBAAqB,EACxB,MAAM,4BAA4B,CAAC;AAGpC,OAAO,EAAC,OAAO,IAAI,OAAO,EAAC,MAAM,eAAe,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,YAAY,EACR,aAAa,EACb,mBAAmB,EACnB,gBAAgB,EAChB,eAAe,EACf,WAAW,EACX,SAAS,EACT,UAAU,EACV,QAAQ,EACR,OAAO,EACV,MAAM,eAAe,CAAC;AAGvB,OAAO,EACH,WAAW,EACX,mBAAmB,EACtB,MAAM,eAAe,CAAC;AAGvB,YAAY,EACR,cAAc,EACd,qBAAqB,EACrB,iBAAiB,EACjB,mBAAmB,EACnB,mBAAmB,EACnB,iBAAiB,EACjB,iBAAiB,EACjB,mBAAmB,EACnB,oBAAoB,EACpB,oBAAoB,EACpB,0BAA0B,EAC1B,wBAAwB,EACxB,2BAA2B,EAC3B,iCAAiC,EACjC,uBAAuB,EACvB,qBAAqB,EACrB,qBAAqB,EACxB,MAAM,4BAA4B,CAAC;AAGpC,OAAO,EAAC,OAAO,IAAI,OAAO,EAAC,MAAM,eAAe,CAAC"}
|
|
@@ -52,6 +52,34 @@ export interface EnergyDataPoint {
|
|
|
52
52
|
readonly petro: EnergySectorBreakdown;
|
|
53
53
|
readonly heat: EnergySectorBreakdown;
|
|
54
54
|
}
|
|
55
|
+
/** Logo shown at the left of the header. `src` and `href` must be https:// URLs. */
|
|
56
|
+
export interface SankeyHeaderLogo {
|
|
57
|
+
readonly src: string;
|
|
58
|
+
readonly href?: string;
|
|
59
|
+
readonly alt?: string;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Text and logo of the header above the chart.
|
|
63
|
+
*
|
|
64
|
+
* For every field: leave it out to keep the default, set it to `null` to hide
|
|
65
|
+
* that element. Text fields and affiliation lines may use the placeholders
|
|
66
|
+
* `{country}`, `{firstYear}` and `{lastYear}`. All values are rendered as text,
|
|
67
|
+
* never as HTML.
|
|
68
|
+
*
|
|
69
|
+
* The animated W/capita total and year are always shown, whatever is hidden.
|
|
70
|
+
*/
|
|
71
|
+
export interface SankeyHeaderOptions {
|
|
72
|
+
/** Default: `"{country} energy usage"` */
|
|
73
|
+
readonly title?: string | null;
|
|
74
|
+
/** Default: `"Energy Transitions in {country} History, {firstYear}-{lastYear}"` */
|
|
75
|
+
readonly subtitle?: string | null;
|
|
76
|
+
/** Default: `"Suits, Matteson, and Moyer (2020)"` */
|
|
77
|
+
readonly attribution?: string | null;
|
|
78
|
+
/** One entry per line. Default: the two RDCEP affiliation lines. */
|
|
79
|
+
readonly affiliation?: readonly string[] | null;
|
|
80
|
+
/** Default: the RDCEP logo linking to https://www.rdcep.org/ */
|
|
81
|
+
readonly logo?: SankeyHeaderLogo | null;
|
|
82
|
+
}
|
|
55
83
|
export interface SankeyOptions {
|
|
56
84
|
readonly data: EnergyDataPoint[];
|
|
57
85
|
readonly country: string;
|
|
@@ -72,6 +100,7 @@ export interface SankeyOptions {
|
|
|
72
100
|
readonly heatBoxY?: number;
|
|
73
101
|
readonly loopAnimation?: boolean;
|
|
74
102
|
readonly debugLogging?: boolean;
|
|
103
|
+
readonly header?: SankeyHeaderOptions;
|
|
75
104
|
/**
|
|
76
105
|
* Event handlers registered before asynchronous initialization begins.
|
|
77
106
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAKH,OAAO,KAAK,EAAC,mBAAmB,EAAC,MAAM,6BAA6B,CAAC;AAGrE,qBAAa,WAAY,SAAQ,KAAK;IACE,IAAI,CAAC,EAAE,MAAM;gBAArC,OAAO,EAAE,MAAM,EAAS,IAAI,CAAC,EAAE,MAAM,YAAA;CAIpD;AAED,qBAAa,mBAAoB,SAAQ,WAAW;IACZ,KAAK,CAAC,EAAE,MAAM;gBAAtC,OAAO,EAAE,MAAM,EAAS,KAAK,CAAC,EAAE,MAAM,YAAA;CAIrD;AAGD,MAAM,WAAW,qBAAqB;IAClC,QAAQ,CAAC,IA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HTML rendering for the title block above the chart.
|
|
4
|
+
*
|
|
5
|
+
* Every value that reaches the markup goes through escapeHtml(). Header text and
|
|
6
|
+
* `country` come from host configuration that, in the Energy Visualization
|
|
7
|
+
* Platform, is user-entered and rendered on public share pages, so they must
|
|
8
|
+
* never be interpreted as HTML.
|
|
9
|
+
*/
|
|
10
|
+
import type { SankeyHeaderOptions } from '../index';
|
|
11
|
+
export interface HeaderContext {
|
|
12
|
+
readonly country: string;
|
|
13
|
+
readonly firstYear: number;
|
|
14
|
+
readonly lastYear: number;
|
|
15
|
+
}
|
|
16
|
+
/** Fully resolved header: `null` means the element is not rendered. */
|
|
17
|
+
export interface ResolvedHeader {
|
|
18
|
+
readonly title: string | null;
|
|
19
|
+
readonly subtitle: string | null;
|
|
20
|
+
readonly attribution: string | null;
|
|
21
|
+
readonly affiliation: readonly string[] | null;
|
|
22
|
+
readonly logo: {
|
|
23
|
+
readonly src: string;
|
|
24
|
+
readonly href: string | null;
|
|
25
|
+
readonly alt: string;
|
|
26
|
+
} | null;
|
|
27
|
+
}
|
|
28
|
+
/** The header shown when no `header` option is given (unchanged from earlier releases). */
|
|
29
|
+
export declare const DEFAULT_HEADER: {
|
|
30
|
+
readonly title: "{country} energy usage";
|
|
31
|
+
readonly subtitle: "Energy Transitions in {country} History, {firstYear}-{lastYear}";
|
|
32
|
+
readonly attribution: "Suits, Matteson, and Moyer (2020)";
|
|
33
|
+
readonly affiliation: readonly ["Center for Robust Decision-making on", "Climate and Energy Policy, UChicago"];
|
|
34
|
+
readonly logo: {
|
|
35
|
+
readonly src: "https://images.squarespace-cdn.com/content/v1/54dcfad0e4b0eaff5e0068bf/1446137765478-FX9WM00VV1LWAFUJRZBI/rdcep+sig2.png";
|
|
36
|
+
readonly href: "https://www.rdcep.org/";
|
|
37
|
+
readonly alt: "The Center for Robust Decision-making on Climate and Energy Policy (RDCEP)";
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
export declare function escapeHtml(value: string): string;
|
|
41
|
+
export declare function isHttpsUrl(value: string): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Validate the shape of the `header` option. Throws SankeyError so a bad
|
|
44
|
+
* configuration fails at construction time, like the existing data checks.
|
|
45
|
+
*/
|
|
46
|
+
export declare function validateHeaderOptions(header: unknown): void;
|
|
47
|
+
export declare function resolveHeader(header: SankeyHeaderOptions | undefined, context: HeaderContext): ResolvedHeader;
|
|
48
|
+
/**
|
|
49
|
+
* Render the header block. The `.evs-energy-usage-overlay` and
|
|
50
|
+
* `.evs-year-overlay` containers are always emitted: RenderingService mounts the
|
|
51
|
+
* animated W/capita total and year into them, whatever text is hidden.
|
|
52
|
+
*/
|
|
53
|
+
export declare function renderHeaderHtml(header: ResolvedHeader): string;
|
|
54
|
+
//# sourceMappingURL=header.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"header.d.ts","sourceRoot":"","sources":["../../../src/utils/header.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,KAAK,EAAmB,mBAAmB,EAAC,MAAM,SAAS,CAAC;AAEnE,MAAM,WAAW,aAAa;IAC1B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC7B;AAED,uEAAuE;AACvE,MAAM,WAAW,cAAc;IAC3B,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACjC,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IACpC,QAAQ,CAAC,WAAW,EAAE,SAAS,MAAM,EAAE,GAAG,IAAI,CAAC;IAC/C,QAAQ,CAAC,IAAI,EAAE;QAAE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;QAAC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAAC;CACtG;AAID,2FAA2F;AAC3F,eAAO,MAAM,cAAc;;;;;;;;;;CAUjB,CAAC;AAKX,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAOhD;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAMjD;AAYD;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,OAAO,GAAG,IAAI,CA6C3D;AA4BD,wBAAgB,aAAa,CAAC,MAAM,EAAE,mBAAmB,GAAG,SAAS,EAAE,OAAO,EAAE,aAAa,GAAG,cAAc,CAkB7G;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,cAAc,GAAG,MAAM,CA8D/D"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "energy-visualization-sankey",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "A modern TypeScript library for creating interactive Sankey diagrams that visualize energy flows from sources to consumption sectors.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sankey",
|
package/src/core/Sankey.ts
CHANGED
|
@@ -16,6 +16,7 @@ import type {D3DivSelection, D3SVGSelection, EnergyDataPoint, GraphData, SankeyO
|
|
|
16
16
|
// Import validation errors
|
|
17
17
|
import {DataValidationError, SankeyError} from '@/types';
|
|
18
18
|
import {Logger} from "@/utils/Logger";
|
|
19
|
+
import {renderHeaderHtml, resolveHeader, validateHeaderOptions} from "@/utils/header";
|
|
19
20
|
import {ConfigurationService} from "@/services/ConfigurationService";
|
|
20
21
|
import {DataValidationService} from "@/services/data/DataValidationService";
|
|
21
22
|
import {DataService} from "@/services/data/DataService";
|
|
@@ -593,47 +594,13 @@ export default class Sankey {
|
|
|
593
594
|
* Creates the DOM structure needed for controls, timeline, and chart display
|
|
594
595
|
*/
|
|
595
596
|
private injectHTML(): void {
|
|
596
|
-
const
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
<div class="evs-header-logo">
|
|
604
|
-
<a href="https://www.rdcep.org/" target="_blank">
|
|
605
|
-
<img src="https://images.squarespace-cdn.com/content/v1/54dcfad0e4b0eaff5e0068bf/1446137765478-FX9WM00VV1LWAFUJRZBI/rdcep+sig2.png"
|
|
606
|
-
alt="The Center for Robust Decision-making on Climate and Energy Policy (RDCEP)"
|
|
607
|
-
title="The Center for Robust Decision-making on Climate and Energy Policy (RDCEP)">
|
|
608
|
-
</a>
|
|
609
|
-
</div>
|
|
610
|
-
|
|
611
|
-
<div class="evs-header-main">
|
|
612
|
-
<div class="evs-header-title-section">
|
|
613
|
-
<h1 class="evs-main-title">${country} energy usage</h1>
|
|
614
|
-
<div class="evs-energy-usage-overlay">
|
|
615
|
-
<!-- SVG overlay for energy usage will be positioned here -->
|
|
616
|
-
</div>
|
|
617
|
-
</div>
|
|
618
|
-
</div>
|
|
619
|
-
|
|
620
|
-
<div class="evs-header-year">
|
|
621
|
-
<div class="evs-year-overlay">
|
|
622
|
-
<!-- SVG overlay for animated year will be positioned here -->
|
|
623
|
-
</div>
|
|
624
|
-
</div>
|
|
625
|
-
|
|
626
|
-
<div class="evs-header-info">
|
|
627
|
-
<div class="evs-subtitle">Energy Transitions in ${country} History, ${firstYear}-${lastYear}</div>
|
|
628
|
-
<div class="evs-attribution">Suits, Matteson, and Moyer (2020)</div>
|
|
629
|
-
</div>
|
|
630
|
-
|
|
631
|
-
<div class="evs-header-affiliation">
|
|
632
|
-
<div class="evs-affiliation-text">Center for Robust Decision-making on</div>
|
|
633
|
-
<div class="evs-affiliation-text">Climate and Energy Policy, UChicago</div>
|
|
634
|
-
</div>
|
|
635
|
-
</div>
|
|
636
|
-
</div>
|
|
597
|
+
const header = resolveHeader(this.options.header, {
|
|
598
|
+
country: this.options.country,
|
|
599
|
+
firstYear: this.services.dataService!.firstYear!,
|
|
600
|
+
lastYear: this.services.dataService!.lastYear!
|
|
601
|
+
});
|
|
602
|
+
|
|
603
|
+
let html = `${renderHeaderHtml(header)}
|
|
637
604
|
<div class="evs-us-energy-sankey-wrapper" style="width: ${this.services['configurationService']?.WIDTH}px;">
|
|
638
605
|
<div class="evs-sankey" style="line-height: 0;"></div>
|
|
639
606
|
`;
|
|
@@ -1274,6 +1241,8 @@ export default class Sankey {
|
|
|
1274
1241
|
|
|
1275
1242
|
// Validate data structure
|
|
1276
1243
|
this.validateDataStructure(options.data);
|
|
1244
|
+
|
|
1245
|
+
validateHeaderOptions(options.header);
|
|
1277
1246
|
}
|
|
1278
1247
|
|
|
1279
1248
|
private validateDataStructure(data: EnergyDataPoint[]): void {
|
package/src/index.ts
CHANGED
package/src/types/index.ts
CHANGED
|
@@ -69,6 +69,36 @@ export interface EnergyDataPoint {
|
|
|
69
69
|
readonly heat: EnergySectorBreakdown;
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
+
/** Logo shown at the left of the header. `src` and `href` must be https:// URLs. */
|
|
73
|
+
export interface SankeyHeaderLogo {
|
|
74
|
+
readonly src: string;
|
|
75
|
+
readonly href?: string;
|
|
76
|
+
readonly alt?: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Text and logo of the header above the chart.
|
|
81
|
+
*
|
|
82
|
+
* For every field: leave it out to keep the default, set it to `null` to hide
|
|
83
|
+
* that element. Text fields and affiliation lines may use the placeholders
|
|
84
|
+
* `{country}`, `{firstYear}` and `{lastYear}`. All values are rendered as text,
|
|
85
|
+
* never as HTML.
|
|
86
|
+
*
|
|
87
|
+
* The animated W/capita total and year are always shown, whatever is hidden.
|
|
88
|
+
*/
|
|
89
|
+
export interface SankeyHeaderOptions {
|
|
90
|
+
/** Default: `"{country} energy usage"` */
|
|
91
|
+
readonly title?: string | null;
|
|
92
|
+
/** Default: `"Energy Transitions in {country} History, {firstYear}-{lastYear}"` */
|
|
93
|
+
readonly subtitle?: string | null;
|
|
94
|
+
/** Default: `"Suits, Matteson, and Moyer (2020)"` */
|
|
95
|
+
readonly attribution?: string | null;
|
|
96
|
+
/** One entry per line. Default: the two RDCEP affiliation lines. */
|
|
97
|
+
readonly affiliation?: readonly string[] | null;
|
|
98
|
+
/** Default: the RDCEP logo linking to https://www.rdcep.org/ */
|
|
99
|
+
readonly logo?: SankeyHeaderLogo | null;
|
|
100
|
+
}
|
|
101
|
+
|
|
72
102
|
// Options interface
|
|
73
103
|
export interface SankeyOptions {
|
|
74
104
|
readonly data: EnergyDataPoint[];
|
|
@@ -90,6 +120,7 @@ export interface SankeyOptions {
|
|
|
90
120
|
readonly heatBoxY?: number;
|
|
91
121
|
readonly loopAnimation?: boolean;
|
|
92
122
|
readonly debugLogging?: boolean;
|
|
123
|
+
readonly header?: SankeyHeaderOptions;
|
|
93
124
|
|
|
94
125
|
/**
|
|
95
126
|
* Event handlers registered before asynchronous initialization begins.
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Header configuration: defaults, validation, placeholder substitution and
|
|
3
|
+
* HTML rendering for the title block above the chart.
|
|
4
|
+
*
|
|
5
|
+
* Every value that reaches the markup goes through escapeHtml(). Header text and
|
|
6
|
+
* `country` come from host configuration that, in the Energy Visualization
|
|
7
|
+
* Platform, is user-entered and rendered on public share pages, so they must
|
|
8
|
+
* never be interpreted as HTML.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import {SankeyError} from '@/types';
|
|
12
|
+
import type {SankeyHeaderLogo, SankeyHeaderOptions} from '@/types';
|
|
13
|
+
|
|
14
|
+
export interface HeaderContext {
|
|
15
|
+
readonly country: string;
|
|
16
|
+
readonly firstYear: number;
|
|
17
|
+
readonly lastYear: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Fully resolved header: `null` means the element is not rendered. */
|
|
21
|
+
export interface ResolvedHeader {
|
|
22
|
+
readonly title: string | null;
|
|
23
|
+
readonly subtitle: string | null;
|
|
24
|
+
readonly attribution: string | null;
|
|
25
|
+
readonly affiliation: readonly string[] | null;
|
|
26
|
+
readonly logo: { readonly src: string; readonly href: string | null; readonly alt: string } | null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const RDCEP_NAME = 'The Center for Robust Decision-making on Climate and Energy Policy (RDCEP)';
|
|
30
|
+
|
|
31
|
+
/** The header shown when no `header` option is given (unchanged from earlier releases). */
|
|
32
|
+
export const DEFAULT_HEADER = {
|
|
33
|
+
title: '{country} energy usage',
|
|
34
|
+
subtitle: 'Energy Transitions in {country} History, {firstYear}-{lastYear}',
|
|
35
|
+
attribution: 'Suits, Matteson, and Moyer (2020)',
|
|
36
|
+
affiliation: ['Center for Robust Decision-making on', 'Climate and Energy Policy, UChicago'],
|
|
37
|
+
logo: {
|
|
38
|
+
src: 'https://images.squarespace-cdn.com/content/v1/54dcfad0e4b0eaff5e0068bf/1446137765478-FX9WM00VV1LWAFUJRZBI/rdcep+sig2.png',
|
|
39
|
+
href: 'https://www.rdcep.org/',
|
|
40
|
+
alt: RDCEP_NAME
|
|
41
|
+
}
|
|
42
|
+
} as const;
|
|
43
|
+
|
|
44
|
+
const HEADER_KEYS = ['title', 'subtitle', 'attribution', 'affiliation', 'logo'] as const;
|
|
45
|
+
const LOGO_KEYS = ['src', 'href', 'alt'] as const;
|
|
46
|
+
|
|
47
|
+
export function escapeHtml(value: string): string {
|
|
48
|
+
return value
|
|
49
|
+
.replace(/&/g, '&')
|
|
50
|
+
.replace(/</g, '<')
|
|
51
|
+
.replace(/>/g, '>')
|
|
52
|
+
.replace(/"/g, '"')
|
|
53
|
+
.replace(/'/g, ''');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function isHttpsUrl(value: string): boolean {
|
|
57
|
+
try {
|
|
58
|
+
return new URL(value).protocol === 'https:';
|
|
59
|
+
} catch {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function invalid(message: string): never {
|
|
65
|
+
throw new SankeyError(`Invalid header option: ${message}`, 'INVALID_HEADER');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function checkText(value: unknown, field: string): void {
|
|
69
|
+
if (value !== undefined && value !== null && typeof value !== 'string') {
|
|
70
|
+
invalid(`${field} must be a string or null`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Validate the shape of the `header` option. Throws SankeyError so a bad
|
|
76
|
+
* configuration fails at construction time, like the existing data checks.
|
|
77
|
+
*/
|
|
78
|
+
export function validateHeaderOptions(header: unknown): void {
|
|
79
|
+
if (header === undefined) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (header === null || typeof header !== 'object' || Array.isArray(header)) {
|
|
83
|
+
invalid('header must be an object');
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const h = header as Record<string, unknown>;
|
|
87
|
+
for (const key of Object.keys(h)) {
|
|
88
|
+
if (!(HEADER_KEYS as readonly string[]).includes(key)) {
|
|
89
|
+
invalid(`unknown key "${key}"`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
checkText(h.title, 'title');
|
|
94
|
+
checkText(h.subtitle, 'subtitle');
|
|
95
|
+
checkText(h.attribution, 'attribution');
|
|
96
|
+
|
|
97
|
+
if (h.affiliation !== undefined && h.affiliation !== null) {
|
|
98
|
+
if (!Array.isArray(h.affiliation) || !h.affiliation.every(line => typeof line === 'string')) {
|
|
99
|
+
invalid('affiliation must be an array of strings or null');
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (h.logo !== undefined && h.logo !== null) {
|
|
104
|
+
if (typeof h.logo !== 'object' || Array.isArray(h.logo)) {
|
|
105
|
+
invalid('logo must be an object or null');
|
|
106
|
+
}
|
|
107
|
+
const logo = h.logo as Record<string, unknown>;
|
|
108
|
+
for (const key of Object.keys(logo)) {
|
|
109
|
+
if (!(LOGO_KEYS as readonly string[]).includes(key)) {
|
|
110
|
+
invalid(`unknown logo key "${key}"`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (typeof logo.src !== 'string' || !isHttpsUrl(logo.src)) {
|
|
114
|
+
invalid('logo.src must be an https:// URL');
|
|
115
|
+
}
|
|
116
|
+
if (logo.href !== undefined && (typeof logo.href !== 'string' || !isHttpsUrl(logo.href))) {
|
|
117
|
+
invalid('logo.href must be an https:// URL');
|
|
118
|
+
}
|
|
119
|
+
if (logo.alt !== undefined && typeof logo.alt !== 'string') {
|
|
120
|
+
invalid('logo.alt must be a string');
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function substitute(template: string, context: HeaderContext): string {
|
|
126
|
+
return template
|
|
127
|
+
.replace(/\{country\}/g, context.country)
|
|
128
|
+
.replace(/\{firstYear\}/g, String(context.firstYear))
|
|
129
|
+
.replace(/\{lastYear\}/g, String(context.lastYear));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** `undefined` -> default, `null` -> hidden, blank after substitution -> hidden. */
|
|
133
|
+
function resolveText(value: string | null | undefined, fallback: string, context: HeaderContext): string | null {
|
|
134
|
+
if (value === null) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
const text = substitute(value ?? fallback, context).trim();
|
|
138
|
+
return text === '' ? null : text;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function resolveLogo(logo: SankeyHeaderLogo | null | undefined): ResolvedHeader['logo'] {
|
|
142
|
+
if (logo === null) {
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
if (logo === undefined) {
|
|
146
|
+
return {...DEFAULT_HEADER.logo};
|
|
147
|
+
}
|
|
148
|
+
return {src: logo.src, href: logo.href ?? null, alt: logo.alt ?? ''};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function resolveHeader(header: SankeyHeaderOptions | undefined, context: HeaderContext): ResolvedHeader {
|
|
152
|
+
const h = header ?? {};
|
|
153
|
+
|
|
154
|
+
let affiliation: string[] | null = null;
|
|
155
|
+
if (h.affiliation !== null) {
|
|
156
|
+
const lines = (h.affiliation ?? DEFAULT_HEADER.affiliation)
|
|
157
|
+
.map(line => substitute(line, context).trim())
|
|
158
|
+
.filter(line => line !== '');
|
|
159
|
+
affiliation = lines.length > 0 ? lines : null;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return {
|
|
163
|
+
title: resolveText(h.title, DEFAULT_HEADER.title, context),
|
|
164
|
+
subtitle: resolveText(h.subtitle, DEFAULT_HEADER.subtitle, context),
|
|
165
|
+
attribution: resolveText(h.attribution, DEFAULT_HEADER.attribution, context),
|
|
166
|
+
affiliation,
|
|
167
|
+
logo: resolveLogo(h.logo)
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Render the header block. The `.evs-energy-usage-overlay` and
|
|
173
|
+
* `.evs-year-overlay` containers are always emitted: RenderingService mounts the
|
|
174
|
+
* animated W/capita total and year into them, whatever text is hidden.
|
|
175
|
+
*/
|
|
176
|
+
export function renderHeaderHtml(header: ResolvedHeader): string {
|
|
177
|
+
let logoHtml = '';
|
|
178
|
+
if (header.logo) {
|
|
179
|
+
const alt = escapeHtml(header.logo.alt);
|
|
180
|
+
const titleAttr = alt ? ` title="${alt}"` : '';
|
|
181
|
+
const img = `<img src="${escapeHtml(header.logo.src)}" alt="${alt}"${titleAttr}>`;
|
|
182
|
+
const content = header.logo.href
|
|
183
|
+
? `<a href="${escapeHtml(header.logo.href)}" target="_blank" rel="noopener noreferrer">${img}</a>`
|
|
184
|
+
: img;
|
|
185
|
+
logoHtml = `
|
|
186
|
+
<div class="evs-header-logo">
|
|
187
|
+
${content}
|
|
188
|
+
</div>
|
|
189
|
+
`;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const titleHtml = header.title
|
|
193
|
+
? `<h1 class="evs-main-title">${escapeHtml(header.title)}</h1>`
|
|
194
|
+
: '';
|
|
195
|
+
|
|
196
|
+
let infoHtml = '';
|
|
197
|
+
if (header.subtitle || header.attribution) {
|
|
198
|
+
infoHtml = `
|
|
199
|
+
<div class="evs-header-info">
|
|
200
|
+
${header.subtitle ? `<div class="evs-subtitle">${escapeHtml(header.subtitle)}</div>` : ''}
|
|
201
|
+
${header.attribution ? `<div class="evs-attribution">${escapeHtml(header.attribution)}</div>` : ''}
|
|
202
|
+
</div>
|
|
203
|
+
`;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
let affiliationHtml = '';
|
|
207
|
+
if (header.affiliation) {
|
|
208
|
+
const lines = header.affiliation
|
|
209
|
+
.map(line => `<div class="evs-affiliation-text">${escapeHtml(line)}</div>`)
|
|
210
|
+
.join('\n ');
|
|
211
|
+
affiliationHtml = `
|
|
212
|
+
<div class="evs-header-affiliation">
|
|
213
|
+
${lines}
|
|
214
|
+
</div>
|
|
215
|
+
`;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return `
|
|
219
|
+
<div class="evs-sankey evs-title-container">
|
|
220
|
+
<div class="evs-header-content">${logoHtml}
|
|
221
|
+
<div class="evs-header-main">
|
|
222
|
+
<div class="evs-header-title-section">
|
|
223
|
+
${titleHtml}
|
|
224
|
+
<div class="evs-energy-usage-overlay">
|
|
225
|
+
<!-- SVG overlay for energy usage will be positioned here -->
|
|
226
|
+
</div>
|
|
227
|
+
</div>
|
|
228
|
+
</div>
|
|
229
|
+
|
|
230
|
+
<div class="evs-header-year">
|
|
231
|
+
<div class="evs-year-overlay">
|
|
232
|
+
<!-- SVG overlay for animated year will be positioned here -->
|
|
233
|
+
</div>
|
|
234
|
+
</div>
|
|
235
|
+
${infoHtml}${affiliationHtml}
|
|
236
|
+
</div>
|
|
237
|
+
</div>`;
|
|
238
|
+
}
|