fix: [instance:settings] Improved support of select and apply correct UI coloring

pull/70/head
mokaddem 2021-07-23 12:03:03 +02:00
parent ac464e4129
commit 22faffb170
3 changed files with 34 additions and 29 deletions

View File

@ -51,9 +51,6 @@ class SettingsProviderTable extends AppTable
if (isset($settings[$key])) { if (isset($settings[$key])) {
$settingConf[$key]['value'] = $settings[$key]; $settingConf[$key]['value'] = $settings[$key];
} }
if (empty($settingConf[$key]['severity'])) {
$settingConf[$key]['severity'] = 'warning';
}
$settingConf[$key] = $this->evaluateLeaf($settingConf[$key], $settingConf); $settingConf[$key] = $this->evaluateLeaf($settingConf[$key], $settingConf);
$settingConf[$key]['setting-path'] = $path; $settingConf[$key]['setting-path'] = $path;
$settingConf[$key]['true-name'] = $key; $settingConf[$key]['true-name'] = $key;
@ -95,7 +92,7 @@ class SettingsProviderTable extends AppTable
$notices[$value['severity']][] = $key; $notices[$value['severity']][] = $key;
} }
} else { } else {
$notices = array_merge($notices, $this->getNoticesFromSettingsConfiguration($value)); $notices = array_merge_recursive($notices, $this->getNoticesFromSettingsConfiguration($value));
} }
} }
return $notices; return $notices;
@ -127,29 +124,31 @@ class SettingsProviderTable extends AppTable
} }
} }
if (!$skipValidation) { if (!$skipValidation) {
if (isset($setting['test'])) { $validationResult = false;
$error = false; if (!isset($setting['value'])) {
$validationResult = $this->settingValidator->testEmptyBecomesDefault(null, $setting);
} else if (isset($setting['test'])) {
$setting['value'] = $setting['value'] ?? ''; $setting['value'] = $setting['value'] ?? '';
if (is_callable($setting['test'])) { // Validate with anonymous function if (is_callable($setting['test'])) { // Validate with anonymous function
$error = $setting['test']($setting['value'], $setting, new Validator()); $validationResult = $setting['test']($setting['value'], $setting, new Validator());
} else if (method_exists($this->settingValidator, $setting['test'])) { // Validate with function defined in settingValidator class } else if (method_exists($this->settingValidator, $setting['test'])) { // Validate with function defined in settingValidator class
$error = $this->settingValidator->{$setting['test']}($setting['value'], $setting); $validationResult = $this->settingValidator->{$setting['test']}($setting['value'], $setting);
} else { } else {
$validator = new Validator(); $validator = new Validator();
if (method_exists($validator, $setting['test'])) { // Validate with cake's validator function if (method_exists($validator, $setting['test'])) { // Validate with cake's validator function
$validator->{$setting['test']}; $validator->{$setting['test']};
$error = $validator->validate($setting['value']); $validationResult = $validator->validate($setting['value']);
} }
} }
if ($error !== true) {
$setting['severity'] = $setting['severity'] ?? 'warning';
if (!in_array($setting['severity'], $this->severities)) {
$setting['severity'] = 'warning';
}
$setting['errorMessage'] = $error;
}
$setting['error'] = $error !== false ? true : false;
} }
if ($validationResult !== true) {
$setting['severity'] = $setting['severity'] ?? 'warning';
if (!in_array($setting['severity'], $this->severities)) {
$setting['severity'] = 'warning';
}
$setting['errorMessage'] = $validationResult;
}
$setting['error'] = $validationResult !== true ? true : false;
} }
return $setting; return $setting;
} }
@ -176,9 +175,9 @@ class SettingsProviderTable extends AppTable
], ],
'app.uuid' => [ 'app.uuid' => [
'description' => __('The Cerebrate instance UUID. This UUID is used to identify this instance.'), 'description' => __('The Cerebrate instance UUID. This UUID is used to identify this instance.'),
'severity' => 'critical',
'default' => '', 'default' => '',
'name' => 'UUID', 'name' => 'UUID',
'severity' => 'critical',
'test' => 'testUuid', 'test' => 'testUuid',
'type' => 'string' 'type' => 'string'
], ],
@ -196,19 +195,20 @@ class SettingsProviderTable extends AppTable
], ],
'sc2.hero' => [ 'sc2.hero' => [
'description' => 'The true hero', 'description' => 'The true hero',
'default' => '', 'default' => 'Sarah Kerrigan',
'name' => 'Hero',
'options' => [ 'options' => [
'Jim Raynor' => 'Jim Raynor', 'Jim Raynor' => 'Jim Raynor',
'Sarah Kerrigan' => 'Sarah Kerrigan', 'Sarah Kerrigan' => 'Sarah Kerrigan',
'Artanis' => 'Artanis', 'Artanis' => 'Artanis',
'Zeratul' => 'Zeratul', 'Zeratul' => 'Zeratul',
], ],
'name' => 'Hero',
'type' => 'select' 'type' => 'select'
], ],
'sc2.antagonist' => [ 'sc2.antagonist' => [
'description' => 'The real bad guy', 'description' => 'The real bad guy',
'default' => '', 'default' => 'Amon',
'name' => 'Antagonist',
'options' => function($settingsProviders) { 'options' => function($settingsProviders) {
return [ return [
'Amon' => 'Amon', 'Amon' => 'Amon',
@ -249,7 +249,6 @@ class SettingsProviderTable extends AppTable
'description' => __('The authentication username for the HTTP proxy.'), 'description' => __('The authentication username for the HTTP proxy.'),
'default' => 'admin', 'default' => 'admin',
'name' => __('User'), 'name' => __('User'),
'test' => 'testEmptyBecomesDefault',
'dependsOn' => 'proxy.host', 'dependsOn' => 'proxy.host',
'type' => 'string', 'type' => 'string',
], ],
@ -257,7 +256,6 @@ class SettingsProviderTable extends AppTable
'description' => __('The authentication password for the HTTP proxy.'), 'description' => __('The authentication password for the HTTP proxy.'),
'default' => '', 'default' => '',
'name' => __('Password'), 'name' => __('Password'),
'test' => 'testEmptyBecomesDefault',
'dependsOn' => 'proxy.host', 'dependsOn' => 'proxy.host',
'type' => 'string', 'type' => 'string',
], ],
@ -284,7 +282,7 @@ class SettingsProviderTable extends AppTable
'description' => __('The debug level of the instance'), 'description' => __('The debug level of the instance'),
'default' => 0, 'default' => 0,
'dependsOn' => 'host', 'dependsOn' => 'host',
'name' => __('User'), 'name' => __('Debug Level'),
'test' => function($value, $setting, $validator) { 'test' => function($value, $setting, $validator) {
$validator->range('value', [0, 3]); $validator->range('value', [0, 3]);
return testValidator($value, $validator); return testValidator($value, $validator);
@ -319,10 +317,11 @@ class SettingValidator
if (!empty($value)) { if (!empty($value)) {
return true; return true;
} else if (!empty($setting['default'])) { } else if (!empty($setting['default'])) {
$setting['severity'] = 'info'; $setting['severity'] = $setting['severity'] ?? 'info';
return __('Setting is not set, fallback to default value: {0}', $setting['default']); return __('Setting is not set, fallback to default value: {0}', $setting['default']);
} else { } else {
return __('Cannot be empty'); $setting['severity'] = $setting['severity'] ?? 'critical';
return __('Cannot be empty. Setting does not have a default value.');
} }
} }

View File

@ -312,7 +312,10 @@ function genInputInteger($settingName, $setting, $appView)
$settingId = str_replace('.', '_', $settingName); $settingId = str_replace('.', '_', $settingName);
return $appView->Bootstrap->genNode('input', [ return $appView->Bootstrap->genNode('input', [
'class' => [ 'class' => [
'form-control' 'form-control',
(!empty($setting['error']) ? 'is-invalid' : ''),
(!empty($setting['error']) ? "border-{$appView->get('variantFromSeverity')[$setting['severity']]}" : ''),
(!empty($setting['error']) ? $appView->get('variantFromSeverity')[$setting['severity']] : ''),
], ],
'type' => 'number', 'type' => 'number',
'min' => '0', 'min' => '0',
@ -343,7 +346,7 @@ function genInputSelect($settingName, $setting, $appView)
'pr-4', 'pr-4',
(!empty($setting['error']) ? 'is-invalid' : ''), (!empty($setting['error']) ? 'is-invalid' : ''),
(!empty($setting['error']) ? "border-{$appView->get('variantFromSeverity')[$setting['severity']]}" : ''), (!empty($setting['error']) ? "border-{$appView->get('variantFromSeverity')[$setting['severity']]}" : ''),
(!empty($setting['error']) && $setting['severity'] == 'warning' ? 'warning' : ''), (!empty($setting['error']) ? $appView->get('variantFromSeverity')[$setting['severity']] : ''),
], ],
'type' => 'text', 'type' => 'text',
'id' => $settingId, 'id' => $settingId,
@ -543,7 +546,7 @@ function isLeaf($setting)
if ($callout.length == 0) { if ($callout.length == 0) {
return return
} }
const $settings = $callout.find('input') const $settings = $callout.find('input, select')
const settingNames = Array.from($settings).map((i) => { const settingNames = Array.from($settings).map((i) => {
return $(i).data('setting-name') return $(i).data('setting-name')
}) })

View File

@ -155,6 +155,9 @@ div.progress-timeline .progress-line.progress-inactive {
.custom-select.is-invalid.warning { .custom-select.is-invalid.warning {
background: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' width='4' height='5' viewBox='0 0 4 5'%3e%3cpath fill='%23343a40' d='M2 0L0 2h4zm0 5L0 3h4z'/%3e%3c/svg%3e") no-repeat right 0.75rem center/8px 10px, url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='none' stroke='%23ffc107' viewBox='0 0 12 12'%3e%3ccircle cx='6' cy='6' r='4.5'/%3e%3cpath stroke-linejoin='round' d='M5.8 3.6h.4L6 6.5z'/%3e%3ccircle cx='6' cy='8.2' r='.6' fill='%23ffc107' stroke='none'/%3e%3c/svg%3e") #fff no-repeat center right 1.75rem/calc(0.75em + 0.375rem) calc(0.75em + 0.375rem); background: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' width='4' height='5' viewBox='0 0 4 5'%3e%3cpath fill='%23343a40' d='M2 0L0 2h4zm0 5L0 3h4z'/%3e%3c/svg%3e") no-repeat right 0.75rem center/8px 10px, url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='none' stroke='%23ffc107' viewBox='0 0 12 12'%3e%3ccircle cx='6' cy='6' r='4.5'/%3e%3cpath stroke-linejoin='round' d='M5.8 3.6h.4L6 6.5z'/%3e%3ccircle cx='6' cy='8.2' r='.6' fill='%23ffc107' stroke='none'/%3e%3c/svg%3e") #fff no-repeat center right 1.75rem/calc(0.75em + 0.375rem) calc(0.75em + 0.375rem);
} }
.custom-select.is-invalid.info {
background: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' width='4' height='5' viewBox='0 0 4 5'%3e%3cpath fill='%23343a40' d='M2 0L0 2h4zm0 5L0 3h4z'/%3e%3c/svg%3e") no-repeat right 0.75rem center/8px 10px, url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='none' stroke='%2317a2b8' viewBox='0 0 12 12'%3e%3ccircle cx='6' cy='6' r='4.5'/%3e%3cpath stroke-linejoin='round' d='M5.8 3.6h.4L6 6.5z'/%3e%3ccircle cx='6' cy='8.2' r='.6' fill='%2317a2b8' stroke='none'/%3e%3c/svg%3e") #fff no-repeat center right 1.75rem/calc(0.75em + 0.375rem) calc(0.75em + 0.375rem);
}
.custom-control-input.is-invalid.warning ~ .custom-control-label { .custom-control-input.is-invalid.warning ~ .custom-control-label {
color: unset; color: unset;