UI: Simplify AdvancedMultiSelect.js

AdvancedMultiSelect.js no longer needs to pull data from definitions since we no longer need the definitions in schema
This commit is contained in:
vakarisz 2022-07-14 15:18:21 +03:00
parent e525fbe330
commit c0ecaa0096
3 changed files with 18 additions and 52 deletions

View File

@ -6,7 +6,7 @@ import {cloneDeep} from 'lodash';
import {getDefaultPaneParams, InfoPane, WarningType} from './InfoPane'; import {getDefaultPaneParams, InfoPane, WarningType} from './InfoPane';
import {MasterCheckbox, MasterCheckboxState} from './MasterCheckbox'; import {MasterCheckbox, MasterCheckboxState} from './MasterCheckbox';
import ChildCheckboxContainer from './ChildCheckbox'; import ChildCheckboxContainer from './ChildCheckbox';
import {getFullDefinitionByKey, getObjectFromRegistryByRef} from './JsonSchemaHelpers'; import {getFullDefinitionByKey} from './JsonSchemaHelpers';
function AdvancedMultiSelectHeader(props) { function AdvancedMultiSelectHeader(props) {
const { const {
@ -38,14 +38,12 @@ class AdvancedMultiSelect extends React.Component {
this.state = { this.state = {
infoPaneParams: getDefaultPaneParams( infoPaneParams: getDefaultPaneParams(
this.props.schema.items.$ref, this.props.schema.items,
this.props.registry,
this.isUnsafeOptionSelected(selectedPluginNames) this.isUnsafeOptionSelected(selectedPluginNames)
), ),
allPluginNames: allPluginNames, allPluginNames: allPluginNames,
masterCheckboxState: this.getMasterCheckboxState(selectedPluginNames), masterCheckboxState: this.getMasterCheckboxState(selectedPluginNames),
pluginDefinitions: getObjectFromRegistryByRef(this.props.schema.items.$ref, pluginDefinitions: this.props.schema.items.pluginDefs,
this.props.registry).pluginDefs,
selectedPluginNames: selectedPluginNames selectedPluginNames: selectedPluginNames
}; };
} }
@ -55,23 +53,17 @@ class AdvancedMultiSelect extends React.Component {
} }
onChange = (strValues) => { onChange = (strValues) => {
let newValues = []; let pluginArray = this.namesToPlugins(strValues, this.state.pluginDefinitions);
for (let j = 0; j < strValues.length; j++) { this.props.onChange(pluginArray)
let found = false; this.setState({selectedPluginNames: pluginArray.map(v => v.name)});
for (let i = 0; i < this.state.allPluginNames.length; i++) {
if (strValues[j] === this.state.allPluginNames[i]['name']) {
newValues.push(JSON.parse(JSON.stringify(this.props.value[i])))
found = true;
break;
} }
namesToPlugins = (names, allPlugins) => {
let plugins = [];
for (let i = 0; i < names.length; i++){
plugins.push(_.clone(allPlugins[names[i]]));
} }
if (!found) { return plugins
newValues.push(this.state.pluginDefinitions[strValues[j]]);
}
}
newValues = JSON.parse(JSON.stringify(newValues));
this.props.onChange(newValues)
this.setState({selectedPluginNames: newValues.map(v => v.name)});
} }
// Sort options alphabetically. "Unsafe" options float to the top so that they // Sort options alphabetically. "Unsafe" options float to the top so that they
@ -146,14 +138,12 @@ class AdvancedMultiSelect extends React.Component {
} }
isSafe = (itemKey) => { isSafe = (itemKey) => {
let fullDef = getFullDefinitionByKey(this.props.schema.items.$ref, let fullDef = getFullDefinitionByKey(this.props.schema.items, itemKey);
this.props.registry, itemKey);
return fullDef.safe; return fullDef.safe;
} }
setPaneInfo = (itemKey) => { setPaneInfo = (itemKey) => {
let definitionObj = getFullDefinitionByKey(this.props.schema.items.$ref, let definitionObj = getFullDefinitionByKey(this.props.schema.items, itemKey);
this.props.registry, itemKey);
this.setState( this.setState(
{ {
infoPaneParams: { infoPaneParams: {

View File

@ -1,19 +1,6 @@
import {resolveObjectPath} from './utils/ObjectPathResolver'; function getFullDefinitionByKey(items, itemKey) {
let fullArray = items.anyOf;
function getFullDefinitionByKey(refString, registry, itemKey) {
let fullArray = getFullDefinitionsFromRegistry(refString, registry);
return fullArray.filter(e => (e.enum[0] === itemKey))[0]; return fullArray.filter(e => (e.enum[0] === itemKey))[0];
} }
// Definitions passed to components only contains value and label, export {getFullDefinitionByKey};
// custom fields like "info" or "links" must be pulled from registry object using this function
function getFullDefinitionsFromRegistry(refString, registry) {
return getObjectFromRegistryByRef(refString, registry).anyOf;
}
function getObjectFromRegistryByRef(refString, registry) {
let refArray = refString.replace('#', '').split('/');
return resolveObjectPath(refArray, registry);
}
export {getFullDefinitionByKey, getObjectFromRegistryByRef};

View File

@ -1,11 +0,0 @@
// Resolves object's path if it's specified in a dot notation.
// (e.g. params: "firstLevel.secondLevel.property", myObject)
export function resolveObjectPath(pathArray, obj) {
return pathArray.reduce(function(prev, curr) {
if(curr === '')
return prev;
else
return prev ? prev[curr] : null;
}, obj || self)
}