View Javadoc
1   /**
2    * This file Copyright (c) 2012-2018 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           console.log("Warning: call to jQuery's unload() method, which is deprecated and has been removed without replacement.");
204           var jq = this;
205           this.unload(function() {
206                if (callbacks != null) {
207                     callbacks.fire(jq);
208                }
209           });
210     }-*/;
211 
212     public final native String attr(final String property) /*-{
213           return this.attr(property);
214     }-*/;
215 
216     public final native void setAttr(final String property, final String value) /*-{
217           this.attr(property, value);
218     }-*/;
219 
220     public final native String css(final String property) /*-{
221           return this.css(property);
222     }-*/;
223 
224     public final native void setCss(final String property, final String value) /*-{
225           this.css(property, value);
226     }-*/;
227 
228     public final void setCss(JSONObject value) {
229         final JavaScriptObject jso = value.isObject().getJavaScriptObject();
230         setCss(jso);
231     }
232 
233     public final native void setCss(JavaScriptObject value) /*-{
234           this.css(value);
235     }-*/;
236 
237     public final native void setCssPx(final String property, int pxVal) /*-{
238           var pxVal = pxVal + 'px';
239           this.css(property, pxVal);
240     }-*/;
241 
242     public final native Integer cssInt(final String property) /*-{
243           var result = this.css(property);
244           return @info.magnolia.ui.vaadin.gwt.client.jquerywrapper.JQueryWrapper::parseInt(Ljava/lang/String;)(result);
245     }-*/;
246 
247     public final native Position position() /*-{
248         return this.position();
249     }-*/;
250 
251     public native final Element get(int index) /*-{
252           return this.get(index);
253     }-*/;
254 
255     public native final JsArray<Element> get() /*-{
256           return this.get();
257     }-*/;
258 
259     public native final void setScrollTop(int scrollTop)  /*-{
260         this.scrollTop(scrollTop);
261     }-*/;
262 
263     public native final int getScrollTop()  /*-{
264         return this.scrollTop();
265     }-*/;
266 
267     public native final int size() /*-{
268         return this.length;
269     }-*/;
270 
271     public final boolean isAnimationInProgress() {
272         return is(":animated");
273     }
274 
275     public static native Integer parseInt(final String value) /*-{
276           var number = parseInt(value, 10);
277           if (isNaN(number))
278                return null;
279           else
280                return @java.lang.Integer::valueOf(I)(number);
281     }-*/;
282 
283     public final native void removeClass(String className) /*-{
284         this.removeClass(className);
285     }-*/;
286 }