Penambahan Menu Setting

This commit is contained in:
Ariyutta
2026-11-06 08:04:50 +07:00
parent 288782ddaa
commit e72f3acb2b
24 changed files with 3124 additions and 7 deletions
+494
View File
@@ -1182,6 +1182,9 @@ const enhanceSelect = (el) => {
const placeholder = name ? `Pilih ${name}` : 'Pilih...';
const $el = $(el);
// Di dalam <dialog> (lapisan atas) dropdown harus ikut di dalam dialog; di body ia tertutup modal
const dialog = el.closest('dialog');
$el.select2({
width: '100%',
placeholder: hasEmpty ? placeholder : undefined,
@@ -1190,6 +1193,7 @@ const enhanceSelect = (el) => {
minimumResultsForSearch: el.options.length > 8 ? 0 : Infinity,
dropdownCssClass: 'select2-glass',
selectionCssClass: 'select2-glass',
dropdownParent: dialog ? $(dialog) : $(document.body),
});
// Select2 -> Alpine: x-model dan x-on:change mendengar event bawaan
@@ -1238,4 +1242,494 @@ document.addEventListener('alpine:initialized', () => {
}).observe(document.body, { childList: true, subtree: true });
});
// Setting > Setting Prodi rincian (akun DB lama): tab bagian dan modal Edit (PUT JSON, lalu halaman dimuat ulang)
Alpine.data('satuSettingProdi', (config) => ({
tab: 'krs',
form: JSON.parse(JSON.stringify(config.form)),
saving: false,
openForm() {
this.form = JSON.parse(JSON.stringify(config.form));
const dialog = this.$refs.form;
if (dialog && !dialog.open) dialog.showModal();
},
closeForm() {
this.$refs.form?.close();
},
backdrop(event) {
if (event.target === this.$refs.form) this.closeForm();
},
async save() {
if (this.saving) return;
const section = (s) => ({ buka: Boolean(s.buka), awal: s.buka ? s.awal || null : null, akhir: s.buka ? s.akhir || null : null });
const body = {
periode: config.periode,
kurikulum: this.form.kurikulum || null,
krs: section(this.form.krs),
nilai: section(this.form.nilai),
registrasi: section(this.form.registrasi),
};
this.saving = true;
try {
const response = await fetch(config.url, {
method: 'PUT',
headers: { 'Content-Type': 'application/json', Accept: 'application/json', 'X-CSRF-TOKEN': config.token, 'X-Requested-With': 'XMLHttpRequest' },
body: JSON.stringify(body),
});
const data = await response.json().catch(() => null);
if (!response.ok) {
const first = data?.errors ? Object.values(data.errors).flat()[0] : null;
throw new Error(first || data?.message || 'Permintaan gagal. Muat ulang halaman lalu coba lagi.');
}
console.info(`[SIAKAD] PUT ${config.url}`, { hasil: data?.message });
window.toast('success', data.message);
this.closeForm();
window.location.reload();
} catch (error) {
window.toast('danger', error.message);
} finally {
this.saving = false;
}
},
}));
// Setting > Periode Akademik (akun DB lama): daftar di sisi klien, tambah/ubah lewat modal formulir, hapus lewat modal konfirmasi
Alpine.data('satuPeriode', (config) => ({
rows: config.rows,
urls: config.urls,
token: config.token,
q: '',
per: config.per,
page: 1,
editing: null,
form: { thakad: '', semester: 1, perkuliahan: 1, mulai: '', selesai: '' },
saving: false,
pending: null,
get filtered() {
const q = this.q.trim().toLowerCase();
if (q === '') return this.rows;
return this.rows.filter((row) => [row.kode, row.nama, row.awal, row.akhir, row.aktif].some((value) => String(value).toLowerCase().includes(q)));
},
get pages() {
return Math.max(1, Math.ceil(this.filtered.length / this.per));
},
get view() {
const start = (this.page - 1) * this.per;
return this.filtered.slice(start, start + this.per);
},
get window() {
const start = Math.max(1, Math.min(this.page - 2, this.pages - 4));
return Array.from({ length: Math.min(5, this.pages) }, (_, i) => start + i);
},
reset() {
this.page = 1;
},
go(page) {
this.page = Math.min(Math.max(1, page), this.pages);
},
startInsert() {
this.editing = 'new';
this.form = { thakad: '', semester: 1, perkuliahan: 1, mulai: '', selesai: '' };
this.openForm();
},
startEdit(row) {
this.editing = row.id;
this.form = { thakad: row.thakad, semester: row.semester, perkuliahan: row.perkuliahan, mulai: row.mulai ?? '', selesai: row.selesai ?? '' };
this.openForm();
},
openForm() {
const dialog = this.$refs.form;
if (dialog && !dialog.open) dialog.showModal();
this.$nextTick(() => this.$refs.thakad?.focus());
},
closeDialog(ref) {
this.$refs[ref]?.close();
},
backdrop(event, ref) {
if (event.target === this.$refs[ref]) this.closeDialog(ref);
},
async request(url, method, body) {
const response = await fetch(url, {
method,
headers: { 'Content-Type': 'application/json', Accept: 'application/json', 'X-CSRF-TOKEN': this.token, 'X-Requested-With': 'XMLHttpRequest' },
body: body === undefined ? undefined : JSON.stringify(body),
});
const data = await response.json().catch(() => null);
if (!response.ok) {
const first = data?.errors ? Object.values(data.errors).flat()[0] : null;
throw new Error(first || data?.message || 'Permintaan gagal. Muat ulang halaman lalu coba lagi.');
}
console.info(`[SIAKAD] ${method} ${url}`, { hasil: data?.message });
return data;
},
async save() {
if (this.saving || this.editing === null) return;
if (!/^\d{4}\/\d{4}$/.test(this.form.thakad.trim())) {
window.toast('warning', 'Tahun akademik harus berformat TTTT/TTTT, misalnya 2025/2026.');
this.$refs.thakad?.focus();
return;
}
const isNew = this.editing === 'new';
const url = isNew ? this.urls.store : this.urls.update.replace('__ID__', this.editing);
const body = {
thakad: this.form.thakad.trim(),
semester: Number(this.form.semester),
perkuliahan: Number(this.form.perkuliahan),
mulai: this.form.mulai || null,
selesai: this.form.selesai || null,
};
this.saving = true;
try {
const data = await this.request(url, isNew ? 'POST' : 'PUT', body);
this.rows = data.rows;
window.toast('success', data.message);
this.closeDialog('form');
} catch (error) {
window.toast('danger', error.message);
} finally {
this.saving = false;
}
},
askRemove(row) {
this.pending = row;
const dialog = this.$refs.dialog;
if (dialog && !dialog.open) dialog.showModal();
},
async confirmRemove() {
if (!this.pending || this.saving) return;
this.saving = true;
try {
const data = await this.request(this.urls.destroy.replace('__ID__', this.pending.id), 'DELETE');
this.rows = data.rows;
if (this.page > this.pages) this.page = this.pages;
window.toast('success', data.message);
this.closeDialog('dialog');
} catch (error) {
window.toast('danger', error.message);
} finally {
this.saving = false;
}
},
}));
/* Setting > Kategori Kuesioner Layanan: daftar di sisi klien, tambah/ubah lewat modal satu kolom, hapus lewat konfirmasi. */
Alpine.data('satuKategoriKuesioner', (config) => ({
rows: config.rows,
urls: config.urls,
token: config.token,
ready: config.ready,
q: '',
per: config.per,
page: 1,
editing: null,
form: { nama: '' },
saving: false,
pending: null,
get filtered() {
const q = this.q.trim().toLowerCase();
if (q === '') return this.rows;
return this.rows.filter((row) => row.nama.toLowerCase().includes(q));
},
get pages() {
return Math.max(1, Math.ceil(this.filtered.length / this.per));
},
get view() {
const start = (this.page - 1) * this.per;
return this.filtered.slice(start, start + this.per);
},
get window() {
const start = Math.max(1, Math.min(this.page - 2, this.pages - 4));
return Array.from({ length: Math.min(5, this.pages) }, (_, i) => start + i);
},
reset() {
this.page = 1;
},
go(page) {
this.page = Math.min(Math.max(1, page), this.pages);
},
startInsert() {
this.editing = 'new';
this.form = { nama: '' };
this.openForm();
},
startEdit(row) {
this.editing = row.id;
this.form = { nama: row.nama };
this.openForm();
},
openForm() {
const dialog = this.$refs.form;
if (dialog && !dialog.open) dialog.showModal();
this.$nextTick(() => this.$refs.nama?.focus());
},
closeDialog(ref) {
this.$refs[ref]?.close();
},
backdrop(event, ref) {
if (event.target === this.$refs[ref]) this.closeDialog(ref);
},
async request(url, method, body) {
const response = await fetch(url, {
method,
headers: { 'Content-Type': 'application/json', Accept: 'application/json', 'X-CSRF-TOKEN': this.token, 'X-Requested-With': 'XMLHttpRequest' },
body: body === undefined ? undefined : JSON.stringify(body),
});
const data = await response.json().catch(() => null);
if (!response.ok) {
const first = data?.errors ? Object.values(data.errors).flat()[0] : null;
throw new Error(first || data?.message || 'Permintaan gagal. Muat ulang halaman lalu coba lagi.');
}
console.info(`[SIAKAD] ${method} ${url}`, { hasil: data?.message });
return data;
},
async save() {
if (this.saving || this.editing === null) return;
if (this.form.nama.trim() === '') {
window.toast('warning', 'Nama kategori wajib diisi.');
this.$refs.nama?.focus();
return;
}
const isNew = this.editing === 'new';
const url = isNew ? this.urls.store : this.urls.update.replace('__ID__', this.editing);
this.saving = true;
try {
const data = await this.request(url, isNew ? 'POST' : 'PUT', { nama: this.form.nama.trim() });
this.rows = data.rows;
if (this.page > this.pages) this.page = this.pages;
window.toast('success', data.message);
this.closeDialog('form');
} catch (error) {
window.toast('danger', error.message);
} finally {
this.saving = false;
}
},
askRemove(row) {
this.pending = row;
const dialog = this.$refs.dialog;
if (dialog && !dialog.open) dialog.showModal();
},
async confirmRemove() {
if (!this.pending || this.saving) return;
this.saving = true;
try {
const data = await this.request(this.urls.destroy.replace('__ID__', this.pending.id), 'DELETE');
this.rows = data.rows;
if (this.page > this.pages) this.page = this.pages;
window.toast('success', data.message);
this.closeDialog('dialog');
} catch (error) {
window.toast('danger', error.message);
} finally {
this.saving = false;
}
},
}));
/* Setting > Jenis Jawaban: daftar di sisi klien dengan centang baris, modal nama + daftar jawaban, hapus massal. */
Alpine.data('satuJenisJawaban', (config) => ({
rows: config.rows,
urls: config.urls,
token: config.token,
ready: config.ready,
q: '',
per: config.per,
page: 1,
picked: [],
editing: null,
form: { nama: '', jawaban: [] },
keySeq: 0,
saving: false,
pending: [],
get filtered() {
const q = this.q.trim().toLowerCase();
if (q === '') return this.rows;
return this.rows.filter((row) => row.nama.toLowerCase().includes(q) || row.jawaban.some((a) => a.teks.toLowerCase().includes(q)));
},
get pages() {
return Math.max(1, Math.ceil(this.filtered.length / this.per));
},
get view() {
const start = (this.page - 1) * this.per;
return this.filtered.slice(start, start + this.per);
},
get window() {
const start = Math.max(1, Math.min(this.page - 2, this.pages - 4));
return Array.from({ length: Math.min(5, this.pages) }, (_, i) => start + i);
},
get allPicked() {
return this.view.length > 0 && this.view.every((row) => this.picked.includes(row.id));
},
get pendingLabel() {
const names = this.rows.filter((row) => this.pending.includes(row.id)).map((row) => row.nama);
return names.length > 3 ? `${names.length} jenis jawaban` : names.join(', ');
},
reset() {
this.page = 1;
},
go(page) {
this.page = Math.min(Math.max(1, page), this.pages);
},
toggleAll() {
const ids = this.view.map((row) => row.id);
this.picked = this.allPicked ? this.picked.filter((id) => !ids.includes(id)) : [...new Set([...this.picked, ...ids])];
},
answer(nilai = '', teks = '') {
return { key: ++this.keySeq, nilai, teks };
},
addAnswer() {
const last = this.form.jawaban[this.form.jawaban.length - 1];
this.form.jawaban.push(this.answer(Number.isInteger(last?.nilai) ? last.nilai + 1 : 1));
},
removeAnswer(i) {
if (this.form.jawaban.length > 1) this.form.jawaban.splice(i, 1);
},
startInsert() {
this.editing = 'new';
this.form = { nama: '', jawaban: [this.answer(1)] };
this.openForm();
},
startEdit(row) {
this.editing = row.id;
this.form = { nama: row.nama, jawaban: row.jawaban.map((a) => this.answer(a.nilai, a.teks)) };
this.openForm();
},
openForm() {
const dialog = this.$refs.form;
if (dialog && !dialog.open) dialog.showModal();
this.$nextTick(() => this.$refs.nama?.focus());
},
closeDialog(ref) {
this.$refs[ref]?.close();
},
backdrop(event, ref) {
if (event.target === this.$refs[ref]) this.closeDialog(ref);
},
async request(url, method, body) {
const response = await fetch(url, {
method,
headers: { 'Content-Type': 'application/json', Accept: 'application/json', 'X-CSRF-TOKEN': this.token, 'X-Requested-With': 'XMLHttpRequest' },
body: body === undefined ? undefined : JSON.stringify(body),
});
const data = await response.json().catch(() => null);
if (!response.ok) {
const first = data?.errors ? Object.values(data.errors).flat()[0] : null;
throw new Error(first || data?.message || 'Permintaan gagal. Muat ulang halaman lalu coba lagi.');
}
console.info(`[SIAKAD] ${method} ${url}`, { hasil: data?.message });
return data;
},
async save() {
if (this.saving || this.editing === null) return;
if (this.form.nama.trim() === '') {
window.toast('warning', 'Nama jenis jawaban wajib diisi.');
this.$refs.nama?.focus();
return;
}
const jawaban = this.form.jawaban.filter((a) => a.teks.trim() !== '').map((a) => ({ nilai: Number(a.nilai), teks: a.teks.trim() }));
if (jawaban.length === 0) {
window.toast('warning', 'Isi minimal satu jawaban.');
return;
}
const isNew = this.editing === 'new';
const url = isNew ? this.urls.store : this.urls.update.replace('__ID__', this.editing);
this.saving = true;
try {
const data = await this.request(url, isNew ? 'POST' : 'PUT', { nama: this.form.nama.trim(), jawaban });
this.rows = data.rows;
if (this.page > this.pages) this.page = this.pages;
window.toast('success', data.message);
this.closeDialog('form');
} catch (error) {
window.toast('danger', error.message);
} finally {
this.saving = false;
}
},
remove() {
if (!this.picked.length) {
window.toast('warning', 'Pilih data yang akan dihapus terlebih dahulu.');
return;
}
this.askRemove(this.picked);
},
askRemove(ids) {
this.pending = [...ids];
const dialog = this.$refs.dialog;
if (dialog && !dialog.open) dialog.showModal();
},
async confirmRemove() {
const ids = [...this.pending];
if (!ids.length || this.saving) return;
this.saving = true;
try {
const data = await this.request(this.urls.destroy, 'POST', { ids });
this.rows = data.rows;
this.picked = this.picked.filter((id) => !ids.includes(id));
if (this.page > this.pages) this.page = this.pages;
window.toast('success', data.message);
this.closeDialog('dialog');
} catch (error) {
window.toast('danger', error.message);
} finally {
this.saving = false;
}
},
}));
Alpine.start();