stop event propagation for Vaadin Flow buttons - vaadin

I have a Vaadin 23 application where I have a Div element with a Button in it (among other content) and a click listener added to it, as well as to the button:
Button button = new Button();
button.addClickListener(event -> { ... });
Div wrapper = new Div(button);
wrapper.addClickListener(event -> { ... });
The problem is that both listeners are firing when the button is clicked.
How can I make sure that only the button click listener is fired in that case? I want the click listener on the Div to fire when I click anywhere inside the Div except on the button.
Alternatively, if I could detect in the click listener on the Div that the click was actually on the button, I could simply ignore it. But I don't see how to do that either.
This is a port from a Vaadin 8 app where it was working correctly. The wrapper Div in that case was a CssLayout with a LayoutClickListener added.

There is a highly voted issue for adding this feature to Vaadin Flow. Different workarounds are also described in the issue.
In your case, I think the workaround would look like this:
button.getElement().addEventListener("click", ignore -> {}).addEventData("event.stopPropagation()");

Related

Vaadin flow grid prevent itemClickListener() to fire on click on buttons in ComponentColumns

With vaadin 23.1.x you can set a itemClickHandler when a user click on a item/row in the grid with myGrid.addItemClickListener(..)
This works fine.
But if you have a component column, with a button in it, then the ClickEvent of the Button is fired and also the itemClickListener of the grid row.
Is there a way to prevent the button click from also triggering the itemClickListener?
You must use this method to add the listener them you can
myGrid.getElement().addEventListener("item-click",
event -> ...)
.addEventData("event.stopPropagation()");
I'm not 100% sure if the even is item-click or just click.

Vaadin Accordion with Button in summary

In my Vaadin 14 app, I want to add an Accordion component that has several components in its summary (which is always displayed), among which a Button. Clicking in the summary normally toggles the display of the AccordionPanel content. How can I prevent the AccordionPanel to collapse/expand when the button in the summary is clicked?
Objects are created simply as follows:
Accordion accordion = new Accordion();
MyPanel panel = new MyPanel();
accordion.add(panel);
with MyPanel constructor simply calling setSummary() with a layout containing the button.
I found the answer in this thread on the forum.
It turns out you can prevent the propagation of the button click with this hack:
button.getElement().addEventListener("click", click -> {
//do nothing
}).addEventData("event.stopPropagation()");
This seems like a core functionality that the framework should provide out of the box, but this ticket is still open.
Adding this to your view:
UI.getCurrent().getPage()
.executeJs("Array.from(document.getElementsByTagName('vaadin-accordion-panel')).forEach(element=>element.shadowRoot.querySelector(\"div[role=button]\").replaceWith(element.shadowRoot.querySelector(\"div[role=button]\").cloneNode(true)))");
will disable all clicks on all the accordions toggle and accordion summary. However you will need to include a button or trigger for opening and closing the accordion. I don't know if this is what you want?

Simulate a click() programmatically on a Div

I'm trying to fire an event programmatically. My problem is that I have two SVG on two DIVs and I want to be able to change the border of the DIV I have clicked. To do that I thought to pass the DIV inside my classes and then trigger a click on it once I click on anything. (if there is a better way, please tell me)
I have the following code:
div = querySelector(divName);
svgElement = new svg.SvgSvgElement();
div.append(svgElement);
div.onClick.listen(_setBorders(1));
later I pass the svgElement to another class
ell.show(svgElement);
where show is
show(svg.SvgElement element) {
if (element.parent is DivElement){
_parentDiv= element.parent as DivElement;
element.children.add(_group);
}
}
_parentDiv is of course a DivElement, which I use for an internal onClick()
_onClick(MouseEvent e) {
window.console.info("onClick Ell");
_parentDiv.click();
}
I'm expecting to see the _setBorders(1); I defined with the main div, but it doesn't work. The weird thing is that when I check with the debugger set to the _parentDiv.click() I see that _parentDiv has the event correctly set.
I suppose click() doesn't work as I expected. Any Idea?
If you want that _setBorders(1) is called on click events you have to use :
div.onClick.listen((_) => _setBorders(1));

How do you use the showhide effect found in the Dart Widget Package

I am trying to use the ShowHide effect that comes with Kevin Moore's widget package here:
http://dart-lang.github.io/widget.dart/#showhide
Not sure how to use this. Anyone got an example I can look at ?
Basically all I want is for a dropdown to show with one of those effects if a certain event happens.
Your tips appreciated.
Thanks.
You need to add a listener for an event to an element in the DOM, and then use ShowHide.toggle(element, effect) to trigger an effect. Here is an example which listens for a click on a button, and toggles FadeEffect on an image each time it is pressed:
var button = query("#fadeButton")
..onClick.listen((event) {
ShowHide.toggle(query("#fadeImage"), effect: new FadeEffect());
});
If you wanted to fade in/out a dropdown when you click on a menu bar, then substitute "fadeButton" for the menu which listens for clicks, and "fadeImage" with the dropdown element.
Also, any other effect can be substituted for FadeEffect, such as DoorEffect, ScaleEffect, ShrinkEffect, etc.

Jquery Mobile 1.3 slider conflicts with panel

I am trying to set up a JQuery Mobile 1.3 site that uses a panel and a slider.
Problem is, that using the slider triggers the panel, which opens on a "swiperight" event, as I am moving the slider to the right. The slider will be for pagination, the panel for a menu.
Code here:
http://jsfiddle.net/kMARn/1/
Move the slider to the right and the panel will open.
I have tried using the .not() selector for the panel to not react on the slider:
$(document).not("#slider").on("swiperight", function(event, ui) {
$("#myPanel").panel("open");
});
But it won't work, the panel opens when i move the slider to the right. Tried a bunch of variants too, but I'm lost...
Any ideas?
Thanks!
A bit late to the party, but you can disable swipe-to-close by setting the data-swipe-close attribute to "false" on the panel div.
http://jquerymobile.com/demos/1.3.0-beta.1/docs/panels/options.html
In my case I used this simple code, without data-swipe-close = "false" in panel.
Keeping panel close with swipe right, outside of the slider.
$('#panel').find('#slider')
.on('slidestop',function(e,ui) {
var value = e.target.value;
//...operations with slider value...
})
.parent().on('swiperight',function(e,ui) {
e.stopPropagation(); //block panel close
})
From the 1.3.0b1 Docs for Swipe:
"Triggers when a horizontal drag of 30px or more (and less than 75px
vertically) occurs within 1 second duration"
This applies to and can be configured for swiperight too. You can make the slider small in length and this would ensure that both the slider event stop and the swipe are not triggered at the same time, yet that may not be practical for all scenarios.
What might be better is to bind the swipe right to a DIV or section of the page. By this, I mean if you have a 75 px div box on the left hand side of the display, and when a swipe event occurred within that div, it could trigger the menu.
I feel the logic here might be better controlled by a button, much like used in the Facebook App to display there slide out menu. In the Dolphin browser on Android, this type of event also triggers a bookmark menu, so if a page has a swiperight event and trigger it, I sometimes get both the event and the bookmark menu from the App. Annoying!
I did fork your jsfiddle and will play with it more (http://jsfiddle.net/Twisty/Hg2pw/). FYI, they have JQM 1.3.0b1 in their available frameworks so you don't have to link it in your HTML. If I find some more info, I will comment here.
The following solution is more a workaround. It should be relatively reliable though.
$(document).ready( function () {
var menu_available = true;
$(document).on("swiperight", function(event, ui) {
if (menu_available) $("#myPanel").panel("open");
});
$("#slider").on("slidestop", function( event, ui ) {
menu_available = false;
window.setTimeout(function() {menu_available= true;},250);
});
});
The variable menu_available is false for a 250 milliseconds right after the slide stops. The window.setTimeout block will reset the variable so that the menu is available again.
This is a stupid workaround, but jQuerys function event.stopEventPropagation(), which IMHO would be the correct way to go, didn't work.

Resources