Skip to content

Commit cfc011c

Browse files
committed
decouple basic visibility rules from customConfig rules
1 parent 89ff447 commit cfc011c

File tree

1 file changed

+46
-43
lines changed

1 file changed

+46
-43
lines changed

src/lib/unpackPlotProps.js

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,49 @@ import nestedProperty from 'plotly.js/src/lib/nested_property';
44
import isNumeric from 'fast-isnumeric';
55
import {MULTI_VALUED, MULTI_VALUED_PLACEHOLDER} from './constants';
66

7-
export function computeVisibility(props, fullValue, customConfig) {
8-
let isVisible = false;
7+
const hasFullValue = fullValue => fullValue !== void 0 && fullValue !== null;
98

10-
const hasFullValue = () => fullValue !== void 0 && fullValue !== null;
11-
12-
const isValidCustomConfigObject = customConfig => {
13-
if (customConfig.visibility_rules) {
14-
const logRuleError = () => {
15-
console.error(
16-
'invalid customConfig: all customConfig visibility rules must include type and regex_match keys.'
17-
);
18-
};
19-
20-
if (
21-
(customConfig.visibility_rules.blacklist &&
22-
customConfig.visibility_rules.blacklist.some(r => !r.type || !r.regex_match)) ||
23-
(customConfig.visibility_rules.whitelist &&
24-
customConfig.visibility_rules.whitelist.some(r => !r.type || !r.regex_match))
25-
) {
26-
logRuleError();
27-
return false;
28-
}
9+
export function isValidCustomConfigObject(customConfig) {
10+
if (
11+
customConfig &&
12+
customConfig === Object(customConfig) &&
13+
Object.keys(customConfig).length &&
14+
customConfig.visibility_rules
15+
) {
16+
const logRuleError = () => {
17+
console.error(
18+
'invalid customConfig: all customConfig visibility rules must include type and regex_match keys.'
19+
);
20+
};
2921

30-
if (
31-
customConfig.visibility_rules.order &&
32-
Array.isArray(customConfig.visibility_rules.order) &&
33-
customConfig.visibility_rules.order.some(o => !['blacklist', 'whitelist'].includes(o))
34-
) {
35-
console.error(
36-
"invalid customConfig: only 2 values are accepted in the visibility_rules.order array: 'blacklist' and 'whitelist'"
37-
);
38-
return false;
39-
}
22+
if (
23+
(customConfig.visibility_rules.blacklist &&
24+
customConfig.visibility_rules.blacklist.some(r => !r.type || !r.regex_match)) ||
25+
(customConfig.visibility_rules.whitelist &&
26+
customConfig.visibility_rules.whitelist.some(r => !r.type || !r.regex_match))
27+
) {
28+
logRuleError();
29+
return false;
30+
}
4031

41-
return true;
32+
if (
33+
customConfig.visibility_rules.order &&
34+
Array.isArray(customConfig.visibility_rules.order) &&
35+
customConfig.visibility_rules.order.some(o => !['blacklist', 'whitelist'].includes(o))
36+
) {
37+
console.error(
38+
"invalid customConfig: only 2 values are accepted in the visibility_rules.order array: 'blacklist' and 'whitelist'"
39+
);
40+
return false;
4241
}
43-
return false;
44-
};
42+
43+
return true;
44+
}
45+
return false;
46+
}
47+
48+
export function applyCustomConfigVisibility(props, fullValue, customConfig) {
49+
let isVisible = false;
4550

4651
if (
4752
customConfig &&
@@ -68,34 +73,30 @@ export function computeVisibility(props, fullValue, customConfig) {
6873

6974
if (order[0] === 'blacklist') {
7075
const shouldBlacklist = matches('blacklist') && !matches('whitelist');
71-
if (!shouldBlacklist && hasFullValue()) {
76+
if (!shouldBlacklist && hasFullValue(fullValue)) {
7277
isVisible = true;
7378
}
7479
}
7580

7681
if (order[0] === 'whitelist') {
7782
const shouldWhitelist = matches('whitelist') && !matches('blacklist');
78-
if (shouldWhitelist && hasFullValue()) {
83+
if (shouldWhitelist && hasFullValue(fullValue)) {
7984
isVisible = true;
8085
}
8186
}
8287
} else {
8388
if (customConfig.visibility_rules.blacklist) {
84-
if (!matches('blacklist') && hasFullValue()) {
89+
if (!matches('blacklist') && hasFullValue(fullValue)) {
8590
isVisible = true;
8691
}
8792
}
8893

8994
if (customConfig.visibility_rules.whitelist) {
90-
if (matches('whitelist') && hasFullValue()) {
95+
if (matches('whitelist') && hasFullValue(fullValue)) {
9196
isVisible = true;
9297
}
9398
}
9499
}
95-
} else {
96-
if (props.show || hasFullValue()) {
97-
isVisible = true;
98-
}
99100
}
100101

101102
return isVisible;
@@ -127,7 +128,9 @@ export default function unpackPlotProps(props, context) {
127128
multiValued = true;
128129
}
129130

130-
const isVisible = computeVisibility(props, fullValue, customConfig);
131+
const isVisible = isValidCustomConfigObject(customConfig)
132+
? applyCustomConfigVisibility(props, fullValue, customConfig)
133+
: Boolean(props.show || hasFullValue(fullValue));
131134

132135
let defaultValue = props.defaultValue;
133136
if (defaultValue === void 0 && defaultContainer) {

0 commit comments

Comments
 (0)