View Javadoc

1   /**
2    * This file Copyright (c) 2012-2014 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                    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                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                 function() {
119                     if (settings != null) {
120                         settings.@info.magnolia.ui.vaadin.gwt.client.jquerywrapper.AnimationSettings::getCallbacks()()
121                                 .fire(jq);
122                     }
123                 });
124     }-*/;
125 
126     public final native JQueryWrapper stop() /*-{
127           this.stop();
128           return this;
129     }-*/;
130 
131     public final native void fadeIn(int duration, Callbacks callbacks) /*-{
132           var jq = this;
133           this.fadeIn(duration, function() {
134                if (callbacks != null) {
135                     callbacks.fire(jq);
136                }
137           });
138     }-*/;
139 
140     public final native void fadeOut(int duration, Callbacks callbacks) /*-{
141           var el = this.get();
142           var jq = this;
143           this.fadeOut(duration, function() {
144                if (callbacks != null) {
145                     callbacks.fire(jq);
146                }
147           });
148     }-*/;
149 
150     public final native void slideUp(int duration, Callbacks callbacks) /*-{
151           var jq = this;
152           this.slideUp(duration, function() {
153                if (callbacks != null) {
154                     callbacks.fire(jq);
155                }
156           });
157     }-*/;
158 
159     public final native void slideDown(int duration, Callbacks callbacks) /*-{
160           var jq = this;
161           this.slideDown(duration, function() {
162                if (callbacks != null) {
163                     callbacks.fire(jq);
164                }
165           });
166     }-*/;
167 
168     public final native void show(int duration, Callbacks callbacks) /*-{
169           var jq = this;
170           this.show(duration, function() {
171                if (callbacks != null) {
172                     callbacks.fire(jq);
173                }
174           });
175     }-*/;
176 
177     public final native void hide(int duration, Callbacks callbacks) /*-{
178           var jq = this;
179           this.hide(duration, function() {
180                if (callbacks != null) {
181                     callbacks.fire(jq);
182                }
183           });
184     }-*/;
185 
186     public final native void ready(final Callbacks callbacks) /*-{
187           var jq = this;
188           this.ready(function() {
189                if (callbacks != null) {
190                     callbacks.fire(jq);
191                }
192           });
193     }-*/;
194 
195     public final native void unload(final Callbacks callbacks) /*-{
196           var jq = this;
197           this.unload(function() {
198                if (callbacks != null) {
199                     callbacks.fire(jq);
200                }
201           });
202     }-*/;
203 
204     public final native String attr(final String property) /*-{
205           return this.attr(property);
206     }-*/;
207 
208     public final native void setAttr(final String property, final String value) /*-{
209           this.attr(property, value);
210     }-*/;
211 
212     public final native String css(final String property) /*-{
213           return this.css(property);
214     }-*/;
215 
216     public final native void setCss(final String property, final String value) /*-{
217           this.css(property, value);
218     }-*/;
219 
220     public final void setCss(JSONObject value) {
221         final JavaScriptObject jso = value.isObject().getJavaScriptObject();
222         setCss(jso);
223     }
224 
225     public final native void setCss(JavaScriptObject value) /*-{
226           this.css(value);
227     }-*/;
228 
229     public final native void setCssPx(final String property, int pxVal) /*-{
230           var pxVal = pxVal + 'px';
231           this.css(property, pxVal);
232     }-*/;
233 
234     public final native Integer cssInt(final String property) /*-{
235           var result = this.css(property);
236           return @info.magnolia.ui.vaadin.gwt.client.jquerywrapper.JQueryWrapper::parseInt(Ljava/lang/String;)(result);
237     }-*/;
238 
239     public final native Position position() /*-{
240         return this.position();
241     }-*/;
242 
243     public native final Element get(int index) /*-{
244           return this.get(index);
245     }-*/;
246 
247     public native final JsArray<Element> get() /*-{
248           return this.get();
249     }-*/;
250 
251     public final boolean isAnimationInProgress() {
252         return is(":animated");
253     }
254 
255     public static native Integer parseInt(final String value) /*-{
256           var number = parseInt(value, 10);
257           if (isNaN(number))
258                return null;
259           else
260                return @java.lang.Integer::valueOf(I)(number);
261     }-*/;
262 }