diff --git a/js/DI/collection.js b/js/DI/collection.js index ff8e8ee33..06b4d45f1 100644 --- a/js/DI/collection.js +++ b/js/DI/collection.js @@ -79,10 +79,13 @@ const Collection = function(oForm, objectFormUrl){ ); // create item element - const item = oToolkit.createElementFromHtml(text); + const fragment = oToolkit.createElementFromHtml(text); + collectionHolder.appendChild(fragment); + + // form handling + const item = collectionHolder.querySelector('tr:last-child'); listenRemoveItem(item); oForm.handleElement(item); - collectionHolder.appendChild(item); // store new index collectionHolder.dataset.index++; diff --git a/js/DI/dynamic.js b/js/DI/dynamic.js new file mode 100644 index 000000000..4415b3ff8 --- /dev/null +++ b/js/DI/dynamic.js @@ -0,0 +1,116 @@ +/** + * Dynamics handling. + * + * @returns {{handleElement: handleElement}} + * @constructor + */ +const Dynamic = function(){ + + const aSelectors = { + dataHideWhen: '[data-hide-when]', + dataDisableWhen: '[data-disable-when]', + dataAttCode: '[data-att-code="{0}"]', + dataBlockContainer: '[data-block="container"]', + }; + + /** + * hideEmptyContainers. + * + * The purpose of this function is to hide empty containers. + * Ex: FieldSetType with no children + * + */ + function hideEmptyContainers(oElement){ + $('.combodo-field-set', oElement).each(function(){ + $(this).parent().toggle($(this).children().length !== 0); + }); + } + + /** + * initDynamicsInvisible. + * + * @param oElement + */ + function initDynamicsInvisible(oElement){ + + // get all dynamic hide fields + const aInvisibleFields = oElement.querySelectorAll(aSelectors.dataHideWhen); + + // iterate throw fields... + aInvisibleFields.forEach(function (oInvisibleField) { + + // retrieve condition + const aHideWhenCondition = JSON.parse(oInvisibleField.dataset.hideWhen); + + // retrieve condition data + const oHideWhenElement = oElement.querySelector(String.format(aSelectors.dataAttCode, aHideWhenCondition.att_code)); + + // initial hidden state + oInvisibleField.closest(aSelectors.dataBlockContainer).hidden = (oHideWhenElement.value === aHideWhenCondition.value); + + // listen for changes + oHideWhenElement.addEventListener('change', (e) => { + oInvisibleField.closest(aSelectors.dataBlockContainer).hidden = (e.target.value === aHideWhenCondition.value); + oInvisibleField.closest(aSelectors.dataBlockContainer).style.visibility = (e.target.value === aHideWhenCondition.value) ? 'hidden' : ''; + }); + }); + + } + + /** + * initDynamicsDisable. + * + * @param oElement + */ + function initDynamicsDisable(oElement){ + + // get all dynamic hide fields + const aDisabledFields = oElement.querySelectorAll(aSelectors.dataDisableWhen); + + // iterate throw fields... + aDisabledFields.forEach(function (oDisabledField) { + + // retrieve condition + const aDisableWhenCondition = JSON.parse(oDisabledField.dataset.disableWhen); + + // retrieve condition data + const oDisableWhenElement = oElement.querySelector(`[data-att-code="${aDisableWhenCondition.att_code}"]`); + + // initial disabled state + oDisabledField.closest(aSelectors.dataBlockContainer).disabled = (oDisableWhenElement.value === aDisableWhenCondition.value); + + // listen for changes + oDisableWhenElement.addEventListener('change', (e) => { + oDisabledField.closest(aSelectors.dataBlockContainer).disabled = (e.target.value === aDisableWhenCondition.value); + }); + }); + } + + /** + * handleElement. + * + * @param element + */ + function handleElement(element){ + hideEmptyContainers(element); + initDynamicsInvisible(element); + initDynamicsDisable(element); + } + + return { + handleElement, + } +}; + + + + + + + + + + + + + diff --git a/js/DI/form.js b/js/DI/form.js index 33acdd97a..e86f8d778 100644 --- a/js/DI/form.js +++ b/js/DI/form.js @@ -2,17 +2,16 @@ * Forms handling. * * @param oWidget + * @param oDynamic * @returns {{handleElement: handleElement}} * @constructor */ -const Form = function(oWidget){ +const Form = function(oWidget, oDynamic){ const DEPENDS_ON_SEPARATOR = ' '; const aSelectors = { dataDependsOn: '[data-depends-on]', - dataHideWhen: '[data-hide-when]', - dataDisableWhen: '[data-disable-when]', dataBlockContainer: '[data-block="container"]', dataAttCode: '[data-att-code="{0}"]' }; @@ -261,11 +260,9 @@ const Form = function(oWidget){ * @param element */ function handleElement(element){ - hideEmptyContainers(element); initDependencies(element); - initDynamicsInvisible(element); - initDynamicsDisable(element); oWidget.handleElement(element); + oDynamic.handleElement(element); } return { diff --git a/sources/DI/Controller/ConfigurationController.php b/sources/DI/Controller/ConfigurationController.php new file mode 100644 index 000000000..c720e5e23 --- /dev/null +++ b/sources/DI/Controller/ConfigurationController.php @@ -0,0 +1,34 @@ +createForm(ConfigurationType::class, []); + + // handle HTTP request + $oForm->handleRequest($request); + + // return object form + return $this->renderForm('DI/configuration/edit.html.twig', [ + 'form' => $oForm + ]); + } +} diff --git a/sources/DI/Controller/DefaultController.php b/sources/DI/Controller/DefaultController.php new file mode 100644 index 000000000..c82ce5652 --- /dev/null +++ b/sources/DI/Controller/DefaultController.php @@ -0,0 +1,31 @@ +redirectToRoute('home'); + } + + /** + * @Route ("/home", name="home") + */ + public function home(): Response + { + return $this->render('DI/home.html.twig'); + } + +} diff --git a/sources/DI/Controller/Controller.php b/sources/DI/Controller/ObjectController.php similarity index 83% rename from sources/DI/Controller/Controller.php rename to sources/DI/Controller/ObjectController.php index 6de896e88..6770b74a7 100644 --- a/sources/DI/Controller/Controller.php +++ b/sources/DI/Controller/ObjectController.php @@ -2,7 +2,6 @@ namespace Combodo\iTop\DI\Controller; -use Combodo\iTop\DI\Form\Type\Compound\ConfigurationType; use Combodo\iTop\DI\Form\Type\Compound\PartialObjectType; use Combodo\iTop\DI\Form\Type\Compound\ObjectType; use Combodo\iTop\DI\Services\ObjectService; @@ -15,25 +14,14 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\Routing\Annotation\Route; -use Symfony\Component\Stopwatch\Stopwatch; -class Controller extends AbstractController +/** + * Object controller. + * + */ +class ObjectController extends AbstractController { - /** - * @Route ("/", name="root") - */ - public function root(): Response - { - return $this->redirectToRoute('home'); - } - /** - * @Route ("/home", name="home") - */ - public function home(): Response - { - return $this->render('DI/home.html.twig'); - } /** * @Route ("/{class<\w+>}/{id<\d+>}/view", name="object_view") @@ -83,7 +71,7 @@ class Controller extends AbstractController { // retrieve object try{ - $oObject= $oObjectService->getObject($class, $id); + $oObject = $oObjectService->getObject($class, $id); } catch(Exception $e){ throw $this->createNotFoundException("The $class $id does not exist"); @@ -103,15 +91,19 @@ class Controller extends AbstractController // submitted and valid if ($oForm->isSubmitted() && $oForm->isValid()) { - // retrieve object - $oObject = $oForm->getData(); - try { + // handle link set (apply DbInsert, DbDelete, DbUpdate) could be automatic ? $oObjectService->handleLinkSetDB($oObject); // save object - $oObject->DBUpdate(); + if($id === 0){ + $id = $oObject->DBInsert(); + } + else{ + $oObject->DBUpdate(); + } + } catch(Exception $e){ throw new HttpException(500, 'Error while trying to save object'); @@ -141,7 +133,7 @@ class Controller extends AbstractController { // retrieve object try{ - $oObject= $oObjectService->getObject($class, $id); + $oObject = $oObjectService->getObject($class, $id); } catch(Exception $e){ throw $this->createNotFoundException("The $class $id does not exist"); @@ -168,9 +160,6 @@ class Controller extends AbstractController // submitted and valid if ($oForm->isSubmitted() && $oForm->isValid()) { - // retrieve object - $oObject = $oForm->getData(); - try { // handle link set (apply DbInsert, DbDelete, DbUpdate) could be automatic ? $oObjectService->handleLinkSetDB($oObject); @@ -203,7 +192,7 @@ class Controller extends AbstractController { // retrieve object try{ - $oObject= $oObjectService->getObject($class, $id); + $oObject = $oObjectService->getObject($class, $id); } catch(Exception $e){ throw $this->createNotFoundException("The $class $id does not exist"); @@ -233,20 +222,4 @@ class Controller extends AbstractController ]); } - /** - * @Route("/configuration/edit", name="configuration_edit") - */ - public function configurationEdit(Request $request) : Response - { - // create object form - $oForm = $this->createForm(ConfigurationType::class, []); - - // handle HTTP request - $oForm->handleRequest($request); - - // return object form - return $this->renderForm('DI/configuration/edit.html.twig', [ - 'form' => $oForm - ]); - } } diff --git a/templates/DI/base.html.twig b/templates/DI/base.html.twig index 905466214..25ece686e 100644 --- a/templates/DI/base.html.twig +++ b/templates/DI/base.html.twig @@ -24,9 +24,6 @@ {# JS #} - - - {# - BLOCK HEAD - #} {% block head %}{% endblock %} @@ -58,6 +55,17 @@