001package org.gwtbootstrap3.client.ui; 002 003/* 004 * #%L 005 * GwtBootstrap3 006 * %% 007 * Copyright (C) 2013 GwtBootstrap3 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 org.gwtbootstrap3.client.shared.event.CarouselSlidEvent; 024import org.gwtbootstrap3.client.shared.event.CarouselSlidHandler; 025import org.gwtbootstrap3.client.shared.event.CarouselSlideEvent; 026import org.gwtbootstrap3.client.shared.event.CarouselSlideHandler; 027import org.gwtbootstrap3.client.ui.constants.Attributes; 028import org.gwtbootstrap3.client.ui.constants.Styles; 029import org.gwtbootstrap3.client.ui.html.Div; 030 031import com.google.gwt.event.shared.HandlerRegistration; 032import com.google.gwt.user.client.Event; 033 034/** 035 * @author Joshua Godi 036 */ 037public class Carousel extends Div { 038 public static final String HOVER = "hover"; 039 public static final String CAROUSEL = "carousel"; 040 public static final String CYCLE = "cycle"; 041 public static final String PAUSE = "pause"; 042 public static final String PREV = "prev"; 043 public static final String NEXT = "next"; 044 045 // Bootstrap default values: http://getbootstrap.com/javascript/#carousel 046 private int interval = 5000; 047 private String pause = HOVER; 048 private boolean wrap = true; 049 050 public Carousel() { 051 // Set the default styles 052 setStyleName(Styles.CAROUSEL); 053 addStyleName(Styles.SLIDE); 054 055 // Set the default attribute 056 getElement().setAttribute(Attributes.DATA_RIDE, CAROUSEL); 057 } 058 059 @Override 060 protected void onLoad() { 061 super.onLoad(); 062 063 // Bind jquery events 064 bindJavaScriptEvents(getElement()); 065 066 // Configure the carousel 067 carousel(getElement(), interval, pause, wrap); 068 } 069 070 @Override 071 protected void onUnload() { 072 super.onUnload(); 073 074 // Unbind events 075 unbindJavaScriptEvents(getElement()); 076 } 077 078 public void setInterval(final int interval) { 079 this.interval = interval; 080 } 081 082 public void setPause(final String pause) { 083 this.pause = pause; 084 } 085 086 public void setWrap(final boolean wrap) { 087 this.wrap = wrap; 088 } 089 090 /** 091 * Causes the carousel to cycle 092 */ 093 public void cycleCarousel() { 094 fireMethod(getElement(), CYCLE); 095 } 096 097 /** 098 * Causes the carousel to pause movement 099 */ 100 public void pauseCarousel() { 101 fireMethod(getElement(), PAUSE); 102 } 103 104 /** 105 * Causes the carousel to jump to that slide 106 */ 107 public void jumpToSlide(final int slideNumber) { 108 fireMethod(getElement(), slideNumber); 109 } 110 111 /** 112 * Causes the carousel to go back 113 */ 114 public void goToPrev() { 115 fireMethod(getElement(), PREV); 116 } 117 118 /** 119 * Causes the carousel to go to the next slide 120 */ 121 public void goToNext() { 122 fireMethod(getElement(), NEXT); 123 } 124 125 public HandlerRegistration addSlideHandler(final CarouselSlideHandler carouselSlideHandler) { 126 return addHandler(carouselSlideHandler, CarouselSlideEvent.getType()); 127 } 128 129 public HandlerRegistration addSlidHandler(final CarouselSlidHandler slidHandler) { 130 return addHandler(slidHandler, CarouselSlidEvent.getType()); 131 } 132 133 /** 134 * Fired when the carousel is starting to change slides 135 * 136 * @param evt event 137 */ 138 private void onSlide(final Event evt) { 139 fireEvent(new CarouselSlideEvent(this, evt)); 140 } 141 142 /** 143 * Fired when the carousel is finished changing slides 144 * 145 * @param evt event 146 */ 147 private void onSlid(final Event evt) { 148 fireEvent(new CarouselSlidEvent(this, evt)); 149 } 150 151 private native void bindJavaScriptEvents(final com.google.gwt.dom.client.Element e) /*-{ 152 var target = this; 153 var $carousel = $wnd.jQuery(e); 154 155 $carousel.on('slide.bs.carousel', function (evt) { 156 target.@org.gwtbootstrap3.client.ui.Carousel::onSlide(Lcom/google/gwt/user/client/Event;)(evt); 157 }); 158 159 $carousel.on('slid.bs.carousel', function (evt) { 160 target.@org.gwtbootstrap3.client.ui.Carousel::onSlid(Lcom/google/gwt/user/client/Event;)(evt); 161 }); 162 }-*/; 163 164 private native void unbindJavaScriptEvents(final com.google.gwt.dom.client.Element e) /*-{ 165 $wnd.jQuery(e).off('slide.bs.carousel'); 166 $wnd.jQuery(e).off('slid.bs.carousel'); 167 }-*/; 168 169 private native void carousel(final com.google.gwt.dom.client.Element e, final int interval, final String pause, 170 final boolean wrap) /*-{ 171 $wnd.jQuery(e).carousel({ 172 interval: interval, 173 pause: pause, 174 wrap: wrap 175 }); 176 }-*/; 177 178 private native void fireMethod(final com.google.gwt.dom.client.Element e, String method) /*-{ 179 $wnd.jQuery(e).carousel(method); 180 }-*/; 181 182 private native void fireMethod(final com.google.gwt.dom.client.Element e, int slideNumber) /*-{ 183 $wnd.jQuery(e).carousel(slideNumber); 184 }-*/; 185}