/* ===== Radius.js ===== */
/* ---------- Shared Utilities (global) ---------- */
function loadScript(src) {
return new Promise((resolve, reject) => {
if (document.querySelector(`script[src="${src}"]`)) {
return resolve();
}
const script = document.createElement('script');
script.src = src;
script.onload = resolve;
script.onerror = () => reject(new Error(`Script load error for ${src}`));
document.head.appendChild(script);
});
}
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;
}
}
function formatBytes(bytes, decimals = 2) {
bytes = parseInt(bytes, 10);
if (!bytes || bytes === 0) return '0 Bytes';
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
}
function formatDuration(seconds) {
if (!seconds || seconds < 0) return '0s';
seconds = parseInt(seconds, 10);
const d = Math.floor(seconds / (3600*24));
const h = Math.floor(seconds % (3600*24) / 3600);
const m = Math.floor(seconds % 3600 / 60);
if (d > 0) return `${d}t ${h}h`;
if (h > 0) return `${h}h ${m}m`;
if (m > 0) return `${m}m`;
return `< 1m`;
}
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
);
}
window.RadiusUtils = { calculateSimilarity, validateData, copyToClipboard, formatBytes, formatDuration, loadScript };
/* ---------- Reusable Component: radius-table-view ---------- */
Vue.component('radius-table-view', {
props: {
items: Array,
isLoading: Boolean,
hasSearched: Boolean,
density: { type: String, default: 'compact' },
tableClass: { type: String, default: '' },
tableStyle: Object,
tableMinHeight: { type: String, default: 'auto' },
initialPlaceholderIcon: { type: String, default: 'fa-duotone fa-keyboard' },
initialPlaceholderText: { type: String, default: 'Beginnen Sie Ihre Suche.' },
noResultsPlaceholderIcon: { type: String, default: 'fa-duotone fa-database' },
noResultsPlaceholderText: { type: String, default: 'Keine Ergebnisse gefunden.' },
skeletonRowCount: { type: Number, default: 6 }
},
template: `
{{ initialPlaceholderText }}
{{ noResultsPlaceholderText }}
`
});
/* ---------- Reusable Component: radius-file-drop ---------- */
Vue.component('radius-file-drop', {
data: () => ({ dragCounter: 0 }),
computed: { isDragging() { return this.dragCounter > 0; } },
template: `
`,
methods: { onDrop(e) { this.dragCounter = 0; const file = e.dataTransfer.files?.[0]; if (file) this.$emit('file-selected', file); } }
});
/* ---------- Reusable Component: radius-processing-indicator ---------- */
Vue.component('radius-processing-indicator', {
props: ['progress', 'currentRow', 'totalRows', 'currentSerial'],
template: `
Verarbeitung läuft...
Aktuell: {{ currentSerial || '—' }}
Verarbeite Zeile {{ currentRow + 1 }} von {{ totalRows }}
`
});
/* ---------- Online state chip (fetches radacct when visible) ---------- */
Vue.component('radius-online-state', {
props: { username: String },
data: () => ({
data: null,
observed: false,
ob: null,
isHovering: false,
ctrlPressed: false,
tooltipText: 'IP-Adresse kopieren'
}),
template: `
{{ data.ip || '—' }}
`,
watch: {
data(newData) {
// Update tooltip text when data is loaded
if (newData && newData.ip) {
this.tooltipText = 'IP-Adresse kopieren';
} else {
this.tooltipText = null;
}
}
},
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);
// Listen for Ctrl/Meta key presses globally
document.addEventListener('keydown', this.handleKey);
document.addEventListener('keyup', this.handleKey);
},
beforeDestroy() {
this.ob?.disconnect();
// Clean up global listeners
document.removeEventListener('keydown', this.handleKey);
document.removeEventListener('keyup', this.handleKey);
},
methods: {
async fetchState() {
try {
const r = await fetch(`${window.TT_CONFIG['BASE_PATH']}/Radius/proxyUnsecureHTTPRequestToRadius?action2=fetchRadacct&username=${encodeURIComponent(this.username)}`);
this.data = r.ok ? await r.json() : { online: false, ip: null };
} catch {
this.data = { online: false, ip: null };
}
},
async copyIp(event) {
if (!this.data?.ip) return;
const c = event.currentTarget;
if (!c || c.classList.contains('is-copied')) return;
await window.RadiusUtils.copyToClipboard(this.data.ip);
c.classList.add('is-copied');
// Temporarily change tooltip to "Kopiert!"
const originalTooltip = this.tooltipText;
this.tooltipText = 'Kopiert!';
setTimeout(() => {
c.classList.remove('is-copied');
// Restore original tooltip
this.tooltipText = originalTooltip;
// Re-run updateTooltip in case Ctrl is still pressed
this.updateTooltip();
}, 1500);
},
// --- New methods for Ctrl+Click ---
handleKey(event) {
const newCtrlPressed = event.ctrlKey || event.metaKey;
if (newCtrlPressed !== this.ctrlPressed) {
this.ctrlPressed = newCtrlPressed;
// If hovering, update tooltip live
if (this.isHovering) {
this.updateTooltip();
}
}
},
onIpMouseOver(event) {
this.isHovering = true;
this.ctrlPressed = event.ctrlKey || event.metaKey;
this.updateTooltip();
},
onIpMouseOut() {
this.isHovering = false;
this.ctrlPressed = false; // Reset on mouse out
this.updateTooltip();
},
updateTooltip() {
if (!this.data?.ip) {
this.tooltipText = null;
} else if (this.isHovering && this.ctrlPressed) {
this.tooltipText = 'Scan starten & verbinden';
} else {
this.tooltipText = 'IP-Adresse kopieren';
}
},
onClickIp(event) {
if (!this.data?.ip) return;
if (event.ctrlKey || event.metaKey) {
// Ctrl+Click or Meta+Click
event.preventDefault();
this.$emit('scan-ip', { ip: this.data.ip });
} else {
// Normal click
this.copyIp(event);
}
}
// --- End new methods ---
}
});
/* ---------- 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, mode: 'autocomplete', logoDropdownOpen: false, hasMoreResults: false }; }, watch: { value(v){ if (v !== this.q) { this.q = v; if (this.mode === 'autocomplete') this.debouncedFetch(); } } },
template: `Klicken Sie auf das Logo, um die Kundenbasis zu wechseln
XINON (Suche)
ESTMK (Eingabe)Keine Treffer
- {{ disp }}
- Mehr Ergebnisse verfügbar
`,
computed: { highlightedId(){ const k=Object.keys(this.items); return k[this.highlighted] || null; }, placeholderText() { return this.mode === 'autocomplete' ? (this.placeholder || 'Rechnungsadresse suchen') : 'Partner-Kundennummer eingeben'; } },
created() { this.debouncedFetch = this.debounce(() => this.fetchItems(), 220); },
methods: {
toggleLogoDropdown() { this.logoDropdownOpen = !this.logoDropdownOpen; if (this.logoDropdownOpen) this.open = false; },
selectMode(m) { if (this.mode !== m) { this.mode = m; this.$emit('mode-change', m); this.clear(); } this.logoDropdownOpen = false; this.$nextTick(() => this.$refs.mainInput.focus()); },
onInput() { this.$emit('input', this.q); if (this.mode === 'autocomplete') this.debouncedFetch(); },
onEnter() { if (this.mode === 'autocomplete') this.chooseHighlighted(true); else this.$emit('enter'); },
maybeOpen(){ this.open = true; if (this.q) this.debouncedFetch(); },
deferClose(){ setTimeout(()=> { this.open = false; this.logoDropdownOpen = false; }, 150); },
clear(){ this.q = ''; this.items={}; this.highlighted=-1; this.emitSelection('', ''); if (this.mode === 'autocomplete') { this.open = true; this.debouncedFetch(); } },
move(d){ const k=Object.keys(this.items); if (!k.length) return; this.highlighted=(this.highlighted+d+k.length)%k.length; this.$nextTick(() => { const a = this.$refs.resultsList?.querySelector('.is-active'); if (a) a.scrollIntoView({ block: 'center', behavior: 'smooth' }); }); },
chooseHighlighted(e){ const i=this.highlightedId; if (i) this.choose(i, this.items[i], e); else if (e) this.$emit('enter'); },
choose(id, display, emitEnter){ const c=(display.match(/\[(\d+)\]/)||[])[1] || ''; this.emitSelection(c, display); 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.mode !== 'autocomplete' || !this.q || this.q.length < 2) { this.items = {}; this.hasMoreResults = false; return; } this.busy = true; try { const b = window.TT_CONFIG.BASE_PATH || ''; const r = await fetch(`${b}/Address/Api?do=findAddress&fibu_primary_account=1&q=${encodeURIComponent(this.q)}`); if (r.ok) { const j = await r.json(); const addresses = j?.result?.addresses || {}; if (addresses.more) { this.hasMoreResults = true; delete addresses.more; } else { this.hasMoreResults = false; } this.items = addresses; this.highlighted = 0; } else { this.items = {}; this.hasMoreResults = false; } } catch { this.items = {}; this.hasMoreResults = false; } 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, modalClass: String },
template: `
`,
watch: {
show(isShown) {
if (isShown) {
this.$nextTick(() => {
// nodeType 1 is an Element node, this prevents errors if v-if renders a comment node.
if (this.$el && this.$el.nodeType === 1 && this.$el.parentNode !== document.body) {
document.body.appendChild(this.$el);
}
document.body.style.overflow = 'hidden';
});
} else {
document.body.style.overflow = '';
}
}
},
beforeDestroy() {
if (this.show && this.$el && this.$el.nodeType === 1 && this.$el.parentNode === document.body) {
document.body.removeChild(this.$el);
}
document.body.style.overflow = '';
}
});
/* ---------- Root View: ---------- */
Vue.component('radius', {
template: `
`,
data() { return { view: 'users', window: window, _initFlags: {} }; },
computed: {
viewOptions() {
const o = [{ id: 'users', name: 'Benutzer', icon: 'fa-duotone fa-users' },{ id: 'free', name: 'Freie Benutzer', icon: 'fa-duotone fa-user-plus' },{ id: 'unused', name: 'Ungenutzte Benutzer', icon: 'fa-duotone fa-user-clock' }];
if (window.TT_CONFIG.CAN_BILLING === '1') { o.push({ id: 'ont', name: 'ONT Parser', icon: 'fa-duotone fa-diagram-project' }, { id: 'ontReverse', name: 'ONT Reverse', icon: 'fa-duotone fa-arrows-rotate' }); } return o;
}
},
mounted() { this.switchView(this.view); },
methods: {
switchView(v){ this.view=v; if(!this._initFlags||this._initFlags[v])return; let r=''; if(v==='free')r='freeView';else if(v==='unused')r='unusedView'; if(r){this.$nextTick(()=>{const c=this.$refs[r];if(c&&typeof c.initIfNeeded==='function'){c.initIfNeeded();this._initFlags[v]=true;}});}}
}
});