/** * MobileApp PWA - Main Vue Application * * Unified mobile app with module navigation. * Routes: /MobileApp -> Home, /MobileApp/Lager -> Module, /MobileApp/Lager/Inventur -> Submodule */ import { authState, checkAuth, login, logout } from '/mobile/shared/auth.js'; import LoginScreen from '/mobile/components/LoginScreen.js'; import MainMenu from '/mobile/components/MainMenu.js'; import LagerModule from '/mobile/modules/lager/LagerModule.js'; const { createApp, ref, computed, onMounted, onUnmounted, watch } = Vue; // Check if running as installed PWA const isPWAInstalled = () => { // Check display-mode standalone (Android Chrome, desktop) if (window.matchMedia('(display-mode: standalone)').matches) return true; // Check iOS Safari standalone mode if (window.navigator.standalone === true) return true; // Check if launched from TWA (Trusted Web Activity) if (document.referrer.includes('android-app://')) return true; return false; }; // Check if we should require PWA installation const shouldRequirePWA = () => { const hostname = window.location.hostname; // Only require PWA on production domain return hostname === 'thetool.xinon.at'; }; // Parse initial path from config const parseInitialRoute = () => { const initialPath = window.TT_CONFIG?.INITIAL_PATH || '/MobileApp'; const parts = initialPath.replace('/MobileApp', '').split('/').filter(Boolean); return { module: parts[0] || null, submodule: parts[1] || null }; }; const App = { components: { LoginScreen, MainMenu, LagerModule }, setup() { // ==================== STATE ==================== const currentView = ref('loading'); const user = ref(null); const toast = ref({ show: false, message: '', type: 'success' }); const theme = ref('system'); const showSettings = ref(false); // Module-specific settings const lagerSimpleMode = ref(false); // Navigation state const currentModule = ref(null); const currentSubmodule = ref(null); // PWA Install state const showInstallPrompt = ref(false); const deferredInstallPrompt = ref(null); const isIOS = ref(/iPad|iPhone|iPod/.test(navigator.userAgent)); const isAndroid = ref(/Android/.test(navigator.userAgent)); // Can go back? const canGoBack = computed(() => currentModule.value !== null); // ==================== THEME ==================== const applyTheme = () => { const isDark = localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches); document.documentElement.classList.toggle('dark', isDark); const metaThemeColor = document.querySelector('meta[name="theme-color"]'); if (metaThemeColor) { metaThemeColor.setAttribute('content', isDark ? '#0f172a' : '#005384'); } }; const setTheme = (newTheme) => { theme.value = newTheme; if (newTheme === 'system') { localStorage.removeItem('theme'); } else { localStorage.setItem('theme', newTheme); } applyTheme(); }; // ==================== PWA INSTALL ==================== const handleInstallPrompt = (e) => { // Prevent Chrome's default install prompt e.preventDefault(); // Store the event for later use deferredInstallPrompt.value = e; }; const triggerInstall = async () => { if (!deferredInstallPrompt.value) return; // Show the install prompt deferredInstallPrompt.value.prompt(); // Wait for user response const { outcome } = await deferredInstallPrompt.value.userChoice; if (outcome === 'accepted') { showInstallPrompt.value = false; // Reload to get standalone mode window.location.reload(); } deferredInstallPrompt.value = null; }; // ==================== LAGER SETTINGS ==================== const loadLagerSettings = () => { try { const saved = localStorage.getItem('movement_settings'); if (saved) { const settings = JSON.parse(saved); lagerSimpleMode.value = settings.simpleMode || false; } } catch (e) {} }; const setLagerSimpleMode = (value) => { lagerSimpleMode.value = value; try { const saved = localStorage.getItem('movement_settings'); const settings = saved ? JSON.parse(saved) : {}; settings.simpleMode = value; localStorage.setItem('movement_settings', JSON.stringify(settings)); } catch (e) {} }; // ==================== NAVIGATION ==================== const navigate = (module, submodule = null) => { currentModule.value = module; currentSubmodule.value = submodule; // Update browser URL let path = '/MobileApp'; if (module) path += '/' + module; if (submodule) path += '/' + submodule; history.pushState({ module, submodule }, '', path); }; const goHome = () => { navigate(null, null); }; const goBack = () => { if (currentSubmodule.value) { navigate(currentModule.value, null); } else if (currentModule.value) { navigate(null, null); } }; // Handle browser back button window.addEventListener('popstate', (event) => { if (event.state) { currentModule.value = event.state.module; currentSubmodule.value = event.state.submodule; } else { currentModule.value = null; currentSubmodule.value = null; } }); // ==================== AUTH ==================== const handleLogin = async (credentials) => { // Handle 2FA success (already verified in LoginScreen) if (credentials._2faSuccess) { user.value = credentials.user; currentView.value = 'app'; showToast('Erfolgreich angemeldet', 'success'); return { success: true }; } const result = await login(credentials); if (result.success) { user.value = result.user; currentView.value = 'app'; showToast('Erfolgreich angemeldet', 'success'); } return result; }; const handleLogout = async () => { await logout(); user.value = null; currentModule.value = null; currentSubmodule.value = null; currentView.value = 'login'; showToast('Abgemeldet', 'success'); }; // ==================== TOAST ==================== const showToast = (message, type = 'success') => { toast.value = { show: true, message, type }; setTimeout(() => { toast.value.show = false; }, 3000); }; // ==================== COMPUTED ==================== const currentComponent = computed(() => { if (currentView.value !== 'app') return null; if (!currentModule.value) return 'MainMenu'; if (currentModule.value.toLowerCase() === 'lager') return 'LagerModule'; return 'MainMenu'; }); const breadcrumbs = computed(() => { const crumbs = [{ label: 'Home', module: null, submodule: null }]; if (currentModule.value) { crumbs.push({ label: currentModule.value, module: currentModule.value, submodule: null }); } if (currentSubmodule.value) { crumbs.push({ label: currentSubmodule.value, module: currentModule.value, submodule: currentSubmodule.value }); } return crumbs; }); // ==================== LIFECYCLE ==================== onMounted(async () => { // Initialize theme const savedTheme = localStorage.getItem('theme'); if (savedTheme) { theme.value = savedTheme; } applyTheme(); window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', applyTheme); // Load module settings loadLagerSettings(); // Listen for beforeinstallprompt (Android) window.addEventListener('beforeinstallprompt', handleInstallPrompt); // Check if PWA is required but not installed if (shouldRequirePWA() && !isPWAInstalled()) { showInstallPrompt.value = true; currentView.value = 'install'; return; } // Check authentication const result = await checkAuth(); if (result.authenticated) { user.value = result.user; currentView.value = 'app'; // Parse initial route const initialRoute = parseInitialRoute(); currentModule.value = initialRoute.module; currentSubmodule.value = initialRoute.submodule; // Set initial history state history.replaceState( { module: initialRoute.module, submodule: initialRoute.submodule }, '', window.location.pathname ); } else { currentView.value = 'login'; } }); return { currentView, user, toast, theme, showSettings, currentModule, currentSubmodule, currentComponent, canGoBack, breadcrumbs, handleLogin, handleLogout, navigate, goHome, goBack, showToast, setTheme, lagerSimpleMode, setLagerSimpleMode, // PWA Install showInstallPrompt, deferredInstallPrompt, isIOS, isAndroid, triggerInstall, }; }, template: `
Lädt...
Logo

App installieren

Für die beste Erfahrung installiere die App auf deinem Gerät.

So installierst du die App:

  1. 1 Tippe auf das Teilen-Symbol
  2. 2 Scrolle und wähle "Zum Home-Bildschirm"
  3. 3 Tippe auf "Hinzufügen"

So installierst du die App:

  1. 1 Tippe auf das Menü (⋮) oben rechts
  2. 2 Wähle "App installieren" oder "Zum Startbildschirm hinzufügen"
  3. 3 Bestätige mit "Installieren"

Hinweis: Diese App ist für mobile Geräte optimiert. Bitte öffne diese Seite auf deinem Smartphone und installiere die App.

powered by XINON

{{ toast.message }}
` }; createApp(App).mount('#app');