diff options
author | Matti Hosio <mhosio@vaadin.com> | 2014-12-08 10:06:17 +0200 |
---|---|---|
committer | Mika Murtojärvi <mika@vaadin.com> | 2014-12-08 09:15:21 +0000 |
commit | bd78a4c8b2eb84cbba3bc686bcb40b19fd847640 (patch) | |
tree | 65fa99b11b65c7a5f8a0c4b29c21ae0f64d58a55 /server | |
parent | 117bf2b791b70d4e85916acf5e43a5c3b2ba66ca (diff) | |
download | vaadin-framework-bd78a4c8b2eb84cbba3bc686bcb40b19fd847640.tar.gz vaadin-framework-bd78a4c8b2eb84cbba3bc686bcb40b19fd847640.zip |
Declarative support for Responsive (#7749)
Change-Id: I5b5b4ec966d4206e3fe4d31adf6674cd247dfbd2
Diffstat (limited to 'server')
3 files changed, 90 insertions, 1 deletions
diff --git a/server/src/com/vaadin/ui/AbstractComponent.java b/server/src/com/vaadin/ui/AbstractComponent.java index 8ee3f722ab..8349dcfa25 100644 --- a/server/src/com/vaadin/ui/AbstractComponent.java +++ b/server/src/com/vaadin/ui/AbstractComponent.java @@ -43,7 +43,9 @@ import com.vaadin.server.AbstractErrorMessage.ContentMode; import com.vaadin.server.ComponentSizeValidator; import com.vaadin.server.ErrorMessage; import com.vaadin.server.ErrorMessage.ErrorLevel; +import com.vaadin.server.Extension; import com.vaadin.server.Resource; +import com.vaadin.server.Responsive; import com.vaadin.server.Sizeable; import com.vaadin.server.UserError; import com.vaadin.server.VaadinSession; @@ -938,6 +940,9 @@ public abstract class AbstractComponent extends AbstractClientConnector } else { setComponentError(def.getComponentError()); } + // handle responsive + setResponsive(attr.hasKey("responsive") + && !attr.get("responsive").equalsIgnoreCase("false")); // check for unsupported attributes Set<String> supported = new HashSet<String>(); supported.addAll(getDefaultAttributes()); @@ -952,6 +957,46 @@ public abstract class AbstractComponent extends AbstractClientConnector } /** + * Toggles responsiveness of this component. + * + * @since 7.4 + * @param responsive + * boolean enables responsiveness, false disables + */ + private void setResponsive(boolean responsive) { + if (responsive) { + // make responsive if necessary + if (!isResponsive()) { + Responsive.makeResponsive(this); + } + } else { + // remove responsive extensions + List<Extension> extensions = new ArrayList<Extension>( + getExtensions()); + for (Extension e : extensions) { + if (e instanceof Responsive) { + removeExtension(e); + } + } + } + } + + /** + * Returns true if the component is responsive + * + * @since 7.4 + * @return true if the component is responsive + */ + private boolean isResponsive() { + for (Extension e : getExtensions()) { + if (e instanceof Responsive) { + return true; + } + } + return false; + } + + /** * Synchronizes the size of this component from the given design attributes. * If the attributes do not contain relevant size information, defaults is * consulted. @@ -1117,7 +1162,7 @@ public abstract class AbstractComponent extends AbstractClientConnector private static final String[] customAttributes = new String[] { "width", "height", "debug-id", "error", "width-auto", "height-auto", - "width-full", "height-full", "size-auto", "size-full" }; + "width-full", "height-full", "size-auto", "size-full", "responsive" }; /* * (non-Javadoc) @@ -1147,6 +1192,10 @@ public abstract class AbstractComponent extends AbstractClientConnector if (!SharedUtil.equals(errorMsg, defErrorMsg)) { attr.put("error", errorMsg); } + // handle responsive + if (isResponsive()) { + attr.put("responsive", ""); + } } /* diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractcomponent/TestSynchronizeFromDesign.java b/server/tests/src/com/vaadin/tests/server/component/abstractcomponent/TestSynchronizeFromDesign.java index e18f52195d..d32155fd68 100644 --- a/server/tests/src/com/vaadin/tests/server/component/abstractcomponent/TestSynchronizeFromDesign.java +++ b/server/tests/src/com/vaadin/tests/server/component/abstractcomponent/TestSynchronizeFromDesign.java @@ -23,6 +23,7 @@ import org.jsoup.parser.Tag; import com.vaadin.server.ExternalResource; import com.vaadin.server.FileResource; +import com.vaadin.server.Responsive; import com.vaadin.server.ThemeResource; import com.vaadin.ui.AbstractComponent; import com.vaadin.ui.Label; @@ -178,7 +179,34 @@ public class TestSynchronizeFromDesign extends TestCase { assertEquals(12, component.getHeight(), 0.1f); assertEquals(com.vaadin.server.Sizeable.Unit.PIXELS, component.getHeightUnits()); + } + + public void testSynchronizeNotResponsive() { + AbstractComponent component = getComponent(); + Responsive.makeResponsive(component); + Element design = createDesign("responsive", "false"); + component.synchronizeFromDesign(design, ctx); + assertEquals("Component should not have extensions", 0, component + .getExtensions().size()); + } + public void testSynchronizeResponsive() { + AbstractComponent component = getComponent(); + Element design = createDesign("responsive", ""); + component.synchronizeFromDesign(design, ctx); + assertEquals("Component should have one extension", 1, component + .getExtensions().size()); + assertTrue("Extension should be responsive", component.getExtensions() + .iterator().next() instanceof Responsive); + } + + public void testSynchronizeAlreadyResponsive() { + AbstractComponent component = getComponent(); + Responsive.makeResponsive(component); + Element design = createDesign("responsive", ""); + component.synchronizeFromDesign(design, ctx); + assertEquals("Component should have only one extension", 1, component + .getExtensions().size()); } private AbstractComponent getComponent() { diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractcomponent/TestSynchronizeToDesign.java b/server/tests/src/com/vaadin/tests/server/component/abstractcomponent/TestSynchronizeToDesign.java index 408176f4b3..f0eb05f364 100644 --- a/server/tests/src/com/vaadin/tests/server/component/abstractcomponent/TestSynchronizeToDesign.java +++ b/server/tests/src/com/vaadin/tests/server/component/abstractcomponent/TestSynchronizeToDesign.java @@ -28,6 +28,7 @@ import com.vaadin.server.AbstractErrorMessage.ContentMode; import com.vaadin.server.ErrorMessage.ErrorLevel; import com.vaadin.server.ExternalResource; import com.vaadin.server.FileResource; +import com.vaadin.server.Responsive; import com.vaadin.server.ThemeResource; import com.vaadin.server.UserError; import com.vaadin.ui.AbstractComponent; @@ -229,6 +230,17 @@ public class TestSynchronizeToDesign extends TestCase { assertEquals("20px", design.attr("height")); } + public void testSynchronizeResponsive() { + Element design = createDesign(); + AbstractComponent component = getComponent(); + Responsive.makeResponsive(component); + component.synchronizeToDesign(design, ctx); + assertTrue("Design attributes should have key 'responsive'", design + .attributes().hasKey("responsive")); + assertFalse("Responsive attribute should not be 'false'", + design.attr("responsive").equalsIgnoreCase("false")); + } + private AbstractComponent getComponent() { return new Button(); } |