xrfragment/src/3rd/js/three/navigator.js

58 lines
2.1 KiB
JavaScript
Raw Normal View History

2023-05-17 21:31:28 +02:00
xrf.navigator = {}
xrf.navigator.to = (url,flags) => {
2023-05-18 17:11:11 +02:00
if( !url ) throw 'xrf.navigator.to(..) no url given'
2023-05-17 21:31:28 +02:00
return new Promise( (resolve,reject) => {
let {urlObj,dir,file,hash,ext} = xrf.parseUrl(url)
console.log(url)
2023-05-18 17:11:11 +02:00
2023-05-22 17:18:15 +02:00
if( !file || xrf.model.file == file ){ // we're already loaded
xrf.eval( url, xrf.model, flags ) // and eval local URI XR fragments
2023-05-18 17:11:11 +02:00
return resolve(xrf.model)
}
2023-05-17 21:31:28 +02:00
if( xrf.model && xrf.model.scene ) xrf.model.scene.visible = false
const Loader = xrf.loaders[ext]
if( !Loader ) throw 'xrfragment: no loader passed to xrfragment for extension .'+ext
xrf.reset() // clear xrf objects from scene
2023-05-17 21:31:28 +02:00
// force relative path
if( dir ) dir = dir[0] == '.' ? dir : `.${dir}`
const loader = new Loader().setPath( dir )
loader.load( file, function(model){
2023-05-18 12:39:47 +02:00
model.file = file
xrf.add( model.scene )
// only change url when loading *another* file
if( xrf.model ) xrf.navigator.pushState( `${dir}${file}`, hash )
2023-05-17 21:31:28 +02:00
xrf.model = model
xrf.eval( '#', model ) // execute the default projection '#' (if exist)
xrf.eval( url, model ) // and eval URI XR fragments
if( !hash.match(/pos=/) )
xrf.eval( '#pos=0,0,0' ) // set default position if not specified
2023-05-17 21:31:28 +02:00
resolve(model)
})
})
}
xrf.navigator.init = () => {
if( xrf.navigator.init.inited ) return
window.addEventListener('popstate', function (event){
xrf.navigator.to( document.location.search.substr(1) + document.location.hash )
2023-05-17 21:31:28 +02:00
})
xrf.navigator.material = {
selection: new xrf.THREE.LineBasicMaterial({color:0xFF00FF,linewidth:2})
}
2023-05-17 21:31:28 +02:00
xrf.navigator.init.inited = true
}
xrf.navigator.updateHash = (hash) => {
if( hash == document.location.hash || hash.match(/\|/) ) return // skip unnecesary pushState triggers
document.location.hash = hash
xrf.emit('updateHash', {hash} )
}
2023-05-18 17:11:11 +02:00
xrf.navigator.pushState = (file,hash) => {
if( file == document.location.search.substr(1) ) return // page is in its default state
window.history.pushState({},`${file}#${hash}`, document.location.pathname + `?${file}#${hash}` )
2023-05-17 21:31:28 +02:00
}