Reset data bị lỗi, thêm hotspot

This commit is contained in:
2026-06-08 11:50:47 +07:00
parent c495efad36
commit d9ed8032d3
10 changed files with 521 additions and 238 deletions
+34 -11
View File
@@ -1,14 +1,17 @@
let activeViewer = null;
let currentHotspots = [];
let securityApplied = false;
let currentSceneOwnerId = null;
/**
* Initializes and shows the Pannellum 360° panorama viewer with security overlays.
* @param {string} imageUrl - Authorized URL to fetch the secure image stream
* @param {Array} hotspots - List of hotspots from the database
* @param {string} ownerId - ID of the scene owner
*/
function initPanoramaViewer(imageUrl, hotspots = []) {
function initPanoramaViewer(imageUrl, hotspots = [], ownerId = null) {
currentHotspots = hotspots;
currentSceneOwnerId = ownerId;
const container = document.getElementById('viewer-container');
container.style.display = 'block';
@@ -20,15 +23,15 @@ function initPanoramaViewer(imageUrl, hotspots = []) {
// Chuyển đổi dữ liệu hotspots từ DB sang định dạng Pannellum
const pannellumHotspots = hotspots.map(h => ({
pitch: h.pitch,
yaw: h.yaw,
pitch: h.coordinates?.pitch || h.pitch,
yaw: h.coordinates?.yaw || h.yaw,
type: "info",
text: h.text || "Điểm điều hướng",
text: h.title || "Điểm điều hướng",
id: h._id,
clickHandlerFunc: () => {
if (h.targetSceneId) {
if (h.target_scene_id || h.targetSceneId) {
// Gọi hàm openScene từ main_map.js
openScene(h.targetSceneId);
openScene(h.target_scene_id || h.targetSceneId);
}
}
}));
@@ -87,23 +90,43 @@ function applyViewerSecurity() {
// Nếu viewer đang hoạt động, lấy tọa độ Pitch/Yaw tại điểm click
if (activeViewer) {
// Kiểm tra phân quyền trước khi cho phép tương tác chuột phải
const userRole = localStorage.getItem('role');
const currentUserId = localStorage.getItem('userId');
// Phân quyền: Admin (Chủ sở hữu) hoặc Người tạo ra Scene này
const isAdmin = userRole === 'Chủ sở hữu' || userRole === 'admin';
const isAuthorized = isAdmin ||
(currentUserId && currentSceneOwnerId && currentUserId.toString() === currentSceneOwnerId.toString());
// Lấy tọa độ cầu (Pitch/Yaw) từ điểm click chuột
const coords = activeViewer.mouseEventToCoords(e);
if (!coords) return;
if (!coords) return false;
const pitch = coords[0];
const yaw = coords[1];
// Kiểm tra xem có hotspot nào gần điểm click không (ngưỡng 2 độ)
const existing = currentHotspots.find(h =>
Math.abs(h.pitch - pitch) < 2 && Math.abs(h.yaw - yaw) < 2
Math.abs((h.coordinates?.pitch || h.pitch) - pitch) < 2 &&
Math.abs((h.coordinates?.yaw || h.yaw) - yaw) < 2
);
if (typeof window.handleHotspotCreation === 'function') {
window.handleHotspotCreation(pitch, yaw, existing);
// Nếu không được phép, dừng xử lý và chặn menu mặc định
if (!isAuthorized) return false;
if (existing) {
// ĐÃ CÓ Hotspot -> Hiện Menu: [Sửa Hotspot] / [Xóa Hotspot]
if (typeof window.openHotspotMenu === 'function') {
window.openHotspotMenu(existing);
}
} else {
console.log(`Coordinates captured: Pitch ${pitch}, Yaw ${yaw}`);
// CHƯA CÓ Hotspot -> Hiện Form: [Tạo mới Hotspot]
if (typeof window.handleHotspotCreation === 'function') {
window.handleHotspotCreation(pitch, yaw, null);
}
}
console.log(`Coordinates captured: Pitch ${pitch}, Yaw ${yaw}`);
}
return false;
};