View Javadoc
1   /**
2    * This file Copyright (c) 2012-2016 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 int marginHeight() /*-{
80          return this.outerHeight(true);
81      }-*/;
82  
83      public final native int marginWidth() /*-{
84          return this.outerWidth(true);
85      }-*/;
86  
87      public final native boolean is(String selector) /*-{
88          return this.is(selector);
89      }-*/;
90  
91      public final native JQueryWrapper children(String selector) /*-{
92          return this.children(selector);
93      }-*/;
94  
95      public final native JQueryWrapper find(String selector) /*-{
96          return this.find(selector);
97      }-*/;
98  
99      public final native void on(String eventId, String selector, Callbacks callbacks) /*-{
100         this.on(
101               eventId, selector,
102             $entry(function (event) {
103                       var jq = @info.magnolia.ui.vaadin.gwt.client.jquerywrapper.JQueryWrapper::select(Lcom/google/gwt/user/client/Element;)(event.target);
104                       if (callbacks != null) {
105                           callbacks.fire(jq);
106                       }
107             }));
108     }-*/;
109 
110     public final native void animate(int duration, AnimationSettings settings) /*-{
111           var json = settings.@info.magnolia.ui.vaadin.gwt.client.jquerywrapper.AnimationSettings::asJSO()();
112           var jq = this;
113           this.animate(json, duration,
114               $entry(function () {
115                     if (settings != null) {
116                         settings.@info.magnolia.ui.vaadin.gwt.client.jquerywrapper.AnimationSettings::getCallbacks()()
117                                    .fire(jq);
118                     }
119               }));
120     }-*/;
121 
122     public final native void animate(int duration, double delay, AnimationSettings settings) /*-{
123         var json = settings.@info.magnolia.ui.vaadin.gwt.client.jquerywrapper.AnimationSettings::asJSO()();
124         var jq = this;
125         this.delay(delay).animate(json, duration,
126             $entry(function () {
127                     if (settings != null) {
128                         settings.@info.magnolia.ui.vaadin.gwt.client.jquerywrapper.AnimationSettings::getCallbacks()().fire(jq);
129                     }
130             }));
131     }-*/;
132 
133     public final native JQueryWrapper stop() /*-{
134           this.stop();
135           return this;
136     }-*/;
137 
138     public final native void fadeIn(int duration, Callbacks callbacks) /*-{
139           var jq = this;
140           this.fadeIn(duration, function() {
141                if (callbacks != null) {
142                     callbacks.fire(jq);
143                }
144           });
145     }-*/;
146 
147     public final native void fadeOut(int duration, Callbacks callbacks) /*-{
148           var el = this.get();
149           var jq = this;
150           this.fadeOut(duration, function() {
151                if (callbacks != null) {
152                     callbacks.fire(jq);
153                }
154           });
155     }-*/;
156 
157     public final native void slideUp(int duration, Callbacks callbacks) /*-{
158           var jq = this;
159           this.slideUp(duration, function() {
160                if (callbacks != null) {
161                     callbacks.fire(jq);
162                }
163           });
164     }-*/;
165 
166     public final native void slideDown(int duration, Callbacks callbacks) /*-{
167           var jq = this;
168           this.slideDown(duration, function() {
169                if (callbacks != null) {
170                     callbacks.fire(jq);
171                }
172           });
173     }-*/;
174 
175     public final native void show(int duration, Callbacks callbacks) /*-{
176           var jq = this;
177           this.show(duration, function() {
178                if (callbacks != null) {
179                     callbacks.fire(jq);
180                }
181           });
182     }-*/;
183 
184     public final native void hide(int duration, Callbacks callbacks) /*-{
185           var jq = this;
186           this.hide(duration, function() {
187                if (callbacks != null) {
188                     callbacks.fire(jq);
189                }
190           });
191     }-*/;
192 
193     public final native void ready(final Callbacks callbacks) /*-{
194           var jq = this;
195           this.ready(function() {
196                if (callbacks != null) {
197                     callbacks.fire(jq);
198                }
199           });
200     }-*/;
201 
202     public final native void unload(final Callbacks callbacks) /*-{
203           var jq = this;
204           this.unload(function() {
205                if (callbacks != null) {
206                     callbacks.fire(jq);
207                }
208           });
209     }-*/;
210 
211     public final native String attr(final String property) /*-{
212           return this.attr(property);
213     }-*/;
214 
215     public final native void setAttr(final String property, final String value) /*-{
216           this.attr(property, value);
217     }-*/;
218 
219     public final native String css(final String property) /*-{
220           return this.css(property);
221     }-*/;
222 
223     public final native void setCss(final String property, final String value) /*-{
224           this.css(property, value);
225     }-*/;
226 
227     public final void setCss(JSONObject value) {
228         final JavaScriptObject jso = value.isObject().getJavaScriptObject();
229         setCss(jso);
230     }
231 
232     public final native void setCss(JavaScriptObject value) /*-{
233           this.css(value);
234     }-*/;
235 
236     public final native void setCssPx(final String property, int pxVal) /*-{
237           var pxVal = pxVal + 'px';
238           this.css(property, pxVal);
239     }-*/;
240 
241     public final native Integer cssInt(final String property) /*-{
242           var result = this.css(property);
243           return @info.magnolia.ui.vaadin.gwt.client.jquerywrapper.JQueryWrapper::parseInt(Ljava/lang/String;)(result);
244     }-*/;
245 
246     public final native Position position() /*-{
247         return this.position();
248     }-*/;
249 
250     public native final Element get(int index) /*-{
251           return this.get(index);
252     }-*/;
253 
254     public native final JsArray<Element> get() /*-{
255           return this.get();
256     }-*/;
257 
258     public native final void setScrollTop(int scrollTop)  /*-{
259         this.scrollTop(scrollTop);
260     }-*/;
261 
262     public native final int getScrollTop()  /*-{
263         return this.scrollTop();
264     }-*/;
265 
266     public native final int size() /*-{
267         return this.size();
268     }-*/;
269 
270     public final boolean isAnimationInProgress() {
271         return is(":animated");
272     }
273 
274     public static native Integer parseInt(final String value) /*-{
275           var number = parseInt(value, 10);
276           if (isNaN(number))
277                return null;
278           else
279                return @java.lang.Integer::valueOf(I)(number);
280     }-*/;
281 
282     public final native void removeClass(String className) /*-{
283         this.removeClass(className);
284     }-*/;
285 }