001package gwt.material.design.client.base.mixin; 002 003/* 004 * #%L 005 * GwtMaterial 006 * %% 007 * Copyright (C) 2015 GwtMaterialDesign 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023import com.google.gwt.dom.client.Style; 024import com.google.gwt.user.client.ui.Widget; 025import gwt.material.design.client.base.HasFlexbox; 026import gwt.material.design.client.base.helper.BrowserPrefixHelper; 027import gwt.material.design.client.constants.Display; 028import gwt.material.design.client.constants.Flex; 029import gwt.material.design.client.constants.FlexAlignContent; 030import gwt.material.design.client.constants.FlexAlignItems; 031import gwt.material.design.client.constants.FlexAlignSelf; 032import gwt.material.design.client.constants.FlexDirection; 033import gwt.material.design.client.constants.FlexJustifyContent; 034import gwt.material.design.client.constants.FlexWrap; 035 036/** 037 * Mixin for Flexbox layout 038 * 039 * @author chriswjones 040 */ 041public class FlexboxMixin<T extends Widget & HasFlexbox> extends AbstractMixin<T> implements HasFlexbox { 042 043 public FlexboxMixin(T uiObject) { 044 super(uiObject); 045 } 046 047 private Display displayValueBeforeHidden; 048 049 @Override 050 public void setGwtDisplay(Style.Display display) { 051 setDisplay(Display.parse(display)); 052 } 053 054 @Override 055 public void setDisplay(Display display) { 056 if (display == null) { 057 displayValueBeforeHidden = null; 058 uiObject.getElement().getStyle().clearDisplay(); 059 return; 060 } 061 062 if (display != Display.NONE) { 063 displayValueBeforeHidden = display; 064 } 065 066 if (display.equals(Display.FLEX)) { 067 String[] displayValues = {"-webkit-box", "-moz-box", "-ms-box", "-webkit-flex", "-moz-flex", "flex"}; 068 for (String d : displayValues) { 069 uiObject.getElement().getStyle().setProperty("display", d); 070 } 071 return; 072 } 073 074 if (display.getGwtDisplay() != null) { 075 uiObject.getElement().getStyle().setDisplay(display.getGwtDisplay()); 076 } else { 077 uiObject.getElement().getStyle().clearDisplay(); 078 } 079 } 080 081 @Override 082 public void setVisible(boolean visible) { 083 uiObject.setVisible(visible); 084 085 // setVisible(false) sets display:none, if the control is flex before hidden 086 // we need to reset to display:flex when the control is made visible again 087 if (displayValueBeforeHidden != null && visible) { 088 setDisplay(displayValueBeforeHidden); 089 } 090 } 091 092 @Override 093 public void setFlexDirection(FlexDirection flexDirection) { 094 boolean isCurrentlyVisible = uiObject.isVisible(); 095 096 if (flexDirection != null) { 097 setDisplay(Display.FLEX); 098 } 099 BrowserPrefixHelper.updateStyleProperties(uiObject.getElement(), 100 new String[]{"MsFlexDirection", "WebkitFlexDirection", "MozFlexDirection", "flexDirection"}, 101 flexDirection != null ? flexDirection.getValue() : null); 102 103 // Updating the display to Flex will set display:flex and override the visibility of the control 104 // this ensures that if you setVisible(false) it will not become visible unless calling setVisible(true) 105 if (!isCurrentlyVisible) { 106 setVisible(false); 107 } 108 } 109 110 @Override 111 public void setFlex(Flex flex) { 112 if (flex == null) { 113 setFlexGrow(null); 114 setFlexShrink(null); 115 setFlexBasis(null); 116 return; 117 } 118 119 setFlexGrow(flex.getGrow()); 120 setFlexShrink(flex.getShrink()); 121 setFlexBasis(flex.getBasis()); 122 } 123 124 @Override 125 public void setFlexGrow(Integer flexGrow) { 126 BrowserPrefixHelper.updateStyleProperties(uiObject.getElement(), 127 new String[]{"MsFlexGrow", "WebkitFlexGrow", "MozFlexGrow", "flexGrow"}, 128 flexGrow != null ? flexGrow.toString() : null); 129 } 130 131 @Override 132 public void setFlexShrink(Integer flexShrink) { 133 BrowserPrefixHelper.updateStyleProperties(uiObject.getElement(), 134 new String[]{"MsFlexShrink", "WebkitFlexShrink", "MozFlexShrink", "flexShrink"}, 135 flexShrink != null ? flexShrink.toString() : null); 136 } 137 138 @Override 139 public void setFlexBasis(String flexBasis) { 140 BrowserPrefixHelper.updateStyleProperties(uiObject.getElement(), 141 new String[]{"MsFlexBasis", "WebkitFlexBasis", "MozFlexBasis", "flexBasis"}, flexBasis); 142 } 143 144 @Override 145 public void setFlexOrder(Integer flexOrder) { 146 BrowserPrefixHelper.updateStyleProperties(uiObject.getElement(), 147 new String[]{"MsFlexOrder", "WebkitOrder", "MozFlexOrder", "order"}, 148 flexOrder != null ? flexOrder.toString() : null); 149 } 150 151 @Override 152 public void setFlexWrap(FlexWrap flexWrap) { 153 BrowserPrefixHelper.updateStyleProperties(uiObject.getElement(), 154 new String[]{"MsFlexWrap", "WebkitFlexWrap", "MozFlexWrap", "flexWrap"}, 155 flexWrap != null ? flexWrap.getValue() : null); 156 } 157 158 @Override 159 public void setFlexAlignContent(FlexAlignContent flexAlignContent) { 160 BrowserPrefixHelper.updateStyleProperties(uiObject.getElement(), 161 "MsFlexLinePack", new String[]{"WebkitAlignContent", "MozFlexAlignContent", "alignContent"}, flexAlignContent); 162 } 163 164 @Override 165 public void setFlexAlignSelf(FlexAlignSelf flexAlignSelf) { 166 BrowserPrefixHelper.updateStyleProperties(uiObject.getElement(), 167 "MsFlexItemAlign", new String[]{"WebkitAlignSelf", "MozFlexItemAlign", "alignSelf"}, flexAlignSelf); 168 } 169 170 @Override 171 public void setFlexAlignItems(FlexAlignItems flexAlignItems) { 172 BrowserPrefixHelper.updateStyleProperties(uiObject.getElement(), 173 "MsFlexAlign", new String[]{"WebkitAlignItems", "MozFlexAlign", "alignItems"}, flexAlignItems); 174 } 175 176 @Override 177 public void setFlexJustifyContent(FlexJustifyContent flexJustifyContent) { 178 BrowserPrefixHelper.updateStyleProperties(uiObject.getElement(), 179 "MsFlexPack", new String[]{"WebkitJustifyContent", "MozJustifyContent", "justifyContent"}, flexJustifyContent); 180 } 181}