Skip to content

Commit d878556

Browse files
Merge pull request #78 from TobitSoftware/feature/add-set-admin-switch-callback
add setAdminSwitchCallback
2 parents d5a594f + 667ff09 commit d878556

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"jsnext:main": "lib_esnext/index.js",
88
"scripts": {
99
"start": "webpack-dev-server --config ./webpack/dev.babel.js",
10-
"prepublish": "npm run eslint & npm run build",
10+
"prepublishOnly": "npm run eslint & npm run build",
1111
"clean": "",
1212
"build": "npm run build:es && npm run build:cjs && npm run build:lib:es && npm run build:sass",
1313
"build:es": "cross-env NODE_ENV=es rollup -c",
@@ -16,7 +16,7 @@
1616
"build:sass": "node-sass src/ -o ./lib",
1717
"eslint": "./node_modules/.bin/eslint src --ext .jsx --ext .js"
1818
},
19-
"author": "kkonermann",
19+
"author": "Tobit.Software",
2020
"license": "MIT",
2121
"repository": {
2222
"type": "git",

src/react-chayns-modeswitch/component/ModeSwitchHelper.js

+20-14
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ let initialized = false;
66

77
function callCallbacks(data) {
88
callbacks.map((callback) => {
9-
if(callback && window.chayns.utils.isFunction(callback)) {
9+
if (callback && window.chayns.utils.isFunction(callback)) {
1010
callback(data);
1111
}
1212
});
@@ -33,11 +33,11 @@ function setDefaultGroup(mode = 0) {
3333
function getUacIds(group) {
3434
let retval = [];
3535

36-
if(group.uacId && window.chayns.utils.isNumber(group.uacId)) {
36+
if (group.uacId && window.chayns.utils.isNumber(group.uacId)) {
3737
retval.push(group.uacId);
3838
}
3939

40-
if(group.uacIds && window.chayns.utils.isArray(group.uacIds)) {
40+
if (group.uacIds && window.chayns.utils.isArray(group.uacIds)) {
4141
retval = retval.concat(group.uacIds);
4242
}
4343

@@ -50,7 +50,7 @@ function getAllowedUacIdsFromArray(uacArray) {
5050
const allowedUacIds = [];
5151

5252
for (let i = 0, x = userGroups.length; i < x; i += 1) {
53-
if(uacArray.indexOf(userGroups[i].id) !== -1) {
53+
if (uacArray.indexOf(userGroups[i].id) !== -1) {
5454
allowedUacIds.push(userGroups[i].id);
5555
}
5656
}
@@ -82,12 +82,12 @@ function getDefaultMode() {
8282

8383
export default class ModeSwitchHelper {
8484
static init(options) {
85-
if(options.groups) {
86-
if(window.chayns.utils.isFunction(options.onChange)) {
85+
if (options.groups) {
86+
if (window.chayns.utils.isFunction(options.onChange)) {
8787
callbacks.push(options.onChange);
8888
}
8989

90-
if(options.save) {
90+
if (options.save) {
9191
callbacks.push(setSavedMode);
9292
}
9393

@@ -115,7 +115,7 @@ export default class ModeSwitchHelper {
115115

116116
const managerGroup = ModeSwitchHelper.findManagerGroup(groups);
117117

118-
if(managerGroup && data && data.AppUser.AdminMode && !chayns.env.isApp) {
118+
if (managerGroup && data && data.AppUser.AdminMode && !chayns.env.isApp) {
119119
groupObject = getGroupObject(managerGroup.id, managerGroup.name, managerGroup.uacIds);
120120
isChaynsIdAdmin = true;
121121
} else {
@@ -128,11 +128,11 @@ export default class ModeSwitchHelper {
128128

129129
let savedModeId = null;
130130
let changeGroupIndex = 0;
131-
if(options.save) {
131+
if (options.save) {
132132
savedModeId = getSavedMode();
133133
}
134134

135-
if(savedModeId === null && options.defaultMode) {
135+
if (savedModeId === null && options.defaultMode) {
136136
savedModeId = options.defaultMode;
137137
}
138138

@@ -185,6 +185,12 @@ export default class ModeSwitchHelper {
185185
// if (changeGroup) { window.setTimeout(() => { window.chayns.ui.modeSwitch.changeMode(changeGroupIndex); }, 0); }
186186
} else {
187187
setDefaultGroup(isChaynsIdAdmin && managerGroup ? managerGroup.id : 0);
188+
189+
// ToDo: Implement adminSwitchCallback for allowedGroups.length > 1 too
190+
const changeListener = getChangeListener();
191+
chayns.setAdminSwitchCallback(({ mode }) => changeListener({
192+
id: mode,
193+
}));
188194
}
189195
} else {
190196
setDefaultGroup();
@@ -196,13 +202,13 @@ export default class ModeSwitchHelper {
196202
}
197203

198204
static getCurrentMode() {
199-
if(currentMode) return currentMode;
205+
if (currentMode) return currentMode;
200206

201207
return getDefaultMode();
202208
}
203209

204210
static onChange(callback) {
205-
if(window.chayns.utils.isFunction(callback)) {
211+
if (window.chayns.utils.isFunction(callback)) {
206212
callbacks.push(callback);
207213

208214
return true;
@@ -232,15 +238,15 @@ export default class ModeSwitchHelper {
232238
}
233239

234240
static isUserInGroup(uacId) {
235-
if(!window.chayns.env.user.isAuthenticated) return false;
241+
if (!window.chayns.env.user.isAuthenticated) return false;
236242

237243
return !!window.chayns.env.user.groups.find((element) => {
238244
return element.id === uacId;
239245
});
240246
}
241247

242248
static findManagerGroup(groups) {
243-
if(!window.chayns.env.user.isAuthenticated) return false;
249+
if (!window.chayns.env.user.isAuthenticated) return false;
244250

245251
return groups.find((uac) => {
246252
return uac.uacIds && uac.uacIds.length === 1 && uac.uacIds[0] === 1;

0 commit comments

Comments
 (0)