88 lines
5.6 KiB
Vue
88 lines
5.6 KiB
Vue
<script setup lang="ts">
|
|
import {computed, ref, watch, onBeforeUnmount} from 'vue';
|
|
import type {HmiWidget} from '../types';
|
|
import {shouldAutoStartWidget} from './cheerpjAutoStart';
|
|
|
|
const props = defineProps<{widget:HmiWidget;demo:boolean}>();
|
|
const url = computed(()=>String(props.widget.props.jnlpUrl||'').trim());
|
|
const workplace = computed(()=>props.widget.type==='ui.cheerpj-workplace');
|
|
const autoStartEnabled = computed(()=>shouldAutoStartWidget(props.widget.type,props.widget.props));
|
|
const javaVersion = computed(()=>[8,11,17].includes(Number(props.widget.props.javaVersion))?Number(props.widget.props.javaVersion):8);
|
|
const windowProtocolWarning = window.location.protocol==='http:' && !['localhost','127.0.0.1'].includes(window.location.hostname);
|
|
const displayMode = computed(()=>['fit','original'].includes(String(props.widget.props.displayMode))?String(props.widget.props.displayMode):'fit');
|
|
const launchRequested = ref(false);
|
|
const failure = ref('');
|
|
const status = ref('');
|
|
const sequence = ref(0);
|
|
const frame = ref<HTMLIFrameElement|null>(null);
|
|
const requestId = computed(()=>`${props.widget.id}-${sequence.value}`);
|
|
|
|
function validate():string {
|
|
if (!url.value)return 'Bitte im Page-Editor eine JNLP-URL eintragen.';
|
|
try {
|
|
const parsed=new URL(url.value,window.location.origin);
|
|
if (parsed.origin!==window.location.origin)return 'Die JNLP-URL muss zunächst dieselbe Origin wie WidgetWorkbench verwenden (Schema, Host und Port).';
|
|
// The browser may serve an isolated MES installation over HTTP. The user sees a warning.
|
|
// It must remain the exact same origin; cross-origin and mixed-content loading are blocked.
|
|
if(!['http:','https:'].includes(parsed.protocol))return 'Nur HTTP oder HTTPS erlaubt.';
|
|
if(workplace.value && parsed.pathname!=='/backend/api/hmi/v1/cheerpj/workplace/manifest.jnlp')return 'Für iTAC Workplace ist ausschließlich der freigegebene Ressourcenpfad erlaubt.';
|
|
if(parsed.username||parsed.password)return 'Zugangsdaten in JNLP-URLs sind nicht erlaubt.';
|
|
}catch{return 'Ungültige JNLP-URL.';}
|
|
return '';
|
|
}
|
|
function launch(){
|
|
failure.value=validate();status.value='';
|
|
if(failure.value || props.demo)return;
|
|
sequence.value+=1;launchRequested.value=true;
|
|
}
|
|
function stop(){launchRequested.value=false;status.value='';}
|
|
async function openFullscreen(){
|
|
const host=document.getElementById('cheerpj-'+props.widget.id);
|
|
if(host?.requestFullscreen)await host.requestFullscreen();
|
|
}
|
|
function onFrameLoad(){
|
|
if(!frame.value||!launchRequested.value)return;
|
|
frame.value.contentWindow?.postMessage({type:'hmi-cheerpj-launch',requestId:requestId.value,jnlpUrl:new URL(url.value,window.location.origin).href,javaVersion:javaVersion.value,workplace:workplace.value,displayMode:displayMode.value},window.location.origin);
|
|
}
|
|
function onMessage(e:MessageEvent){
|
|
if(e.origin!==window.location.origin||e.source!==frame.value?.contentWindow)return;
|
|
const data=e.data;
|
|
if(!data||data.type!=='hmi-cheerpj-state'||data.requestId!==requestId.value)return;
|
|
status.value=String(data.message||'').slice(0,350);
|
|
if(data.state==='error')failure.value=status.value;
|
|
}
|
|
// The workplace starts automatically in runtime, also for legacy templates with autoStart:false.
|
|
// The designer never starts a VM. A manual stop lasts until the widget/page is reopened.
|
|
watch([url,javaVersion,displayMode],()=>{
|
|
if(!launchRequested.value)return;
|
|
stop();
|
|
if(!props.demo && autoStartEnabled.value && url.value)launch();
|
|
});
|
|
watch(()=>[props.demo,autoStartEnabled.value,url.value] as const,([demo,enabled,jnlp])=>{
|
|
if(demo || !enabled){ if(launchRequested.value)stop(); return; }
|
|
if(jnlp&&!launchRequested.value)launch();
|
|
},{immediate:true});
|
|
window.addEventListener('message',onMessage);
|
|
onBeforeUnmount(()=>window.removeEventListener('message',onMessage));
|
|
</script>
|
|
<template>
|
|
<div :id="'cheerpj-'+widget.id" class="cheerpj-widget" :class="{'cheerpj-widget--running':launchRequested}">
|
|
<div class="cheerpj-toolbar">
|
|
<span class="cheerpj-caption">{{String(widget.props.title||'JNLP-Anwendung')}}</span>
|
|
<span class="cheerpj-badge">{{workplace?'iTAC Workplace · ':''}}CheerpJ {{javaVersion}}</span>
|
|
<button v-if="!demo && workplace" type="button" @click.stop="openFullscreen">⛶ Vollbild</button>
|
|
<button v-if="!demo && !launchRequested" type="button" @click.stop="launch" :disabled="!url">▶ Start</button>
|
|
<template v-else-if="!demo"><button type="button" @click.stop="launch">↻ Neustart</button><button type="button" @click.stop="stop">■ Stop</button></template>
|
|
</div>
|
|
<div v-if="workplace" class="cheerpj-warning">Java 11 · 129 JARs · ResizeBridge V2 · native RXTX-Treiber/JNLP-JVM-Optionen sind nicht garantiert.</div>
|
|
<div v-if="!demo && windowProtocolWarning" class="cheerpj-warning">HTTP aktiv: Anmeldedaten und MES-Daten sind nicht transportverschlüsselt. Nur in geschützten Netzen einsetzen.</div>
|
|
<div v-if="demo" class="cheerpj-placeholder">☕ CheerpJ-Vorschau: Der Java-Client wird erst im Werker-Modus gestartet.<small>{{url||'JNLP-Adresse noch nicht konfiguriert'}}</small></div>
|
|
<div v-else-if="!launchRequested" class="cheerpj-placeholder">☕ JNLP-Anwendung starten<small>{{url||'Bitte eine JNLP-URL im Designer hinterlegen'}}</small></div>
|
|
<div v-else class="cheerpj-display-wrapper" :class="{'cheerpj-display-wrapper--original':displayMode==='original'}">
|
|
<iframe :key="sequence" ref="frame" class="cheerpj-frame" src="/backend/hmi/cheerpj-runner.html" title="CheerpJ Java-Anwendung" allow="clipboard-read; clipboard-write" @load="onFrameLoad" />
|
|
</div>
|
|
<p v-if="failure" role="alert" class="cheerpj-alert">{{failure}}</p>
|
|
<p v-else-if="status" aria-live="polite" class="cheerpj-status">{{status}}</p>
|
|
</div>
|
|
</template>
|