Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support a template-enabled [submitting] element #9442

Merged
merged 3 commits into from
May 22, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions css/amp.css
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ amp-iframe iframe {
/**
* Forms error/success messaging containers should be hidden at first.
*/
form [submitting],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validator file needs to be changed to support a similar rule to spec_name: "FORM > DIV [submit-success]"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

form [submit-success],
form [submit-error] {
display: none;
Expand Down
6 changes: 5 additions & 1 deletion examples/forms.amp.html
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,11 @@ <h4>Subscribe to our weekly Newsletter (POST xhr same origin)</h4>
</label>
<input type="submit" value="Subscribe">
</fieldset>

<div submitting>
<template type="amp-mustache">
Please wait, {{name}}.
</template>
</div>
<div submit-success>
Success! Thanks for subscribing! Please make sure to check your email
to confirm!
Expand Down
1 change: 1 addition & 0 deletions extensions/amp-form/0.1/amp-form.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

form.amp-form-submitting [submitting],
form.amp-form-submit-success [submit-success],
form.amp-form-submit-error [submit-error] {
display: block;
Expand Down
15 changes: 13 additions & 2 deletions extensions/amp-form/0.1/amp-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,16 +441,25 @@ export class AmpForm {
doXhr_(opt_extraValues) {
const isHeadOrGet = this.method_ == 'GET' || this.method_ == 'HEAD';
let xhrUrl, body;
let values = null;
if (isHeadOrGet) {
xhrUrl = addParamsToUrl(dev().assertString(this.xhrAction_),
this.getFormAsObject_(opt_extraValues));
values = this.getFormAsObject_(opt_extraValues);
xhrUrl = addParamsToUrl(dev().assertString(this.xhrAction_), values);
} else {
xhrUrl = this.xhrAction_;
body = new FormData(this.form_);
for (const key in opt_extraValues) {
body.append(key, opt_extraValues[key]);
}
values = {};
const entries = body.entries();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Iterators are not supported everywhere.

let item;
while (!(item = entries.next()).done) {
values[item.value[0]] = item.value[1];
}
}
this.renderTemplate_(values);

return this.xhr_.fetch(dev().assertString(xhrUrl), {
body,
method: this.method_,
Expand All @@ -471,6 +480,7 @@ export class AmpForm {
return response.json().then(json => {
this.triggerAction_(/* success */ true, json);
this.analyticsEvent_('amp-form-submit-success');
this.cleanupRenderedTemplate_();
this.setState_(FormState_.SUBMIT_SUCCESS);
this.renderTemplate_(json || {});
this.maybeHandleRedirect_(response);
Expand All @@ -490,6 +500,7 @@ export class AmpForm {
this.triggerAction_(
/* success */ false, error ? error.responseJson : null);
this.analyticsEvent_('amp-form-submit-error');
this.cleanupRenderedTemplate_();
this.setState_(FormState_.SUBMIT_ERROR);
this.renderTemplate_(error.responseJson || {});
this.maybeHandleRedirect_(error.response);
Expand Down