diff options
author | Teemu Suo-Anttila <teemusa@vaadin.com> | 2015-12-11 14:39:56 +0200 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2015-12-15 09:32:31 +0000 |
commit | 05356b0cb628e7f7604991adf82387c746870104 (patch) | |
tree | d34b4d95e6ffda571b8808f38e64863ab40ec671 | |
parent | dace5ab66322c226b2cce79848a9c0518740f020 (diff) | |
download | vaadin-framework-05356b0cb628e7f7604991adf82387c746870104.tar.gz vaadin-framework-05356b0cb628e7f7604991adf82387c746870104.zip |
Add a method to clear selected text on context click (#19367)
Change-Id: Id7ec7013bcff446cdf3ce0f2088cb20b0708a56b
7 files changed, 158 insertions, 3 deletions
diff --git a/client/src/com/vaadin/client/WidgetUtil.java b/client/src/com/vaadin/client/WidgetUtil.java index 631d28c26c..8161ceff19 100644 --- a/client/src/com/vaadin/client/WidgetUtil.java +++ b/client/src/com/vaadin/client/WidgetUtil.java @@ -1333,6 +1333,18 @@ public class WidgetUtil { }-*/; /** + * JavaScript hack to clear text selection in various browsers. + * + * @since 7.6 + */ + public native static void clearTextSelection() + /*-{ + if ($wnd.getSelection) { + $wnd.getSelection().removeAllRanges(); + } + }-*/; + + /** * The allowed value inaccuracy when comparing two double-typed pixel * values. * <p> diff --git a/client/src/com/vaadin/client/connectors/GridConnector.java b/client/src/com/vaadin/client/connectors/GridConnector.java index 336469139c..0b485a8e5a 100644 --- a/client/src/com/vaadin/client/connectors/GridConnector.java +++ b/client/src/com/vaadin/client/connectors/GridConnector.java @@ -43,6 +43,7 @@ import com.vaadin.client.DeferredWorker; import com.vaadin.client.MouseEventDetailsBuilder; import com.vaadin.client.ServerConnector; import com.vaadin.client.TooltipInfo; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.communication.StateChangeEvent; import com.vaadin.client.communication.StateChangeEvent.StateChangeHandler; import com.vaadin.client.connectors.RpcDataSourceConnector.DetailsListener; @@ -1197,6 +1198,8 @@ public class GridConnector extends AbstractHasComponentsConnector implements getRpcProxy(GridServerRpc.class).contextClick(eventCell.getRowIndex(), rowKey, columnId, section, details); + + WidgetUtil.clearTextSelection(); } /** diff --git a/client/src/com/vaadin/client/ui/AbstractComponentConnector.java b/client/src/com/vaadin/client/ui/AbstractComponentConnector.java index 1b3f5de3b1..f1ed0caf10 100644 --- a/client/src/com/vaadin/client/ui/AbstractComponentConnector.java +++ b/client/src/com/vaadin/client/ui/AbstractComponentConnector.java @@ -45,6 +45,7 @@ import com.vaadin.client.TooltipInfo; import com.vaadin.client.UIDL; import com.vaadin.client.Util; import com.vaadin.client.VConsole; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.annotations.OnStateChange; import com.vaadin.client.communication.StateChangeEvent; import com.vaadin.client.metadata.NoDataException; @@ -132,7 +133,6 @@ public abstract class AbstractComponentConnector extends AbstractConnector // remove the touch handlers as well unregisterTouchHandlers(); - } } @@ -324,6 +324,8 @@ public abstract class AbstractComponentConnector extends AbstractConnector // The default context click implementation only provides the mouse // coordinates relative to root element of widget. getRpcProxy(ContextClickRpc.class).contextClick(details); + + WidgetUtil.clearTextSelection(); } /** diff --git a/client/src/com/vaadin/client/ui/table/TableConnector.java b/client/src/com/vaadin/client/ui/table/TableConnector.java index 7e7995b5e1..cdd9397976 100644 --- a/client/src/com/vaadin/client/ui/table/TableConnector.java +++ b/client/src/com/vaadin/client/ui/table/TableConnector.java @@ -28,11 +28,11 @@ import com.google.gwt.event.shared.HandlerRegistration; import com.google.gwt.user.client.ui.Widget; import com.vaadin.client.ApplicationConnection; import com.vaadin.client.BrowserInfo; -import com.vaadin.client.HasChildMeasurementHintConnector; import com.vaadin.client.ComponentConnector; import com.vaadin.client.ConnectorHierarchyChangeEvent; import com.vaadin.client.ConnectorHierarchyChangeEvent.ConnectorHierarchyChangeHandler; import com.vaadin.client.DirectionalManagedLayout; +import com.vaadin.client.HasChildMeasurementHintConnector; import com.vaadin.client.HasComponentsConnector; import com.vaadin.client.Paintable; import com.vaadin.client.ServerConnector; @@ -56,7 +56,8 @@ import com.vaadin.shared.ui.table.TableState; @Connect(com.vaadin.ui.Table.class) public class TableConnector extends AbstractFieldConnector implements HasComponentsConnector, ConnectorHierarchyChangeHandler, Paintable, - DirectionalManagedLayout, PostLayoutListener, HasChildMeasurementHintConnector { + DirectionalManagedLayout, PostLayoutListener, + HasChildMeasurementHintConnector { private List<ComponentConnector> childComponents; @@ -120,6 +121,8 @@ public class TableConnector extends AbstractFieldConnector implements getRpcProxy(TableServerRpc.class).contextClick(rowKey, colKey, section, details); + + WidgetUtil.clearTextSelection(); } protected VScrollTableRow getScrollTableRow(Element e) { diff --git a/client/src/com/vaadin/client/ui/tree/TreeConnector.java b/client/src/com/vaadin/client/ui/tree/TreeConnector.java index 6fec6b9f38..f49f44e802 100644 --- a/client/src/com/vaadin/client/ui/tree/TreeConnector.java +++ b/client/src/com/vaadin/client/ui/tree/TreeConnector.java @@ -394,5 +394,7 @@ public class TreeConnector extends AbstractComponentConnector implements } getRpcProxy(TreeServerRpc.class).contextClick(key, details); + + WidgetUtil.clearTextSelection(); } } diff --git a/uitest/src/com/vaadin/tests/contextclick/BrowserContextMenuInSubComponent.java b/uitest/src/com/vaadin/tests/contextclick/BrowserContextMenuInSubComponent.java new file mode 100644 index 0000000000..d3edf0ba43 --- /dev/null +++ b/uitest/src/com/vaadin/tests/contextclick/BrowserContextMenuInSubComponent.java @@ -0,0 +1,84 @@ +/* + * 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.tests.contextclick; + +import com.vaadin.annotations.Widgetset; +import com.vaadin.event.ContextClickEvent; +import com.vaadin.event.ContextClickEvent.ContextClickListener; +import com.vaadin.server.AbstractExtension; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.tests.widgetset.TestingWidgetSet; +import com.vaadin.ui.AbstractComponent; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.ui.Notification; +import com.vaadin.ui.Panel; +import com.vaadin.ui.TextArea; +import com.vaadin.ui.VerticalLayout; + +@Widgetset(TestingWidgetSet.NAME) +public class BrowserContextMenuInSubComponent extends AbstractTestUI { + + @Override + protected void setup(VaadinRequest request) { + Panel panel = new Panel(); + + VerticalLayout layout = new VerticalLayout(); + final TextArea textArea = new TextArea(); + // Make TextArea show regular context menu instead of firing the + // server-side event. + BrowserContextMenuExtension.extend(textArea); + final Button button = new Button("Submit", new Button.ClickListener() { + + @Override + public void buttonClick(ClickEvent event) { + Notification.show(textArea.getValue()); + } + }); + + layout.addComponent(textArea); + layout.addComponent(button); + + panel.setContent(layout); + + panel.addContextClickListener(new ContextClickListener() { + + @Override + public void contextClick(ContextClickEvent event) { + button.click(); + } + }); + + addComponent(panel); + } + + /** + * A simple extension for making extended component stop propagation of the + * context click events, so the browser will handle the context click and + * show its own context menu. + */ + public static class BrowserContextMenuExtension extends AbstractExtension { + private BrowserContextMenuExtension(AbstractComponent c) { + super(c); + } + + public static BrowserContextMenuExtension extend(AbstractComponent c) { + return new BrowserContextMenuExtension(c); + } + } + +} diff --git a/uitest/src/com/vaadin/tests/widgetset/client/extension/BrowserContextMenuExtensionConnector.java b/uitest/src/com/vaadin/tests/widgetset/client/extension/BrowserContextMenuExtensionConnector.java new file mode 100644 index 0000000000..9b05ff35d8 --- /dev/null +++ b/uitest/src/com/vaadin/tests/widgetset/client/extension/BrowserContextMenuExtensionConnector.java @@ -0,0 +1,49 @@ +/* + * 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.tests.widgetset.client.extension; + +import com.google.gwt.event.dom.client.ContextMenuEvent; +import com.google.gwt.event.dom.client.ContextMenuHandler; +import com.vaadin.client.ServerConnector; +import com.vaadin.client.extensions.AbstractExtensionConnector; +import com.vaadin.client.ui.AbstractComponentConnector; +import com.vaadin.shared.ui.Connect; +import com.vaadin.tests.contextclick.BrowserContextMenuInSubComponent.BrowserContextMenuExtension; + +/** + * Client-side connector of the {@link BrowserContextMenuExtension}. + */ +@Connect(BrowserContextMenuExtension.class) +public class BrowserContextMenuExtensionConnector extends + AbstractExtensionConnector { + + @Override + protected void extend(ServerConnector target) { + getParent().getWidget().addDomHandler(new ContextMenuHandler() { + + @Override + public void onContextMenu(ContextMenuEvent event) { + // Stop context click events from propagating. + event.stopPropagation(); + } + }, ContextMenuEvent.getType()); + } + + @Override + public AbstractComponentConnector getParent() { + return (AbstractComponentConnector) super.getParent(); + } +} |