Files
radixOS/public/javascript/pluginAPI.js
2026-04-22 11:55:23 +02:00

92 lines
2.0 KiB
JavaScript

const pluginAPI = {
async update(name, updates) {
if(Object.keys(updates)[0] && Object.values(updates)[0] == "") {
updates[Object.keys(updates)[0]] = [ ];
}
return await this._request(
`/api/plugins/${name}/update`,
'POST',
{ updates }
);
},
async activation(name, state) {
return await this._request(
`/api/plugins/activation`,
'POST',
{ name, state }
);
},
async create(name) {
return await this._request(
`/api/plugins/${name}/create`,
'POST'
);
},
async rename(name, newName) {
return await this._request(
`/api/plugins/${name}/rename`,
'POST',
{ newName }
);
},
async delete(name) {
return await this._request(
`/api/plugins/${name}/delete`,
'POST'
);
},
// --- zentrale Request-Funktion ---
async _request(url, method, body) {
try {
const options = { method };
if (body) {
options.headers = { 'Content-Type': 'application/json' };
options.body = JSON.stringify(body);
}
const res = await fetch(url, options);
if (!res.ok) {
const text = await res.text();
writeEventLog(4, 'CLIENT', `Request fehlgeschlagen: ${text}`);
throw new Error(`HTTP ${res.status}: ${text}`);
}
return res.json();
} catch (err) {
writeEventLog(4, 'CLIENT', err);
throw err;
}
}
};
class AttachOnBlurChange {
constructor(el, callback) {
this.el = el;
this.callback = callback;
this.initial = this.el.value;
this.el.addEventListener('blur', () => {
this.handleBlur();
});
}
handleBlur() {
if (this.el.value === this.initial) return;
const oldValue = this.initial;
const newValue = this.el.value;
this.initial = newValue;
this.callback(newValue, oldValue, this.el);
}
}