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

Polyfill reportValidation calls and bubble UI for webkit. #3734

Merged
merged 7 commits into from
Jun 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 26 additions & 1 deletion extensions/amp-form/0.1/amp-form.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,30 @@

form.amp-form-submit-success [submit-success],
form.amp-form-submit-error [submit-error] {
display: block;
display: block;
}


.-amp-validation-bubble {
transform: translate(-50%, -100%);
background-color: white;
box-shadow: 0 5px 15px 0 rgba(0, 0, 0, .5);
max-width: 200px;
position: absolute;
display: none;
box-sizing: border-box;
padding: 10px;
border-radius: 5px;
}

.-amp-validation-bubble:after {
content: ' ';
position: absolute;
bottom: -8px;
left: 30px;
width: 0;
height: 0;
border-left: 8px solid transparent;
border-right: 8px solid transparent;
border-top: 8px solid #fff;
}
100 changes: 95 additions & 5 deletions extensions/amp-form/0.1/amp-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import {templatesFor} from '../../../src/template';
import {removeElement, childElementByAttr} from '../../../src/dom';
import {installStyles} from '../../../src/styles';
import {CSS} from '../../../build/amp-form-0.1.css';
import {ValidationBubble} from './validation-bubble';
import {vsyncFor} from '../../../src/vsync';

/** @type {string} */
const TAG = 'amp-form';
Expand All @@ -37,6 +39,9 @@ const FormState_ = {
SUBMIT_SUCCESS: 'submit-success',
};

/** @type {?./validation-bubble.ValidationBubble|undefined} */
let validationBubble;
Copy link
Member

Choose a reason for hiding this comment

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

add undefined to allowed type if we dont init with null

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


export class AmpForm {

/**
Expand All @@ -50,6 +55,9 @@ export class AmpForm {
/** @const @private {!Element} */
this.form_ = element;

/** @const @private {!../../../src/service/vsync-impl.Vsync} */
this.vsync_ = vsyncFor(this.win_);

/** @const @private {!Templates} */
this.templates_ = templatesFor(this.win_);

Expand Down Expand Up @@ -94,12 +102,20 @@ export class AmpForm {
* @private
*/
handleSubmit_(e) {
if (e.defaultPrevented) {
if (this.state_ == FormState_.SUBMITTING) {
e.preventDefault();
return;
}

if (this.state_ == FormState_.SUBMITTING) {
const shouldValidate = !this.form_.hasAttribute('novalidate');
if (shouldValidate &&
this.form_.checkValidity && !this.form_.checkValidity()) {
e.preventDefault();
// TODO(#3776): Use .mutate method when it supports passing state.
this.vsync_.run({
measure: undefined,
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a TODO #3776, please.

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

mutate: reportValidity,
}, {form: this.form_});
return;
}

Expand Down Expand Up @@ -146,7 +162,7 @@ export class AmpForm {
}

/**
* @param {!Object} data
* @param {!Object=} data
* @private
*/
renderTemplate_(data = {}) {
Expand Down Expand Up @@ -176,6 +192,73 @@ export class AmpForm {
}


/**
* Reports validity of the form passed through state object.
* @param {!Object} state
*/
function reportValidity(state) {
reportFormValidity(state.form);
}


/**
* Reports validity for the first invalid input - if any.
* @param {!HTMLFormElement} form
*/
function reportFormValidity(form) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Naming here is odd, since we're actually reporting invalidity.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The native API is called reportValidity.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ack.

const inputs = form.querySelectorAll('input,select,textarea');
for (let i = 0; i < inputs.length; i++) {
if (!inputs[i].checkValidity()) {
reportInputValidity(inputs[i]);
break;
}
}
}


/**
* Revalidates the currently focused input after a change.
* @param {!KeyboardEvent} event
*/
function onInvalidInputKeyUp_(event) {
if (event.target.checkValidity()) {
validationBubble.hide();
Copy link
Contributor

Choose a reason for hiding this comment

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

One last: We may end up double calling #hide and #show. Storing the current message as a state on ValidationBubble, we can check if we're already hidden, or if we're trying to show the same message again. In either case, we can return without doing anything.

Copy link
Contributor Author

@mkhatib mkhatib Jun 24, 2016

Choose a reason for hiding this comment

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

Yeah we'd also need to store the targetElement and visibility state of the bubble in that state to make sure we're calling show for the same element as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Filed a bug to track this will do in another PR #3782

} else {
validationBubble.show(event.target, event.target.validationMessage);
}
}


/**
* Hides validation bubble and removes listeners on the invalid input.
* @param {!Event} event
*/
function onInvalidInputBlur_(event) {
validationBubble.hide();
event.target.removeEventListener('blur', onInvalidInputBlur_);
event.target.removeEventListener('keyup', onInvalidInputKeyUp_);
}


/**
* Focuses and reports the invalid message of the input in a message bubble.
* @param {!HTMLInputElement} input
*/
function reportInputValidity(input) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto naming.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ack.

input./*OK*/focus();

// Remove any previous listeners on the same input. This avoids adding many
// listeners on the same element when the user submit pressing Enter or any
// other method to submit the form without the element losing focus.
input.removeEventListener('blur', onInvalidInputBlur_);
input.removeEventListener('keyup', onInvalidInputKeyUp_);

input.addEventListener('keyup', onInvalidInputKeyUp_);
input.addEventListener('blur', onInvalidInputBlur_);
validationBubble.show(input, input.validationMessage);
}


/**
* Installs submission handler on all forms in the document.
* @param {!Window} win
Expand All @@ -189,10 +272,17 @@ function installSubmissionHandlers(win) {
}


function installAmpForm(win) {
/**
* @param {!Window} win
* @private visible for testing.
*/
export function installAmpForm(win) {
return getService(win, 'amp-form', () => {
if (isExperimentOn(win, TAG)) {
installStyles(win.document, CSS, () => installSubmissionHandlers(win));
installStyles(win.document, CSS, () => {
validationBubble = new ValidationBubble(win);
installSubmissionHandlers(win);
});
}
return {};
});
Expand Down