001package com.github.gwtbootstrap.client.ui;
002
003import com.github.gwtbootstrap.client.ui.base.MarkupWidget;
004import com.github.gwtbootstrap.client.ui.constants.Constants;
005import com.google.gwt.core.client.Scheduler;
006import com.google.gwt.core.client.Scheduler.ScheduledCommand;
007import com.google.gwt.event.dom.client.ClickEvent;
008import com.google.gwt.event.dom.client.ClickHandler;
009import com.google.gwt.event.dom.client.HasClickHandlers;
010import com.google.gwt.user.client.Element;
011import com.google.gwt.user.client.ui.Widget;
012
013/**
014 * Markup widget of CollapseTrigger
015 * <p>
016 * It's a markup widget (decorator widget).
017 * it's can exchange child to {@link Collapse}'s toggle trigger widget.
018 * 
019 * <p>
020 * <h3>UiBinder Usage:</h3>
021 * </p>
022 * <pre>
023 * {@code
024 * <b:CollapseTrigger target="#toggle1">
025 *      <b:Button>Collapse Trigger</b:Button>
026 * </b:CollapseTrigger>
027 *
028 * <!-- if use collapse trigger, you must set existTrigger=true attribute to Collapse tag. -->
029 * <b:Collapse existTrigger="true" b:id="toggle1" defaultOpen="true">
030 *     <b:FluidRow>
031 *         <b:Column size="12">
032 *             <b:Alert close="false" animation="true" heading="collapsible1">
033 *                 <b:Paragraph>
034 *                     Hello :D
035 *                 </b:Paragraph>
036 *             </b:Alert>
037 *         </b:Column>
038 *     </b:FluidRow>
039 * </b:Collapse>
040 * }
041 * </pre>
042 * 
043 * @since 2.2.1.0
044 * @author ohashi keisuke
045 * @see Accordion
046 * @see Collapse
047 * @see CollapseTrigger
048 * @see <a href="http://twitter.github.com/bootstrap/javascript.html#collapse">Twitter Bootstrap document</a>
049 *
050 */
051public class CollapseTrigger extends MarkupWidget {
052
053    private String target;
054    
055    private String parent;
056    
057    /**
058     * Create an empty widget with target selector
059     * @param target selector (eg: #myCollapse)
060     */
061    public CollapseTrigger(String target) {
062        this.target = target;
063    }
064    
065    /**
066     * Create an empty widget.
067     */
068    public CollapseTrigger() {
069    }
070    
071    /**
072     * {@inheritDoc}
073     */
074    @Override
075    public Widget asWidget() {
076        
077        if(widget != null) {
078            Element element = widget.getElement();
079            
080            if(element.hasAttribute(Constants.DATA_TOGGLE) 
081                    && widget instanceof HasClickHandlers
082                    && !widget.isAttached()) {
083                Scheduler.get().scheduleDeferred(new ScheduledCommand() {
084                    
085                    @Override
086                    public void execute() {
087                        Collapse.configure(target, parent, false);
088                        ((HasClickHandlers)widget).addClickHandler(new ClickHandler() {
089                            
090                            @Override
091                            public void onClick(ClickEvent event) {
092                                Collapse.changeVisibility(target, "toggle");
093                            }
094                        });
095                    }
096                });
097                return super.asWidget();
098            }
099            
100            
101            element.setAttribute(Constants.DATA_TOGGLE, Constants.COLLAPSE);
102            element.setAttribute(Constants.DATA_TARGET, target);
103            
104            if(parent != null && !parent.isEmpty()) {
105                setParent(parent);
106            }
107            
108        }
109        
110        return super.asWidget();
111    }
112    
113    /**
114     * Set target collapse selector.
115     * @param target selector of target. (eg:#myCollapse)
116     */
117    public void setTarget(String target) {
118        this.target = target;
119        
120        if(widget != null) {
121            Element element = widget.getElement();
122            element.setAttribute(Constants.DATA_TARGET, target);
123        }
124    }
125    
126    /**
127     * Get target collapse selector.
128     * @return selector.
129     */
130    public String getTarget(){
131        return this.target;
132    }
133
134
135    /**
136     * Set parent selector.
137     * 
138     * it only work with {@link AccordionGroup},
139     * Please see <a href="https://github.com/twitter/bootstrap/issues/4988">this issue</a>.
140     * 
141     * @param parent parent selector
142     */
143    public void setParent(String parent) {
144        this.parent = parent;
145        
146        if(widget != null) {
147            widget.getElement().setAttribute("data-parent", parent);
148        }
149        
150    }
151    
152}