This repository has been archived on 2026-11-05. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
thetool/public/js/pages/Radius/Radius.js
T

243 lines
10 KiB
JavaScript

/* ===== Radius.js =====
* Main entry point for the Radius module (light theme).
* Navigation jetzt innerhalb der Hauptkarte; Free-Tab lädt erst bei erstem Klick.
*/
/* ---------- Shared utils (global) ---------- */
function calculateSimilarity(str1, str2) {
if (!str1 || !str2) return 0;
str1 = ('' + str1).toLowerCase();
str2 = ('' + str2).toLowerCase();
let match = 0;
for (let c of str1) if (str2.includes(c)) match++;
return (match / str1.length) * 100;
}
function validateData(strasse, plz, stadt, info) {
const thresholds = 90;
return !(
calculateSimilarity(strasse, info) < thresholds ||
calculateSimilarity(plz, info) < thresholds ||
calculateSimilarity(stadt, info) < thresholds
);
}
async function copyToClipboard(text) {
try {
await navigator.clipboard.writeText(text || '');
return true;
} catch {
const ta = document.createElement('textarea');
ta.value = text || '';
ta.style.position = 'fixed'; ta.style.opacity = '0';
document.body.appendChild(ta); ta.select();
try { document.execCommand('copy'); } catch {}
document.body.removeChild(ta);
return false;
}
}
window.RadiusUtils = { calculateSimilarity, validateData, copyToClipboard };
/* ---------- Online state chip (fetches radacct when visible) ---------- */
Vue.component('radius-online-state', {
props: { username: String },
data: () => ({ data: null, observed: false, ob: null }),
template: `
<div class="radius-scope ros-wrap" ref="root">
<template v-if="data===null">
<span class="ros-chip skeleton">
<span class="dot"></span>
<span class="skeleton-line" style="width: 80px; height: 10px; margin: auto;"></span>
</span>
</template>
<template v-else-if="data!==null">
<span
class="ros-chip"
:class="[data.online ? 'on' : 'off', {'is-clickable': data.ip}]"
:title="data.ip ? 'Klicken zum Kopieren' : ''"
@click="copyIp"
>
<span class="dot"></span>
<span class="ip">{{ data.ip || '—' }}</span>
</span>
</template>
</div>
`,
mounted() {
this.ob = new IntersectionObserver((en) => {
if (en[0].isIntersecting && !this.observed) {
this.observed = true; this.fetchState();
}
}, { threshold: 0.1 });
if (this.$refs.root) {
this.ob.observe(this.$refs.root);
}
},
beforeDestroy() { this.ob?.disconnect(); },
methods: {
async fetchState() {
try {
const url = `${window.TT_CONFIG['BASE_PATH']}/Radius/proxyUnsecureHTTPRequestToRadius?action2=fetchRadacct&username=${encodeURIComponent(this.username)}`;
const r = await fetch(url);
if (r.ok) this.data = await r.json();
else this.data = { online: false, ip: null };
} catch { this.data = { online: false, ip: null }; }
},
async copyIp() {
if (!this.data?.ip) return;
await window.RadiusUtils.copyToClipboard(this.data.ip);
if (window.notify) {
window.notify('success', 'IP-Adresse kopiert!');
}
}
}
});
/* ---------- Autocomplete ---------- */
Vue.component('radius-autocomplete', {
props: { value: String, placeholder: String, wide: { type: Boolean, default: true } },
data() {
return { q: this.value || '', open: false, items: {}, highlighted: -1, busy: false, selectedDisplay: '', selectedCustnum: '' };
},
watch: { value(v){ if(v!==this.q) this.q=v; } },
template: `
<div class="radius-scope ac-root" :data-wide="wide ? '1' : null"
@keydown.down.prevent="move(1)" @keydown.up.prevent="move(-1)" @keydown.enter.prevent="chooseHighlighted(true)">
<div class="input-wrap">
<i class="fa-duotone fa-magnifying-glass input-icon" aria-hidden="true"></i>
<input
:placeholder="placeholder || 'Rechnungsadresse suchen…'"
class="ri"
v-model="q"
autocomplete="off"
autocapitalize="none"
autocorrect="off"
@input="debouncedFetch"
@focus="maybeOpen"
@blur="deferClose"
/>
<button class="btn-clear" :disabled="!q" @mousedown.prevent="clear" title="Feld leeren"><i class="fa-duotone fa-xmark"></i></button>
</div>
<transition name="ac-pop">
<div v-if="open" class="ac-panel" :class="{'wide': wide}">
<div v-if="busy" class="ac-skel">
<div class="skeleton-line"></div><div class="skeleton-line"></div><div class="skeleton-line"></div>
</div>
<template v-else>
<div v-if="!Object.keys(items).length" class="ac-empty muted">Keine Treffer</div>
<ul class="ac-list" role="listbox">
<li v-for="(disp, id) in items" :key="id"
:class="['ac-item', highlightedId===id ? 'is-active' : '']"
@mousedown.prevent="choose(id, disp)">
<i class="fa-duotone fa-address-card"></i>
<span class="txt">{{ disp }}</span>
</li>
</ul>
</template>
</div>
</transition>
</div>
`,
computed: { highlightedId(){ const keys=Object.keys(this.items); return keys[this.highlighted] || null; } },
created() { this.debouncedFetch = this.debounce(() => this.fetchItems(), 220); },
methods: {
maybeOpen(){ this.open = true; if (this.q) this.debouncedFetch(); },
deferClose(){ setTimeout(()=> this.open=false, 150); },
clear(){ this.q=''; this.items={}; this.highlighted=-1; this.emitSelection('', ''); },
move(dir){ const keys=Object.keys(this.items); if (!keys.length) return; this.highlighted=(this.highlighted+dir+keys.length)%keys.length; },
chooseHighlighted(emitEnter){ const id=this.highlightedId; if (id) this.choose(id, this.items[id], emitEnter); else if (emitEnter) this.$emit('enter'); },
choose(id, display, emitEnter){ this.q=display; this.selectedCustnum=(display.match(/\[(\d+)\]/)||[])[1] || ''; this.selectedDisplay=display; this.emitSelection(this.selectedCustnum, this.selectedDisplay); this.open=false; if (emitEnter) this.$emit('enter'); },
emitSelection(custnum, display){ this.$emit('select', { custnum, display }); this.$emit('input', display); this.$emit('change', display); },
async fetchItems(){
if (!this.q || this.q.length < 2) { this.items={}; return; }
this.busy = true;
try{
const base = window.TT_CONFIG['BASE_PATH'] || '';
const url = `${base}/Address/Api?do=findAddress&fibu_primary_account=1&q=${encodeURIComponent(this.q)}`;
const r = await fetch(url);
if (r.ok) {
const j = await r.json();
this.items = (j && j.result && j.result.addresses) ? j.result.addresses : {};
this.highlighted = 0;
} else { this.items = {}; }
} catch { this.items = {}; }
this.busy = false;
},
debounce(fn, ms){ let t; return (...a)=>{ clearTimeout(t); t=setTimeout(()=>fn(...a), ms); }; }
}
});
/* ---------- Generic Modal ---------- */
Vue.component('radius-modal', {
props: { show: Boolean, title: String },
template: `
<transition name="fade">
<div v-if="show" class="radius-scope modal-overlay" @click.self="$emit('close')">
<div class="modal-card pop">
<div class="modal-head">
<div class="modal-title"><i class="fa-duotone fa-database"></i> {{ title }}</div>
<button class="icon-btn" @click="$emit('close')" aria-label="Close" title="Schließen"><i class="fa-duotone fa-xmark"></i></button>
</div>
<div class="modal-body"><slot/></div>
</div>
</div>
</transition>
`
});
/* ---------- Root View: <radius> ---------- */
Vue.component('radius', {
template: `
<div class="radius-scope radius-container">
<section class="card card-in">
<div class="pane-header">
<div class="title"><span class="logo-dot"></span><span>Radius</span></div>
<nav class="view-tabs">
<button v-for="item in viewOptions" :key="item.id" class="tab-btn" :class="{active:view===item.id}" @click="switchView(item.id)">
<i :class="item.icon"></i> {{ item.name }}
</button>
</nav>
<div class="view-select-wrap select">
<select v-model="view">
<option v-for="item in viewOptions" :value="item.id">{{ item.name }}</option>
</select>
</div>
</div>
<!-- Views -->
<section v-show="view==='users'" class="card-in"><radius-users/></section>
<section v-show="view==='free'" class="card-in"><radius-free-users ref="freeView"/></section>
<section v-show="view==='ont'" class="card-in"><radius-ont-parser/></section>
<section v-show="view==='ontReverse'" class="card-in"><radius-ont-finder/></section>
</section>
</div>
`,
data() { return { view: 'users', window: window, _freeInitDone: false }; },
computed: {
viewOptions() {
const options = [
{ id: 'users', name: 'Benutzer', icon: 'fa-duotone fa-users' },
{ id: 'free', name: 'Freie Benutzer', icon: 'fa-duotone fa-user-plus' }
];
if (window['TT_CONFIG']['CAN_BILLING'] === '1') {
options.push({ id: 'ont', name: 'ONT Parser', icon: 'fa-duotone fa-diagram-project' });
options.push({ id: 'ontReverse', name: 'ONT Reverse', icon: 'fa-duotone fa-arrows-rotate' });
}
return options;
}
},
watch: {
view(newView) {
this.switchView(newView);
}
},
methods: {
switchView(v){
this.view = v;
if (v==='free' && !this._freeInitDone) {
this.$nextTick(()=>{ this.$refs.freeView?.initIfNeeded?.(); this._freeInitDone = true; });
}
}
}
});