@@ -13,18 +13,18 @@ function callCallbacks(data) {
13
13
}
14
14
15
15
function getChangeListener ( ) {
16
- return function ( data ) {
16
+ return ( data ) => {
17
17
callCallbacks ( data ) ;
18
18
19
19
currentMode = data ;
20
20
} ;
21
21
}
22
22
23
- function setDefaultGroup ( ) {
23
+ function setDefaultGroup ( mode = 0 ) {
24
24
initialized = true ;
25
25
26
26
getChangeListener ( ) ( {
27
- id : 0
27
+ id : mode
28
28
} ) ;
29
29
30
30
window . chayns . ui . modeSwitch . changeMode ( 0 ) ;
@@ -92,6 +92,7 @@ export default class ModeSwitchHelper {
92
92
}
93
93
94
94
const allowedGroups = [ ] ;
95
+ let isChaynsIdAdmin = false ;
95
96
96
97
const groups = [ ] ;
97
98
if ( options . groups ) {
@@ -107,81 +108,88 @@ export default class ModeSwitchHelper {
107
108
} ) ;
108
109
}
109
110
110
- if ( window . chayns . env . user . isAuthenticated ) {
111
- const groupObject = getGroupObject ( 0 , window . chayns . env . user . name , [ 0 ] ) ;
112
- groupObject . default = true ;
113
- allowedGroups . push ( groupObject ) ;
111
+ chayns . ready . then ( ( data ) => {
112
+ if ( window . chayns . env . user . isAuthenticated ) {
113
+ // Condition if adminMode ChaynsId
114
+ let groupObject ;
114
115
116
+ const managerGroup = ModeSwitchHelper . findManagerGroup ( groups ) ;
115
117
116
- let savedModeId = null ;
117
- let changeGroupIndex = 0 ;
118
- if ( options . save ) {
119
- savedModeId = getSavedMode ( ) ;
120
- }
118
+ if ( managerGroup && data && data . AppUser . AdminMode ) {
119
+ groupObject = getGroupObject ( managerGroup . id , managerGroup . name , managerGroup . uacIds ) ;
120
+ isChaynsIdAdmin = true ;
121
+ } else {
122
+ groupObject = getGroupObject ( 0 , window . chayns . env . user . name , [ 0 ] ) ;
123
+ groupObject . default = true ;
124
+ }
121
125
122
- if ( savedModeId === null && options . defaultMode ) {
123
- savedModeId = options . defaultMode ;
124
- }
126
+ allowedGroups . push ( groupObject ) ;
125
127
126
- let changeGroup = false ;
127
- let changeGroupValue = null ;
128
128
129
- for ( let i = 0 , x = groups . length ; i < x ; i += 1 ) {
130
- if ( ! groups [ i ] . uacId && ! groups [ i ] . uacIds ) {
131
- const addGroupObject = getGroupObject ( groups [ i ] . id , groups [ i ] . name , [ 0 ] ) ;
132
- allowedGroups . push ( addGroupObject ) ;
129
+ let savedModeId = null ;
130
+ let changeGroupIndex = 0 ;
131
+ if ( options . save ) {
132
+ savedModeId = getSavedMode ( ) ;
133
+ }
133
134
134
- if ( addGroupObject . id === savedModeId ) {
135
- changeGroup = true ;
136
- changeGroupIndex = allowedGroups . length - 1 ;
137
- changeGroupValue = addGroupObject ;
138
- }
139
- } else {
140
- const uacIds = getUacIds ( groups [ i ] ) ;
141
- const allowedUacs = getAllowedUacIdsFromArray ( uacIds ) ;
135
+ if ( savedModeId === null && options . defaultMode ) {
136
+ savedModeId = options . defaultMode ;
137
+ }
138
+
139
+ let changeGroup = false ;
140
+ let changeGroupValue = null ;
142
141
143
- if ( allowedUacs . length > 0 ) {
144
- const addGroupObject = getGroupObject ( groups [ i ] . id , groups [ i ] . name , allowedUacs ) ;
142
+ for ( let i = 0 , x = groups . length ; i < x ; i += 1 ) {
143
+ if ( ! groups [ i ] . uacId && ! groups [ i ] . uacIds ) {
144
+ const addGroupObject = getGroupObject ( groups [ i ] . id , groups [ i ] . name , [ 0 ] ) ;
145
145
allowedGroups . push ( addGroupObject ) ;
146
146
147
147
if ( addGroupObject . id === savedModeId ) {
148
148
changeGroup = true ;
149
149
changeGroupIndex = allowedGroups . length - 1 ;
150
150
changeGroupValue = addGroupObject ;
151
151
}
152
+ } else {
153
+ const uacIds = getUacIds ( groups [ i ] ) ;
154
+ const allowedUacs = getAllowedUacIdsFromArray ( uacIds ) ;
155
+
156
+ if ( allowedUacs . length > 0 && ! ( allowedUacs . find ( uac => uac === 1 ) ) ) {
157
+ const addGroupObject = getGroupObject ( groups [ i ] . id , groups [ i ] . name , allowedUacs ) ;
158
+ allowedGroups . push ( addGroupObject ) ;
159
+
160
+ if ( addGroupObject . id === savedModeId ) {
161
+ changeGroup = true ;
162
+ changeGroupIndex = allowedGroups . length - 1 ;
163
+ changeGroupValue = addGroupObject ;
164
+ }
165
+ }
152
166
}
153
167
}
154
- }
155
168
156
- if ( allowedGroups . length > 1 ) {
157
- window . chayns . ui . modeSwitch . init ( {
158
- items : allowedGroups ,
159
- callback : getChangeListener ( )
160
- } ) ;
169
+ if ( allowedGroups . length > 1 ) {
170
+ window . chayns . ui . modeSwitch . init ( {
171
+ items : allowedGroups ,
172
+ callback : getChangeListener ( )
173
+ } ) ;
161
174
175
+ initialized = true ;
162
176
163
- initialized = true ;
177
+ if ( changeGroup ) {
178
+ getChangeListener ( ) ( changeGroupValue ) ;
164
179
165
- if ( changeGroup ) {
166
- getChangeListener ( ) ( changeGroupValue ) ;
180
+ window . chayns . ui . modeSwitch . changeMode ( changeGroupIndex ) ;
181
+ } else {
182
+ setDefaultGroup ( isChaynsIdAdmin && managerGroup ? managerGroup . id : 0 ) ;
183
+ }
167
184
168
- window . chayns . ui . modeSwitch . changeMode ( changeGroupIndex ) ;
185
+ // if (changeGroup) { window.setTimeout(() => { window. chayns.ui.modeSwitch.changeMode(changeGroupIndex); }, 0); }
169
186
} else {
170
- setDefaultGroup ( ) ;
171
- }
172
-
173
-
174
- if ( changeGroup ) {
175
- window . setTimeout ( ( ) => {
176
- window . chayns . ui . modeSwitch . changeMode ( changeGroupIndex ) ;
177
- } , 0 ) ;
187
+ setDefaultGroup ( isChaynsIdAdmin && managerGroup ? managerGroup . id : 0 ) ;
178
188
}
179
189
} else {
180
190
setDefaultGroup ( ) ;
181
191
}
182
- } else {
183
- setDefaultGroup ( ) ;
184
- }
192
+ } ) ;
185
193
} else {
186
194
console . warn ( 'No groups specified' ) ;
187
195
}
@@ -231,6 +239,16 @@ export default class ModeSwitchHelper {
231
239
} ) ;
232
240
}
233
241
242
+ static findManagerGroup ( groups ) {
243
+ if ( ! window . chayns . env . user . isAuthenticated ) return false ;
244
+
245
+ return groups . find ( ( uac ) => {
246
+ return uac . uacIds && uac . uacIds . length === 1 && uac . uacIds [ 0 ] === 1 ;
247
+ } ) || groups . find ( ( uac ) => {
248
+ return uac . uacIds && uac . uacIds . find ( id => id === 1 ) ;
249
+ } ) ;
250
+ }
251
+
234
252
static isChaynsManager ( ) {
235
253
return this . isUserInGroup ( 1 ) ;
236
254
}
0 commit comments