2023-05-17 21:31:28 +02:00
|
|
|
xrf.navigator = {}
|
|
|
|
|
|
2023-07-04 17:15:23 +02:00
|
|
|
xrf.navigator.to = (url,flags,loader,data) => {
|
2023-05-18 17:11:11 +02:00
|
|
|
if( !url ) throw 'xrf.navigator.to(..) no url given'
|
2023-06-22 08:49:24 +02:00
|
|
|
|
2023-05-17 21:31:28 +02:00
|
|
|
return new Promise( (resolve,reject) => {
|
|
|
|
|
let {urlObj,dir,file,hash,ext} = xrf.parseUrl(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
|
2023-06-22 08:49:24 +02:00
|
|
|
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
|
2023-07-04 17:15:23 +02:00
|
|
|
if( !loader ){
|
|
|
|
|
const Loader = xrf.loaders[ext]
|
|
|
|
|
if( !Loader ) throw 'xrfragment: no loader passed to xrfragment for extension .'+ext
|
|
|
|
|
loader = loader || new Loader().setPath( dir )
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-17 21:31:28 +02:00
|
|
|
// force relative path
|
|
|
|
|
if( dir ) dir = dir[0] == '.' ? dir : `.${dir}`
|
2023-07-05 16:43:07 +02:00
|
|
|
url = url.replace(dir,"")
|
2023-07-04 17:15:23 +02:00
|
|
|
loader = loader || new Loader().setPath( dir )
|
|
|
|
|
const onLoad = (model) => {
|
|
|
|
|
xrf.reset() // clear xrf objects from scene
|
2023-05-18 12:39:47 +02:00
|
|
|
model.file = file
|
2023-05-18 12:32:57 +02:00
|
|
|
xrf.add( model.scene )
|
2023-06-22 08:49:24 +02:00
|
|
|
// 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
|
2023-06-22 08:49:24 +02:00
|
|
|
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)
|
2023-07-04 17:15:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( data ) loader.parse(data, "", onLoad )
|
|
|
|
|
else loader.load(url, onLoad )
|
2023-05-17 21:31:28 +02:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
xrf.navigator.init = () => {
|
|
|
|
|
if( xrf.navigator.init.inited ) return
|
|
|
|
|
window.addEventListener('popstate', function (event){
|
2023-06-22 08:49:24 +02:00
|
|
|
xrf.navigator.to( document.location.search.substr(1) + document.location.hash )
|
2023-05-17 21:31:28 +02:00
|
|
|
})
|
2023-06-08 17:45:21 +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
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-22 08:49:24 +02:00
|
|
|
xrf.navigator.updateHash = (hash) => {
|
|
|
|
|
if( hash == document.location.hash || hash.match(/\|/) ) return // skip unnecesary pushState triggers
|
2023-06-22 13:59:17 +02:00
|
|
|
console.log(`URL: ${document.location.search.substr(1)}#${hash}`)
|
2023-06-22 08:49:24 +02:00
|
|
|
document.location.hash = hash
|
|
|
|
|
xrf.emit('updateHash', {hash} )
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-18 17:11:11 +02:00
|
|
|
xrf.navigator.pushState = (file,hash) => {
|
2023-05-18 12:32:57 +02:00
|
|
|
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
|
|
|
}
|