Skip to content

Commit

Permalink
Migrate JSNI code to JSInterop
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagoricciardi committed Nov 6, 2018
1 parent b2987ef commit 6380422
Show file tree
Hide file tree
Showing 19 changed files with 520 additions and 271 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public interface GwtBootstrap3ClientBundle extends ClientBundle {

static final GwtBootstrap3ClientBundle INSTANCE = GWT.create(GwtBootstrap3ClientBundle.class);

@Source("resource/js/gwtbootstrap3.js")
TextResource gwtBootstrap3();

@Source("resource/js/jquery-1.12.4.min.cache.js")
TextResource jQuery();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.ScriptInjector;

import jsinterop.annotations.JsMethod;

/**
* Provides script injection for jQuery and boostrap if they aren't already loaded.
*
Expand All @@ -32,26 +34,27 @@
public class GwtBootstrap3EntryPoint implements EntryPoint {

/**
* Check to see if Boostrap is loaded already.
* Check to see if Bootstrap is loaded already.
*
* @return true is Boostreap loaded, false otherwise.
* @return true is Bootstrap loaded, false otherwise.
*/
private native boolean isBootstrapLoaded() /*-{
return typeof $wnd['jQuery'].fn.emulateTransitionEnd !== 'undefined'
}-*/;
@JsMethod
private static native boolean isBootstrapLoaded();

/**
* Check to see if jQuery is loaded already
*
* @return true is jQuery is loaded, false otherwise
*/
private native boolean isjQueryLoaded() /*-{
return (typeof $wnd['jQuery'] !== 'undefined');
}-*/;
@JsMethod
private static native boolean isjQueryLoaded();

/** {@inheritDoc} */
@Override
public void onModuleLoad() {
ScriptInjector.fromString(GwtBootstrap3ClientBundle.INSTANCE.gwtBootstrap3().getText())
.setWindow(ScriptInjector.TOP_WINDOW)
.inject();
if (!isjQueryLoaded()) {
ScriptInjector.fromString(GwtBootstrap3ClientBundle.INSTANCE.jQuery().getText())
.setWindow(ScriptInjector.TOP_WINDOW)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.gwtbootstrap3.client.shared.js;

/*
* #%L
* GwtBootstrap3
* %%
* Copyright (C) 2013 - 2018 GwtBootstrap3
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

import com.google.gwt.user.client.Event;

import jsinterop.annotations.JsFunction;

/**
* EventHandler helper functional interface to trigger
* Java event functions inside JavaScript
* @author Thiago Ricciardi
*
*/
@JsFunction
@FunctionalInterface
public interface EventHandler {

void callEventHandler(Event event);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package org.gwtbootstrap3.client.shared.js;

/*
* #%L
* GwtBootstrap3
* %%
* Copyright (C) 2013 - 2018 GwtBootstrap3
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

import com.google.gwt.dom.client.Element;

import jsinterop.annotations.JsMethod;
import jsinterop.annotations.JsPackage;
import jsinterop.annotations.JsType;

/**
* jQuery and Bootstrap methods wrapper
* @author Thiago Ricciardi
*
*/
@JsType(isNative=true, namespace=JsPackage.GLOBAL, name="jQuery")
public class JQuery {

/**
* Create a JQuery object
* @param element the element to jQuerify
* @return JQuery object of element
*/
@JsMethod(namespace=JsPackage.GLOBAL, name="jQuery")
public static native JQuery jQuery(Element element);
/**
* Select jQuery elements and create a JQuery object
* @param selector jQuery selector
* @return JQuery object of elements selected
*/
@JsMethod(namespace=JsPackage.GLOBAL, name="jQuery")
public static native JQuery jQuery(String selector);

/**
* Bootstrap button() method
* @param method the method string
* @return JQuery object for chaining purposes
*/
public native JQuery button(String method);

/**
* jQuery html() method
* @param htmlString A string of HTML to set as the content of each matched element
* @return JQuery object for chaining purposes
*/
public native JQuery html(String htmlString);

/**
* jQuery on() method
* @param events One or more space-separated event types and optional namespaces,
* such as "click" or "keydown.myPlugin"
* @param function A function to execute when the event is triggered
* @return JQuery object for chaining purposes
*/
public native JQuery on(String events, EventHandler function);

/**
* jQuery off() method
* @param events One or more space-separated event types and optional namespaces,
* or just namespaces, such as "click", "keydown.myPlugin", or ".myPlugin"
* @return JQuery object for chaining purposes
*/
public native JQuery off(String events);

/**
* Bootstrap alert() method
* @param method the method string
* @return JQuery object for chaining purposes
*/
public native JQuery alert(String method);

/**
* Bootstrap carousel() method
* @param method the method string
* @return JQuery object for chaining purposes
*/
public native JQuery carousel(String method);
/**
* Bootstrap carousel() method
* @param slideNumber particular frame (0 based, similar to an array)
* @return JQuery object for chaining purposes
*/
public native JQuery carousel(int slideNumber);

/**
* Bootstrap collapse() method
* @param method the method string
* @return JQuery object for chaining purposes
*/
public native JQuery collapse(String method);

/**
* Bootstrap modal() method
* @param method the method string
* @return JQuery object for chaining purposes
*/
public native JQuery modal(String method);

/**
* Bootstrap popover() method
* @return JQuery object for chaining purposes
*/
public native JQuery popover();
/**
* Bootstrap popover() method
* @param method the method string
* @return JQuery object for chaining purposes
*/
public native JQuery popover(String method);

/**
* Bootstrap scrollspy() method
* @param method the method string
* @return JQuery object for chaining purposes
*/
public native JQuery scrollspy(String method);

/**
* Bootstrap tab() method
* @param method the method string
* @return JQuery object for chaining purposes
*/
public native JQuery tab(String method);

/**
* Bootstrap tooltip() method
* @return JQuery object for chaining purposes
*/
public native JQuery tooltip();
/**
* Bootstrap tooltip() method
* @param method the method string
* @return JQuery object for chaining purposes
*/
public native JQuery tooltip(String method);

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.UIObject;

import jsinterop.annotations.JsMethod;

/**
* An Affix is an element/container that gets "pinned" as soon as a certain
* amount of pixels have been scrolled.
Expand Down Expand Up @@ -78,9 +80,6 @@ public static void affix(final UIObject object, final int offset) {

// @formatter:off

private static native void internalAffix(final Element e, final int offset) /*-{
$wnd.jQuery(e).affix({
offset: offset
});
}-*/;
@JsMethod
private static native void internalAffix(final Element e, final int offset);
}
30 changes: 15 additions & 15 deletions gwtbootstrap3/src/main/java/org/gwtbootstrap3/client/ui/Alert.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.gwtbootstrap3.client.shared.event.AlertCloseHandler;
import org.gwtbootstrap3.client.shared.event.AlertClosedEvent;
import org.gwtbootstrap3.client.shared.event.AlertClosedHandler;
import org.gwtbootstrap3.client.shared.js.JQuery;
import org.gwtbootstrap3.client.ui.base.HasResponsiveness;
import org.gwtbootstrap3.client.ui.base.HasType;
import org.gwtbootstrap3.client.ui.base.button.CloseButton;
Expand Down Expand Up @@ -239,25 +240,24 @@ public HandlerRegistration addClosedHandler(final AlertClosedHandler handler) {
}

// @formatter:off
private native void alert(final Element e, final String arg) /*-{
$wnd.jQuery(e).alert(arg);
}-*/;
private void alert(final Element e, final String arg) {
JQuery.jQuery(e).alert(arg);
}

private native void bindJavaScriptEvents(final Element e) /*-{
var target = this;
var $alert = $wnd.jQuery(e);
private void bindJavaScriptEvents(final Element e) {
JQuery alert = JQuery.jQuery(e);

$alert.on('close.bs.alert', function (evt) {
[email protected]::onClose(Lcom/google/gwt/user/client/Event;)(evt);
alert.on("close.bs.alert", (evt) -> {
onClose(evt);
});

$alert.on('closed.bs.alert', function (evt) {
[email protected]::onClosed(Lcom/google/gwt/user/client/Event;)(evt);
alert.on("closed.bs.alert", (evt) -> {
onClosed(evt);
});
}-*/;
}

private native void unbindJavaScriptEvents(final Element e) /*-{
$wnd.jQuery(e).off('close.bs.alert');
$wnd.jQuery(e).off('closed.bs.alert');
}-*/;
private void unbindJavaScriptEvents(final Element e) {
JQuery.jQuery(e).off("close.bs.alert");
JQuery.jQuery(e).off("closed.bs.alert");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@
import org.gwtbootstrap3.client.shared.event.CarouselSlidHandler;
import org.gwtbootstrap3.client.shared.event.CarouselSlideEvent;
import org.gwtbootstrap3.client.shared.event.CarouselSlideHandler;
import org.gwtbootstrap3.client.shared.js.JQuery;
import org.gwtbootstrap3.client.ui.constants.Attributes;
import org.gwtbootstrap3.client.ui.constants.Styles;
import org.gwtbootstrap3.client.ui.html.Div;

import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Event;

import jsinterop.annotations.JsMethod;

/**
* @author Joshua Godi
*/
Expand Down Expand Up @@ -148,38 +151,32 @@ private void onSlid(final Event evt) {
fireEvent(new CarouselSlidEvent(this, evt));
}

private native void bindJavaScriptEvents(final com.google.gwt.dom.client.Element e) /*-{
var target = this;
var $carousel = $wnd.jQuery(e);
private void bindJavaScriptEvents(final com.google.gwt.dom.client.Element e) {
JQuery carousel = JQuery.jQuery(e);

$carousel.on('slide.bs.carousel', function (evt) {
[email protected]::onSlide(Lcom/google/gwt/user/client/Event;)(evt);
carousel.on("slide.bs.carousel", (evt) -> {
onSlide(evt);
});

$carousel.on('slid.bs.carousel', function (evt) {
[email protected]::onSlid(Lcom/google/gwt/user/client/Event;)(evt);
});
}-*/;

private native void unbindJavaScriptEvents(final com.google.gwt.dom.client.Element e) /*-{
$wnd.jQuery(e).off('slide.bs.carousel');
$wnd.jQuery(e).off('slid.bs.carousel');
}-*/;

private native void carousel(final com.google.gwt.dom.client.Element e, final int interval, final String pause,
final boolean wrap) /*-{
$wnd.jQuery(e).carousel({
interval: interval,
pause: pause,
wrap: wrap
carousel.on("slid.bs.carousel", (evt) -> {
onSlid(evt);
});
}-*/;
}

private void unbindJavaScriptEvents(final com.google.gwt.dom.client.Element e) {
JQuery.jQuery(e).off("slide.bs.carousel");
JQuery.jQuery(e).off("slid.bs.carousel");
}

@JsMethod
private static native void carousel(final com.google.gwt.dom.client.Element e, final int interval, final String pause,
final boolean wrap);

private native void fireMethod(final com.google.gwt.dom.client.Element e, String method) /*-{
$wnd.jQuery(e).carousel(method);
}-*/;
private void fireMethod(final com.google.gwt.dom.client.Element e, String method) {
JQuery.jQuery(e).carousel(method);
}

private native void fireMethod(final com.google.gwt.dom.client.Element e, int slideNumber) /*-{
$wnd.jQuery(e).carousel(slideNumber);
}-*/;
private void fireMethod(final com.google.gwt.dom.client.Element e, int slideNumber) {
JQuery.jQuery(e).carousel(slideNumber);
}
}
Loading

0 comments on commit 6380422

Please sign in to comment.