1+ /* eslint-disable no-console */
2+
13import nestedProperty from 'plotly.js/src/lib/nested_property' ;
24import isNumeric from 'fast-isnumeric' ;
35import { MULTI_VALUED , MULTI_VALUED_PLACEHOLDER } from './constants' ;
46
7+ const hasFullValue = fullValue => fullValue !== void 0 && fullValue !== null ;
8+
9+ export function hasValidCustomConfigVisibilityRules ( customConfig ) {
10+ if (
11+ customConfig &&
12+ customConfig === Object ( customConfig ) &&
13+ Object . keys ( customConfig ) . length &&
14+ customConfig . visibility_rules
15+ ) {
16+ if ( customConfig . visibility_rules . blacklist && customConfig . visibility_rules . whitelist ) {
17+ console . error (
18+ 'customConfig.visibility_rules can have a blacklist OR whitelist key, both are present in your config.'
19+ ) ;
20+ return false ;
21+ }
22+
23+ if (
24+ ! Object . keys ( customConfig . visibility_rules ) . some ( key =>
25+ [ 'blacklist' , 'whitelist' ] . includes ( key )
26+ )
27+ ) {
28+ console . error (
29+ 'customConfig.visibility_rules must have at least a blacklist or whitelist key.'
30+ ) ;
31+ return false ;
32+ }
33+
34+ const isValidRule = rule => {
35+ if ( rule . exceptions ) {
36+ return rule . exceptions . every ( isValidRule ) ;
37+ }
38+ return rule . type && [ 'attrName' , 'controlType' ] . includes ( rule . type ) && rule . regex_match ;
39+ } ;
40+
41+ const errorMessage =
42+ "All rules and exceptions must have a type (one of: 'attrName' or 'controlType') and regex_match key." ;
43+
44+ if (
45+ customConfig . visibility_rules . blacklist &&
46+ ! customConfig . visibility_rules . blacklist . every ( isValidRule )
47+ ) {
48+ console . error ( errorMessage ) ;
49+ return false ;
50+ }
51+
52+ if (
53+ customConfig . visibility_rules . whitelist &&
54+ ! customConfig . visibility_rules . whitelist . every ( isValidRule )
55+ ) {
56+ console . error ( errorMessage ) ;
57+ return false ;
58+ }
59+
60+ return true ;
61+ }
62+ return false ;
63+ }
64+
65+ export function computeCustomConfigVisibility ( props , customConfig , wrappedComponentDisplayName ) {
66+ let isVisible ;
67+
68+ const isRegexMatch = rule => {
69+ const stringToTest = rule . type === 'attrName' ? props . attr : wrappedComponentDisplayName ;
70+ return RegExp ( rule . regex_match ) . test ( stringToTest ) ;
71+ } ;
72+
73+ const passesTest = rule => {
74+ const hasException = rule => {
75+ if ( rule . exceptions ) {
76+ return rule . exceptions . some ( exception => passesTest ( exception ) ) ;
77+ }
78+ return false ;
79+ } ;
80+ return isRegexMatch ( rule ) && ! hasException ( rule ) ;
81+ } ;
82+
83+ if ( customConfig . visibility_rules . blacklist ) {
84+ isVisible = ! customConfig . visibility_rules . blacklist . some ( passesTest ) ;
85+ }
86+
87+ if ( customConfig . visibility_rules . whitelist ) {
88+ isVisible = customConfig . visibility_rules . whitelist . some ( passesTest ) ;
89+ }
90+
91+ return isVisible ;
92+ }
93+
94+ export function isVisibleGivenCustomConfig ( initial , nextProps , nextContext , componentDisplayName ) {
95+ let show = initial ;
96+ if ( show && nextContext . hasValidCustomConfigVisibilityRules ) {
97+ show = computeCustomConfigVisibility ( nextProps , nextContext . customConfig , componentDisplayName ) ;
98+ }
99+ return show ;
100+ }
101+
5102export default function unpackPlotProps ( props , context ) {
6103 const { container, getValObject, defaultContainer, updateContainer} = context ;
7104
@@ -28,10 +125,7 @@ export default function unpackPlotProps(props, context) {
28125 multiValued = true ;
29126 }
30127
31- let isVisible = false ;
32- if ( props . show || ( fullValue !== void 0 && fullValue !== null ) ) {
33- isVisible = true ;
34- }
128+ const isVisible = Boolean ( hasFullValue ( fullValue ) || props . show ) ;
35129
36130 let defaultValue = props . defaultValue ;
37131 if ( defaultValue === void 0 && defaultContainer ) {
0 commit comments