improve and extend configuration, using Hugo's $.Param method

This commit is contained in:
Helmut Merz 2023-03-10 13:43:41 +01:00
parent 463fdb266b
commit 59bb87d204
4 changed files with 28 additions and 19 deletions

View file

@ -1,30 +1,32 @@
import { createApp } from 'petite-vue' import { createApp } from 'petite-vue'
import { api, polling } from './settings' import { api, polling } from './settings'
import { pageid } from '@params' import { pageid, domain } from '@params'
const interaction = createRandString(1) type Config = {
type confdata = {
apiurl: string apiurl: string
pollurl: string pollurl: string
domain: string
itemid: string itemid: string
sid: string sid: string
iid: string
} }
export function config (api, polling): confdata { export function config (api, polling): Config {
const pu = polling ? `${api.path}/${polling.msgbase.join('/')}` : '' const pu = polling ? `${api.path}/${polling.msgbase.join('/')}` : ''
const urlparams = new URL(location.href).searchParams const urlparams = new URL(location.href).searchParams
return { return {
apiurl: api.path, apiurl: api.path,
pollurl: pu, pollurl: pu,
domain: domain,
itemid: urlparams.get('id'), itemid: urlparams.get('id'),
sid: getSid() sid: getSid(),
iid: createRandString(1)
} }
} }
export const pvapp = { export const pvapp = {
run(conf: confdata) { run(conf: Config) {
appdata.conf = conf appdata.conf = conf
createApp(appdata).mount() createApp(appdata).mount()
if (appdata.conf.pollurl) { if (appdata.conf.pollurl) {
@ -35,7 +37,7 @@ export const pvapp = {
const appdata = { const appdata = {
$delimiters: ['{|', '|}'], $delimiters: ['{|', '|}'],
conf: {} as confdata, conf: {} as Config,
components: {}, components: {},
output: '', output: '',
handle, handle,
@ -46,7 +48,6 @@ const appdata = {
Data Data
} }
// components // components
function Data(name: string): object { function Data(name: string): object {
@ -62,12 +63,14 @@ function Data(name: string): object {
// appdata and component method definitions // appdata and component method definitions
function save() { function save() {
const data = this.data
let value = '' let value = ''
for (const k of Object.keys(this.data)) { for (const k of Object.keys(data)) {
value += `${k}: ${this.data[k]}, ` value += `${k}: ${data[k]}, `
} }
this.output += '\n' + value this.output += '\n' + value
console.log('save:', value) console.log('save:', value)
// sendMsg(this.conf, [conf.domain, 'data', this.name, data.id], data)
} }
function poll() { function poll() {
@ -84,8 +87,8 @@ function handle(msg) {
// basic functions - move to api.ts // basic functions - move to api.ts
async function send(url: string, data: any) { async function send(url: string, conf: Config, data: any) {
data._interaction = interaction data._interaction = conf.iid
const body = JSON.stringify(data) const body = JSON.stringify(data)
return fetch(url, { return fetch(url, {
method: 'POST', method: 'POST',
@ -119,7 +122,7 @@ async function dopoll(app: typeof appdata) {
const wait_time = 10000 const wait_time = 10000
while (true) { while (true) {
try { try {
const res = await(send(app.conf.pollurl, {})) const res = await(send(app.conf.pollurl, app.conf, {}))
const msg = await res.json() const msg = await res.json()
console.log(msg) console.log(msg)
switch (msg.status) { switch (msg.status) {

View file

@ -21,6 +21,10 @@ params:
name: Helmut Merz name: Helmut Merz
link: https://blog.cy7.eu link: https://blog.cy7.eu
api:
domain: demo
module: main.ts
taxonomies: taxonomies:
domain: domains domain: domains
topic: topics topic: topics

View file

@ -5,11 +5,12 @@ img:
pageid: test1 pageid: test1
domains: [App] domains: [App]
topics: [Examples] topics: [Examples]
date: 2023-03-07 date: 2023-03-10
author: helmutm author: helmutm
draft: false draft: false
weight: 10 weight: 10
jsModule: main.ts api:
domain: test
--- ---
cyberscopes example site - use petite-vue in Hugo-generated sites. cyberscopes example site - use petite-vue in Hugo-generated sites.

View file

@ -1,6 +1,7 @@
{{- if $jsmod := .Params.JsModule -}} {{- if $jsmod := $.Param "api.module" -}}
{{- $pageid := or .Params.Pageid (and .File .File.UniqueID) "unknown" -}} {{- $pageid := or .Params.Pageid (and .File .File.UniqueID) "unknown" -}}
{{- $params := dict "pageid" $pageid -}} {{- $params := dict "pageid" $pageid
"domain" ($.Param "api.domain") -}}
{{- $js := resources.Get (printf "js/%s" $jsmod) | js.Build ( {{- $js := resources.Get (printf "js/%s" $jsmod) | js.Build (
dict "minify" false "params" $params) -}} dict "minify" false "params" $params) -}}
<script src="{{ $js.Permalink }}" defer></script> <script src="{{ $js.Permalink }}" defer></script>