Skip to content

Commit

Permalink
fix #936 datetime box/picker enhancements, more delayed actions confi…
Browse files Browse the repository at this point in the history
…gurations
  • Loading branch information
vegegoku committed Jul 14, 2024
1 parent c8aefa1 commit fbcbb51
Show file tree
Hide file tree
Showing 14 changed files with 211 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright © 2019 Dominokit
*
* 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 org.dominokit.domino.ui.config;

public interface DelayedActionConfig extends ComponentConfig {

/**
* Use to globally configure the delayed actions delay.
*
* @return the delay in milliseconds, default to 200ms
*/
default int getDelayedExecutionDefaultDelay() {
return 200;
}

default int getDefaultSelectableBoxTypeAheadDelay() {
return 1000;
}

default int getSuggestBoxTypeAheadDelay() {
return getDefaultSelectableBoxTypeAheadDelay();
}

default int getSelectBoxTypeAheadDelay() {
return getDefaultSelectableBoxTypeAheadDelay();
}

default int getDefaultNotificationDuration() {
return 4000;
}

default int getDateBoxDefaultInputParseDelay() {
return getDelayedExecutionDefaultDelay();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* Implementations of this interface can be used to configure defaults for {@link
* org.dominokit.domino.ui.search.SearchBox} component
*/
public interface SearchConfig extends ComponentConfig {
public interface SearchConfig extends DelayedActionConfig {

/**
* Use this method to define the default auto search delay for SearchBox in milliseconds
Expand All @@ -29,7 +29,7 @@ public interface SearchConfig extends ComponentConfig {
* @return an integer delay in milliseconds
*/
default int getAutoSearchDelay() {
return 200;
return getDelayedExecutionDefaultDelay();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ public interface UIConfig
UploadConfig,
StepperConfig,
CalendarConfig,
TimePickerConfig {}
TimePickerConfig,
DelayedActionConfig {}
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,10 @@ public Calendar(Date date, DateTimeFormatInfo dateTimeFormatInfo, CalendarInitCo
evt.stopPropagation();
CalendarCustomEvents.UpdateDateEventData dateData =
CalendarCustomEvents.UpdateDateEventData.of((CustomEvent<?>) evt);
Date updatedDate = new Date(dateData.getTimestamp());
onDateViewUpdate(updatedDate);
this.date = new Date(dateData.getTimestamp());
onDateViewUpdate(this.date);
calendarMonth.show();
yearMonthPicker.hide();
this.date = updatedDate;
});

this.root.addEventListener(
Expand All @@ -148,9 +147,8 @@ public Calendar(Date date, DateTimeFormatInfo dateTimeFormatInfo, CalendarInitCo
CalendarCustomEvents.UpdateDateEventData dateData =
CalendarCustomEvents.UpdateDateEventData.of((CustomEvent<?>) evt);
Date oldDate = this.date;
Date updatedDate = new Date(dateData.getTimestamp());
this.date = updatedDate;
onDateSelectionChanged(updatedDate);
this.date = new Date(dateData.getTimestamp());
onDateSelectionChanged(this.date);
calendarMonth.show();
yearMonthPicker.hide();
triggerChangeListeners(oldDate, this.date);
Expand Down Expand Up @@ -313,6 +311,17 @@ public Calendar setDate(Date date) {
return this;
}

/**
* Resets the calendar view to the month view if it is on month/year selection view
*
* @return same Calendar instance
*/
public Calendar resetView() {
calendarMonth.show();
yearMonthPicker.hide();
return this;
}

/** @return the {@link DateTimeFormatInfo} used by this calendar instance */
@Override
public DateTimeFormatInfo getDateTimeFormatInfo() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ public DateBox(
.setOpenOnClick(this.openOnClick)
.setPosition(BEST_MIDDLE_DOWN_UP)
.appendChild(this.calendar)
.addCloseListener(component -> calendar.resetView())
.addOnRemoveListener(
popover -> {
withOpenOnFocusToggleListeners(false, field -> focus());
Expand Down Expand Up @@ -183,13 +184,37 @@ public DateBox(
clearInvalid();
} catch (IllegalArgumentException ignored) {
if (parseStrict) {
invalidate("Unable to parse date value " + value);
invalidate(getLabels().calendarInvalidDateFormat(value));
}
DomGlobal.console.warn("Unable to parse date value " + value);
}
}
});

getInputElement()
.addEventListener(
"input",
evt -> {
DelayedExecution.execute(
() -> {
String value = getStringValue();
if (value.isEmpty()) {
clear();
} else {
try {
withValue(getFormattedValue(value));
clearInvalid();
} catch (IllegalArgumentException ignored) {
if (parseStrict) {
invalidate(getLabels().calendarInvalidDateFormat(value));
}
DomGlobal.console.warn("Unable to parse date value " + value);
}
}
},
config().getUIConfig().getDateBoxDefaultInputParseDelay());
});

appendChild(
PrimaryAddOn.of(
getConfig()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public TimeBox(Date date, DateTimeFormatInfo dateTimeFormatInfo) {
clearInvalid();
} catch (IllegalArgumentException ignored) {
if (parseStrict) {
invalidate("Unable to parse date value " + value);
invalidate(getLabels().timePickerInvalidTimeFormat(value));
}
DomGlobal.console.warn("Unable to parse date value " + value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public abstract class AbstractSelect<
private SpanElement placeHolderElement;
private InputElement inputElement;
private InputElement typingElement;
private int typeAheadDelay = -1;

/**
* Default constructor which initializes the underlying structures, sets up event listeners, and
Expand All @@ -103,7 +104,7 @@ public AbstractSelect() {
.setTabIndex(-1)
.onKeyPress(keyEvents -> keyEvents.alphanumeric(Event::stopPropagation)));

DelayedTextInput.create(typingElement, 1000)
DelayedTextInput.create(typingElement, getTypeAheadDelay())
.setDelayedAction(
() -> {
optionsMenu
Expand Down Expand Up @@ -209,6 +210,17 @@ public AbstractSelect() {
})));
}

private int getTypeAheadDelay() {
return typeAheadDelay > 0
? typeAheadDelay
: config().getUIConfig().getSelectBoxTypeAheadDelay();
}

public C setTypeAheadDelay(int delay) {
this.typeAheadDelay = delay;
return (C) this;
}

/**
* Opens the options menu allowing user to select an option, unless the select is read-only or
* disabled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Optional;
import jsinterop.base.Js;
import org.dominokit.domino.ui.IsElement;
import org.dominokit.domino.ui.config.DelayedActionConfig;
import org.dominokit.domino.ui.elements.DivElement;
import org.dominokit.domino.ui.elements.InputElement;
import org.dominokit.domino.ui.forms.AbstractFormElement;
Expand Down Expand Up @@ -110,7 +111,7 @@ public abstract class AbstractSuggestBox<
private boolean autoSelect = true;

/** The type-ahead delay in milliseconds. */
private int typeAheadDelay = 1000;
private int typeAheadDelay = -1;

/**
* Creates an instance of {@code AbstractSuggestBox} with the specified suggestions store.
Expand Down Expand Up @@ -263,12 +264,16 @@ public C setAutoSelect(boolean autoSelect) {
}

/**
* Gets the type-ahead delay in milliseconds.
* Gets the type-ahead delay in milliseconds; this will return the value specified using {@link
* AbstractSuggestBox#setTypeAheadDelay(int)} if greater than 0 otherwise, this will return the
* value specified in {@link DelayedActionConfig#getSuggestBoxTypeAheadDelay()}.
*
* @return The type-ahead delay in milliseconds.
*/
public int getTypeAheadDelay() {
return typeAheadDelay;
return typeAheadDelay > 0
? typeAheadDelay
: config().getUIConfig().getSuggestBoxTypeAheadDelay();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,13 @@ public interface CalendarLabels extends Labels {
default String calendarInvalidDateFormat() {
return "Invalid date format";
}
/**
* Returns the localized label for the "Invalid date format" message related to calendar input.
*
* @param value the current invalid value
* @return The localized label for "Invalid date format".
*/
default String calendarInvalidDateFormat(String value) {
return calendarInvalidDateFormat() + " : " + value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,14 @@ default String ampm() {
default String timePickerInvalidTimeFormat() {
return "Invalid time format";
}

/**
* Gets the error message for an invalid time format in the time picker.
*
* @param value the current invalid value
* @return The error message for an invalid time format.
*/
default String timePickerInvalidTimeFormat(String value) {
return timePickerInvalidTimeFormat() + " : " + value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class Notification extends BaseDominoElement<HTMLDivElement, Notification
private final LazyChild<SpanElement> messageSpan;
private final LazyChild<RemoveButton> closeButton;

private int duration = 4000;
private int duration = -1;
private Transition inTransition = Transition.FADE_IN;
private Transition outTransition = Transition.FADE_OUT;
private SwapCssClass position = SwapCssClass.of(dui_ntf_top_left);
Expand Down Expand Up @@ -277,13 +277,19 @@ public Notification expand() {
.callback(
e -> {
if (!infinite) {
close(duration);
close(getDuration());
}
})
.animate();
return this;
}

private int getDuration() {
return this.duration > 0
? this.duration
: config().getUIConfig().getDefaultNotificationDuration();
}

/** Closes the notification immediately. */
public final void close() {
close(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
public class SearchBox extends BaseDominoElement<HTMLDivElement, SearchBox>
implements HasLabels<QuickSearchLabels>, HasComponentConfig<SearchConfig> {

private int autoSearchDelay;
private int autoSearchDelay = -1;
private DivElement root;
private final TextBox textBox;
private boolean autoSearch = true;
Expand All @@ -94,7 +94,7 @@ public static SearchBox create() {
/** Constructs a new `SearchBox` instance with default settings. */
public SearchBox() {
init(this);
this.autoSearchDelay = getConfig().getAutoSearchDelay();

root = div().addCss(dui_quick_search);
searchIcon =
Icons.magnify()
Expand Down Expand Up @@ -152,7 +152,7 @@ public void run() {
autoSearchEventListener =
evt -> {
autoSearchTimer.cancel();
autoSearchTimer.schedule(autoSearchDelay);
autoSearchTimer.schedule(getAutoSearchDelay());
};

setAutoSearch(true);
Expand Down Expand Up @@ -224,7 +224,7 @@ public SearchBox setAutoSearch(boolean autoSearch) {
* @return The auto search delay.
*/
public int getAutoSearchDelay() {
return autoSearchDelay;
return this.autoSearchDelay > 0 ? this.autoSearchDelay : getConfig().getAutoSearchDelay();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.dominokit.domino.ui.utils;

import org.dominokit.domino.ui.config.DelayedActionConfig;
import org.gwtproject.timer.client.Timer;

/**
Expand All @@ -33,6 +34,16 @@
*/
public class DelayedExecution {

/**
* Executes the specified {@code delayedAction} after the specified delay defined in {@link
* DelayedActionConfig#getDelayedExecutionDefaultDelay()}.
*
* @param delayedAction The action to be executed after the delay.
*/
public static void execute(DelayedAction delayedAction) {
execute(delayedAction, DominoUIConfig.CONFIG.getUIConfig().getDelayedExecutionDefaultDelay());
}

/**
* Executes the specified {@code delayedAction} after the specified {@code delay} in milliseconds.
*
Expand Down
Loading

0 comments on commit fbcbb51

Please sign in to comment.