Skip to content

Commit

Permalink
fix #902 add scrollwheel support to carousel
Browse files Browse the repository at this point in the history
  • Loading branch information
vegegoku committed Jul 28, 2024
1 parent 5bf1745 commit 1a7800a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@
import static org.dominokit.domino.ui.utils.Domino.*;

import elemental2.dom.HTMLDivElement;
import elemental2.dom.WheelEvent;
import java.util.ArrayList;
import java.util.List;
import jsinterop.base.Js;
import org.dominokit.domino.ui.config.CarouselConfig;
import org.dominokit.domino.ui.config.HasComponentConfig;
import org.dominokit.domino.ui.elements.AnchorElement;
import org.dominokit.domino.ui.elements.DivElement;
import org.dominokit.domino.ui.elements.OListElement;
import org.dominokit.domino.ui.events.EventType;
import org.dominokit.domino.ui.icons.lib.Icons;
import org.dominokit.domino.ui.style.CssClass;
import org.dominokit.domino.ui.style.GenericCss;
Expand All @@ -39,7 +44,8 @@
*
* @see BaseDominoElement
*/
public class Carousel extends BaseDominoElement<HTMLDivElement, Carousel> {
public class Carousel extends BaseDominoElement<HTMLDivElement, Carousel>
implements HasComponentConfig<CarouselConfig> {

private final OListElement indicatorsElement;
private final DivElement slidesElement;
Expand All @@ -57,6 +63,7 @@ public class Carousel extends BaseDominoElement<HTMLDivElement, Carousel> {
private Slide targetSlide;
private int autoSlideDuration = 3000;
private boolean attached = false;
private CarouselConfig carouselConfig;

/**
* Factory method to create an empty Carousel
Expand All @@ -69,7 +76,6 @@ public static Carousel create() {

/** Creates and empty Carousel */
public Carousel() {

element =
div()
.appendChild(indicatorsElement = ol().addCss(carousel_indicators))
Expand All @@ -89,11 +95,7 @@ public Carousel() {
.appendChild(
Icons.chevron_right()
.addCss(GenericCss.dui_vertical_center, dui_font_size_12))
.addEventListener(
"click",
evt -> {
next();
}))
.addEventListener("click", evt -> next()))
.addCss(carousel);
timer =
new Timer() {
Expand All @@ -105,6 +107,21 @@ public void run() {
addAttachListener();
addDetachListener();
init(this);
addEventListener(
EventType.wheel.getName(),
evt -> {
if (getConfig().isScrollCarouselWithWheel()) {
evt.preventDefault();
WheelEvent wheelEvent = Js.uncheckedCast(evt);
if (wheelEvent.deltaY > 0) {
nextSlide();
}

if (wheelEvent.deltaY < 0) {
prevSlide();
}
}
});
}

/**
Expand Down Expand Up @@ -406,6 +423,16 @@ public Slide getActiveSlide() {
return activeSlide;
}

@Override
public CarouselConfig getOwnConfig() {
return carouselConfig;
}

public Carousel setConfig(CarouselConfig config) {
this.carouselConfig = config;
return this;
}

public enum SlideDirection {
NONE,
/** CSS class for previous indicator */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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 CarouselConfig extends ComponentConfig {
/** @return boolean, true to allow scrolling a carousel with mouse scroll wheel. */
default boolean isScrollCarouselWithWheel() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ public interface UIConfig
CalendarConfig,
TimePickerConfig,
DelayedActionConfig,
DatatableConfig {}
DatatableConfig,
CarouselConfig {}

0 comments on commit 1a7800a

Please sign in to comment.