Skip to content

add embedding #323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/lib/helpers/types/pluginTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,19 @@
* @property {string} label
* @property {string} icon
* @property {string} link
* @property {string?} [embedUrl]
* @property {EmbeddingInfoModel?} [embeddingInfo]
* @property {boolean} isHeader
*/

/**
* @typedef {Object} EmbeddingInfoModel
* @property {string} source
* @property {string?} [scriptSrc]
* @property {string?} [scriptType]
* @property {string?} [url]
* @property {string?} [htmlTag]
*/

/**
* @typedef {Object} PluginFilter
* @property {import('$commonTypes').Pagination} pager - Pagination
Expand Down
64 changes: 43 additions & 21 deletions src/routes/page/agent/metrics/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,45 +1,67 @@
<script>
import { onDestroy } from 'svelte';
import { onMount, onDestroy } from 'svelte';
import { page } from '$app/stores';
import { _ } from 'svelte-i18n';
import { Card, CardBody, Col, Row } from '@sveltestrap/sveltestrap';
import HeadTitle from "$lib/common/HeadTitle.svelte";
import Breadcrumb from '$lib/common/Breadcrumb.svelte';
import { globalMenuStore } from '$lib/helpers/store';

/** @type {string} */
let embedUrl = '';
/** @type {any} */
let unsubscriber;

const unsubscriber = globalMenuStore.subscribe((/** @type {import('$pluginTypes').PluginMenuDefModel[]} */ menu) => {
const url = $page.url.pathname;
const pageInfo = menu.find(x => x.link === url) || null;
embedUrl = pageInfo?.embedUrl || '';
onMount(async () => {
unsubscriber = globalMenuStore.subscribe((/** @type {import('$pluginTypes').PluginMenuDefModel[]} */ menu) => {
const url = $page.url.pathname;
const data = menu.find(x => x.link === url)?.embeddingInfo || null;
embed(data);
});
});

onDestroy(() => {
unsubscriber?.();
});


/** @param {import('$pluginTypes').EmbeddingInfoModel?} data */
function embed(data) {
if (!data) return;

if (data.scriptSrc) {
const script = document.createElement("script");
script.id = data.source;
script.src = data.scriptSrc;
script.async = true;

if (data.scriptType) {
script.type = data.scriptType;
}
document.head.appendChild(script);
}

const div = document.querySelector('#agent-metrics-content');
if (!data.url || !div) return;

if (data.htmlTag) {
const elem = document.createElement(data.htmlTag);
elem.id = data.source;
elem.style.width = '100%';
elem.style.height = '100%';
elem.style.justifyContent = 'center';
// @ts-ignore
elem.src = data.url;
div.appendChild(elem);
}
}
</script>

<HeadTitle title="{$_('Metrics')}" />
<Breadcrumb title="{$_('Agent')}" pagetitle="{$_('Metrics')}" />

{#if embedUrl}
<Row>
<Col lg="12">
<Card>
<CardBody>
<iframe
title="agent-metrics"
height="100%"
width="100%"
src={embedUrl}
frameborder="0"
allowfullscreen
>
</iframe>
</CardBody>
<CardBody id="agent-metrics-content"></CardBody>
</Card>
</Col>
</Row>
{/if}
</Row>