001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.user.client.ui.impl;
017:
018: import com.google.gwt.user.client.Element;
019:
020: /**
021: * Implementation class used by {@link com.google.gwt.user.client.ui.FormPanel}.
022: */
023: public class FormPanelImpl {
024:
025: /**
026: * Gets the response html from the loaded iframe.
027: *
028: * @param iframe the iframe from which the response html is to be extracted
029: * @return the response html
030: */
031: public native String getContents(Element iframe) /*-{
032: try {
033: // Make sure the iframe's window & document are loaded.
034: if (!iframe.contentWindow || !iframe.contentWindow.document)
035: return null;
036:
037: // Get the body's entire inner HTML.
038: return iframe.contentWindow.document.body.innerHTML;
039: } catch (e) {
040: return null;
041: }
042: }-*/;
043:
044: /**
045: * Gets the form element's encoding.
046: *
047: * @param form the form whose encoding is to be retrieved
048: * @return the form's encoding type
049: */
050: public native String getEncoding(Element form) /*-{
051: // We can always get 'enctype', no matter which browser, because we set
052: // both 'encoding' and 'enctype' in setEncoding().
053: return form.enctype;
054: }-*/;
055:
056: /**
057: * Hooks the iframe's onLoad event and the form's onSubmit event.
058: *
059: * @param iframe the iframe whose onLoad event is to be hooked
060: * @param form the form whose onSubmit event is to be hooked
061: * @param listener the listener to receive notification
062: */
063: public native void hookEvents(Element iframe, Element form,
064: FormPanelImplHost listener) /*-{
065: if (iframe) {
066: iframe.onload = function() {
067: // If there is no __formAction yet, this is a spurious onload
068: // generated when the iframe is first added to the DOM.
069: if (!iframe.__formAction)
070: return;
071:
072: listener.@com.google.gwt.user.client.ui.impl.FormPanelImplHost::onFrameLoad()();
073: };
074: }
075:
076: form.onsubmit = function() {
077: // Hang on to the form's action url, needed in the
078: // onload/onreadystatechange handler.
079: if (iframe)
080: iframe.__formAction = form.action;
081: return listener.@com.google.gwt.user.client.ui.impl.FormPanelImplHost::onFormSubmit()();
082: };
083: }-*/;
084:
085: /**
086: * Sets the form element's encoding.
087: *
088: * @param form the form whose encoding is to be set
089: * @param encoding the new encoding type
090: */
091: public native void setEncoding(Element form, String encoding) /*-{
092: // To be safe, setting both.
093: form.enctype = encoding;
094: form.encoding = encoding;
095: }-*/;
096:
097: /**
098: * Submits a form.
099: *
100: * @param form the form to be submitted
101: * @param iframe the iframe that is targetted, or <code>null</code>
102: */
103: public native void submit(Element form, Element iframe) /*-{
104: // Hang on to the form's action url, needed in the
105: // onload/onreadystatechange handler.
106: if (iframe)
107: iframe.__formAction = form.action;
108: form.submit();
109: }-*/;
110:
111: /**
112: * Unhooks the iframe's onLoad event.
113: *
114: * @param iframe the iframe whose onLoad event is to be unhooked
115: * @param form the form whose onSubmit event is to be unhooked
116: */
117: public native void unhookEvents(Element iframe, Element form) /*-{
118: if (iframe)
119: iframe.onload = null;
120: form.onsubmit = null;
121: }-*/;
122: }
|