Handle the OnBeforeUnload events to call OnFormCancel handlers when needed so that the plug-ins have a chance to perform some cleanup.

SVN:trunk[1328]
This commit is contained in:
Denis Flaven
2011-07-05 08:46:31 +00:00
parent 8a547bf104
commit 52fb63c0ee
5 changed files with 44 additions and 7 deletions

View File

@@ -1564,7 +1564,8 @@ EOF
} }
$iTransactionId = utils::GetNewTransactionId(); $iTransactionId = utils::GetNewTransactionId();
$oPage->SetTransactionId($iTransactionId); $oPage->SetTransactionId($iTransactionId);
$oPage->add("<form action=\"$sFormAction\" id=\"form_{$this->m_iFormId}\" enctype=\"multipart/form-data\" method=\"post\" onSubmit=\"return CheckFields('form_{$this->m_iFormId}', true)\">\n"); $oPage->add("<form action=\"$sFormAction\" id=\"form_{$this->m_iFormId}\" enctype=\"multipart/form-data\" method=\"post\" onSubmit=\"return OnSubmit('form_{$this->m_iFormId}');\">\n");
$oPage->add_ready_script("$(window).unload(function() { OnUnload('$iTransactionId') } );\n");
$oPage->AddTabContainer(OBJECT_PROPERTIES_TAB, $sPrefix); $oPage->AddTabContainer(OBJECT_PROPERTIES_TAB, $sPrefix);
$oPage->SetCurrentTabContainer(OBJECT_PROPERTIES_TAB); $oPage->SetCurrentTabContainer(OBJECT_PROPERTIES_TAB);

View File

@@ -10,6 +10,8 @@ var oObj = {};
// of Id 'att_2' in the form // of Id 'att_2' in the form
var aFieldsMap = new Array; var aFieldsMap = new Array;
window.bInSubmit = false; // For handling form cancellation via OnBeforeUnload events
// Update the whole object from the form and also update its // Update the whole object from the form and also update its
// JSON (serialized) representation in the (hidden) field // JSON (serialized) representation in the (hidden) field
function UpdateObjectFromForm(aFieldsMap, oObj) function UpdateObjectFromForm(aFieldsMap, oObj)
@@ -116,6 +118,28 @@ function ActivateStep(iTargetStep)
// ); // );
// } // }
//} //}
function OnUnload(sTransactionId)
{
if (!window.bInSubmit)
{
// If it's not a submit, then it's a "cancel" (Pressing the Cancel button, closing the window, using the back button...)
$.post('../pages/ajax.render.php', {operation: 'on_form_cancel', transaction_id: sTransactionId }, function()
{
// Do nothing for now...
});
}
}
function OnSubmit(sFormId)
{
window.bInSubmit=true; // This is a submit, make sure that when the page gets unloaded we don't cancel the action
var bResult = CheckFields(sFormId, true);
if (!bResult)
{
window.bInSubmit = false; // Submit is/will be canceled
}
return bResult;
}
// Store the result of the form validation... there may be several forms per page, beware // Store the result of the form validation... there may be several forms per page, beware
var oFormErrors = { err_form0: 0 }; var oFormErrors = { err_form0: 0 };
@@ -129,7 +153,7 @@ function CheckFields(sFormId, bDisplayAlert)
// The two 'fields' below will be updated when the 'validate' event is processed // The two 'fields' below will be updated when the 'validate' event is processed
oFormErrors['err_'+sFormId] = 0; // Number of errors encountered when validating the form oFormErrors['err_'+sFormId] = 0; // Number of errors encountered when validating the form
oFormErrors['input_'+sFormId] = null; // First 'input' with an error, to set the focus to it oFormErrors['input_'+sFormId] = null; // First 'input' with an error, to set the focus to it
$('#'+sFormId+' :input').each( function() $('#'+sFormId+' :input[type!=hidden]').each( function()
{ {
validateEventResult = $(this).trigger('validate', sFormId); validateEventResult = $(this).trigger('validate', sFormId);
} }

View File

@@ -1474,7 +1474,7 @@ EOF
$oObj->DisplayBareProperties($oP); $oObj->DisplayBareProperties($oP);
$oP->add('</div>'); $oP->add('</div>');
$oP->add("<div class=\"wizContainer\">\n"); $oP->add("<div class=\"wizContainer\">\n");
$oP->add("<form id=\"apply_stimulus\" method=\"post\" onSubmit=\"return CheckFields('apply_stimulus', true);\">\n"); $oP->add("<form id=\"apply_stimulus\" method=\"post\" onSubmit=\"return OnSubmit('apply_stimulus');\">\n");
$oP->add("<table><tr><td>\n"); $oP->add("<table><tr><td>\n");
$oP->details($aDetails); $oP->details($aDetails);
$oP->add("</td></tr></table>\n"); $oP->add("</td></tr></table>\n");
@@ -1685,7 +1685,7 @@ EOF
$aExpectedAttributes = $aTargetState['attribute_list']; $aExpectedAttributes = $aTargetState['attribute_list'];
$oP->add("<h1>$sActionDetails</h1>\n"); $oP->add("<h1>$sActionDetails</h1>\n");
$oP->add("<div class=\"wizContainer\">\n"); $oP->add("<div class=\"wizContainer\">\n");
$oP->add("<form id=\"apply_stimulus\" method=\"post\" onSubmit=\"return CheckFields('apply_stimulus', true);\">\n"); $oP->add("<form id=\"apply_stimulus\" method=\"post\" onSubmit=\"return OnSubmit('apply_stimulus');\">\n");
$aDetails = array(); $aDetails = array();
$iFieldIndex = 0; $iFieldIndex = 0;
$aFieldsMap = array(); $aFieldsMap = array();

View File

@@ -485,7 +485,19 @@ try
// Can be useful in case a user got some corrupted prefs... // Can be useful in case a user got some corrupted prefs...
appUserPreferences::ClearPreferences(); appUserPreferences::ClearPreferences();
break; break;
case 'on_form_cancel':
// Called when a creation/modification form is cancelled by the end-user
// Let's take this opportunity to inform the plug-ins so that they can perform some cleanup
$iTransactionId = utils::ReadParam('transaction_id', 0);
$sTempId = session_id().'_'.$iTransactionId;
foreach (MetaModel::EnumPlugins('iApplicationObjectExtension') as $oExtensionInstance)
{
$oExtensionInstance->OnFormCancel($sTempId);
}
break;
default: default:
$oPage->p("Invalid query."); $oPage->p("Invalid query.");
} }

View File

@@ -283,7 +283,7 @@ function RequestCreationForm($oP, $oUserOrg)
// Starts the validation when the page is ready // Starts the validation when the page is ready
CheckFields('request_form', false); CheckFields('request_form', false);
$('#request_form').submit( function() { $('#request_form').submit( function() {
return CheckFields('request_form', true); return OnSubmit('request_form');
}); });
EOF EOF
); );
@@ -745,7 +745,7 @@ function DisplayResolvedRequestForm($oP, UserRequest $oRequest)
// Starts the validation when the page is ready // Starts the validation when the page is ready
CheckFields('request_form', false); CheckFields('request_form', false);
$('#request_form').submit( function() { $('#request_form').submit( function() {
return CheckFields('request_form', true); return OnSubmit('request_form');
}); });
EOF EOF
); );