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

Allow basic usage of form and polyfill validate check globally #3373

Merged
merged 8 commits into from
Jun 6, 2016

Conversation

mkhatib
Copy link
Contributor

@mkhatib mkhatib commented May 28, 2016

Kick off form support #3343.

This is a prequel to #3344 as discussed to provide basic support for form before we introduce some more features.

/**
* @param {!Window} window
*/
export function uninstallGlobalSubmitListener(window) {
Copy link
Member

Choose a reason for hiding this comment

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

We don't seem to use this?

Could we simplify this file and get rid of the service?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we just store a local variable in this file and memoize it? How do we handle multiple window situations? Would something like the following be ok?

/**
 * @type {?SubmitHandler} Store the global handler for form submits.
 */
let submitHandler_;

/**
 * @param {!Window} window
 */
export function installGlobalSubmitListener(window) {
  submitHandlerFor(window);
}

/**
 * @param {!Window} window
 */
function submitHandlerFor(window) {
  return submitHandler_ || (submitHandler_ = new SubmitHandler(window));
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cramforce let me know if the above is good or if we handle this in a different way?

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. Let me know if this looks good.

As per chat, we only need to install this once on the passed window.

// available per input.validity object. We need to figure out a way of
// displaying these.
if (form.checkValidity && !form.checkValidity()) {
form.classList.remove('amp-form-valid');
Copy link
Contributor

Choose a reason for hiding this comment

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

So, when we check for validity via form.checkValidity - does Safari set :invalid on the form itself? If so, I'd like to defer our own classes until the next PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

:invalid would be applied on load to the form not on submit. Happy to drop all the classes from this PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's do. I think it'd be easier to add them in one go later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Dropped.

@mkhatib
Copy link
Contributor Author

mkhatib commented Jun 3, 2016

@dvoytenko @cramforce thanks for all comments 😄 . PTAL 👀

/**
* Removes all event listeners.
*/
cleanup() {
Copy link
Member

Choose a reason for hiding this comment

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

There is no caller of this. Can this entire class just be inlined into the function installGlobalSubmitListener

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Dropped.

/**
* @param {!Window} window
*/
function submitHandlerFor(window) {
Copy link
Member

Choose a reason for hiding this comment

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

add return

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This has been dropped.

@mkhatib
Copy link
Contributor Author

mkhatib commented Jun 3, 2016

PTAL 👀

@dvoytenko
Copy link
Contributor

LGTM

if (prefix.length > string.length) {
return false;
}
return string.indexOf(prefix) == 0;
Copy link
Contributor

@jridgewell jridgewell Jun 6, 2016

Choose a reason for hiding this comment

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

We can optimize this using a rather nifty trick:

return string.lastIndexOf(prefix, 0) === 0;

This prevents a full scan of the string, only checking index 0 (and possibly 1, 2, up to prefix.length if they match).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Woah, that's pretty cool 👍 . Done.

@mkhatib mkhatib merged commit 1a56650 into ampproject:master Jun 6, 2016
@mkhatib mkhatib deleted the document-submit branch June 6, 2016 19:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants