Skip to content

Commit

Permalink
New login form (#8171)
Browse files Browse the repository at this point in the history
The legacy LoginForm is substituted with the new one which is compatible
in API.
The patch contains modified code from the addon
https://vaadin.com/directory#addon/loginform

Change-Id: I2178291c97c2f66840f832a0bf932271534beb49
  • Loading branch information
ingokegel authored and Henri Sara committed Jun 1, 2016
1 parent fb63a60 commit c0babd7
Show file tree
Hide file tree
Showing 7 changed files with 566 additions and 232 deletions.
3 changes: 3 additions & 0 deletions all/src/main/templates/release-notes.html
Expand Up @@ -116,6 +116,9 @@ <h3 id="incompatible">Incompatible or Behavior-altering Changes in @version-mino
<li>The return type of BootstrapHandler.getWidgetsetForUI() has changed.</li> <li>The return type of BootstrapHandler.getWidgetsetForUI() has changed.</li>
<li>Vaadin shared no longer depends on a custom build of Guava. Any project that depends on Guava as a transitive dependency should use standard Guava.</li> <li>Vaadin shared no longer depends on a custom build of Guava. Any project that depends on Guava as a transitive dependency should use standard Guava.</li>
<li>Valo theme field error styles now apply to NativeSelect, ListSelect and TwinColSelect as well.</li> <li>Valo theme field error styles now apply to NativeSelect, ListSelect and TwinColSelect as well.</li>
<li>The LoginForm component has been rewritten to better support auto-completion of passwords on modern browsers.
Its API, look and DOM structure have changed somewhat, so any application level customizations of LoginForm need changes.
See also <a href="http://dev.vaadin.com/ticket/8171">#8171</a></li>
<li>The way we handle GWT dependencies has been completely changed. See <a href="#gwtdep">this section</a> for more details</li> <li>The way we handle GWT dependencies has been completely changed. See <a href="#gwtdep">this section</a> for more details</li>
</ul> </ul>
<h3 id="knownissues">Known Issues and Limitations</h3> <h3 id="knownissues">Known Issues and Limitations</h3>
Expand Down
@@ -0,0 +1,194 @@
/*
* Copyright 2000-2014 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.vaadin.client.ui.loginform;

import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.FocusWidget;
import com.google.gwt.user.client.ui.FormPanel;
import com.vaadin.client.ComponentConnector;
import com.vaadin.client.ConnectorHierarchyChangeEvent;
import com.vaadin.client.communication.StateChangeEvent;
import com.vaadin.client.ui.AbstractSingleComponentContainerConnector;
import com.vaadin.client.ui.VTextField;
import com.vaadin.client.ui.button.ButtonConnector;
import com.vaadin.client.ui.nativebutton.NativeButtonConnector;
import com.vaadin.client.ui.textfield.TextFieldConnector;
import com.vaadin.shared.Connector;
import com.vaadin.shared.ui.Connect;
import com.vaadin.shared.ui.loginform.LoginFormConstants;
import com.vaadin.shared.ui.loginform.LoginFormRpc;
import com.vaadin.shared.ui.loginform.LoginFormState;
import com.google.gwt.core.client.Scheduler;

@Connect(com.vaadin.ui.LoginForm.class)
public class LoginFormConnector extends
AbstractSingleComponentContainerConnector {

private VTextField passwordField;
private VTextField userField;
private LoginFormRpc loginFormRpc;

@Override
public void updateCaption(ComponentConnector connector) {

}

@Override
public VLoginForm getWidget() {
return (VLoginForm) super.getWidget();
}

@Override
protected void init() {
super.init();

loginFormRpc = getRpcProxy(LoginFormRpc.class);
getWidget().addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
@Override
public void onSubmitComplete(FormPanel.SubmitCompleteEvent event) {
valuesChanged();
loginFormRpc.submitCompleted();
}
});
}

@Override
public LoginFormState getState() {
return (LoginFormState) super.getState();
}

@Override
public void onConnectorHierarchyChange(ConnectorHierarchyChangeEvent event) {
ComponentConnector content = getContent();
if (content != null) {
getWidget().setWidget(getContentWidget());
}
}

@Override
public void onStateChanged(StateChangeEvent stateChangeEvent) {
super.onStateChanged(stateChangeEvent);

LoginFormState state = getState();
userField = configureTextField(state.userNameFieldConnector, "username");
passwordField = configureTextField(state.passwordFieldConnector,
"password");
addSubmitButtonClickHandler(state.loginButtonConnector);
getWidget().setAction(
getResourceUrl(LoginFormConstants.LOGIN_RESOURCE_NAME));
}

private VTextField configureTextField(Connector connector, String id) {
if (connector != null) {
VTextField textField = ((TextFieldConnector) connector).getWidget();

textField.addKeyDownHandler(new SubmitKeyHandler());

Element element = textField.getElement();
String externalId = element.getId();
if (externalId == null || externalId.isEmpty()
|| externalId.startsWith("gwt-")) {
element.setId(id);
}
DOM.setElementAttribute(element, "name", id);
DOM.setElementAttribute(element, "autocomplete", "on");

return textField;
} else {
return null;
}
}

private void loginLater() {
Scheduler.get().scheduleFixedDelay(new Scheduler.RepeatingCommand() {
@Override
public boolean execute() {
login();
return false;
}
}, 100);
}

private void login() {
getWidget().submit();
}

private void addSubmitButtonClickHandler(Connector buttonConnector) {
if (buttonConnector instanceof ButtonConnector) {
addSubmitButtonClickHandler(((ButtonConnector) buttonConnector)
.getWidget());
} else if (buttonConnector instanceof NativeButtonConnector) {
addSubmitButtonClickHandler(((NativeButtonConnector) buttonConnector)
.getWidget());
}
}

private void addSubmitButtonClickHandler(FocusWidget button) {
button.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
login();
}
});
}

private void valuesChanged() {
if (passwordField != null) {
passwordField.valueChange(true);
}
if (userField != null) {
userField.valueChange(true);
}
}

private class SubmitKeyHandler implements KeyDownHandler {

private int previousKeyCode;

@Override
public void onKeyDown(KeyDownEvent event) {
int keyCode = event.getNativeKeyCode();
if (keyCode == KeyCodes.KEY_ENTER) {
if (isInAutoComplete()) {
previousKeyCode = keyCode;
} else {
loginLater();
}
} else {
previousKeyCode = keyCode;
}
}

private boolean isInAutoComplete() {
switch (previousKeyCode) {
case KeyCodes.KEY_PAGEUP:
case KeyCodes.KEY_PAGEDOWN:
case KeyCodes.KEY_UP:
case KeyCodes.KEY_DOWN:
return true;
default:
return false;
}
}
}
}
@@ -0,0 +1,27 @@
/*
* Copyright 2000-2014 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.vaadin.client.ui.loginform;

import com.google.gwt.user.client.ui.FormPanel;

public class VLoginForm extends FormPanel {

public VLoginForm() {
getElement().setId("loginForm");
setMethod(METHOD_POST);
}
}

0 comments on commit c0babd7

Please sign in to comment.