new: [ui:formInfo] Rafactored formInfo and added support of field description

Can be done by using the `tooltip` key on the field configuration
refacto/CRUDComponent
Sami Mokaddem 2023-02-24 10:03:45 +01:00
parent b4df0ce289
commit 6eb5106153
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
5 changed files with 86 additions and 61 deletions

View File

@ -8,6 +8,15 @@ class FormFieldMassageHelper extends Helper
{ {
public function prepareFormElement(\Cake\View\Helper\FormHelper $form, array $controlParams, array $fieldData): string public function prepareFormElement(\Cake\View\Helper\FormHelper $form, array $controlParams, array $fieldData): string
{ {
if ($fieldData['tooltip']) {
$form->setTemplates([
'label' => '{{text}}{{tooltip}}',
]);
$controlParams['templateVars'] = array_merge(
$controlParams['templateVars'] ?? [],
['tooltip' => $fieldData['tooltip'], ]
);
}
if (!empty($fieldData['stateDependence'])) { if (!empty($fieldData['stateDependence'])) {
$controlParams['data-dependence-source'] = h($fieldData['stateDependence']['source']); $controlParams['data-dependence-source'] = h($fieldData['stateDependence']['source']);
$controlParams['data-dependence-option'] = h($fieldData['stateDependence']['option']); $controlParams['data-dependence-option'] = h($fieldData['stateDependence']['option']);

View File

@ -1,43 +1,43 @@
<?php <?php
$random = Cake\Utility\Security::randomString(8); $random = Cake\Utility\Security::randomString(8);
$params['div'] = false; $params['div'] = false;
$this->Form->setTemplates([
'inputContainer' => '{{content}}', $genUUIDButton = $this->Bootstrap->button([
'inputContainerError' => '{{content}}', 'id' => "uuid-gen-{$random}",
'formGroup' => '{{input}}', 'variant' => 'secondary',
]); 'text' => __('Generate'),
$label = $fieldData['label']; ]);
$formElement = $this->FormFieldMassage->prepareFormElement($this->Form, $params, $fieldData);
$temp = sprintf( $this->Form->setTemplates([
'<div class="row mb-3"> 'input' => sprintf('<div class="input-group">%s{{genUUIDButton}}</div>', $this->Form->getTemplates('input')),
<div class="col-sm-2 form-label">%s</div> ]);
<div class="col-sm-10"> $params['templateVars'] = [
<div class="input-group"> 'genUUIDButton' => $genUUIDButton,
%s<span>%s</span> ];
</div>
</div> $formElement = $this->FormFieldMassage->prepareFormElement($this->Form, $params, $fieldData);
</div>', echo $formElement;
h($label),
$formElement,
sprintf(
'<span id="uuid-gen-%s" class="btn btn-secondary">%s</span>',
$random,
__('Generate')
)
);
echo $temp;
?> ?>
<script type="text/javascript"> <script type="text/javascript">
$(document).ready(function() { $(document).ready(function() {
$('#uuid-gen-<?= h($random) ?>').on('click', function() { const $node = $('#uuid-gen-<?= h($random) ?>')
$.ajax({ $node.click(fetchUUID)
success:function (data, textStatus) {
$('#uuid-field').val(data["uuid"]); function fetchUUID() {
}, const urlGet = '/organisations/generateUUID'
type: "get", const options = {
cache: false, statusNode: $node,
url: "/organisations/generateUUID", }
}); return AJAXApi.quickFetchJSON(urlGet, options)
}); .then(function(data) {
$('#uuid-field').val(data["uuid"])
})
.catch((e) => {
UI.toast({
variant: 'danger',
text: '<?= __('Could not generate UUID') ?>'
})
})
}
}); });
</script> </script>

View File

@ -11,11 +11,12 @@
$fieldData['label'] = \Cake\Utility\Inflector::humanize($fieldData['field']); $fieldData['label'] = \Cake\Utility\Inflector::humanize($fieldData['field']);
} }
} }
if (!empty($fieldDesc[$fieldData['field']])) { $fieldDescription = $fieldData['tooltip'] ?? ($fieldDesc[$fieldData['field']] ?? false);
$fieldData['label'] .= $this->element( if (!empty($fieldDescription)) {
$fieldData['tooltip'] = $this->element(
'genericElements/Form/formInfo', array( 'genericElements/Form/formInfo', array(
'field' => $fieldData, 'field' => $fieldData,
'fieldDesc' => $fieldDesc[$fieldData['field']], 'fieldDesc' => $fieldDescription,
'modelForForm' => $modelForForm 'modelForForm' => $modelForForm
) )
); );

View File

@ -1,4 +1,5 @@
<?php <?php
$seed = mt_rand();
if (!is_array($fieldDesc)) { if (!is_array($fieldDesc)) {
$fieldDesc = array('info' => $fieldDesc); $fieldDesc = array('info' => $fieldDesc);
$default = 'info'; $default = 'info';
@ -16,32 +17,46 @@
$default = 'info'; $default = 'info';
} }
} }
echo sprintf( $popoverID = sprintf("%sInfoPopover%s", h($field['field']), $seed);
'<span id = "%sInfoPopover" class="icon-info-sign" data-bs-toggle="popover" data-bs-trigger="hover"></span>', echo $this->Bootstrap->icon('info-circle', [
h($field['field']) 'id' => $popoverID,
); 'class' => ['ms-1'],
'attrs' => [
'data-bs-toggle' => 'popover',
'data-bs-trigger' => 'hover',
]
]);
?> ?>
<script type="text/javascript"> <script type="text/javascript">
$(document).ready(function() { $(document).ready(function() {
new bootstrap.Popover('#<?php echo h($field['field']); ?>InfoPopover', { new bootstrap.Popover('#<?= $popoverID ?>', {
html: true, html: true,
content: function() { content: function() {
var tempSelector = '#<?php echo h($modelForForm . \Cake\Utility\Inflector::camelize($field['field'])); ?>'; return $('<div>')
if ($(tempSelector)[0].nodeName === "SELECT" && Object.keys(fieldDesc).length > 1) { .append(
return $('<div>').append( $('<span>')
$('<span>').attr('class', 'blue bold').text($(tempSelector +" option:selected").text()) .attr('class', 'text-primary fw-bold')
).append( .text('<?php echo h(\Cake\Utility\Inflector::humanize($field['field'])); ?>')
$('<span>').text(': ' + fieldDesc[$(tempSelector).val()]) )
.append(
$('<span>').text(": <?= h($fieldDesc["info"]) ?>")
); );
} else { // var tempSelector = '#<?php echo h($modelForForm . \Cake\Utility\Inflector::camelize($field['field'])); ?>';
return $('<div>').append( // if ($(tempSelector)[0].nodeName === "SELECT" && Object.keys(fieldDesc).length > 1) {
$('<span>').attr('class', 'blue bold').text('<?php echo h(\Cake\Utility\Inflector::humanize($field['field'])); ?>') // return $('<div>').append(
).append( // $('<span>').attr('class', 'blue bold').text($(tempSelector +" option:selected").text())
$('<span>').text(": " + fieldDesc["info"]) // ).append(
); // $('<span>').text(': ' + fieldDesc[$(tempSelector).val()])
} // );
// } else {
// return $('<div>').append(
// $('<span>').attr('class', 'blue bold').text('<?php echo h(\Cake\Utility\Inflector::humanize($field['field'])); ?>')
// ).append(
// $('<span>').text(": " + fieldDesc["info"])
// );
// }
} }
}); });
var fieldDesc = <?php echo json_encode($fieldDesc); ?>; // var fieldDesc = <?php echo json_encode($fieldDesc); ?>;
}); });
</script> </script>

View File

@ -19,7 +19,7 @@
$entity = isset($entity) ? $entity : null; $entity = isset($entity) ? $entity : null;
$fieldsString = ''; $fieldsString = '';
$simpleFieldWhitelist = [ $simpleFieldWhitelist = [
'default', 'type', 'placeholder', 'label', 'empty', 'rows', 'div', 'required', 'templates', 'options', 'value', 'checked' 'default', 'type', 'placeholder', 'label', 'empty', 'rows', 'div', 'required', 'templates', 'options', 'value', 'checked',
]; ];
if (empty($data['url'])) { if (empty($data['url'])) {
$data['url'] = ["controller" => $this->request->getParam('controller'), "action" => $this->request->getParam('url')]; $data['url'] = ["controller" => $this->request->getParam('controller'), "action" => $this->request->getParam('url')];