Skip to content

Commit

Permalink
remove more inline scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Boldewyn committed Feb 28, 2024
1 parent 574b36b commit 7a8b4f9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
8 changes: 4 additions & 4 deletions codepoints.net/views/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

$head_extra = <<<HEAD_EXTRA
$noindex
<script>
var script_age = $script_age;
var region_to_block = $region_to_block;
</script>
HEAD_EXTRA;
include 'partials/header.php' ?>
<script type="application/json" id="search_metadata">
{ "script_age": <?=$script_age?>,
"region_to_block": <?=$region_to_block?>}
</script>
<main class="main main--search"<?php if ($search_result):?> data-count="<?=q((string)$search_result->count())?>"<?php endif ?>>
<h1><?=_q($title)?></h1>

Expand Down
7 changes: 4 additions & 3 deletions src/js/main.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import 'vite/modulepreload-polyfill';

import barba from '@barba/core';
import { initGlossary } from './_glossary.js';
import { trackByContent } from './_tracker.js';

import './components.js';

document.documentElement.classList.add('js');

function init(context) {
initGlossary(context);

/* embedded context? Open the full view in a new tab when
* clicking on the image. */
if (window.parent !== window) {
Expand All @@ -23,6 +20,10 @@ function init(context) {
const main = context.querySelector('main');
if (main && main.classList.contains('main--scripts')) {
import('./modules/scripts.js').then((mod) => { mod.default(context); });
} else if (main && main.classList.contains('main--glossary')) {
import('./modules/glossary.js').then((mod) => { mod.default(context); });
} else if (main && main.classList.contains('main--search')) {
import('./modules/search.js').then((mod) => { mod.default(context); });
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/js/_glossary.js → src/js/modules/glossary.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {gettext as _} from './_i18n.ts';
import {gettext as _} from '../_i18n.ts';

/**
* create the A-B-C jump bar at the top of the actual glossary
*/
export function initGlossary(context) {
export default function(context) {
const glossary = context.querySelector('#glossary');
if (!glossary || glossary.dataset.upgraded) {
return;
Expand Down
12 changes: 12 additions & 0 deletions src/js/modules/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default function(context) {
const data = context.querySelector('#search_metadata');
if (data) {
const parsedData = JSON.parse(data.textContent);
if ('script_age' in parsedData) {
window.script_age = parsedData.script_age;
}
if ('region_to_block' in parsedData) {
window.region_to_block = parsedData.region_to_block;
}
}
}

0 comments on commit 7a8b4f9

Please sign in to comment.