@ordojs/mobile
Version:
Mobile and PWA support for OrdoJS applications
2 lines • 5.78 kB
JavaScript
'use strict';var s=class{config;features=new Map;constructor(e={}){this.config={features:{camera:true,geolocation:true,contacts:true,calendar:true,notifications:true,storage:true,network:true,device:true},permissions:[],plugins:[],...e},this.initializeFeatures();}initializeFeatures(){this.config.features.camera&&this.features.set("camera",{name:"Camera",available:this.isFeatureAvailable("camera"),permission:"camera",description:"Access device camera for photo/video capture"}),this.config.features.geolocation&&this.features.set("geolocation",{name:"Geolocation",available:this.isFeatureAvailable("geolocation"),permission:"geolocation",description:"Access device location services"}),this.config.features.contacts&&this.features.set("contacts",{name:"Contacts",available:this.isFeatureAvailable("contacts"),permission:"contacts",description:"Access device contacts"}),this.config.features.calendar&&this.features.set("calendar",{name:"Calendar",available:this.isFeatureAvailable("calendar"),permission:"calendar",description:"Access device calendar"}),this.config.features.notifications&&this.features.set("notifications",{name:"Notifications",available:this.isFeatureAvailable("notifications"),permission:"notifications",description:"Send push notifications"}),this.config.features.storage&&this.features.set("storage",{name:"Storage",available:this.isFeatureAvailable("storage"),description:"Access device storage"}),this.config.features.network&&this.features.set("network",{name:"Network",available:this.isFeatureAvailable("network"),description:"Access network information"}),this.config.features.device&&this.features.set("device",{name:"Device",available:this.isFeatureAvailable("device"),description:"Access device information"});}isFeatureAvailable(e){if(typeof window>"u")return false;switch(e){case "camera":return "mediaDevices"in navigator&&"getUserMedia"in navigator.mediaDevices;case "geolocation":return "geolocation"in navigator;case "contacts":return "contacts"in navigator||"ContactsManager"in window;case "calendar":return "Calendar"in window;case "notifications":return "Notification"in window;case "storage":return "localStorage"in window||"indexedDB"in window;case "network":return "connection"in navigator||"onLine"in navigator;case "device":return "deviceMemory"in navigator||"hardwareConcurrency"in navigator;default:return false}}async getDeviceInfo(){let e={platform:"web",version:"",model:"",manufacturer:"",screenWidth:window.screen.width,screenHeight:window.screen.height,pixelRatio:window.devicePixelRatio||1,orientation:window.screen.width>window.screen.height?"landscape":"portrait",isOnline:navigator.onLine,connectionType:"unknown",batteryLevel:0,isCharging:false},t=navigator.userAgent.toLowerCase();if(t.includes("iphone")||t.includes("ipad")?e.platform="ios":t.includes("android")&&(e.platform="android"),"connection"in navigator){let a=navigator.connection;a&&(e.connectionType=a.effectiveType||"unknown");}if("getBattery"in navigator)try{let a=await navigator.getBattery();e.batteryLevel=a.level,e.isCharging=a.charging;}catch(a){console.warn("Battery API not available:",a);}return e}async requestPermission(e){let t=this.features.get(e);if(!t)throw new Error(`Feature ${e} not found`);if(!t.available)throw new Error(`Feature ${e} not available on this device`);switch(e){case "camera":return this.requestCameraPermission();case "geolocation":return this.requestGeolocationPermission();case "notifications":return this.requestNotificationPermission();default:return true}}async requestCameraPermission(){try{return (await navigator.mediaDevices.getUserMedia({video:!0})).getTracks().forEach(t=>t.stop()),!0}catch(e){return console.error("Camera permission denied:",e),false}}async requestGeolocationPermission(){return new Promise(e=>{navigator.geolocation.getCurrentPosition(()=>e(true),()=>e(false),{timeout:5e3});})}async requestNotificationPermission(){return "Notification"in window?await Notification.requestPermission()==="granted":false}async takePhoto(e={}){if(!this.features.get("camera")?.available)throw new Error("Camera not available");if(!await this.requestPermission("camera"))throw new Error("Camera permission denied");return new Promise((a,n)=>{let i=document.createElement("input");i.type="file",i.accept="image/*",i.capture=e.source==="camera"?"camera":void 0,i.onchange=c=>{let o=c.target.files?.[0];if(o){let r=new FileReader;r.onload=()=>{a({dataUrl:r.result,file:o,width:0,height:0});},r.onerror=()=>n(new Error("Failed to read file")),r.readAsDataURL(o);}else n(new Error("No file selected"));},i.click();})}async getLocation(e={}){if(!this.features.get("geolocation")?.available)throw new Error("Geolocation not available");if(!await this.requestPermission("geolocation"))throw new Error("Geolocation permission denied");return new Promise((a,n)=>{navigator.geolocation.getCurrentPosition(i=>{a({latitude:i.coords.latitude,longitude:i.coords.longitude,accuracy:i.coords.accuracy,altitude:i.coords.altitude??void 0,heading:i.coords.heading??void 0,speed:i.coords.speed??void 0,timestamp:i.timestamp});},i=>{n(new Error(`Geolocation error: ${i.message}`));},{enableHighAccuracy:e.highAccuracy||false,timeout:e.timeout||1e4,maximumAge:e.maximumAge||6e4});})}async sendNotification(e){if(!this.features.get("notifications")?.available)throw new Error("Notifications not available");if(!await this.requestPermission("notifications"))throw new Error("Notification permission denied");"Notification"in window&&new Notification(e.title,e);}getAvailableFeatures(){return Array.from(this.features.values())}updateConfig(e){this.config={...this.config,...e},this.initializeFeatures();}getConfig(){return {...this.config}}};exports.NativeManager=s;//# sourceMappingURL=native.js.map
//# sourceMappingURL=native.js.map