- Implement nsISupports for nsXFormsControl so that we can QI to it

- Add a Refresh() method to nsXFormsControl, which will soon be called on all form controls by the model, in response to a refresh event.
- Move mInstanceNode onto nsXFormsControl, since all controls will have one.
- Move creation of input to OnCreated so that we always have a visualContent to return.
- Move binding of input control from DocumentChanged to AttributeSet
- Implment Refresh for InputElement, which fetches the value from the instance data and sets it in the text or checkbox control.


git-svn-id: svn://10.0.0.236/branches/FORMS_20040722_BRANCH@163108 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
bryner%brianryner.com
2004-10-01 20:32:50 +00:00
parent 58b80d908a
commit 846ad899bb
3 changed files with 81 additions and 43 deletions

View File

@@ -49,7 +49,8 @@
#include "nsIDOMXPathEvaluator.h"
#include "nsIDOMXPathResult.h"
#include "nsIDOMXPathNSResolver.h"
#include "nsXFormsModelElement.h"
NS_IMPL_ISUPPORTS1(nsXFormsControl, nsXFormsControl)
nsXFormsModelElement*
nsXFormsControl::GetModelAndBind(nsIDOMElement **aBindElement)

View File

@@ -48,16 +48,27 @@
class nsXFormsModelElement;
class nsIDOMElement;
#define NS_XFORMSCONTROL_IID \
{0x49bad098, 0xaa62, 0x49be, {0x98, 0x7b, 0xdb, 0x32, 0x66, 0x92, 0x1e, 0x0a}}
// Common implementation for XForms form controls
class nsXFormsControl : public nsXFormsElement
class nsXFormsControl : public nsXFormsElement,
public nsISupports
{
public:
NS_DEFINE_STATIC_IID_ACCESSOR(NS_XFORMSCONTROL_IID)
NS_DECL_ISUPPORTS
virtual NS_HIDDEN_(void) Refresh() = 0;
NS_HIDDEN_(nsXFormsModelElement*) GetModelAndBind(nsIDOMElement **aBindElement);
NS_HIDDEN_(already_AddRefed<nsIDOMNode>) FindInstanceNode();
protected:
nsCOMPtr<nsIXTFXMLVisualWrapper> mWrapper;
nsCOMPtr<nsIDOMNode> mInstanceNode;
};
#endif

View File

@@ -48,6 +48,8 @@
#include "nsXFormsControl.h"
#include "nsISchema.h"
#include "nsXFormsModelElement.h"
#include "nsIDOMHTMLInputElement.h"
#include "nsXFormsAtoms.h"
static const nsIID sScriptingIIDs[] = {
NS_IDOMELEMENT_IID,
@@ -63,9 +65,11 @@ public:
NS_DECL_NSIXTFXMLVISUAL
NS_DECL_NSIXTFELEMENT
// nsXFormsControl
virtual NS_HIDDEN_(void) Refresh();
private:
nsCOMPtr<nsIDOMNode> mInstanceNode;
nsCOMPtr<nsIDOMElement> mInput;
nsCOMPtr<nsIDOMHTMLInputElement> mInput;
};
NS_IMPL_ISUPPORTS2(nsXFormsInputElement, nsIXTFXMLVisual, nsIXTFElement)
@@ -76,6 +80,20 @@ NS_IMETHODIMP
nsXFormsInputElement::OnCreated(nsIXTFXMLVisualWrapper *aWrapper)
{
mWrapper = aWrapper;
nsCOMPtr<nsIDOMElement> node;
mWrapper->GetElementNode(getter_AddRefs(node));
nsCOMPtr<nsIDOMDocument> domDoc;
node->GetOwnerDocument(getter_AddRefs(domDoc));
nsCOMPtr<nsIDOMElement> inputElement;
domDoc->CreateElementNS(NS_LITERAL_STRING("http://www.w3.org/1999/xhtml"),
NS_LITERAL_STRING("input"),
getter_AddRefs(inputElement));
mInput = do_QueryInterface(inputElement);
NS_ENSURE_TRUE(mInput, NS_ERROR_FAILURE);
return NS_OK;
}
@@ -119,7 +137,7 @@ nsXFormsInputElement::GetScriptingInterfaces(PRUint32 *aCount, nsIID ***aArray)
NS_IMETHODIMP
nsXFormsInputElement::GetNotificationMask(PRUint32 *aMask)
{
*aMask = nsIXTFElement::NOTIFY_DOCUMENT_CHANGED;
*aMask = nsIXTFElement::NOTIFY_ATTRIBUTE_SET;
return NS_OK;
}
@@ -132,44 +150,6 @@ nsXFormsInputElement::WillChangeDocument(nsISupports *aNewDocument)
NS_IMETHODIMP
nsXFormsInputElement::DocumentChanged(nsISupports *aNewDocument)
{
// For now, just create an HTML input element (type=text).
// For correct behavior, this should:
// - locate the <model> for this control
// - get the model's instance data
// - evaluate the 'ref' or 'bind' attribute on this control as an
// xpath expression on the instance data
// - check for a "type" model item property on the result node
// - construct appropriate UI for xsd:boolean, xsd:date, etc
mInstanceNode = FindInstanceNode();
nsCOMPtr<nsIDOMElement> node;
mWrapper->GetElementNode(getter_AddRefs(node));
nsCOMPtr<nsIDOMDocument> domDoc;
node->GetOwnerDocument(getter_AddRefs(domDoc));
domDoc->CreateElementNS(NS_LITERAL_STRING("http://www.w3.org/1999/xhtml"),
NS_LITERAL_STRING("input"),
getter_AddRefs(mInput));
nsCOMPtr<nsIDOMElement> bindElement;
nsXFormsModelElement *model = GetModelAndBind(getter_AddRefs(bindElement));
if (model) {
nsCOMPtr<nsISchemaType> type = model->GetTypeForControl(this);
nsCOMPtr<nsISchemaBuiltinType> biType = do_QueryInterface(type);
if (biType) {
PRUint16 typeValue = 0;
biType->GetBuiltinType(&typeValue);
if (typeValue == nsISchemaBuiltinType::BUILTIN_TYPE_BOOLEAN) {
mInput->SetAttribute(NS_LITERAL_STRING("type"),
NS_LITERAL_STRING("checkbox"));
}
}
}
return NS_OK;
}
@@ -230,6 +210,11 @@ nsXFormsInputElement::WillSetAttribute(nsIAtom *aName, const nsAString &aValue)
NS_IMETHODIMP
nsXFormsInputElement::AttributeSet(nsIAtom *aName, const nsAString &aValue)
{
if (aName == nsXFormsAtoms::bind || aName == nsXFormsAtoms::ref) {
mInstanceNode = FindInstanceNode();
Refresh();
}
return NS_OK;
}
@@ -251,6 +236,47 @@ nsXFormsInputElement::DoneAddingChildren()
return NS_OK;
}
void
nsXFormsInputElement::Refresh()
{
if (!mInstanceNode)
mInstanceNode = FindInstanceNode();
if (!mInstanceNode || !mInput)
return;
// Fetch our value from the instance data
nsCOMPtr<nsIDOM3Node> node = do_QueryInterface(mInstanceNode);
NS_ASSERTION(node, "unexpected QI failure");
nsAutoString textContent;
node->GetTextContent(textContent);
// Revalidate our type
nsCOMPtr<nsIDOMElement> bindElement;
nsXFormsModelElement *model = GetModelAndBind(getter_AddRefs(bindElement));
if (model) {
nsCOMPtr<nsISchemaType> type = model->GetTypeForControl(this);
nsCOMPtr<nsISchemaBuiltinType> biType = do_QueryInterface(type);
PRUint16 typeValue = nsISchemaBuiltinType::BUILTIN_TYPE_STRING;
if (biType)
biType->GetBuiltinType(&typeValue);
if (typeValue == nsISchemaBuiltinType::BUILTIN_TYPE_BOOLEAN) {
mInput->SetAttribute(NS_LITERAL_STRING("type"),
NS_LITERAL_STRING("checkbox"));
mInput->SetChecked(textContent.EqualsASCII("true") ||
textContent.EqualsASCII("1"));
} else {
mInput->RemoveAttribute(NS_LITERAL_STRING("type"));
mInput->SetValue(textContent);
}
}
}
NS_HIDDEN_(nsresult)
NS_NewXFormsInputElement(nsIXTFElement **aResult)
{