1
1
import AbstractParamConfig from '@/router/storeSync/abstractParamConfig.class'
2
2
3
- function readValueFromObjectOrReturnNull ( object , paramName , type ) {
4
- if ( object && paramName in object ) {
5
- return type ( object [ paramName ] )
6
- }
7
- return null
8
- }
9
-
10
3
/**
11
- * @param query
4
+ * Reads the camera position from the single URL param. Returns null if the camera position is not
5
+ * defined or not complete
6
+ *
7
+ * @param urlParamValue
12
8
* @returns {CameraPosition | null }
13
9
*/
14
- function parseCameraFromQuery ( query ) {
15
- const camera = readValueFromObjectOrReturnNull ( query , 'camera' , String )
16
- if ( camera ) {
17
- let cameraValues = camera . split ( ',' )
18
- // the split must have 6 components (x, y, z, pitch, yaw and roll)
10
+ export function readCameraFromUrlParam ( urlParamValue ) {
11
+ if ( urlParamValue ) {
12
+ let cameraValues = urlParamValue . split ( ',' )
13
+ // the split must have 6 components (x, y, z, pitch, heading and roll)
19
14
if ( cameraValues . length === 6 ) {
20
15
// parsing to number all values (default to 0 if the value is empty)
21
16
cameraValues = cameraValues . map ( ( value ) => ( value === '' ? 0 : Number ( value ) ) )
22
- const [ x , y , z , pitch , yaw , roll ] = cameraValues
17
+ const [ x , y , z , pitch , heading , roll ] = cameraValues
23
18
return {
24
19
x,
25
20
y,
26
21
z,
27
22
pitch,
28
- yaw ,
23
+ heading ,
29
24
roll,
30
25
}
31
26
}
32
27
}
33
28
return null
34
29
}
35
30
31
+ function dispatchCameraFromUrlIntoStore ( store , urlParamValue ) {
32
+ const promisesForAllDispatch = [ ]
33
+ const camera = readCameraFromUrlParam ( urlParamValue )
34
+ if ( camera ) {
35
+ promisesForAllDispatch . push ( store . dispatch ( 'setCameraPosition' , camera ) )
36
+ }
37
+ return Promise . all ( promisesForAllDispatch )
38
+ }
39
+
40
+ function generateCameraUrlParamFromStoreValues ( store ) {
41
+ if ( store . state . ui . showIn3d && store . state . position . camera !== null ) {
42
+ const { x, y, z, pitch, heading, roll } = store . state . position . camera
43
+ const valuesAsString = [ x , y , z , pitch , heading , roll ] . map ( ( value ) =>
44
+ value === 0 ? '' : `${ value } `
45
+ )
46
+ return valuesAsString . join ( ',' )
47
+ }
48
+ return null
49
+ }
50
+
36
51
/**
37
52
* Definition of a set of URL params to store the position camera for the 3D viewer
38
53
*
39
54
* It will generate a unique camera URL param that will be a concat of all relevant camera values
40
- * (x,y,z,yaw ,pitch,roll), this param will only be added to the URL when 3D is active
55
+ * (x,y,z,heading ,pitch,roll), this param will only be added to the URL when 3D is active
41
56
*
42
57
* This param parsing is based on the value of the 3D flag in the store, and not the one in the URL.
43
58
*/
@@ -46,83 +61,10 @@ export default class CameraParamConfig extends AbstractParamConfig {
46
61
super (
47
62
'camera' ,
48
63
'setCameraPosition' ,
49
- ( ) => { } ,
50
- ( ) => { } ,
64
+ dispatchCameraFromUrlIntoStore ,
65
+ generateCameraUrlParamFromStoreValues ,
51
66
false ,
52
67
String
53
68
)
54
69
}
55
-
56
- /**
57
- * Reads the camera position from the single URL param
58
- *
59
- * @param query
60
- * @returns {CameraPosition | null }
61
- * @override
62
- */
63
- readValueFromQuery ( query ) {
64
- return parseCameraFromQuery ( query )
65
- }
66
-
67
- /**
68
- * Adds the camera URL param if 3D is active, or removes the camera URL param when not active
69
- *
70
- * @param query
71
- * @param store
72
- * @override
73
- */
74
- populateQueryWithStoreValue ( query , store ) {
75
- if ( store . state . ui . showIn3d ) {
76
- const { x, y, z, pitch, yaw, roll } = store . state . position . camera
77
- const valuesAsString = [ x , y , z , pitch , yaw , roll ] . map ( ( value ) =>
78
- value === 0 ? '' : `${ value } `
79
- )
80
- query [ 'camera' ] = valuesAsString . join ( ',' )
81
- } else {
82
- delete query [ 'camera' ]
83
- }
84
- }
85
-
86
- /**
87
- * Dispatches to the store the camera position from the URL, if 3D is active
88
- *
89
- * @param {Vuex.Store } store
90
- * @param query
91
- * @returns {Promise<Awaited[]> }
92
- * @override
93
- */
94
- populateStoreWithQueryValue ( store , query ) {
95
- const promisesSetValuesInStore = [ ]
96
- if ( store . state . ui . showIn3d ) {
97
- const cameraInQuery = parseCameraFromQuery ( query )
98
- if ( cameraInQuery ) {
99
- promisesSetValuesInStore . push ( store . dispatch ( 'setCameraPosition' , cameraInQuery ) )
100
- }
101
- }
102
- return Promise . all ( promisesSetValuesInStore )
103
- }
104
-
105
- /**
106
- * Checks if the camera in the URL is different from the one in the store, this check happens
107
- * only when 3D is active
108
- *
109
- * @param query
110
- * @param store
111
- * @returns {boolean }
112
- * @override
113
- */
114
- valuesAreDifferentBetweenQueryAndStore ( query , store ) {
115
- if ( store . state . ui . showIn3d ) {
116
- const queryCamera = parseCameraFromQuery ( query )
117
- if ( ! queryCamera ) {
118
- return true
119
- }
120
- const camera = store . state . position . camera
121
- let isEqual = true
122
- Object . entries ( camera ) . forEach ( ( [ key , value ] ) => {
123
- isEqual &= value === queryCamera [ key ]
124
- } )
125
- return ! isEqual
126
- }
127
- }
128
70
}
0 commit comments