pertama dan terakhir
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
import { initMap } from './modules/map.js';
|
||||
import { setupDrawControls } from './modules/draw.js';
|
||||
import { renderForm } from './modules/form.js';
|
||||
import { spbuService, jalanService, kavlingService } from './services/api.service.js';
|
||||
|
||||
let appMap;
|
||||
let drawControl;
|
||||
|
||||
// Custom SVG Icons Generator
|
||||
export const createIcon = (svgPath, color) => {
|
||||
return L.divIcon({
|
||||
className: 'custom-icon',
|
||||
html: `<div style="background-color: ${color}; width: 34px; height: 34px; border-radius: 50%; display: flex; align-items: center; justify-content: center; box-shadow: 0 4px 10px rgba(0,0,0,0.5); border: 2px solid rgba(255,255,255,0.8); color: white;">
|
||||
<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24">${svgPath}</svg>
|
||||
</div>`,
|
||||
iconSize: [34, 34],
|
||||
iconAnchor: [17, 34],
|
||||
popupAnchor: [0, -34]
|
||||
});
|
||||
};
|
||||
|
||||
export const iconSPBU = (is24h) => createIcon('<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17.657 16.657L13.414 20.9a1.998 1.998 0 01-2.827 0l-4.242-4.243a8 8 0 1111.314 0z"></path><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 11a3 3 0 11-6 0 3 3 0 016 0z"></path>', is24h ? '#10B981' : '#EF4444');
|
||||
|
||||
window.showToast = (msg, type='success') => {
|
||||
const t = document.createElement('div');
|
||||
t.className = `toast ${type}`;
|
||||
t.innerHTML = msg;
|
||||
document.getElementById('toast-container').appendChild(t);
|
||||
setTimeout(() => { t.style.transform='translateX(100%)'; t.style.opacity=0; setTimeout(()=>t.remove(),300); }, 3000);
|
||||
};
|
||||
|
||||
document.addEventListener('DOMContentLoaded', async () => {
|
||||
appMap = initMap('map');
|
||||
drawControl = setupDrawControls(appMap, handleGeometryCreated);
|
||||
await loadAllData();
|
||||
});
|
||||
|
||||
const loadAllData = async () => {
|
||||
try {
|
||||
const spbu = await spbuService.getAll();
|
||||
L.geoJSON(spbu, {
|
||||
pointToLayer: (f, latlng) => L.marker(latlng, { icon: iconSPBU(f.properties.buka_24_jam) }),
|
||||
onEachFeature: (f, l) => bindPopup(l, 'spbu', f.properties)
|
||||
}).addTo(appMap);
|
||||
|
||||
const jalan = await jalanService.getAll();
|
||||
L.geoJSON(jalan, { style: { color: '#F59E0B', weight: 4 }, onEachFeature: (f, l) => bindPopup(l, 'jalan', f.properties) }).addTo(appMap);
|
||||
|
||||
const kavling = await kavlingService.getAll();
|
||||
L.geoJSON(kavling, { style: { color: '#3B82F6', weight: 2, fillColor: '#3B82F6', fillOpacity: 0.3 }, onEachFeature: (f, l) => bindPopup(l, 'kavling', f.properties) }).addTo(appMap);
|
||||
} catch (e) {
|
||||
window.showToast("Gagal meload data: "+e.message, 'error');
|
||||
}
|
||||
};
|
||||
|
||||
const bindPopup = (layer, type, props) => {
|
||||
const ext = props.buka_24_jam !== undefined ? `<p>Buka 24 Jam: <strong style="color:${props.buka_24_jam?'#10B981':'#EF4444'}">${props.buka_24_jam ? 'Ya' : 'Tidak'}</strong></p>` : '';
|
||||
layer.bindPopup(`
|
||||
<div class="popup-custom">
|
||||
<h3>${props.nama}</h3>
|
||||
<p>${props.deskripsi || ''}</p>
|
||||
${ext}
|
||||
<button class="btn-delete" onclick="window.deleteData('${type}', ${props.id})">Hapus Data</button>
|
||||
</div>
|
||||
`);
|
||||
};
|
||||
|
||||
window.deleteData = async (type, id) => {
|
||||
if(!confirm('Yakin ingin menghapus?')) return;
|
||||
try {
|
||||
if(type==='spbu') await spbuService.delete(id);
|
||||
if(type==='jalan') await jalanService.delete(id);
|
||||
if(type==='kavling') await kavlingService.delete(id);
|
||||
window.showToast('Data dihapus');
|
||||
setTimeout(()=>location.reload(), 800);
|
||||
} catch(e) { window.showToast('Gagal hapus', 'error'); }
|
||||
};
|
||||
|
||||
const handleGeometryCreated = (type, geometry, layer) => {
|
||||
const tempLayer = L.geoJSON(geometry).addTo(appMap);
|
||||
renderForm(type, geometry, async (payload) => {
|
||||
try {
|
||||
if(type==='spbu') await spbuService.create(payload);
|
||||
if(type==='jalan') await jalanService.create(payload);
|
||||
if(type==='kavling') await kavlingService.create(payload);
|
||||
window.showToast('Berhasil disimpan');
|
||||
setTimeout(()=>location.reload(), 800);
|
||||
} catch(e) { window.showToast('Gagal simpan', 'error'); }
|
||||
}, () => {
|
||||
appMap.removeLayer(tempLayer);
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* config.js
|
||||
* Tanggung Jawab: Menyimpan konstanta dan konfigurasi global aplikasi.
|
||||
*/
|
||||
|
||||
export const CONFIG = {
|
||||
BASE_URL: '/01/backend/api',
|
||||
|
||||
// Konstanta tipe fitur
|
||||
FEATURE_TYPES: {
|
||||
SPBU: 'Point',
|
||||
JALAN: 'LineString',
|
||||
KAVLING: 'Polygon'
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,31 @@
|
||||
export const setupDrawControls = (map, onGeometryCreated) => {
|
||||
const drawControl = new L.Control.Draw({
|
||||
draw: { marker: true, polyline: true, polygon: true, circle: false, circlemarker: false, rectangle: false }
|
||||
});
|
||||
map.addControl(drawControl);
|
||||
|
||||
map.on(L.Draw.Event.CREATED, function (event) {
|
||||
const layer = event.layer;
|
||||
const type = event.layerType;
|
||||
|
||||
// Peta draw default tipe: marker -> spbu, polyline -> jalan, polygon -> kavling
|
||||
let customType = type;
|
||||
if(type === 'marker') customType = 'spbu';
|
||||
if(type === 'polyline') customType = 'jalan';
|
||||
if(type === 'polygon') customType = 'kavling';
|
||||
if(window.currentDrawType) customType = window.currentDrawType; // Untuk P2/P3
|
||||
|
||||
onGeometryCreated(customType, layer.toGeoJSON().geometry, layer);
|
||||
});
|
||||
|
||||
// Custom Menu Handlers
|
||||
const markerDrawer = new L.Draw.Marker(map, drawControl.options.draw.marker);
|
||||
const polylineDrawer = new L.Draw.Polyline(map, drawControl.options.draw.polyline);
|
||||
const polygonDrawer = new L.Draw.Polygon(map, drawControl.options.draw.polygon);
|
||||
|
||||
document.getElementById('btn-draw-spbu')?.addEventListener('click', () => { window.currentDrawType='spbu'; markerDrawer.enable(); });
|
||||
document.getElementById('btn-draw-jalan')?.addEventListener('click', () => { window.currentDrawType='jalan'; polylineDrawer.enable(); });
|
||||
document.getElementById('btn-draw-kavling')?.addEventListener('click', () => { window.currentDrawType='kavling'; polygonDrawer.enable(); });
|
||||
|
||||
return drawControl;
|
||||
};
|
||||
@@ -0,0 +1,50 @@
|
||||
export const renderForm = (type, geometry, onSubmit, onCancel) => {
|
||||
const container = document.getElementById('form-container');
|
||||
container.style.display = 'flex';
|
||||
|
||||
const extraFields = type === 'spbu' ? `
|
||||
<div class="form-group">
|
||||
<label class="checkbox-group">
|
||||
<input type="checkbox" id="input-24jam">
|
||||
<span>Buka 24 Jam</span>
|
||||
</label>
|
||||
</div>
|
||||
` : '';
|
||||
|
||||
container.innerHTML = `
|
||||
<div class="form-panel">
|
||||
<h3 style="margin:0 0 10px 0; font-size:1rem; border-bottom:1px solid rgba(255,255,255,0.1); padding-bottom:8px;">Simpan Data (${type.toUpperCase()})</h3>
|
||||
<div class="form-group">
|
||||
<input type="text" id="input-nama" class="form-control" placeholder="Nama / Label" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<input type="text" id="input-deskripsi" class="form-control" placeholder="Deskripsi Singkat">
|
||||
</div>
|
||||
${extraFields}
|
||||
<div style="display:flex; gap:10px; margin-top:8px;">
|
||||
<button id="btn-save" class="btn-submit" style="flex:1;">Simpan</button>
|
||||
<button id="btn-cancel" class="btn-submit" style="background:rgba(255,255,255,0.1); color:var(--text-light); flex:1;">Batal</button>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
|
||||
document.getElementById('btn-save').addEventListener('click', () => {
|
||||
const payload = {
|
||||
type,
|
||||
geometry,
|
||||
nama: document.getElementById('input-nama').value,
|
||||
deskripsi: document.getElementById('input-deskripsi').value,
|
||||
};
|
||||
if (type === 'spbu') payload.buka_24_jam = document.getElementById('input-24jam').checked;
|
||||
|
||||
onSubmit(payload);
|
||||
container.style.display = 'none';
|
||||
container.innerHTML = '';
|
||||
});
|
||||
|
||||
document.getElementById('btn-cancel').addEventListener('click', () => {
|
||||
if(onCancel) onCancel();
|
||||
container.style.display = 'none';
|
||||
container.innerHTML = '';
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* map.js
|
||||
* Tanggung Jawab: Inisialisasi peta Leaflet dasar dan base layer.
|
||||
*/
|
||||
|
||||
export const initMap = (containerId) => {
|
||||
// Pusat peta default (Universitas Tanjungpura, Pontianak)
|
||||
const map = L.map(containerId, {
|
||||
zoomControl: false
|
||||
}).setView([-0.0583, 109.3448], 15);
|
||||
|
||||
// Pindahkan zoom control ke kanan bawah
|
||||
L.control.zoom({
|
||||
position: 'bottomright'
|
||||
}).addTo(map);
|
||||
|
||||
// Tile Layer Premium (CartoDB Dark Matter untuk Glass UI)
|
||||
L.tileLayer('https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}{r}.png', {
|
||||
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors © <a href="https://carto.com/attributions">CARTO</a>',
|
||||
subdomains: 'abcd',
|
||||
maxZoom: 20
|
||||
}).addTo(map);
|
||||
|
||||
return map;
|
||||
};
|
||||
@@ -0,0 +1,41 @@
|
||||
export const BASE_URL = '';
|
||||
|
||||
const createService = (endpoint) => ({
|
||||
getAll: async () => {
|
||||
const res = await fetch(`${BASE_URL}/${endpoint}`);
|
||||
const json = await res.json();
|
||||
return json.data;
|
||||
},
|
||||
create: async (data) => {
|
||||
const res = await fetch(`${BASE_URL}/${endpoint}`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
const json = await res.json();
|
||||
return json.data;
|
||||
},
|
||||
delete: async (id) => {
|
||||
const res = await fetch(`${BASE_URL}/${endpoint}?id=${id}`, {
|
||||
method: 'DELETE'
|
||||
});
|
||||
const json = await res.json();
|
||||
return json.data;
|
||||
}
|
||||
});
|
||||
|
||||
// P01
|
||||
export const spbuService = createService('01/backend/api/spbu.php');
|
||||
export const jalanService = createService('01/backend/api/jalan.php');
|
||||
export const kavlingService = createService('01/backend/api/kavling.php');
|
||||
|
||||
// P02
|
||||
export const rumahIbadahService = createService('02/backend/api/rumah_ibadah.php');
|
||||
export const wargaMiskinService = createService('02/backend/api/warga_miskin.php');
|
||||
export const haversineService = {
|
||||
getDalamRadius: async (id, radius) => {
|
||||
const res = await fetch(`${BASE_URL}/02/backend/api/haversine.php?rumah_ibadah_id=${id}&radius_km=${radius}`);
|
||||
const json = await res.json();
|
||||
return json.data;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* jalan.service.js
|
||||
* Tanggung Jawab: Komunikasi HTTP ke endpoint API jalan.php
|
||||
*/
|
||||
import { CONFIG } from '../config.js';
|
||||
|
||||
export const jalanService = {
|
||||
getAll: async () => {
|
||||
const response = await fetch(`${CONFIG.BASE_URL}/jalan.php`);
|
||||
return await response.json();
|
||||
},
|
||||
|
||||
save: async (data) => {
|
||||
const response = await fetch(`${CONFIG.BASE_URL}/jalan.php`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
return await response.json();
|
||||
},
|
||||
|
||||
delete: async (id) => {
|
||||
const response = await fetch(`${CONFIG.BASE_URL}/jalan.php?id=${id}`, {
|
||||
method: 'DELETE'
|
||||
});
|
||||
return await response.json();
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* kavling.service.js
|
||||
* Tanggung Jawab: Komunikasi HTTP ke endpoint API kavling.php
|
||||
*/
|
||||
import { CONFIG } from '../config.js';
|
||||
|
||||
export const kavlingService = {
|
||||
getAll: async () => {
|
||||
const response = await fetch(`${CONFIG.BASE_URL}/kavling.php`);
|
||||
return await response.json();
|
||||
},
|
||||
|
||||
save: async (data) => {
|
||||
const response = await fetch(`${CONFIG.BASE_URL}/kavling.php`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
return await response.json();
|
||||
},
|
||||
|
||||
delete: async (id) => {
|
||||
const response = await fetch(`${CONFIG.BASE_URL}/kavling.php?id=${id}`, {
|
||||
method: 'DELETE'
|
||||
});
|
||||
return await response.json();
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* spbu.service.js
|
||||
* Tanggung Jawab: Komunikasi HTTP ke endpoint API spbu.php
|
||||
*/
|
||||
import { CONFIG } from '../config.js';
|
||||
|
||||
export const spbuService = {
|
||||
getAll: async () => {
|
||||
const response = await fetch(`${CONFIG.BASE_URL}/spbu.php`);
|
||||
return await response.json();
|
||||
},
|
||||
|
||||
save: async (data) => {
|
||||
const response = await fetch(`${CONFIG.BASE_URL}/spbu.php`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
return await response.json();
|
||||
},
|
||||
|
||||
delete: async (id) => {
|
||||
const response = await fetch(`${CONFIG.BASE_URL}/spbu.php?id=${id}`, {
|
||||
method: 'DELETE'
|
||||
});
|
||||
return await response.json();
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* style.util.js
|
||||
* Tanggung Jawab: Mendefinisikan style untuk GeoJSON berdasarkan properti.
|
||||
*/
|
||||
|
||||
export const getGeoJsonStyle = (feature) => {
|
||||
// Style default untuk Kavling (Polygon)
|
||||
if (feature.geometry.type === 'Polygon') {
|
||||
return {
|
||||
color: '#10B981', // Secondary color
|
||||
weight: 2,
|
||||
opacity: 0.8,
|
||||
fillColor: '#34D399',
|
||||
fillOpacity: 0.4
|
||||
};
|
||||
}
|
||||
|
||||
// Style default untuk Jalan (LineString)
|
||||
if (feature.geometry.type === 'LineString') {
|
||||
const type = feature.properties.jenis_jalan;
|
||||
let color = '#4F46E5'; // Primary color
|
||||
|
||||
if (type === 'Arteri') color = '#EF4444'; // Danger color
|
||||
else if (type === 'Kolektor') color = '#F59E0B'; // Warning color
|
||||
|
||||
return {
|
||||
color: color,
|
||||
weight: 4,
|
||||
opacity: 0.9
|
||||
};
|
||||
}
|
||||
|
||||
return {};
|
||||
};
|
||||
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* ui.util.js
|
||||
* Tanggung Jawab: Manipulasi DOM umum seperti Toast notification.
|
||||
*/
|
||||
|
||||
export const showToast = (message, type = 'success') => {
|
||||
const container = document.getElementById('toast-container');
|
||||
if (!container) return;
|
||||
|
||||
const toast = document.createElement('div');
|
||||
toast.className = `toast ${type}`;
|
||||
toast.textContent = message;
|
||||
|
||||
container.appendChild(toast);
|
||||
|
||||
// Hapus toast setelah 3 detik
|
||||
setTimeout(() => {
|
||||
toast.style.opacity = '0';
|
||||
toast.style.transform = 'translateY(20px)';
|
||||
toast.style.transition = 'all 0.3s ease';
|
||||
|
||||
setTimeout(() => toast.remove(), 300);
|
||||
}, 3000);
|
||||
};
|
||||
|
||||
export const clearFormContainer = () => {
|
||||
const container = document.getElementById('form-container');
|
||||
if (container) {
|
||||
container.innerHTML = '';
|
||||
container.style.display = 'none';
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user