From 8fed129e53431a1d20dba147088a43d91145db49 Mon Sep 17 00:00:00 2001 From: "allan%beaufour.dk" Date: Fri, 18 Feb 2005 11:16:15 +0000 Subject: [PATCH] Disables submit button until submit is done. Bug 280312, r=aaronr+me, patch by smaug@welho.com --- extensions/xforms/Makefile.in | 2 + .../xforms/nsIXFormsSubmissionElement.idl | 48 +++++++++++++ extensions/xforms/nsIXFormsSubmitElement.idl | 48 +++++++++++++ .../xforms/nsXFormsSubmissionElement.cpp | 69 +++++++++---------- extensions/xforms/nsXFormsSubmissionElement.h | 8 ++- extensions/xforms/nsXFormsTriggerElement.cpp | 42 ++++++----- 6 files changed, 160 insertions(+), 57 deletions(-) create mode 100644 extensions/xforms/nsIXFormsSubmissionElement.idl create mode 100644 extensions/xforms/nsIXFormsSubmitElement.idl diff --git a/extensions/xforms/Makefile.in b/extensions/xforms/Makefile.in index 5435d96b3979..11c14d444505 100644 --- a/extensions/xforms/Makefile.in +++ b/extensions/xforms/Makefile.in @@ -85,6 +85,8 @@ XPIDLSRCS = \ nsIXFormsValueElement.idl \ nsIXFormsRepeatElement.idl \ nsIXFormsRepeatItemElement.idl \ + nsIXFormsSubmitElement.idl \ + nsIXFormsSubmissionElement.idl \ $(NULL) CPPSRCS = \ diff --git a/extensions/xforms/nsIXFormsSubmissionElement.idl b/extensions/xforms/nsIXFormsSubmissionElement.idl new file mode 100644 index 000000000000..3369c0c39cf8 --- /dev/null +++ b/extensions/xforms/nsIXFormsSubmissionElement.idl @@ -0,0 +1,48 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla XForms support. + * + * The Initial Developer of the Original Code is + * Olli Pettay. + * Portions created by the Initial Developer are Copyright (C) 2005 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Olli Pettay (original author) + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include "nsIXFormsSubmitElement.idl" + +/** + * Interface implemented by the submission element. + */ +[uuid(d132ff87-f6ce-4b7d-909e-323188966694)] +interface nsIXFormsSubmissionElement : nsISupports +{ + void setActivator(in nsIXFormsSubmitElement aActivator); +}; diff --git a/extensions/xforms/nsIXFormsSubmitElement.idl b/extensions/xforms/nsIXFormsSubmitElement.idl new file mode 100644 index 000000000000..4c22d78de208 --- /dev/null +++ b/extensions/xforms/nsIXFormsSubmitElement.idl @@ -0,0 +1,48 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla XForms support. + * + * The Initial Developer of the Original Code is + * Olli Pettay. + * Portions created by the Initial Developer are Copyright (C) 2005 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Olli Pettay (original author) + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include "nsISupports.idl" + +/** + * Interface implemented by the submit element. + */ +[uuid(83427654-2cdd-48f6-bfef-285cb521554a)] +interface nsIXFormsSubmitElement : nsISupports +{ + void setDisabled(in boolean aDisable); +}; diff --git a/extensions/xforms/nsXFormsSubmissionElement.cpp b/extensions/xforms/nsXFormsSubmissionElement.cpp index 6f8993a65ad1..ac46d101dbaf 100644 --- a/extensions/xforms/nsXFormsSubmissionElement.cpp +++ b/extensions/xforms/nsXFormsSubmissionElement.cpp @@ -261,9 +261,10 @@ public: // nsISupports -NS_IMPL_ISUPPORTS_INHERITED1(nsXFormsSubmissionElement, +NS_IMPL_ISUPPORTS_INHERITED2(nsXFormsSubmissionElement, nsXFormsStubElement, - nsIRequestObserver) + nsIRequestObserver, + nsIXFormsSubmissionElement) // nsIXTFElement @@ -280,9 +281,16 @@ nsXFormsSubmissionElement::HandleDefault(nsIDOMEvent *aEvent, PRBool *aHandled) nsAutoString type; aEvent->GetType(type); if (type.EqualsLiteral("xforms-submit")) { - nsresult rv = Submit(); - if (NS_FAILED(rv)) - SubmitEnd(PR_FALSE); + // If the submission is already active, do nothing. + if (!mSubmissionActive && NS_FAILED(Submit())) { + mSubmissionActive = PR_FALSE; + if (mActivator) { + mActivator->SetDisabled(PR_FALSE); + mActivator = nsnull; + } + nsXFormsUtils::DispatchEvent(mElement, eEvent_SubmitError); + } + *aHandled = PR_TRUE; } else { *aHandled = PR_FALSE; @@ -291,6 +299,16 @@ nsXFormsSubmissionElement::HandleDefault(nsIDOMEvent *aEvent, PRBool *aHandled) return NS_OK; } +// nsIXFormsSubmissionElement + +NS_IMETHODIMP +nsXFormsSubmissionElement::SetActivator(nsIXFormsSubmitElement* aActivator) +{ + if (!mActivator && !mSubmissionActive) + mActivator = aActivator; + return NS_OK; +} + // nsIXTFGenericElement NS_IMETHODIMP @@ -348,7 +366,13 @@ nsXFormsSubmissionElement::OnStopRequest(nsIRequest *request, nsISupports *ctx, mPipeIn = 0; - SubmitEnd(succeeded); + mSubmissionActive = PR_FALSE; + if (mActivator) { + mActivator->SetDisabled(PR_FALSE); + mActivator = nsnull; + } + nsXFormsUtils::DispatchEvent(mElement, succeeded ? + eEvent_SubmitDone : eEvent_SubmitError); return NS_OK; } @@ -466,6 +490,9 @@ nsXFormsSubmissionElement::Submit() // 1. ensure that we are not currently processing a xforms-submit (see E37) NS_ENSURE_STATE(!mSubmissionActive); mSubmissionActive = PR_TRUE; + + if (mActivator) + mActivator->SetDisabled(PR_TRUE); // XXX seems to be required by // http://www.w3.org/TR/2003/REC-xforms-20031014/slice4.html#evt-revalidate @@ -510,36 +537,6 @@ nsXFormsSubmissionElement::Submit() return rv; } -nsresult -nsXFormsSubmissionElement::SubmitEnd(PRBool succeeded) -{ - LOG(("xforms submission complete [%s]\n", succeeded ? "success" : "failure")); - - // XForms 1.0 errata (E37) limits a element to performing - // only one submission at a time, contrary to the spec which places the - // limitation on the element. - mSubmissionActive = PR_FALSE; - - nsCOMPtr domDoc; - mElement->GetOwnerDocument(getter_AddRefs(domDoc)); - nsCOMPtr doc = do_QueryInterface(domDoc); - - nsCOMPtr event; - doc->CreateEvent(NS_LITERAL_STRING("Events"), getter_AddRefs(event)); - NS_ENSURE_TRUE(event, NS_ERROR_OUT_OF_MEMORY); - - event->InitEvent(succeeded ? NS_LITERAL_STRING("xforms-submit-done") - : NS_LITERAL_STRING("xforms-submit-error"), - PR_TRUE, PR_FALSE); - - // XForms 1.0 errata (E10) states that the target of the xforms-submit-error - // event is the element instead of the element. - nsCOMPtr target = do_QueryInterface(mElement); - - PRBool cancelled; - return target->DispatchEvent(event, &cancelled); -} - nsresult nsXFormsSubmissionElement::GetSelectedInstanceData(nsIDOMNode **result) { diff --git a/extensions/xforms/nsXFormsSubmissionElement.h b/extensions/xforms/nsXFormsSubmissionElement.h index ede05183e1de..07fa25fd1aff 100644 --- a/extensions/xforms/nsXFormsSubmissionElement.h +++ b/extensions/xforms/nsXFormsSubmissionElement.h @@ -44,6 +44,8 @@ #include "nsIInputStream.h" #include "nsCOMPtr.h" #include "nsIModelElementPrivate.h" +#include "nsIXFormsSubmitElement.h" +#include "nsIXFormsSubmissionElement.h" class nsIMultiplexInputStream; class nsIDOMElement; @@ -60,11 +62,13 @@ class SubmissionAttachmentArray; * @see http://www.w3.org/TR/xforms/slice3.html#structure-model-submission */ class nsXFormsSubmissionElement : public nsXFormsStubElement, - public nsIRequestObserver + public nsIRequestObserver, + public nsIXFormsSubmissionElement { public: NS_DECL_ISUPPORTS_INHERITED NS_DECL_NSIREQUESTOBSERVER + NS_DECL_NSIXFORMSSUBMISSIONELEMENT nsXFormsSubmissionElement() : mElement(nsnull) @@ -81,7 +85,6 @@ public: NS_HIDDEN_(nsresult) LoadReplaceInstance(nsIChannel *); NS_HIDDEN_(nsresult) LoadReplaceAll(nsIChannel *); NS_HIDDEN_(nsresult) Submit(); - NS_HIDDEN_(nsresult) SubmitEnd(PRBool succeeded); NS_HIDDEN_(PRBool) GetBooleanAttr(const nsAString &attrName, PRBool defaultVal = PR_FALSE); NS_HIDDEN_(void) GetDefaultInstanceData(nsIDOMNode **result); NS_HIDDEN_(nsresult) GetSelectedInstanceData(nsIDOMNode **result); @@ -102,6 +105,7 @@ public: private: nsIDOMElement *mElement; PRBool mSubmissionActive; + nsCOMPtr mActivator; // input end of pipe, which contains response data. nsCOMPtr mPipeIn; diff --git a/extensions/xforms/nsXFormsTriggerElement.cpp b/extensions/xforms/nsXFormsTriggerElement.cpp index 53a4b128f488..a4cd28c02ab3 100644 --- a/extensions/xforms/nsXFormsTriggerElement.cpp +++ b/extensions/xforms/nsXFormsTriggerElement.cpp @@ -48,6 +48,8 @@ #include "nsIXTFXMLVisualWrapper.h" #include "nsXFormsUtils.h" #include "nsXFormsControlStub.h" +#include "nsIXFormsSubmitElement.h" +#include "nsIXFormsSubmissionElement.h" class nsXFormsTriggerElement : public nsXFormsControlStub { @@ -128,13 +130,21 @@ nsXFormsTriggerElement::TryFocus(PRBool* aOK) //----------------------------------------------------------------------------- -class nsXFormsSubmitElement : public nsXFormsTriggerElement +class nsXFormsSubmitElement : public nsXFormsTriggerElement, + public nsIXFormsSubmitElement { public: + NS_DECL_ISUPPORTS_INHERITED + NS_DECL_NSIXFORMSSUBMITELEMENT + // nsIXTFElement overrides NS_IMETHOD HandleDefault(nsIDOMEvent *aEvent, PRBool *aHandled); }; +NS_IMPL_ISUPPORTS_INHERITED1(nsXFormsSubmitElement, + nsXFormsTriggerElement, + nsIXFormsSubmitElement) + // nsIXTFElement NS_IMETHODIMP @@ -163,33 +173,27 @@ nsXFormsSubmitElement::HandleDefault(nsIDOMEvent *aEvent, PRBool *aHandled) nsCOMPtr submissionElement; ownerDoc->GetElementById(submissionID, getter_AddRefs(submissionElement)); + nsCOMPtr xfSubmission(do_QueryInterface(submissionElement)); - if (!submissionElement || - !nsXFormsUtils::IsXFormsElement(submissionElement, submission)) { + if (!xfSubmission) { return nsXFormsUtils::DispatchEvent(mElement, eEvent_BindingException); } - nsCOMPtr targ = do_QueryInterface(submissionElement); - NS_ASSERTION(targ, "All elements should support nsIDOMEventTarget"); - - nsCOMPtr docEvent = do_QueryInterface(ownerDoc); - NS_ASSERTION(targ, "All documents should support nsIDOMDocumentEvent"); - - nsCOMPtr event; - docEvent->CreateEvent(NS_LITERAL_STRING("Events"), getter_AddRefs(event)); - NS_ENSURE_STATE(event); - - rv = event->InitEvent(NS_LITERAL_STRING("xforms-submit"), PR_TRUE, PR_TRUE); - NS_ENSURE_SUCCESS(rv, NS_ERROR_UNEXPECTED); - - PRBool unused; - rv = targ->DispatchEvent(event, &unused); - NS_ENSURE_SUCCESS(rv, NS_ERROR_UNEXPECTED); + xfSubmission->SetActivator(this); + nsXFormsUtils::DispatchEvent(submissionElement, eEvent_Submit); *aHandled = PR_TRUE; return NS_OK; } +NS_IMETHODIMP +nsXFormsSubmitElement::SetDisabled(PRBool aDisable) +{ + if (mButton) + mButton->SetDisabled(aDisable); + return NS_OK; +} + //----------------------------------------------------------------------------- NS_HIDDEN_(nsresult)