Procházet zdrojové kódy

Refactor ComboBox pending selection handling (#19929)

Move the handling of selection when a navigation operation
pends on server reply to the connector, with a callback
that actually performs the selection.

Change-Id: I941defbf2fe85d0f4d6ed58b7e65799c35a11aa1
tags/8.0.0.alpha1
Henri Sara před 8 roky
rodič
revize
db95805c02

+ 14
- 53
client/src/main/java/com/vaadin/client/ui/VFilterSelect.java Zobrazit soubor

@@ -1146,7 +1146,6 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler,
// null is not visible on pages != 0, and not visible when
// filtering: handle separately
connector.requestFirstPage();
afterUpdateClientVariables();

suggestionPopup.hide();
return;
@@ -1195,7 +1194,6 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler,
*/
lastNewItemString = enteredItemValue;
connector.sendNewItem(enteredItemValue);
afterUpdateClientVariables();
}
} else if (item != null
&& !"".equals(lastFilter)
@@ -1480,14 +1478,6 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler,
/** For internal use only. May be removed or replaced in the future. */
public String lastFilter = "";

/** For internal use only. May be removed or replaced in the future. */
public enum Select {
NONE, FIRST, LAST
}

/** For internal use only. May be removed or replaced in the future. */
public Select selectPopupItemWhenResponseIsReceived = Select.NONE;

/**
* The current suggestion selected from the dropdown. This is one of the
* values in currentSuggestions except when filtering, in this case
@@ -1736,7 +1726,6 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler,

setWaitingForFilteringResponse(true);
connector.requestPage(filter, page);
afterUpdateClientVariables();

lastFilter = filter;
currentPage = page;
@@ -1857,7 +1846,6 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler,
if (!(newKey.equals(selectedOptionKey) || ("".equals(newKey) && selectedOptionKey == null))) {
selectedOptionKey = newKey;
connector.sendSelection(selectedOptionKey);
afterUpdateClientVariables();

// currentPage = -1; // forget the page
}
@@ -1869,7 +1857,6 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler,
selectedOptionKey = newKey;
explicitSelectedCaption = null;
connector.sendSelection(selectedOptionKey);
afterUpdateClientVariables();
}

suggestionPopup.hide();
@@ -2117,7 +2104,12 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler,
private void selectPrevPage() {
if (currentPage > 0) {
filterOptions(currentPage - 1, lastFilter);
setSelectPopupItemWhenResponseIsReceived(Select.LAST);
connector.setNavigateAfterPageReceivedCallback(new Runnable() {
@Override
public void run() {
suggestionPopup.selectLastItem();
}
});
}
}

@@ -2127,7 +2119,12 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler,
private void selectNextPage() {
if (hasNextPage()) {
filterOptions(currentPage + 1, lastFilter);
setSelectPopupItemWhenResponseIsReceived(Select.FIRST);
connector.setNavigateAfterPageReceivedCallback(new Runnable() {
@Override
public void run() {
suggestionPopup.selectFirstItem();
}
});
}
}

@@ -2311,9 +2308,7 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler,
}
addStyleDependentName("focus");

if (connector.sendFocusEvent()) {
afterUpdateClientVariables();
}
connector.sendFocusEvent();

connector.getConnection().getVTooltip()
.showAssistive(connector.getTooltipInfo(getElement()));
@@ -2378,9 +2373,7 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler,
}
removeStyleDependentName("focus");

if (connector.sendBlurEvent()) {
afterUpdateClientVariables();
}
connector.sendBlurEvent();
}

/*
@@ -2560,16 +2553,6 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler,
AriaHelper.bindCaption(tb, captionElement);
}

/*
* Anything that should be set after the client updates the server.
*/
private void afterUpdateClientVariables() {
// We need this here to be consistent with the all the calls.
// Then set your specific selection type only after
// client.updateVariable() method call.
setSelectPopupItemWhenResponseIsReceived(Select.NONE);
}

@Override
public boolean isWorkPending() {
return isWaitingForFilteringResponse()
@@ -2649,28 +2632,6 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler,
this.updateSelectionWhenReponseIsReceived = updateSelectionWhenReponseIsReceived;
}

/**
* For internal use only - this method will be removed in the future.
*
* @return enum Select indicating which item (if any) to select when a new
* page of data is received
*/
public Select getSelectPopupItemWhenResponseIsReceived() {
return selectPopupItemWhenResponseIsReceived;
}

/**
* For internal use only - this method will be removed in the future.
*
* @param selectPopupItemWhenResponseIsReceived
* enum Select indicating which item (if any) to select when a
* new page of data is received
*/
public void setSelectPopupItemWhenResponseIsReceived(
Select selectPopupItemWhenResponseIsReceived) {
this.selectPopupItemWhenResponseIsReceived = selectPopupItemWhenResponseIsReceived;
}

/**
* For internal use only - this method will be removed in the future.
*

+ 48
- 28
client/src/main/java/com/vaadin/client/ui/combobox/ComboBoxConnector.java Zobrazit soubor

@@ -54,6 +54,8 @@ public class ComboBoxConnector extends AbstractFieldConnector implements

private boolean immediate;

private Runnable pageChangeCallback;

@Override
protected void init() {
super.init();
@@ -225,16 +227,8 @@ public class ComboBoxConnector extends AbstractFieldConnector implements

getWidget().setWaitingForFilteringResponse(false);

if (!getWidget().isPopupOpenerClicked()
&& getWidget().getSelectPopupItemWhenResponseIsReceived() != VFilterSelect.Select.NONE) {

// we're paging w/ arrows
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
@Override
public void execute() {
navigateItemAfterPageChange();
}
});
if (!getWidget().isPopupOpenerClicked()) {
navigateItemAfterPageChange();
}

if (getWidget().isUpdateSelectionWhenReponseIsReceived()) {
@@ -274,17 +268,21 @@ public class ComboBoxConnector extends AbstractFieldConnector implements
* #11333
*/
private void navigateItemAfterPageChange() {
if (getWidget().getSelectPopupItemWhenResponseIsReceived() == VFilterSelect.Select.LAST) {
getWidget().suggestionPopup.selectLastItem();
} else {
getWidget().suggestionPopup.selectFirstItem();
if (pageChangeCallback != null) {
// pageChangeCallback is not reset here but after any server request
// in case you are in between two requests both changing the page
// back and forth

// we're paging w/ arrows
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
@Override
public void execute() {
if (pageChangeCallback != null) {
pageChangeCallback.run();
}
}
});
}

// If you're in between 2 requests both changing the page back and
// forth, you don't want this here, instead you need it before any
// other request.
// getWidget().selectPopupItemWhenResponseIsReceived =
// VFilterSelect.Select.NONE; // reset
}

private void performSelection(String selectedKey) {
@@ -404,6 +402,7 @@ public class ComboBoxConnector extends AbstractFieldConnector implements
*/
public void sendNewItem(String itemValue) {
rpc.createNewItem(itemValue);
afterSendRequestToServer();
}

/**
@@ -435,6 +434,7 @@ public class ComboBoxConnector extends AbstractFieldConnector implements
*/
public void requestPage(String filter, int page) {
rpc.requestPage(filter, page);
afterSendRequestToServer();
}

/**
@@ -449,6 +449,7 @@ public class ComboBoxConnector extends AbstractFieldConnector implements
*/
public void sendSelection(String selection) {
rpc.setSelectedItem(selection);
afterSendRequestToServer();
}

/**
@@ -461,15 +462,13 @@ public class ComboBoxConnector extends AbstractFieldConnector implements
* versions.
*
* @since
* @return true if an event was sent (there are registered listeners), false
* otherwise
*/
public boolean sendFocusEvent() {
public void sendFocusEvent() {
boolean registeredListeners = hasEventListener(EventId.FOCUS);
if (registeredListeners) {
focusAndBlurRpc.focus();
afterSendRequestToServer();
}
return registeredListeners;
}

/**
@@ -482,15 +481,36 @@ public class ComboBoxConnector extends AbstractFieldConnector implements
* versions.
*
* @since
* @return true if an event was sent (there are registered listeners), false
* otherwise
*/
public boolean sendBlurEvent() {
public void sendBlurEvent() {
boolean registeredListeners = hasEventListener(EventId.BLUR);
if (registeredListeners) {
focusAndBlurRpc.blur();
afterSendRequestToServer();
}
return registeredListeners;
}

/**
* Set a callback that is invoked when a page change occurs if there have
* not been intervening requests to the server. The callback is reset when
* any additional request is made to the server.
*
* @since
* @param callback
*/
public void setNavigateAfterPageReceivedCallback(Runnable callback) {
pageChangeCallback = callback;

}

/*
* Anything that should be set after the client updates the server.
*/
private void afterSendRequestToServer() {
// We need this here to be consistent with the all the calls.
// Then set your specific selection type only after
// a server request method call.
pageChangeCallback = null;
}

}

Načítá se…
Zrušit
Uložit