Files
WidgetWorkbench/hmi-frontend/src/PageManager.vue

123 lines
8.6 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<script setup lang="ts">
import { computed, ref, watch, onMounted } from 'vue';
import { api } from './api';
import type { HmiPage } from './types';
import { descendantPageIds } from './navigation';
const props=defineProps<{pages:HmiPage[];activeId?:string}>();
const emit=defineEmits<{close:[];saved:[id:string|null]}>();
const currentId=ref<string>('');
const roles=ref<{key:string;description:string}[]>([]);
const visibleToRoles=ref<string[]>([]);
const rolesLoaded=ref(false);
onMounted(async()=>{try{roles.value=await api('/roles');rolesLoaded.value=true;}catch(e){error.value=String(e);}});
const key=ref('');const title=ref('');const menuGroup=ref('Fertigung');
const parentPageId=ref('');const order=ref(0);const enabled=ref(true);
const error=ref('');const notice=ref('');const busy=ref(false);
const roots=computed(()=>Array.from(new Set(props.pages.map(p=>p.menuGroup||'Allgemein'))).sort((a,b)=>a.localeCompare(b,'de')));
const blockedParents=computed(()=>currentId.value?new Set([currentId.value,...descendantPageIds(props.pages,currentId.value)]):new Set<string>());
const candidates=computed(()=>props.pages.filter(p=>!blockedParents.value.has(p.id))
.slice().sort((a,b)=>a.menuGroup.localeCompare(b.menuGroup,'de')||a.order-b.order||a.title.localeCompare(b.title,'de')));
const selectedParent=computed(()=>props.pages.find(p=>p.id===parentPageId.value));
const isExisting=computed(()=>!!currentId.value);
function selectPage(id:string) {
const p=props.pages.find(page=>page.id===id);
if(!p){createNew();return;}
visibleToRoles.value=[...(p.visibleToRoles||[])];
currentId.value=p.id;key.value=p.key;title.value=p.title;
menuGroup.value=p.menuGroup||'Allgemein';parentPageId.value=p.parentPageId||'';
order.value=p.order;enabled.value=p.enabled;error.value='';notice.value='';
}
function createNew(parentId='') {
visibleToRoles.value=[];
currentId.value='';key.value='';title.value='';parentPageId.value=parentId;
menuGroup.value=props.pages.find(p=>p.id===parentId)?.menuGroup || 'Fertigung';
order.value=props.pages.length+1;enabled.value=true;error.value='';notice.value='';
}
watch(parentPageId,()=>{if(selectedParent.value)menuGroup.value=selectedParent.value.menuGroup;});
async function save(){
if(busy.value)return;
if(!title.value.trim()||(!isExisting.value&&!/^[A-Za-z0-9_.-]{1,120}$/.test(key.value))){
error.value='Anzeigename und ein gültiger technischer Schlüssel sind erforderlich.';return;
}
busy.value=true;error.value='';notice.value='';
const payload={title:title.value.trim(),menuGroup:menuGroup.value.trim(),parentPageId:parentPageId.value||null,
order:order.value,enabled:enabled.value,visibleToRoles:[...visibleToRoles.value]};
try{
let id=currentId.value;
if(currentId.value) await api(`/pages/${encodeURIComponent(currentId.value)}`,'PUT',payload);
else {const created=await api<{id:string}>('/pages','POST',{...payload,key:key.value.trim()});id=created.id;}
notice.value='Seite gespeichert';emit('saved',id);
}catch(e){error.value=e instanceof Error?e.message:String(e);}finally{busy.value=false;}
}
const renameOld=ref('');const renameNew=ref('');
async function renameRoot(){
if(!renameOld.value||!renameNew.value.trim())return;
try{await api('/navigation/root','PUT',{previous:renameOld.value,next:renameNew.value.trim()});
notice.value='Root-Menü umbenannt';renameOld.value='';renameNew.value='';emit('saved',null);}
catch(e){error.value=String(e);}
}
function editChild(){const parent=currentId.value||props.activeId||'';createNew(parent);}
createNew();
</script>
<template>
<section class="page-admin">
<header class="page-admin-head"><div><h2>Pages & Navigation</h2><p>Root-Menüs, Unterseiten, Reihenfolge und Sichtbarkeit verwalten.</p></div>
<button class="subtle-button" @click="emit('close')">Schließen ×</button></header>
<p v-if="error" class="notice error-notice" role="alert">{{error}}</p><p v-if="notice" class="success-msg">{{notice}}</p>
<div class="page-admin-columns">
<div class="page-admin-list"><h3>Vorhandene Pages</h3>
<button class="primary-button" @click="createNew()">+ Neue Root-Page</button>
<div v-for="root in roots" :key="root" class="page-admin-section">
<strong>{{root}}</strong>
<button v-for="page in pages.filter(p=>p.menuGroup===root).sort((a,b)=>a.order-b.order)" :key="page.id"
class="page-admin-entry" :class="{selected:currentId===page.id}" @click="selectPage(page.id)">
{{page.parentPageId?'↳':'▤'}} {{page.title}} <small v-if="!page.enabled">(inaktiv)</small>
</button>
</div>
</div>
<form class="page-admin-form" @submit.prevent="save">
<h3>{{isExisting?'Seite bearbeiten':'Neue Seite'}}</h3>
<label>Technischer Schlüssel<input v-model.trim="key" :disabled="isExisting" placeholder="z.B. qualitaetspruefung" maxlength="120" /></label>
<label>Anzeigename<input v-model.trim="title" required maxlength="255" /></label>
<label>Root-Menü <input v-model.trim="menuGroup" :disabled="!!parentPageId" list="hmi-root-menus" required maxlength="120" placeholder="z.B. Fertigung"/>
<datalist id="hmi-root-menus"><option v-for="root in roots" :key="root" :value="root"/></datalist>
</label>
<label>Übergeordnete Page
<select v-model="parentPageId"><option value="">Keine – direkt unter Root-Menü</option>
<option v-for="p in candidates" :key="p.id" :value="p.id">{{p.menuGroup}} / {{p.title}}</option>
</select>
</label>
<p class="page-admin-hint">Unterseiten übernehmen das Root-Menü der übergeordneten Page. Beim Umhängen bleiben alle Widgets erhalten.</p>
<label>Reihenfolge <input type="number" v-model.number="order" min="0" max="100000" required/></label>
<label class="page-check"><input type="checkbox" v-model="enabled"/> Seite aktiviert</label>
<fieldset><legend>Sichtbar für Rollen</legend>
<p class="page-admin-hint">Keine Auswahl: alle angemeldeten Benutzer. Mindestens eine ausgewählte Rolle ist erforderlich; Einschränkungen übergeordneter Seiten gelten zusätzlich. Designer und Administratoren behalten ihren Verwaltungszugriff.</p>
<label v-for="role in roles" :key="role.key" class="page-check"><input type="checkbox" v-model="visibleToRoles" :value="role.key"/>{{role.key}}</label>
<p v-if="!rolesLoaded">Rollen werden geladen oder konnten nicht geladen werden.</p>
</fieldset>
<div class="page-form-actions">
<button class="primary-button" type="submit" :disabled="busy || !rolesLoaded">{{busy?'Speichere…':'Seite speichern'}}</button>
<button type="button" class="subtle-button" @click="editChild">+ Unterseite anlegen</button>
</div>
</form>
<div class="page-admin-list"><h3>Root-Menü umbenennen</h3>
<label>Vorhandenes Menü<select v-model="renameOld"><option value="">Bitte auswählen</option><option v-for="root in roots" :key="root" :value="root">{{root}}</option></select></label>
<label>Neuer Titel<input v-model.trim="renameNew" maxlength="120" placeholder="Neuer Menüname" /></label>
<button class="subtle-button" type="button" :disabled="!renameOld||!renameNew.trim()" @click="renameRoot">Root-Menü umbenennen</button>
<p class="page-admin-hint">Ein Root-Menü entsteht, sobald eine Page mit diesem Menünamen gespeichert wird. Ein leeres Root-Menü wird nicht angezeigt.</p>
</div>
</div>
</section>
</template>
<style scoped>
.page-admin{margin:14px 0;background:var(--panel);border:1px solid var(--line);border-radius:16px;padding:20px}
.page-admin-head{display:flex;align-items:center;justify-content:space-between;gap:10px}.page-admin-head h2{margin:0}.page-admin-head p,.page-admin-hint{color:var(--muted);font-size:.86em}
.page-admin-columns{display:grid;grid-template-columns:minmax(180px,1fr) minmax(270px,1.5fr) minmax(180px,1fr);gap:18px;margin-top:18px}
.page-admin-list,.page-admin-form{background:var(--panel-deep);border:1px solid var(--line);border-radius:12px;padding:15px;display:flex;flex-direction:column;gap:11px;min-width:0;align-self:start}
.page-admin h3{margin:0 0 10px}.page-admin-section{display:flex;flex-direction:column;gap:5px;margin-bottom:12px}.page-admin-section>strong{color:var(--muted);font-size:.9em}
.page-admin-entry{text-align:left;background:transparent;border:1px solid var(--line);border-radius:8px;color:var(--text);padding:10px}.page-admin-entry.selected{border-color:var(--hmi-accent);background:var(--subtle)}
.page-admin label{display:flex;flex-direction:column;gap:6px;font-size:.9em}.page-admin input,.page-admin select{background:var(--input);border:1px solid var(--line);border-radius:8px;padding:10px;color:var(--text);width:100%}
.page-check{flex-direction:row!important;align-items:center}.page-check input{width:auto}.page-form-actions{display:flex;flex-wrap:wrap;gap:10px}
@media(max-width:1100px){.page-admin-columns{grid-template-columns:1fr}}
</style>