Skip to content

Commit

Permalink
migrate: Add frontend Javascript; Add <site-busy-spinner> WebC (#75)
Browse files Browse the repository at this point in the history
* JS custom elements: <my-feed>, <my-search>, <my-analytics>
  • Loading branch information
nfreear committed Mar 18, 2023
1 parent 8814aac commit 99993f1
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 22 deletions.
7 changes: 6 additions & 1 deletion _components/site-analytics.webc
Expand Up @@ -12,7 +12,12 @@
</script>

<img
webc:if="analytics.enable"
webc:if="analytics.enableGoatCounter"
:src="`https://${analytics.gcId}.goatcounter.com/count?p=${_enc(analytics.host + page.url)}&t=${_enc(title)}&b=0`"
alt=""
/>

<my-analytics
webc:if="analytics.enableGoogleAnalytics"
:analytics-id="analytics.googleAID"
></my-analytics>
36 changes: 36 additions & 0 deletions _components/site-busy-spinner.webc
@@ -0,0 +1,36 @@

<div class="site-busy-spinner">
<div class="spinner-text">
<slot>
<p> Loading &hellip; </p>
</slot>
</div>
<div class="spinner-animation"></div>
</div>

<style>
.site-busy-spinner {
margin: 2rem auto;
text-align: center;
}
.site-busy-spinner .spinner-animation {
border: 2.5vh solid teal;
border-bottom-color: #ddd;
border-radius: 50%;
margin: 1rem auto;
height: 35vh;
width: 35vh;

animation-direction: normal;
animation-duration: 2s;
animation-iteration-count: infinite;
animation-timing-function: linear; /* << +1 ! */
animation-play-state: running;
animation-name: Rotate;
}

@keyframes Rotate {
0% { transform: rotateZ(0deg); }
100% { transform: rotateZ(360deg); }
}
</style>
10 changes: 6 additions & 4 deletions _data/analytics.js
@@ -1,13 +1,15 @@
/**
* Site analytics.
* Site analytics settings.
*
* @see _components/site-analytics.webc
*/

// console.log('analytics.js:', process.env, this, global);

module.exports = {
"enable": true,
"gcId": "ndf812",
"host": process.env.CI ? "nfreear.github.io" : "localhost:8080"
"enableGoatCounter": true,
"gcId": "ndf812",
"enableGoogleAnalytics": true,
"googleAID": "UA-8330079-6",
"host": process.env.CI ? "nfreear.github.io" : "localhost:8080"
};
14 changes: 10 additions & 4 deletions _data/site.js
@@ -1,10 +1,16 @@
/**
* Site settings.
*
* @legacy Carried from Jekyll.
* General site settings.
*/

const frontendJavascript = () => process.env.JS === '1';

module.exports = {
date_format: "%-d %B %Y",
frontendJavascript: frontendJavascript(),
googSearchId: '001222343498871500969:-u73i2qfu2s',

/** @legacy Carried from Jekyll. */
date_format: "%-d %B %Y",
excerpt_truncate: 28
};

console.debug('[11ty]', 'Javascript (env):', frontendJavascript()); // typeof process.env.JS;
8 changes: 7 additions & 1 deletion _includes/layouts/base.webc
@@ -1,5 +1,5 @@
<!doctype html>
<html :lang="metadata.language">
<html :lang="metadata.language" class="no-js">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Expand Down Expand Up @@ -40,5 +40,11 @@
<site-analytics webc:nokeep></site-analytics>
</footer>

<script
webc:if="site.frontendJavascript"
webc:keep
:src="url('/lib/app.mjs')"
type="module"
></script>
</body>
</html>
15 changes: 10 additions & 5 deletions _pages/links.html
@@ -1,7 +1,8 @@
---
layout: page
title: Links & Bookmarks
permalink: /links
title: Bookmarks
# title: Links & Bookmarks
permalink: /links/
x-nav-class: sr-only
x-created: 2022-12-09
---
Expand All @@ -12,7 +13,11 @@
include="accessibility,disability,usability,research,security,youtube"
href="https://nfreear.github.io/bookmarks/feed.json"
>
<my-busy-spinner class="loading-container">
<p> Loading bookmarks … </p>
</my-busy-spinner>
<site-busy-spinner webc:nokeep>
<p> Loading bookmarks … (Javascript) </p>
</site-busy-spinner>
</my-feed>

<p webc:if="!site.enableJavascript">
[Site search & Javascript disabled.]
</p>
23 changes: 20 additions & 3 deletions _pages/search.html
Expand Up @@ -10,8 +10,20 @@

<h1 class="page-heading"><label for="gsc-i-id1">Search</label></h1>


<script>
<my-search
webc:keep
:cx="site.googSearchId"
>
<site-busy-spinner webc:nokeep>
<p id="x-cse-loading"> Loading search … (Javascript) </p>
</site-busy-spinner>
</my-search>

<p webc:if="!site.enableJavascript">
[Site search & Javascript disabled.]
</p>

<script webc:nokeep>
(function (document, L) {
if (L.search.match(/[\?&]q=\w+/)) {
var cx = '001222343498871500969:-u73i2qfu2s';
Expand All @@ -26,7 +38,12 @@ <h1 class="page-heading"><label for="gsc-i-id1">Search</label></h1>
})(window.document, window.location);
</script>

<gcse:search><form><p id="x-cse-loading"> Search loading ... </p></form></gcse:search>
<!-- <gcse:search>
<form>
<p id="x-cse-loading"> Loading search … (Javascript) </p>
<site-busy-spinner webc:nokeep></site-busy-spinner>
</form>
</gcse:search> -->


<p id="plugin"></p>
Expand Down
10 changes: 10 additions & 0 deletions css/style.css
Expand Up @@ -79,11 +79,21 @@ p:last-child {
margin-bottom: 0;
}

my-feed {
display: block;
margin: 2rem 0;
}

p,
li {
line-height: 1.5;
}

my-feed::part(a) {
line-height: 2;
}

my-feed::part(a),
a[href] {
color: var(--text-color-link);
}
Expand Down
3 changes: 2 additions & 1 deletion eleventy.config.js
Expand Up @@ -22,6 +22,7 @@ module.exports = (eleventyConfig) => {
// eleventyConfig.addPassthroughCopy('assets/fonts');

eleventyConfig.addPassthroughCopy('css');
eleventyConfig.addPassthroughCopy('lib'); // Was: 'js'
eleventyConfig.addPassthroughCopy('favicon.ico');

layoutAlias.forEach(alias => {
Expand All @@ -31,7 +32,7 @@ module.exports = (eleventyConfig) => {

eleventyConfig.on('eleventy.after', async ({ dir, results, runMode, outputMode }) => {
// console.debug('> 11ty.CFG:', eleventyConfig);
console.debug('> 11ty.After:', dir, runMode, outputMode); // Not: results;
console.debug('[11ty]', 'After:', dir, runMode, outputMode); // Not: results;
});

return {
Expand Down
9 changes: 9 additions & 0 deletions lib/app.mjs
@@ -0,0 +1,9 @@

import customImport from 'https://nfreear.github.io/elements/custom.js';

await customImport('my-analytics, my-feed, my-search');

document.documentElement.classList.remove('no-js');
document.documentElement.classList.add('js');

console.debug('app.mjs');
7 changes: 4 additions & 3 deletions package.json
Expand Up @@ -12,10 +12,11 @@
"luxon": "^3.2.1"
},
"scripts": {
"build": "npx @11ty/eleventy",
"build": "JS=1 npx @11ty/eleventy",
"clean": "rm -rf _site",
"start:debug": "DEBUG=Eleventy* npx @11ty/eleventy --serve",
"start": "npx @11ty/eleventy --serve --quiet"
"start:debug": "JS=1 DEBUG=Eleventy* npx @11ty/eleventy --serve",
"start:nojs": "JS=0 npx @11ty/eleventy --serve --quiet",
"start": "JS=1 npx @11ty/eleventy --serve --quiet"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 99993f1

Please sign in to comment.