diff --git a/application/wizardhelper.class.inc.php b/application/wizardhelper.class.inc.php index 65d68280e..b03f532f8 100644 --- a/application/wizardhelper.class.inc.php +++ b/application/wizardhelper.class.inc.php @@ -351,6 +351,7 @@ class WizardHelper /** * @return string JS code to be executed for fields update * @since 3.0.0 N°3198 + * @deprecated 3.0.3-2 3.0.4 3.1.1 3.2.0 Use {@see \WizardHelper::AddJsForUpdateFields()} instead */ public function GetJsForUpdateFields() { @@ -363,6 +364,32 @@ class WizardHelper JS; } + /** + * Add necessary JS snippets (to the page) to be executed for fields update + * + * @param \WebPage $oPage + * @return void + * @since 3.0.3-2 3.0.4 3.1.1 3.2.0 N°6766 + */ + public function AddJsForUpdateFields(WebPage $oPage) + { + $sWizardHelperJsVar = (!is_null($this->m_aData['m_sWizHelperJsVarName'])) ? utils::Sanitize($this->m_aData['m_sWizHelperJsVarName'], '', utils::ENUM_SANITIZATION_FILTER_PARAMETER) : 'oWizardHelper'.$this->GetFormPrefix(); + $sWizardHelperJson = $this->ToJSON(); + + $oPage->add_script(<<add_ready_script(<< $value) - { + foreach ($aLinkObj as $sAttCode => $value) { $oAttDef = MetaModel::GetAttributeDef($sLinkClass, $sAttCode); if (($oAttDef->IsExternalKey()) && ($value != '') && ($value > 0)) { diff --git a/js/wizardhelper.js b/js/wizardhelper.js index b445eb2c7..f159c22a5 100644 --- a/js/wizardhelper.js +++ b/js/wizardhelper.js @@ -74,6 +74,11 @@ function WizardHelper(sClass, sFormPrefix, sState, sInitialState, sStimulus) { 'm_sWizHelperJsVarName': null // if set will use this name when server returns JS code in \WizardHelper::GetJsForUpdateFields }; this.m_oData.m_sClass = sClass; + /** + * Promise resolve callback when dependencies have been updated + * @since 3.0.3-2 3.0.4 3.1.1 3.2.0 N°6766 + * */ + this.m_oDependenciesUpdatedPromiseResolve = null; // Setting optional transition data if (sInitialState !== undefined) @@ -152,6 +157,7 @@ function WizardHelper(sClass, sFormPrefix, sState, sInitialState, sStimulus) { }; this.UpdateFields = function () { + const me = this; var aRefreshed = []; //console.log('** UpdateFields **'); // Set the full HTML for the input field @@ -185,10 +191,17 @@ function WizardHelper(sClass, sFormPrefix, sState, sInitialState, sStimulus) { } } // For each "refreshed" field, asynchronously trigger a change in case there are dependent fields to update - for (i = 0; i < aRefreshed.length; i++) - { + for (i = 0; i < aRefreshed.length; i++) { var sString = "$('#"+aRefreshed[i]+"').trigger('change').trigger('update');"; - window.setTimeout(sString, 1); // Synchronous 'trigger' does nothing, call it asynchronously + const oPromise = new Promise(function (resolve) { + // Store the resolve callback so we can call it later from outside + me.m_oDependenciesUpdatedPromiseResolve = resolve; + }); + oPromise.then(function () { + window.setTimeout(sString, 1); // Synchronous 'trigger' does nothing, call it asynchronously + // Resolve callback is reinitialized in case the redirection fails for any reason and we might need to retry + me.m_oDependenciesUpdatedPromiseResolve = null; + }); } if($('[data-field-status="blocked"]').length === 0) { $('.disabledDuringFieldLoading').prop("disabled", false).removeClass('disabledDuringFieldLoading'); diff --git a/pages/ajax.render.php b/pages/ajax.render.php index ffdb84bc6..76715f46d 100644 --- a/pages/ajax.render.php +++ b/pages/ajax.render.php @@ -561,7 +561,7 @@ try } } } - $oPage->add_script($oWizardHelper->GetJsForUpdateFields()); + $oWizardHelper->AddJsForUpdateFields($oPage); break; case 'obj_creation_form':