Created
September 24, 2017 13:39
-
-
Save kevzlou7979/dc76d45c300febb39a12fa061dd8f811 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MaterialDropDown extends UnorderedList implements Loader, HasConfiguration, HasSelectionHandlers<Widget>, HasInOutDurationTransition { | |
private int inDuration = 300; | |
private int outDuration = 225; | |
private int gutter = 0; | |
private boolean constrainWidth = true; | |
private boolean hover = false; | |
private boolean belowOrigin = false; | |
private Alignment alignment = Alignment.LEFT; | |
private List<Widget> children = new ArrayList<>(); | |
private String activator; | |
private Element activatorElement; | |
private JsDropdownOptions options; | |
public MaterialDropDown() { | |
setInitialClasses(CssName.DROPDOWN_CONTENT); | |
setId(DOM.createUniqueId()); | |
} | |
/** | |
* Add a list item selection when button, link, icon button pressed. | |
* | |
* @param activator data-activates attribute name of your dropdown activator. | |
*/ | |
@UiConstructor | |
public MaterialDropDown(String activator) { | |
this(); | |
this.activator = activator; | |
getElement().setId(this.activator); | |
} | |
public MaterialDropDown(Element activatorElement) { | |
this(); | |
activatorElement.setAttribute("data-activates", getId()); | |
this.activatorElement = activatorElement; | |
} | |
public MaterialDropDown(UIObject activator) { | |
this(activator.getElement()); | |
} | |
@Override | |
protected void onLoad() { | |
super.onLoad(); | |
Widget parent = getParent(); | |
if (parent instanceof HasActivates) { | |
String uid = DOM.createUniqueId(); | |
((HasActivates) parent).setActivates(uid); | |
setId(uid); | |
activatorElement = parent.getElement(); | |
} else if (activatorElement == null) { | |
activatorElement = DOMHelper.getElementByAttribute("data-activates", activator); | |
if (activatorElement == null) { | |
GWT.log("There is no activator element with id: '" + activator + "' in the DOM, " + | |
"cannot instantiate MaterialDropDown without a data-activates.", new IllegalStateException()); | |
} | |
} | |
configure(); | |
load(); | |
} | |
@Override | |
public void configure() { | |
options = new JsDropdownOptions(); | |
options.constrain_width = constrainWidth; | |
options.inDuration = inDuration; | |
options.outDuration = outDuration; | |
options.hover = hover; | |
options.gutter = gutter; | |
options.belowOrigin = belowOrigin; | |
options.alignment = alignment.getCssName(); | |
} | |
@Override | |
public void load() { | |
$(activator).dropdown(options); | |
setupActivator(); | |
} | |
@Override | |
public void reload() { | |
remove(activatorElement); | |
configure(); | |
} | |
@Override | |
public void add(final Widget child) { | |
String tagName = child.getElement().getTagName(); | |
if (child instanceof ListItem || tagName.toLowerCase().startsWith("li")) { | |
child.getElement().getStyle().setDisplay(Style.Display.BLOCK); | |
add(child, (Element) getElement()); | |
} else { | |
ListItem li = new ListItem(child); | |
children.add(child); | |
registerHandler(child.addDomHandler(event -> SelectionEvent.fire(MaterialDropDown.this, child), ClickEvent.getType())); | |
// Checks if there are sub dropdown components | |
if (child instanceof MaterialLink) { | |
MaterialLink link = (MaterialLink) child; | |
for (int i = 0; i < link.getWidgetCount(); i++) { | |
if (link.getWidget(i) instanceof MaterialDropDown) { | |
link.addClickHandler(DomEvent::stopPropagation); | |
link.stopTouchStartEvent(); | |
} | |
} | |
} | |
if (child instanceof HasWaves) { | |
li.setWaves(((HasWaves) child).getWaves()); | |
((HasWaves) child).setWaves(null); | |
} | |
li.getElement().getStyle().setDisplay(Style.Display.BLOCK); | |
add(li, (Element) getElement()); | |
} | |
} | |
protected void remove(Element activator) { | |
$(activator).dropdown("remove"); | |
} | |
@Override | |
public void setInDuration(int durationMillis) { | |
this.inDuration = durationMillis; | |
} | |
@Override | |
public int getInDuration() { | |
return inDuration; | |
} | |
@Override | |
public void setOutDuration(int durationMillis) { | |
this.outDuration = durationMillis; | |
} | |
@Override | |
public int getOutDuration() { | |
return outDuration; | |
} | |
/** | |
* If true, constrainWidth to the size of the dropdown activator. Default: true | |
*/ | |
public void setConstrainWidth(boolean constrainWidth) { | |
this.constrainWidth = constrainWidth; | |
} | |
public boolean isConstrainWidth() { | |
return constrainWidth; | |
} | |
/** | |
* If true, the dropdown will open on hover. Default: false | |
*/ | |
public void setHover(boolean hover) { | |
this.hover = hover; | |
} | |
public boolean isHover() { | |
return hover; | |
} | |
/** | |
* This defines the spacing from the aligned edge. Default: 0 | |
*/ | |
public void setGutter(int gutter) { | |
this.gutter = gutter; | |
} | |
public int getGutter() { | |
return gutter; | |
} | |
/** | |
* If true, the dropdown will show below the activator. Default: false | |
*/ | |
public void setBelowOrigin(boolean belowOrigin) { | |
this.belowOrigin = belowOrigin; | |
} | |
public boolean isBelowOrigin() { | |
return belowOrigin; | |
} | |
/** | |
* Defines the edge the menu is aligned to. Default: 'left' | |
*/ | |
public void setAlignment(Alignment alignment) { | |
this.alignment = alignment; | |
} | |
public Alignment getAlignment() { | |
return alignment; | |
} | |
/** | |
* Get the unique activator set by material widget e.g links, icons, buttons to trigger the dropdown. | |
*/ | |
public String getActivator() { | |
return activator; | |
} | |
/** | |
* Set the unique activator of each dropdown component and it must be unique | |
*/ | |
public void setActivator(String activator) { | |
this.activator = activator; | |
setId(activator); | |
} | |
@Override | |
public void setEnabled(boolean enabled) { | |
super.setEnabled(enabled); | |
reload(); | |
} | |
protected void setupActivator() { | |
Widget parent = getParent(); | |
if (parent instanceof HasEnabled) { | |
((HasEnabled) parent).setEnabled(isEnabled()); | |
} | |
} | |
public List<Widget> getItems() { | |
return children; | |
} | |
public Element getActivatorElement() { | |
return activatorElement; | |
} | |
@Override | |
public HandlerRegistration addSelectionHandler(final SelectionHandler<Widget> handler) { | |
return addHandler(new SelectionHandler<Widget>() { | |
@Override | |
public void onSelection(SelectionEvent<Widget> event) { | |
Widget widget = event.getSelectedItem(); | |
if (widget instanceof HasEnabled) { | |
if (((HasEnabled) widget).isEnabled() && isEnabled()) { | |
handler.onSelection(event); | |
} | |
} | |
} | |
}, SelectionEvent.getType()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment