View Javadoc
1   /**
2    * This file Copyright (c) 2012-2015 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.ui.vaadin.gwt.client.jquerywrapper;
35  
36  import com.google.gwt.core.client.JavaScriptObject;
37  import com.google.gwt.core.client.JsArray;
38  import com.google.gwt.json.client.JSONObject;
39  import com.google.gwt.user.client.Element;
40  import com.google.gwt.user.client.ui.Widget;
41  
42  /**
43   * JQuery library wrapper. The functionality covered is ruled by the needs of MagnoliaShell (and its parts) implementation.
44   */
45  public class JQueryWrapper extends JavaScriptObject {
46  
47      protected JQueryWrapper() {
48      }
49  
50      public final static native JQueryWrapper select(final String style) /*-{
51          return $wnd.jQuery(style);
52      }-*/;
53  
54      public final static native JQueryWrapper selectId(final String id) /*-{
55          return $wnd.jQuery('#' + id);
56      }-*/;
57  
58      public final static native JQueryWrapper select(final Element el) /*-{
59          return $wnd.jQuery(el);
60      }-*/;
61  
62      public final static JQueryWrapper select(final Widget w) {
63          return select(w.getElement());
64      }
65  
66      public final native void on(String eventId, Callbacks callbacks) /*-{
67          //var jq = this;
68          var win = $wnd;
69          win.alert('reg');
70          this.on(eventId,
71              function(event) {
72                  win.alert('Fire');
73                  if (callbacks != null) {
74                      callbacks.fire(null);
75                  }
76          });
77      }-*/;
78  
79      public final native boolean is(String selector) /*-{
80          return this.is(selector);
81      }-*/;
82  
83      public final native JQueryWrapper children(String selector) /*-{
84          return this.children(selector);
85      }-*/;
86  
87      public final native JQueryWrapper find(String selector) /*-{
88          return this.find(selector);
89      }-*/;
90  
91      public final native void on(String eventId, String selector, Callbacks callbacks) /*-{
92          this.on(
93                eventId, selector,
94              $entry(function (event) {
95                        var jq = @info.magnolia.ui.vaadin.gwt.client.jquerywrapper.JQueryWrapper::select(Lcom/google/gwt/user/client/Element;)(event.target);
96                        if (callbacks != null) {
97                            callbacks.fire(jq);
98                        }
99              }));
100     }-*/;
101 
102     public final native void animate(int duration, AnimationSettings settings) /*-{
103           var json = settings.@info.magnolia.ui.vaadin.gwt.client.jquerywrapper.AnimationSettings::asJSO()();
104           var jq = this;
105           this.animate(json, duration,
106               $entry(function () {
107                     if (settings != null) {
108                         settings.@info.magnolia.ui.vaadin.gwt.client.jquerywrapper.AnimationSettings::getCallbacks()()
109                                    .fire(jq);
110                     }
111               }));
112     }-*/;
113 
114     public final native void animate(int duration, double delay, AnimationSettings settings) /*-{
115         var json = settings.@info.magnolia.ui.vaadin.gwt.client.jquerywrapper.AnimationSettings::asJSO()();
116         var jq = this;
117         this.delay(delay).animate(json, duration,
118             $entry(function () {
119                     if (settings != null) {
120                         settings.@info.magnolia.ui.vaadin.gwt.client.jquerywrapper.AnimationSettings::getCallbacks()().fire(jq);
121                     }
122             }));
123     }-*/;
124 
125     public final native JQueryWrapper stop() /*-{
126           this.stop();
127           return this;
128     }-*/;
129 
130     public final native void fadeIn(int duration, Callbacks callbacks) /*-{
131           var jq = this;
132           this.fadeIn(duration, function() {
133                if (callbacks != null) {
134                     callbacks.fire(jq);
135                }
136           });
137     }-*/;
138 
139     public final native void fadeOut(int duration, Callbacks callbacks) /*-{
140           var el = this.get();
141           var jq = this;
142           this.fadeOut(duration, function() {
143                if (callbacks != null) {
144                     callbacks.fire(jq);
145                }
146           });
147     }-*/;
148 
149     public final native void slideUp(int duration, Callbacks callbacks) /*-{
150           var jq = this;
151           this.slideUp(duration, function() {
152                if (callbacks != null) {
153                     callbacks.fire(jq);
154                }
155           });
156     }-*/;
157 
158     public final native void slideDown(int duration, Callbacks callbacks) /*-{
159           var jq = this;
160           this.slideDown(duration, function() {
161                if (callbacks != null) {
162                     callbacks.fire(jq);
163                }
164           });
165     }-*/;
166 
167     public final native void show(int duration, Callbacks callbacks) /*-{
168           var jq = this;
169           this.show(duration, function() {
170                if (callbacks != null) {
171                     callbacks.fire(jq);
172                }
173           });
174     }-*/;
175 
176     public final native void hide(int duration, Callbacks callbacks) /*-{
177           var jq = this;
178           this.hide(duration, function() {
179                if (callbacks != null) {
180                     callbacks.fire(jq);
181                }
182           });
183     }-*/;
184 
185     public final native void ready(final Callbacks callbacks) /*-{
186           var jq = this;
187           this.ready(function() {
188                if (callbacks != null) {
189                     callbacks.fire(jq);
190                }
191           });
192     }-*/;
193 
194     public final native void unload(final Callbacks callbacks) /*-{
195           var jq = this;
196           this.unload(function() {
197                if (callbacks != null) {
198                     callbacks.fire(jq);
199                }
200           });
201     }-*/;
202 
203     public final native String attr(final String property) /*-{
204           return this.attr(property);
205     }-*/;
206 
207     public final native void setAttr(final String property, final String value) /*-{
208           this.attr(property, value);
209     }-*/;
210 
211     public final native String css(final String property) /*-{
212           return this.css(property);
213     }-*/;
214 
215     public final native void setCss(final String property, final String value) /*-{
216           this.css(property, value);
217     }-*/;
218 
219     public final void setCss(JSONObject value) {
220         final JavaScriptObject jso = value.isObject().getJavaScriptObject();
221         setCss(jso);
222     }
223 
224     public final native void setCss(JavaScriptObject value) /*-{
225           this.css(value);
226     }-*/;
227 
228     public final native void setCssPx(final String property, int pxVal) /*-{
229           var pxVal = pxVal + 'px';
230           this.css(property, pxVal);
231     }-*/;
232 
233     public final native Integer cssInt(final String property) /*-{
234           var result = this.css(property);
235           return @info.magnolia.ui.vaadin.gwt.client.jquerywrapper.JQueryWrapper::parseInt(Ljava/lang/String;)(result);
236     }-*/;
237 
238     public final native Position position() /*-{
239         return this.position();
240     }-*/;
241 
242     public native final Element get(int index) /*-{
243           return this.get(index);
244     }-*/;
245 
246     public native final JsArray<Element> get() /*-{
247           return this.get();
248     }-*/;
249 
250     public native final void setScrollTop(int scrollTop)  /*-{
251         this.scrollTop(scrollTop);
252     }-*/;
253 
254     public native final int getScrollTop()  /*-{
255         return this.scrollTop();
256     }-*/;
257 
258     public native final int size() /*-{
259         return this.size();
260     }-*/;
261 
262     public final boolean isAnimationInProgress() {
263         return is(":animated");
264     }
265 
266     public static native Integer parseInt(final String value) /*-{
267           var number = parseInt(value, 10);
268           if (isNaN(number))
269                return null;
270           else
271                return @java.lang.Integer::valueOf(I)(number);
272     }-*/;
273 
274     public final native void removeClass(String className) /*-{
275         this.removeClass(className);
276     }-*/;
277 }