Pārlūkot izejas kodu

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

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@1328 a333f486-631f-4898-b8df-5754b55c2be0
dflaven 14 gadi atpakaļ
vecāks
revīzija
f6b4b31f52

+ 2 - 1
application/cmdbabstract.class.inc.php

@@ -1564,7 +1564,8 @@ EOF
 		}
 		$iTransactionId = utils::GetNewTransactionId();
 		$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->SetCurrentTabContainer(OBJECT_PROPERTIES_TAB);

+ 25 - 1
js/forms-json-utils.js

@@ -10,6 +10,8 @@ var oObj = {};
 // of Id 'att_2' in the form 
 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
 // JSON (serialized) representation in the (hidden) field
 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
 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
 	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
-	$('#'+sFormId+' :input').each( function()
+	$('#'+sFormId+' :input[type!=hidden]').each( function()
 	{
 		validateEventResult = $(this).trigger('validate', sFormId);
 	}

+ 2 - 2
pages/UI.php

@@ -1474,7 +1474,7 @@ EOF
 			$oObj->DisplayBareProperties($oP);
 			$oP->add('</div>');
 			$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->details($aDetails);
 			$oP->add("</td></tr></table>\n");
@@ -1685,7 +1685,7 @@ EOF
 			$aExpectedAttributes = $aTargetState['attribute_list'];
 			$oP->add("<h1>$sActionDetails</h1>\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();
 			$iFieldIndex = 0;
 			$aFieldsMap = array();

+ 13 - 1
pages/ajax.render.php

@@ -485,7 +485,19 @@ try
 		// Can be useful in case a user got some corrupted prefs...
 		appUserPreferences::ClearPreferences();
 		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:
 		$oPage->p("Invalid query.");
 	}

+ 2 - 2
portal/index.php

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