xrsh-com/com/launcher.js

255 lines
8.0 KiB
JavaScript
Raw Normal View History

2024-01-22 10:38:16 +01:00
/*
2024-04-19 16:42:46 +02:00
* ## launcher
2024-01-22 10:38:16 +01:00
*
* displays app (icons) for enduser to launch
*
* ```javascript
* <a-entity app="app/launcher.js"/>
* ```
2024-04-19 16:42:46 +02:00
*
2024-01-22 10:38:16 +01:00
* | property | type | example |
* |--------------|--------------------|----------------------------------------------------------------------------------------|
2024-04-19 16:42:46 +02:00
* | `registries` | `array` of strings | <a-entity app="app/launcher.js; registers: https://foo.com/index.json, ./index.json"/> |
2024-01-22 10:38:16 +01:00
*
* | event | target | info |
* |--------------|-------------------------------------------------------------------------------------------------------------|
2024-04-19 16:42:46 +02:00
* | `launcher` | an app | when pressing an app icon, `launcher` event will be send to the respective app |
2024-01-22 10:38:16 +01:00
*/
AFRAME.registerComponent('launcher', {
2024-04-19 16:42:46 +02:00
schema: {
2024-05-23 14:14:53 +02:00
attach: { type:"selector"}
2024-01-22 10:38:16 +01:00
},
2024-05-23 14:14:53 +02:00
dependencies:['dom'],
init: async function () {
2024-01-22 10:38:16 +01:00
this.data.apps = []
2024-05-23 14:14:53 +02:00
await AFRAME.utils.require({
html: "https://unpkg.com/aframe-htmlmesh@2.1.0/build/aframe-html.js", // html to AFRAME
dom: "./com/dom.js",
svgfile: "https://7dir.github.io/aframe-svgfile-component/aframe-svgfile-component.min.js",
2024-01-22 10:38:16 +01:00
})
2024-05-23 14:14:53 +02:00
this.el.setAttribute("dom","")
this.render()
this.el.sceneEl.addEventListener('enter-vr', (e) => this.render() )
2024-01-22 10:38:16 +01:00
},
dom: {
scale: 3,
events: ['click'],
2024-05-23 14:14:53 +02:00
html: (me) => `<div id="iconmenu">loading components..</div>`,
2024-04-19 16:42:46 +02:00
css: (me) => `#iconmenu {
2024-01-22 10:38:16 +01:00
z-index: 1000;
display: flex;
2024-04-19 16:42:46 +02:00
flex-direction: row;
align-items: flex-start;
height: 50px;
overflow:hidden;
2024-01-22 10:38:16 +01:00
position: fixed;
2024-04-19 16:42:46 +02:00
right: 162px;
bottom: 0px;
left:20px;
background: transparent;
padding-bottom: 54px;
2024-01-22 10:38:16 +01:00
box-sizing: border-box;
pointer-events: none;
2024-01-22 12:13:41 +01:00
visibility: visible !important;
2024-01-22 10:38:16 +01:00
}
#iconmenu > button {
2024-04-19 16:42:46 +02:00
line-height:0px;
2024-01-22 10:38:16 +01:00
pointer-events:all;
width: 58px;
2024-04-19 16:42:46 +02:00
height: 34px;
padding: 12px 0px 12px 0px;
2024-01-22 10:38:16 +01:00
border-radius: 0px;
color: var(--xrsh-primary);
background: #FFF;
2024-04-19 16:42:46 +02:00
border-top: 2px solid #BBB;
border-bottom: 2px solid #BBB;
font-size:18px;
2024-01-22 10:38:16 +01:00
}
#iconmenu > button:first-child {
2024-04-19 16:42:46 +02:00
border-radius: 5px 0px 0px 5px;
border-bottom: 2px solid #BBB;
border-left: 2px solid #BBB;
padding-bottom:16px;
2024-01-22 10:38:16 +01:00
}
#iconmenu > button:last-child {
2024-04-19 16:42:46 +02:00
border-radius:0px 5px 5px 0px;
border-top: 2px solid #BBB;
border-right: 2px solid #BBB;
2024-01-22 10:38:16 +01:00
padding-top:13px;
}
#iconmenu > button > img {
2024-04-19 16:42:46 +02:00
transform: translate(0px,-14px);
2024-01-22 10:38:16 +01:00
opacity:0.5;
padding: 5px;
border-radius: 0px;
}
2024-01-22 12:13:41 +01:00
#iconmenu > button > img:hover{
background: #AAA;
2024-01-22 10:38:16 +01:00
transition:0.gg3s;
border-radius: 50%;
}`
},
events:{
},
2024-05-23 14:14:53 +02:00
render: async function(){
if( !this.el.dom ) return // too early (dom.js component not ready)
let inVR = this.sceneEl && this.sceneEl.renderer.xr.isPresenting
let items = [...this.el.children]
let requires = []
let i = 0
let colors = [
'#4C73FE',
'#554CFE',
'#864CFE',
'#B44CFE',
'#E24CFE',
'#FE4CD3'
]
const add2D = (launchCom,el,manifest,aentity) => {
let btn = document.createElement('button')
btn.innerHTML = `${ manifest?.icons?.length > 0
? `<img src='${manifest.icons[0].src}' title='${manifest.name}: ${manifest.description}'/>`
: `${manifest.short_name}`
}`
btn.addEventListener('click', () => el.emit('launcher',{}) )
this.el.dom.appendChild(btn)
}
const add3D = (launchCom,el,manifest) => {
let aentity = document.createElement('a-entity')
let atext = document.createElement('a-entity')
aentity.setAttribute("mixin","menuitem")
aentity.setAttribute("position",`${i++ * 0.2} 0 0`)
if( !aentity.getAttribute("material")){
aentity.setAttribute('material',`side: double; color: ${colors[ i % colors.length]}`)
2024-04-19 16:42:46 +02:00
}
2024-05-23 14:14:53 +02:00
atext.setAttribute("text",`value: ${manifest.short_name}; align: baseline; anchor: align; align:center; wrapCount:7`)
atext.setAttribute("scale","0.1 0.1 0.1")
atext.setAttribute("position","0 0 0.0")
aentity.appendChild(atext)
this.el.appendChild(aentity)
return aentity
}
// finally render them!
this.el.dom.innerHTML = '' // clear
this.system.components.map( (c) => {
const launchComponentKey = c.getAttributeNames().pop()
const launchCom = c.components[ launchComponentKey ]
if( !launchCom ) return console.warn(`could not find component '${launchComponentKey}' (forgot to include script-tag?)`)
const manifest = launchCom.manifest
if( manifest ){
add2D(launchCom,c,manifest, add3D(launchCom,c,manifest) )
}
})
2024-04-19 16:42:46 +02:00
2024-05-23 14:14:53 +02:00
if( this.data.attach ){
this.el.object3D.visible = inVR ? true : false
// if( inVR ) this.data.attach.appendChild(this.el)
}
2024-04-19 16:42:46 +02:00
2024-01-22 10:38:16 +01:00
},
manifest: { // HTML5 manifest to identify app to xrsh
2024-04-19 16:42:46 +02:00
"short_name": "launcher",
"name": "App Launcher",
2024-01-22 10:38:16 +01:00
"icons": [
{
"src": "https://css.gg/browser.svg",
"type": "image/svg+xml",
"sizes": "512x512"
}
],
2024-04-19 16:42:46 +02:00
"category":"system",
2024-01-22 10:38:16 +01:00
"id": "/?source=pwa",
"start_url": "/?source=pwa",
"background_color": "#3367D6",
"display": "standalone",
"scope": "/",
"theme_color": "#3367D6",
"shortcuts": [
{
"name": "What is the latest news?",
"cli":{
"usage": "helloworld <type> [options]",
"example": "helloworld news",
"args":{
"--latest": {type:"string"}
}
},
"short_name": "Today",
"description": "View weather information for today",
"url": "/today?source=pwa",
"icons": [{ "src": "/images/today.png", "sizes": "192x192" }]
}
],
"description": "Hello world information",
"screenshots": [
{
"src": "/images/screenshot1.png",
"type": "image/png",
"sizes": "540x720",
"form_factor": "narrow"
}
],
"help":`
2024-04-19 16:42:46 +02:00
Helloworld application
2024-01-22 10:38:16 +01:00
This is a help file which describes the application.
It will be rendered thru troika text, and will contain
headers based on non-punctualized lines separated by linebreaks,
in above's case "\nHelloworld application\n" will qualify as header.
`
}
});
2024-05-23 14:14:53 +02:00
AFRAME.registerSystem('launcher',{
init: function(){
this.components = []
// observer HTML changes in <a-scene>
observer = new MutationObserver( (a,b) => this.getLaunchables(a,b) )
observer.observe( this.sceneEl, {characterData: false, childList: true, attributes: false});
},
getLaunchables: function(mutationsList,observer){
let searchEvent = 'launcher'
let els = [...this.sceneEl.getElementsByTagName("*")]
this.components = els.filter( (el) => {
let hasEvent = false
if( el.components ){
for( let i in el.components ){
if( el.components[i].events && el.components[i].events[searchEvent] ){
hasEvent = true
}
}
}
return hasEvent ? el : null
})
this.updateLauncher()
},
updateLauncher: function(){
let launcher = document.querySelector('[launcher]')
if( launcher ) launcher.components['launcher'].render()
}
})