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