energy-visualization-sankey 1.0.27 → 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 +125 -0
- package/babel.config.cjs +18 -21
- package/dist/.tsbuildinfo +1 -0
- package/dist/sankey.esm.js +490 -160
- package/dist/sankey.esm.js.map +1 -1
- package/dist/sankey.umd.js +490 -160
- package/dist/sankey.umd.js.map +1 -1
- package/dist/types/core/Sankey.d.ts +60 -46
- package/dist/types/core/Sankey.d.ts.map +1 -1
- package/dist/types/core/types/public-events.d.ts +159 -0
- package/dist/types/core/types/public-events.d.ts.map +1 -0
- package/dist/types/index.d.ts +2 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/services/AnimationService.d.ts.map +1 -1
- package/dist/types/services/InteractionService.d.ts.map +1 -1
- package/dist/types/services/data/DataValidationService.d.ts.map +1 -1
- package/dist/types/types/index.d.ts +40 -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/jest.config.cjs +22 -7
- package/package.json +3 -2
- package/src/core/Sankey.ts +291 -61
- package/src/core/types/public-events.ts +189 -0
- package/src/index.ts +23 -0
- package/src/services/AnimationService.ts +34 -19
- package/src/services/InteractionService.ts +49 -31
- package/src/services/data/DataValidationService.ts +8 -5
- package/src/types/index.ts +47 -0
- package/src/utils/header.ts +238 -0
- package/tests/event-bus.test.ts +137 -0
- package/tests/header.test.ts +198 -0
- package/tests/sankey-events.test.ts +289 -0
- package/tests/setup.js +13 -48
package/src/index.ts
CHANGED
|
@@ -17,6 +17,8 @@
|
|
|
17
17
|
// Export public type definitions for TypeScript consumers
|
|
18
18
|
export type {
|
|
19
19
|
SankeyOptions,
|
|
20
|
+
SankeyHeaderOptions,
|
|
21
|
+
SankeyHeaderLogo,
|
|
20
22
|
EnergyDataPoint,
|
|
21
23
|
SummaryData,
|
|
22
24
|
GraphData,
|
|
@@ -31,5 +33,26 @@ export {
|
|
|
31
33
|
DataValidationError
|
|
32
34
|
} from './types/index';
|
|
33
35
|
|
|
36
|
+
// Export the public event API types
|
|
37
|
+
export type {
|
|
38
|
+
SankeyEventMap,
|
|
39
|
+
SankeyPublicEventType,
|
|
40
|
+
SankeyPublicEvent,
|
|
41
|
+
SankeyEventListener,
|
|
42
|
+
SankeyEventHandlers,
|
|
43
|
+
SankeyUnsubscribe,
|
|
44
|
+
SankeyElementType,
|
|
45
|
+
SankeyMousePosition,
|
|
46
|
+
SankeyReadyEventData,
|
|
47
|
+
SankeyErrorEventData,
|
|
48
|
+
SankeyYearChangedEventData,
|
|
49
|
+
SankeyAnimationEventData,
|
|
50
|
+
SankeySpeedChangedEventData,
|
|
51
|
+
SankeyElementInteractionEventData,
|
|
52
|
+
SankeyKeypressEventData,
|
|
53
|
+
SankeySliderEventData,
|
|
54
|
+
SankeyButtonEventData
|
|
55
|
+
} from './core/types/public-events';
|
|
56
|
+
|
|
34
57
|
// Export the main visualization class
|
|
35
58
|
export {default as default} from './core/Sankey';
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import * as d3 from 'd3';
|
|
2
2
|
import {GraphData, SankeyOptions} from '@/types';
|
|
3
3
|
import {EventBus} from '@/core/events/EventBus';
|
|
4
|
+
import type {
|
|
5
|
+
SankeyAnimationEventData,
|
|
6
|
+
SankeySpeedChangedEventData,
|
|
7
|
+
SankeyYearChangedEventData
|
|
8
|
+
} from '@/core/types/public-events';
|
|
4
9
|
import {SummaryService} from '@/services/calculation/SummaryService';
|
|
5
10
|
import {GraphService} from '@/services/calculation/GraphService';
|
|
6
11
|
import {Logger} from "@/utils/Logger";
|
|
@@ -505,16 +510,20 @@ export class AnimationService {
|
|
|
505
510
|
|
|
506
511
|
// EVENT SYSTEM INTEGRATION: Notify other services of year change
|
|
507
512
|
// Enables coordinated updates across the entire visualization system
|
|
513
|
+
// Annotated against the public payload type so that any drift between
|
|
514
|
+
// what is emitted and what SankeyEventMap advertises fails type-check.
|
|
515
|
+
const yearChangedData: SankeyYearChangedEventData = {
|
|
516
|
+
year, // New target year
|
|
517
|
+
previousYear, // Previous year for transition context
|
|
518
|
+
yearIndex, // Array index for efficient data access
|
|
519
|
+
isAnimating: this.state.isAnimating // Animation state for context
|
|
520
|
+
};
|
|
521
|
+
|
|
508
522
|
this.eventBus.emit({
|
|
509
523
|
type: 'year.changed',
|
|
510
524
|
timestamp: Date.now(),
|
|
511
525
|
source: 'AnimationControlService',
|
|
512
|
-
data:
|
|
513
|
-
year, // New target year
|
|
514
|
-
previousYear, // Previous year for transition context
|
|
515
|
-
yearIndex, // Array index for efficient data access
|
|
516
|
-
isAnimating: this.state.isAnimating // Animation state for context
|
|
517
|
-
},
|
|
526
|
+
data: yearChangedData,
|
|
518
527
|
});
|
|
519
528
|
}
|
|
520
529
|
|
|
@@ -823,15 +832,17 @@ export class AnimationService {
|
|
|
823
832
|
|
|
824
833
|
// EVENT SYSTEM INTEGRATION: Broadcast animation start to other services
|
|
825
834
|
// Enables coordinated behavior across visualization components during playback
|
|
835
|
+
const startedData: SankeyAnimationEventData = {
|
|
836
|
+
isPlaying: true,
|
|
837
|
+
currentYear: this.getCurrentYear(), // Starting year for context
|
|
838
|
+
speed: this.state.speed // Playback speed for coordination
|
|
839
|
+
};
|
|
840
|
+
|
|
826
841
|
this.eventBus.emit({
|
|
827
842
|
type: 'animation.started',
|
|
828
843
|
timestamp: Date.now(),
|
|
829
844
|
source: 'AnimationControlService',
|
|
830
|
-
data:
|
|
831
|
-
isPlaying: true,
|
|
832
|
-
currentYear: this.getCurrentYear(), // Starting year for context
|
|
833
|
-
speed: this.state.speed // Playback speed for coordination
|
|
834
|
-
},
|
|
845
|
+
data: startedData,
|
|
835
846
|
});
|
|
836
847
|
}
|
|
837
848
|
|
|
@@ -877,15 +888,17 @@ export class AnimationService {
|
|
|
877
888
|
|
|
878
889
|
// EVENT SYSTEM INTEGRATION: Broadcast animation stop to other services
|
|
879
890
|
// Enables coordinated pause behavior across visualization components
|
|
891
|
+
const stoppedData: SankeyAnimationEventData = {
|
|
892
|
+
isPlaying: false,
|
|
893
|
+
currentYear: this.getCurrentYear(), // Current position preserved
|
|
894
|
+
speed: this.state.speed // Speed settings maintained
|
|
895
|
+
};
|
|
896
|
+
|
|
880
897
|
this.eventBus.emit({
|
|
881
898
|
type: 'animation.stopped',
|
|
882
899
|
timestamp: Date.now(),
|
|
883
900
|
source: 'AnimationControlService',
|
|
884
|
-
data:
|
|
885
|
-
isPlaying: false,
|
|
886
|
-
currentYear: this.getCurrentYear(), // Current position preserved
|
|
887
|
-
speed: this.state.speed // Speed settings maintained
|
|
888
|
-
}
|
|
901
|
+
data: stoppedData
|
|
889
902
|
});
|
|
890
903
|
}
|
|
891
904
|
|
|
@@ -927,13 +940,15 @@ export class AnimationService {
|
|
|
927
940
|
}
|
|
928
941
|
|
|
929
942
|
// Emit speed changed event
|
|
943
|
+
const speedChangedData: SankeySpeedChangedEventData = {
|
|
944
|
+
speed
|
|
945
|
+
};
|
|
946
|
+
|
|
930
947
|
this.eventBus.emit({
|
|
931
948
|
type: 'speed.changed',
|
|
932
949
|
timestamp: Date.now(),
|
|
933
950
|
source: 'AnimationControlService',
|
|
934
|
-
data:
|
|
935
|
-
speed
|
|
936
|
-
}
|
|
951
|
+
data: speedChangedData
|
|
937
952
|
});
|
|
938
953
|
|
|
939
954
|
// Animation speed updated for timeline playback
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import {EventBus} from '@/core/events/EventBus';
|
|
2
|
+
import type {
|
|
3
|
+
SankeyButtonEventData,
|
|
4
|
+
SankeyElementInteractionEventData,
|
|
5
|
+
SankeyKeypressEventData,
|
|
6
|
+
SankeySliderEventData
|
|
7
|
+
} from '@/core/types/public-events';
|
|
2
8
|
import {DataService} from "@/services/data/DataService";
|
|
3
9
|
import {AnimationService} from "@/services/AnimationService";
|
|
4
10
|
import {Logger} from "@/utils/Logger";
|
|
@@ -416,16 +422,18 @@ export class InteractionService {
|
|
|
416
422
|
// Emit keyboard navigation event - Event-Driven Architecture Integration
|
|
417
423
|
// Enables other services to react to keyboard interactions
|
|
418
424
|
// Provides rich event context (key + modifiers) for advanced interactions
|
|
425
|
+
const keypressData: SankeyKeypressEventData = {
|
|
426
|
+
key, // Primary key pressed (normalized to lowercase)
|
|
427
|
+
ctrlKey: event.ctrlKey, // Enables Ctrl+key shortcuts
|
|
428
|
+
shiftKey: event.shiftKey, // Enables Shift+key shortcuts
|
|
429
|
+
altKey: event.altKey // Enables Alt+key shortcuts (power user features)
|
|
430
|
+
};
|
|
431
|
+
|
|
419
432
|
this.eventBus.emit({
|
|
420
433
|
type: 'interaction.keypress',
|
|
421
434
|
timestamp: Date.now(),
|
|
422
435
|
source: 'InteractionService',
|
|
423
|
-
data:
|
|
424
|
-
key, // Primary key pressed (normalized to lowercase)
|
|
425
|
-
ctrlKey: event.ctrlKey, // Enables Ctrl+key shortcuts
|
|
426
|
-
shiftKey: event.shiftKey, // Enables Shift+key shortcuts
|
|
427
|
-
altKey: event.altKey // Enables Alt+key shortcuts (power user features)
|
|
428
|
-
}
|
|
436
|
+
data: keypressData
|
|
429
437
|
});
|
|
430
438
|
|
|
431
439
|
// Call custom handler if provided - Extensibility Pattern
|
|
@@ -477,14 +485,16 @@ export class InteractionService {
|
|
|
477
485
|
// Emit slider interaction event - Event-Driven Architecture
|
|
478
486
|
// Enables other services to react to timeline position changes
|
|
479
487
|
// Provides structured event data for logging and analytics
|
|
488
|
+
const sliderData: SankeySliderEventData = {
|
|
489
|
+
year, // Selected year value (numeric)
|
|
490
|
+
value: year // Duplicate for backwards compatibility
|
|
491
|
+
};
|
|
492
|
+
|
|
480
493
|
this.eventBus.emit({
|
|
481
494
|
type: 'interaction.slider',
|
|
482
495
|
timestamp: Date.now(),
|
|
483
496
|
source: 'InteractionService',
|
|
484
|
-
data:
|
|
485
|
-
year, // Selected year value (numeric)
|
|
486
|
-
value: year // Duplicate for backwards compatibility
|
|
487
|
-
}
|
|
497
|
+
data: sliderData
|
|
488
498
|
});
|
|
489
499
|
|
|
490
500
|
// Call custom handler if provided - Extensibility Pattern
|
|
@@ -544,14 +554,16 @@ export class InteractionService {
|
|
|
544
554
|
// Emit button interaction event - Event-Driven Architecture
|
|
545
555
|
// Enables other services to react to animation state changes
|
|
546
556
|
// Note: Action reflects the RESULT of the button press, not the current state
|
|
557
|
+
const playButtonData: SankeyButtonEventData = {
|
|
558
|
+
buttonId: 'evs-play-button',
|
|
559
|
+
action: this.animationControlService.isPlaying() ? 'play' : 'pause'
|
|
560
|
+
};
|
|
561
|
+
|
|
547
562
|
this.eventBus.emit({
|
|
548
563
|
type: 'interaction.button',
|
|
549
564
|
timestamp: Date.now(),
|
|
550
565
|
source: 'InteractionService',
|
|
551
|
-
data:
|
|
552
|
-
buttonId: 'evs-play-button',
|
|
553
|
-
action: this.animationControlService.isPlaying() ? 'play' : 'pause'
|
|
554
|
-
}
|
|
566
|
+
data: playButtonData
|
|
555
567
|
});
|
|
556
568
|
};
|
|
557
569
|
|
|
@@ -572,15 +584,17 @@ export class InteractionService {
|
|
|
572
584
|
|
|
573
585
|
// Emit speed change event - Event-Driven Architecture
|
|
574
586
|
// Enables UI updates (active button state, speed indicator)
|
|
587
|
+
const speedButtonData: SankeyButtonEventData = {
|
|
588
|
+
buttonId: target.id,
|
|
589
|
+
action: 'speed-change',
|
|
590
|
+
speed // New animation speed in milliseconds
|
|
591
|
+
};
|
|
592
|
+
|
|
575
593
|
this.eventBus.emit({
|
|
576
594
|
type: 'interaction.button',
|
|
577
595
|
timestamp: Date.now(),
|
|
578
596
|
source: 'InteractionService',
|
|
579
|
-
data:
|
|
580
|
-
buttonId: target.id,
|
|
581
|
-
action: 'speed-change',
|
|
582
|
-
speed // New animation speed in milliseconds
|
|
583
|
-
}
|
|
597
|
+
data: speedButtonData
|
|
584
598
|
});
|
|
585
599
|
};
|
|
586
600
|
|
|
@@ -704,16 +718,18 @@ export class InteractionService {
|
|
|
704
718
|
|
|
705
719
|
// Emit hover event - Event-Driven Architecture
|
|
706
720
|
// Provides rich context for tooltip display, analytics, and custom behavior
|
|
721
|
+
const hoverData: SankeyElementInteractionEventData = {
|
|
722
|
+
elementType: element.classList.contains('evs-flow') ? 'flow' : 'box', // Element classification
|
|
723
|
+
fuel: element.getAttribute('data-fuel'), // Energy source identification
|
|
724
|
+
sector: element.getAttribute('data-sector'), // Consumption sector identification
|
|
725
|
+
mousePosition: {x: event.clientX, y: event.clientY} // Coordinates for tooltip positioning
|
|
726
|
+
};
|
|
727
|
+
|
|
707
728
|
this.eventBus.emit({
|
|
708
729
|
type: 'interaction.hover',
|
|
709
730
|
timestamp: Date.now(),
|
|
710
731
|
source: 'InteractionService',
|
|
711
|
-
data:
|
|
712
|
-
elementType: element.classList.contains('evs-flow') ? 'flow' : 'box', // Element classification
|
|
713
|
-
fuel: element.getAttribute('data-fuel'), // Energy source identification
|
|
714
|
-
sector: element.getAttribute('data-sector'), // Consumption sector identification
|
|
715
|
-
mousePosition: {x: event.clientX, y: event.clientY} // Coordinates for tooltip positioning
|
|
716
|
-
}
|
|
732
|
+
data: hoverData
|
|
717
733
|
});
|
|
718
734
|
|
|
719
735
|
// Call custom handler if provided - Extensibility Pattern
|
|
@@ -758,16 +774,18 @@ export class InteractionService {
|
|
|
758
774
|
|
|
759
775
|
// Emit click event - Event-Driven Architecture
|
|
760
776
|
// Provides comprehensive click context for analytics, tooltips, and custom behavior
|
|
777
|
+
const clickData: SankeyElementInteractionEventData = {
|
|
778
|
+
elementType: element.classList.contains('evs-flow') ? 'flow' : 'box', // Element classification
|
|
779
|
+
fuel: element.getAttribute('data-fuel'), // Energy source identification
|
|
780
|
+
sector: element.getAttribute('data-sector'), // Consumption sector identification
|
|
781
|
+
mousePosition: {x: event.clientX, y: event.clientY} // Click coordinates for UI positioning
|
|
782
|
+
};
|
|
783
|
+
|
|
761
784
|
this.eventBus.emit({
|
|
762
785
|
type: 'interaction.click',
|
|
763
786
|
timestamp: Date.now(),
|
|
764
787
|
source: 'InteractionService',
|
|
765
|
-
data:
|
|
766
|
-
elementType: element.classList.contains('evs-flow') ? 'flow' : 'box', // Element classification
|
|
767
|
-
fuel: element.getAttribute('data-fuel'), // Energy source identification
|
|
768
|
-
sector: element.getAttribute('data-sector'), // Consumption sector identification
|
|
769
|
-
mousePosition: {x: event.clientX, y: event.clientY} // Click coordinates for UI positioning
|
|
770
|
-
}
|
|
788
|
+
data: clickData
|
|
771
789
|
});
|
|
772
790
|
|
|
773
791
|
// Call custom handler if provided - Extensibility Pattern
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type {EnergyDataPoint} from '@/types';
|
|
2
2
|
import {DataValidationError} from '@/types';
|
|
3
3
|
import {EventBus} from '@/core/events/EventBus';
|
|
4
|
+
import type {SankeyErrorEventData} from '@/core/types/public-events';
|
|
4
5
|
import {Logger} from "@/utils/Logger";
|
|
5
6
|
import {ConfigurationService} from "@/services/ConfigurationService";
|
|
6
7
|
|
|
@@ -135,15 +136,17 @@ export class DataValidationService {
|
|
|
135
136
|
// Emit validation error event for error handling - Error Recovery Architecture
|
|
136
137
|
// Enables centralized error handling and user notification systems
|
|
137
138
|
// Marks validation errors as non-recoverable (require data fix)
|
|
139
|
+
const validationErrorData: SankeyErrorEventData = {
|
|
140
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
141
|
+
context: 'data_validation', // Error categorization for debugging
|
|
142
|
+
recoverable: false // Validation errors require data correction
|
|
143
|
+
};
|
|
144
|
+
|
|
138
145
|
this.eventBus.emit({
|
|
139
146
|
type: 'system.error',
|
|
140
147
|
timestamp: Date.now(),
|
|
141
148
|
source: 'DataValidationService',
|
|
142
|
-
data:
|
|
143
|
-
error: error instanceof Error ? error : new Error(String(error)),
|
|
144
|
-
context: 'data_validation', // Error categorization for debugging
|
|
145
|
-
recoverable: false // Validation errors require data correction
|
|
146
|
-
}
|
|
149
|
+
data: validationErrorData
|
|
147
150
|
});
|
|
148
151
|
|
|
149
152
|
// Re-throw error to maintain validation contract - Fail-Fast Pattern
|
package/src/types/index.ts
CHANGED
|
@@ -19,6 +19,11 @@
|
|
|
19
19
|
*
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
|
+
// Relative on purpose. The exact `@/types` alias entry is mis-rewritten by
|
|
23
|
+
// tsc-alias to the public barrel, so alias imports reaching the declaration
|
|
24
|
+
// surface are unsafe. Relative specifiers are never rewritten.
|
|
25
|
+
import type {SankeyEventHandlers} from '../core/types/public-events';
|
|
26
|
+
|
|
22
27
|
// Error types
|
|
23
28
|
export class SankeyError extends Error {
|
|
24
29
|
constructor(message: string, public code?: string) {
|
|
@@ -64,6 +69,36 @@ export interface EnergyDataPoint {
|
|
|
64
69
|
readonly heat: EnergySectorBreakdown;
|
|
65
70
|
}
|
|
66
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
|
+
|
|
67
102
|
// Options interface
|
|
68
103
|
export interface SankeyOptions {
|
|
69
104
|
readonly data: EnergyDataPoint[];
|
|
@@ -85,6 +120,18 @@ export interface SankeyOptions {
|
|
|
85
120
|
readonly heatBoxY?: number;
|
|
86
121
|
readonly loopAnimation?: boolean;
|
|
87
122
|
readonly debugLogging?: boolean;
|
|
123
|
+
readonly header?: SankeyHeaderOptions;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Event handlers registered before asynchronous initialization begins.
|
|
127
|
+
*
|
|
128
|
+
* Prefer this over `.on()` for lifecycle events. `system.ready` and
|
|
129
|
+
* `system.error` are produced during the initialization the constructor
|
|
130
|
+
* starts, and the event bus drops events that have no subscriber at emit
|
|
131
|
+
* time, so a subscription added later can miss them entirely. Interaction
|
|
132
|
+
* events can be attached either way.
|
|
133
|
+
*/
|
|
134
|
+
readonly events?: SankeyEventHandlers;
|
|
88
135
|
}
|
|
89
136
|
|
|
90
137
|
// Mathematical structures
|
|
@@ -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
|
+
}
|