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.
@@ -4447,6 +4447,207 @@ class Logger {
4447
4447
  }
4448
4448
  }
4449
4449
 
4450
+ /**
4451
+ * Header configuration: defaults, validation, placeholder substitution and
4452
+ * HTML rendering for the title block above the chart.
4453
+ *
4454
+ * Every value that reaches the markup goes through escapeHtml(). Header text and
4455
+ * `country` come from host configuration that, in the Energy Visualization
4456
+ * Platform, is user-entered and rendered on public share pages, so they must
4457
+ * never be interpreted as HTML.
4458
+ */
4459
+ const RDCEP_NAME = 'The Center for Robust Decision-making on Climate and Energy Policy (RDCEP)';
4460
+ /** The header shown when no `header` option is given (unchanged from earlier releases). */
4461
+ const DEFAULT_HEADER = {
4462
+ title: '{country} energy usage',
4463
+ subtitle: 'Energy Transitions in {country} History, {firstYear}-{lastYear}',
4464
+ attribution: 'Suits, Matteson, and Moyer (2020)',
4465
+ affiliation: ['Center for Robust Decision-making on', 'Climate and Energy Policy, UChicago'],
4466
+ logo: {
4467
+ src: 'https://images.squarespace-cdn.com/content/v1/54dcfad0e4b0eaff5e0068bf/1446137765478-FX9WM00VV1LWAFUJRZBI/rdcep+sig2.png',
4468
+ href: 'https://www.rdcep.org/',
4469
+ alt: RDCEP_NAME
4470
+ }
4471
+ };
4472
+ const HEADER_KEYS = ['title', 'subtitle', 'attribution', 'affiliation', 'logo'];
4473
+ const LOGO_KEYS = ['src', 'href', 'alt'];
4474
+ function escapeHtml(value) {
4475
+ return value
4476
+ .replace(/&/g, '&')
4477
+ .replace(/</g, '&lt;')
4478
+ .replace(/>/g, '&gt;')
4479
+ .replace(/"/g, '&quot;')
4480
+ .replace(/'/g, '&#39;');
4481
+ }
4482
+ function isHttpsUrl(value) {
4483
+ try {
4484
+ return new URL(value).protocol === 'https:';
4485
+ }
4486
+ catch (_a) {
4487
+ return false;
4488
+ }
4489
+ }
4490
+ function invalid(message) {
4491
+ throw new SankeyError(`Invalid header option: ${message}`, 'INVALID_HEADER');
4492
+ }
4493
+ function checkText(value, field) {
4494
+ if (value !== undefined && value !== null && typeof value !== 'string') {
4495
+ invalid(`${field} must be a string or null`);
4496
+ }
4497
+ }
4498
+ /**
4499
+ * Validate the shape of the `header` option. Throws SankeyError so a bad
4500
+ * configuration fails at construction time, like the existing data checks.
4501
+ */
4502
+ function validateHeaderOptions(header) {
4503
+ if (header === undefined) {
4504
+ return;
4505
+ }
4506
+ if (header === null || typeof header !== 'object' || Array.isArray(header)) {
4507
+ invalid('header must be an object');
4508
+ }
4509
+ const h = header;
4510
+ for (const key of Object.keys(h)) {
4511
+ if (!HEADER_KEYS.includes(key)) {
4512
+ invalid(`unknown key "${key}"`);
4513
+ }
4514
+ }
4515
+ checkText(h.title, 'title');
4516
+ checkText(h.subtitle, 'subtitle');
4517
+ checkText(h.attribution, 'attribution');
4518
+ if (h.affiliation !== undefined && h.affiliation !== null) {
4519
+ if (!Array.isArray(h.affiliation) || !h.affiliation.every(line => typeof line === 'string')) {
4520
+ invalid('affiliation must be an array of strings or null');
4521
+ }
4522
+ }
4523
+ if (h.logo !== undefined && h.logo !== null) {
4524
+ if (typeof h.logo !== 'object' || Array.isArray(h.logo)) {
4525
+ invalid('logo must be an object or null');
4526
+ }
4527
+ const logo = h.logo;
4528
+ for (const key of Object.keys(logo)) {
4529
+ if (!LOGO_KEYS.includes(key)) {
4530
+ invalid(`unknown logo key "${key}"`);
4531
+ }
4532
+ }
4533
+ if (typeof logo.src !== 'string' || !isHttpsUrl(logo.src)) {
4534
+ invalid('logo.src must be an https:// URL');
4535
+ }
4536
+ if (logo.href !== undefined && (typeof logo.href !== 'string' || !isHttpsUrl(logo.href))) {
4537
+ invalid('logo.href must be an https:// URL');
4538
+ }
4539
+ if (logo.alt !== undefined && typeof logo.alt !== 'string') {
4540
+ invalid('logo.alt must be a string');
4541
+ }
4542
+ }
4543
+ }
4544
+ function substitute(template, context) {
4545
+ return template
4546
+ .replace(/\{country\}/g, context.country)
4547
+ .replace(/\{firstYear\}/g, String(context.firstYear))
4548
+ .replace(/\{lastYear\}/g, String(context.lastYear));
4549
+ }
4550
+ /** `undefined` -> default, `null` -> hidden, blank after substitution -> hidden. */
4551
+ function resolveText(value, fallback, context) {
4552
+ if (value === null) {
4553
+ return null;
4554
+ }
4555
+ const text = substitute(value !== null && value !== void 0 ? value : fallback, context).trim();
4556
+ return text === '' ? null : text;
4557
+ }
4558
+ function resolveLogo(logo) {
4559
+ var _a, _b;
4560
+ if (logo === null) {
4561
+ return null;
4562
+ }
4563
+ if (logo === undefined) {
4564
+ return { ...DEFAULT_HEADER.logo };
4565
+ }
4566
+ return { src: logo.src, href: (_a = logo.href) !== null && _a !== void 0 ? _a : null, alt: (_b = logo.alt) !== null && _b !== void 0 ? _b : '' };
4567
+ }
4568
+ function resolveHeader(header, context) {
4569
+ var _a;
4570
+ const h = header !== null && header !== void 0 ? header : {};
4571
+ let affiliation = null;
4572
+ if (h.affiliation !== null) {
4573
+ const lines = ((_a = h.affiliation) !== null && _a !== void 0 ? _a : DEFAULT_HEADER.affiliation)
4574
+ .map(line => substitute(line, context).trim())
4575
+ .filter(line => line !== '');
4576
+ affiliation = lines.length > 0 ? lines : null;
4577
+ }
4578
+ return {
4579
+ title: resolveText(h.title, DEFAULT_HEADER.title, context),
4580
+ subtitle: resolveText(h.subtitle, DEFAULT_HEADER.subtitle, context),
4581
+ attribution: resolveText(h.attribution, DEFAULT_HEADER.attribution, context),
4582
+ affiliation,
4583
+ logo: resolveLogo(h.logo)
4584
+ };
4585
+ }
4586
+ /**
4587
+ * Render the header block. The `.evs-energy-usage-overlay` and
4588
+ * `.evs-year-overlay` containers are always emitted: RenderingService mounts the
4589
+ * animated W/capita total and year into them, whatever text is hidden.
4590
+ */
4591
+ function renderHeaderHtml(header) {
4592
+ let logoHtml = '';
4593
+ if (header.logo) {
4594
+ const alt = escapeHtml(header.logo.alt);
4595
+ const titleAttr = alt ? ` title="${alt}"` : '';
4596
+ const img = `<img src="${escapeHtml(header.logo.src)}" alt="${alt}"${titleAttr}>`;
4597
+ const content = header.logo.href
4598
+ ? `<a href="${escapeHtml(header.logo.href)}" target="_blank" rel="noopener noreferrer">${img}</a>`
4599
+ : img;
4600
+ logoHtml = `
4601
+ <div class="evs-header-logo">
4602
+ ${content}
4603
+ </div>
4604
+ `;
4605
+ }
4606
+ const titleHtml = header.title
4607
+ ? `<h1 class="evs-main-title">${escapeHtml(header.title)}</h1>`
4608
+ : '';
4609
+ let infoHtml = '';
4610
+ if (header.subtitle || header.attribution) {
4611
+ infoHtml = `
4612
+ <div class="evs-header-info">
4613
+ ${header.subtitle ? `<div class="evs-subtitle">${escapeHtml(header.subtitle)}</div>` : ''}
4614
+ ${header.attribution ? `<div class="evs-attribution">${escapeHtml(header.attribution)}</div>` : ''}
4615
+ </div>
4616
+ `;
4617
+ }
4618
+ let affiliationHtml = '';
4619
+ if (header.affiliation) {
4620
+ const lines = header.affiliation
4621
+ .map(line => `<div class="evs-affiliation-text">${escapeHtml(line)}</div>`)
4622
+ .join('\n ');
4623
+ affiliationHtml = `
4624
+ <div class="evs-header-affiliation">
4625
+ ${lines}
4626
+ </div>
4627
+ `;
4628
+ }
4629
+ return `
4630
+ <div class="evs-sankey evs-title-container">
4631
+ <div class="evs-header-content">${logoHtml}
4632
+ <div class="evs-header-main">
4633
+ <div class="evs-header-title-section">
4634
+ ${titleHtml}
4635
+ <div class="evs-energy-usage-overlay">
4636
+ <!-- SVG overlay for energy usage will be positioned here -->
4637
+ </div>
4638
+ </div>
4639
+ </div>
4640
+
4641
+ <div class="evs-header-year">
4642
+ <div class="evs-year-overlay">
4643
+ <!-- SVG overlay for animated year will be positioned here -->
4644
+ </div>
4645
+ </div>
4646
+ ${infoHtml}${affiliationHtml}
4647
+ </div>
4648
+ </div>`;
4649
+ }
4650
+
4450
4651
  /**
4451
4652
  * Configuration Service - Mathematical Constants & Visual Parameters
4452
4653
  *
@@ -7063,46 +7264,12 @@ class Sankey {
7063
7264
  */
7064
7265
  injectHTML() {
7065
7266
  var _a;
7066
- const country = this.options.country;
7067
- const firstYear = this.services.dataService.firstYear;
7068
- const lastYear = this.services.dataService.lastYear;
7069
- let html = `
7070
- <div class="evs-sankey evs-title-container">
7071
- <div class="evs-header-content">
7072
- <div class="evs-header-logo">
7073
- <a href="https://www.rdcep.org/" target="_blank">
7074
- <img src="https://images.squarespace-cdn.com/content/v1/54dcfad0e4b0eaff5e0068bf/1446137765478-FX9WM00VV1LWAFUJRZBI/rdcep+sig2.png"
7075
- alt="The Center for Robust Decision-making on Climate and Energy Policy (RDCEP)"
7076
- title="The Center for Robust Decision-making on Climate and Energy Policy (RDCEP)">
7077
- </a>
7078
- </div>
7079
-
7080
- <div class="evs-header-main">
7081
- <div class="evs-header-title-section">
7082
- <h1 class="evs-main-title">${country} energy usage</h1>
7083
- <div class="evs-energy-usage-overlay">
7084
- <!-- SVG overlay for energy usage will be positioned here -->
7085
- </div>
7086
- </div>
7087
- </div>
7088
-
7089
- <div class="evs-header-year">
7090
- <div class="evs-year-overlay">
7091
- <!-- SVG overlay for animated year will be positioned here -->
7092
- </div>
7093
- </div>
7094
-
7095
- <div class="evs-header-info">
7096
- <div class="evs-subtitle">Energy Transitions in ${country} History, ${firstYear}-${lastYear}</div>
7097
- <div class="evs-attribution">Suits, Matteson, and Moyer (2020)</div>
7098
- </div>
7099
-
7100
- <div class="evs-header-affiliation">
7101
- <div class="evs-affiliation-text">Center for Robust Decision-making on</div>
7102
- <div class="evs-affiliation-text">Climate and Energy Policy, UChicago</div>
7103
- </div>
7104
- </div>
7105
- </div>
7267
+ const header = resolveHeader(this.options.header, {
7268
+ country: this.options.country,
7269
+ firstYear: this.services.dataService.firstYear,
7270
+ lastYear: this.services.dataService.lastYear
7271
+ });
7272
+ let html = `${renderHeaderHtml(header)}
7106
7273
  <div class="evs-us-energy-sankey-wrapper" style="width: ${(_a = this.services['configurationService']) === null || _a === void 0 ? void 0 : _a.WIDTH}px;">
7107
7274
  <div class="evs-sankey" style="line-height: 0;"></div>
7108
7275
  `;
@@ -7647,6 +7814,7 @@ class Sankey {
7647
7814
  }
7648
7815
  // Validate data structure
7649
7816
  this.validateDataStructure(options.data);
7817
+ validateHeaderOptions(options.header);
7650
7818
  }
7651
7819
  validateDataStructure(data) {
7652
7820
  // Comprehensive data structure validation