import { SIZE_UNIT } from "./JurassicUnix.constants" ; import { Coordinates } from './types/coordinates' ; import { PerspectiveType } from "./types/perspective-type" ; import { Perspective } from "./types/perspective" ; import { BoxData } from "./types/box-data" ; export function getPerspectiveFor ( x , y , z , width , perspectiveType ) { const objectTopCentre = { x : - width * 0.5 - x , y : - y , z : width * 0.5 - z } ; let offset ; if ( perspectiveType === PerspectiveType . SIDE_VIEW ) { offset = { x : 0 , y : width * 0.4 , z : - width * 0.8 } ; } else { const windowAspectRatio = window . innerWidth / window . innerHeight ; offset = { x : 0 , y : width * Math . max ( 1 , windowAspectRatio ) * 0.6 , z : 0 } ; } return { viewPoint : addCoordinates ( objectTopCentre , offset ) , perspectiveType } ; } export function get3DTranslation ( offset ) { return ( "translate3d(" + offset . x + SIZE_UNIT + ", " + offset . y + SIZE_UNIT + ", " + offset . z + SIZE_UNIT + ")" ) ; } export function get3DRotation ( direction , degrees ) { return ( "rotate" + direction . toUpperCase ( ) + "(" + degrees + "deg)" ) ; } export function addCoordinates ( position1 ? , position2 ? ) { const pos1 = position1 || { x : 0 , y : 0 , z : 0 } ; const pos2 = position2 || { x : 0 , y : 0 , z : 0 } ; return { x : pos1 . x + pos2 . x , y : pos1 . y + pos2 . y , z : pos1 . z + pos2 . z , } ; } export function isPrimitive ( value ) { return value !== Object ( value ) ; } function isPrimitiveProperty ( prop ) { return ! Array . isArray ( prop ) && isPrimitive ( prop ) ; } function isCompoundProperty ( prop ) { return ! Array . isArray ( prop ) && ! isPrimitive ( prop ) ; } function isCompoundArray ( prop ) { return Array . isArray ( prop ) && prop . length > 0 && ! isPrimitive ( prop [ 0 ] ) ; } export function capitalize ( str ) { const spaced = str . split ( '' ) . reduce ( ( newString , nextChar ) => newString + ( isUpperCase ( nextChar ) ? ( ' ' + nextChar . toLowerCase ( ) ) : nextChar ) , '' ) ; return `${ spaced . charAt ( 0 ) . toUpperCase ( ) }${ spaced . slice ( 1 ) }` ; } function isUpperCase ( char ) { return char === char . toUpperCase ( ) && char !== char . toLowerCase ( ) ; } export function mapBoxData ( root ) { let result = [ ] ; const keys = Object . keys ( root ) ; keys . forEach ( ( key ) => { const node = mapBoxDataChildren ( root [ key ] , key + '-' + keys . indexOf ( key ) , capitalize ( key ) ) ; if ( node ) { result . push ( node ) ; } } ) ; return result ; } function getItemName ( item ) { const nameAttr = [ 'awarder' , 'area' , 'studyType' , 'name' , 'language' , 'name' , 'organization' , 'network' ] . find ( nameAttr => item [ nameAttr ] !== undefined ) ; return nameAttr ? item [ nameAttr ] : '' ; } function mapBoxDataChildren ( root , boxKey , boxName ) { if ( isPrimitive ( root ) ) { return null ; } if ( Array . isArray ( root ) ) { if ( root . length === 0 ) { return null ; } if ( root . length === 1 ) { return mapBoxDataChildren ( root [ 0 ] , boxKey , boxName ) ; } const node = { id : boxKey , name : boxName , children : [ ] , } ; root . forEach ( item => { const childBoxKey = boxKey + '-' + root . indexOf ( item ) ; const itemName = getItemName ( item ) ; const childNode = mapBoxDataChildren ( item , childBoxKey , itemName ) ; if ( childNode ) { node . children . push ( childNode ) ; } } ) ; return node ; } const keys = Object . keys ( root ) ; const hasProperties = keys . some ( key => isPrimitiveProperty ( root [ key ] ) ) ; const childrenKeys = keys . filter ( key => ( isCompoundProperty ( root [ key ] ) || isCompoundArray ( root [ key ] ) ) ) ; if ( hasProperties && childrenKeys . length === 0 ) { return { id : boxKey , name : boxName , data : root , children : [ ] , } ; } if ( ! hasProperties && childrenKeys . length === 1 ) { const childBoxKey = boxKey + '-' + childrenKeys [ 0 ] ; const onlyChild = root [ childrenKeys [ 0 ] ] ; return mapBoxDataChildren ( onlyChild , childBoxKey + '-' + 0 , getItemName ( onlyChild ) ) ; } const node = { id : boxKey , name : boxName , children : [ ] , } ; if ( hasProperties ) { const propsBoxKey = boxKey + '-' + 0 ; const dataWithoutChildren = { ... root } ; childrenKeys . forEach ( key => delete dataWithoutChildren [ key ] ) ; node . children . push ( { id : propsBoxKey , name : "Info" , data : dataWithoutChildren , children : [ ] , } ) ; } childrenKeys . forEach ( childKey => { const childBoxKey = boxKey + '-' + childKey + '-' + childrenKeys . indexOf ( childKey ) ; const childNode = mapBoxDataChildren ( root [ childKey ] , childBoxKey , capitalize ( childKey ) ) ; if ( childNode ) { node . children . push ( childNode ) ; } } ) ; return node ; }