You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GQuery.java 84KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796
  1. /*
  2. * Copyright 2009 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.gwt.query.client;
  17. import static com.google.gwt.query.client.plugins.Effects.Effects;
  18. import static com.google.gwt.query.client.plugins.Events.Events;
  19. import static com.google.gwt.query.client.plugins.Widgets.Widgets;
  20. import java.util.ArrayList;
  21. import java.util.Collection;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import com.google.gwt.core.client.GWT;
  25. import com.google.gwt.core.client.JavaScriptObject;
  26. import com.google.gwt.core.client.JsArray;
  27. import com.google.gwt.core.client.JsArrayString;
  28. import com.google.gwt.dom.client.BodyElement;
  29. import com.google.gwt.dom.client.ButtonElement;
  30. import com.google.gwt.dom.client.Document;
  31. import com.google.gwt.dom.client.Element;
  32. import com.google.gwt.dom.client.IFrameElement;
  33. import com.google.gwt.dom.client.InputElement;
  34. import com.google.gwt.dom.client.Node;
  35. import com.google.gwt.dom.client.NodeList;
  36. import com.google.gwt.dom.client.OptionElement;
  37. import com.google.gwt.dom.client.SelectElement;
  38. import com.google.gwt.dom.client.Style.Display;
  39. import com.google.gwt.dom.client.TextAreaElement;
  40. import com.google.gwt.query.client.css.CssProperty;
  41. import com.google.gwt.query.client.css.Length;
  42. import com.google.gwt.query.client.css.Percentage;
  43. import com.google.gwt.query.client.css.TakesLength;
  44. import com.google.gwt.query.client.css.TakesPercentage;
  45. import com.google.gwt.query.client.impl.DocumentStyleImpl;
  46. import com.google.gwt.query.client.plugins.EventsListener;
  47. import com.google.gwt.user.client.DOM;
  48. import com.google.gwt.user.client.Event;
  49. import com.google.gwt.user.client.EventListener;
  50. import com.google.gwt.user.client.Window;
  51. import com.google.gwt.user.client.ui.Widget;
  52. /**
  53. * Gwt Query is a GWT clone of the popular jQuery library.
  54. */
  55. public class GQuery implements Lazy<GQuery, LazyGQuery> {
  56. /**
  57. * A POJO used to store the top/left CSS positioning values of an element.
  58. */
  59. public static class Offset {
  60. public int left;
  61. public int top;
  62. public Offset(int left, int top) {
  63. this.left = left;
  64. this.top = top;
  65. }
  66. public Offset add(int left, int top) {
  67. return new Offset(this.left + left, this.top + top);
  68. }
  69. public String toString() {
  70. return top + "+" + left;
  71. }
  72. }
  73. /**
  74. * A class to store data in an element.
  75. */
  76. protected static final class DataCache extends JavaScriptObject {
  77. protected DataCache() {
  78. }
  79. public native void delete(int name) /*-{
  80. delete this[name];
  81. }-*/;
  82. public native void delete(String name) /*-{
  83. delete this[name];
  84. }-*/;
  85. public native boolean exists(int id) /*-{
  86. return !!this[id];
  87. }-*/;
  88. public native JavaScriptObject get(int id) /*-{
  89. return this[id];
  90. }-*/;
  91. public native JavaScriptObject get(String id) /*-{
  92. return this[id];
  93. }-*/;
  94. public DataCache getCache(int id) {
  95. return get(id).cast();
  96. }
  97. public native double getDouble(int id) /*-{
  98. return this[id];
  99. }-*/;
  100. public native double getDouble(String id) /*-{
  101. return this[id];
  102. }-*/;
  103. public native int getInt(int id) /*-{
  104. return this[id];
  105. }-*/;
  106. public native int getInt(String id) /*-{
  107. return this[id];
  108. }-*/;
  109. public native Object getObject(String id) /*-{
  110. return this[id];
  111. }-*/;
  112. public native String getString(int id) /*-{
  113. return this[id];
  114. }-*/;
  115. public native String getString(String id) /*-{
  116. return this[id];
  117. }-*/;
  118. public native boolean isEmpty() /*-{
  119. var foo = "";
  120. for(foo in this) break;
  121. return !foo;
  122. }-*/;
  123. public native void put(int id, Object obj) /*-{
  124. this[id]=obj;
  125. }-*/;
  126. public native void put(String id, Object obj) /*-{
  127. this[id]=obj;
  128. }-*/;
  129. }
  130. public static final BodyElement body = Document.get().getBody();
  131. public static final Document document = Document.get();
  132. public static boolean fxOff = false;
  133. public static Class<GQuery> GQUERY = GQuery.class;
  134. public static final Element window = window();
  135. private static DataCache dataCache = null;
  136. private static SelectorEngine engine;
  137. private static final int FUNC_PREPEND = 0, FUNC_APPEND = 1, FUNC_AFTER = 2,
  138. FUNC_BEFORE = 3;
  139. private static final String OLD_DATA_PREFIX = "old-";
  140. private static JsMap<Class<? extends GQuery>, Plugin<? extends GQuery>>
  141. plugins;;
  142. private static DocumentStyleImpl styleImpl = GWT.create(DocumentStyleImpl.class);
  143. private static Element windowData = null;
  144. /**
  145. * Create an empty GQuery object.
  146. */
  147. public static GQuery $() {
  148. return new GQuery(JSArray.create());
  149. }
  150. /**
  151. * Wrap a GQuery around a collection of existing widget.
  152. */
  153. public static GQuery $(Collection<Widget> widgetList){
  154. JSArray elements = JSArray.create();
  155. for (Widget w : widgetList){
  156. elements.addNode(w.getElement());
  157. }
  158. return $(elements);
  159. }
  160. /**
  161. * Wrap a GQuery around an existing element.
  162. */
  163. public static GQuery $(Element element) {
  164. return new GQuery(JSArray.create(element));
  165. }
  166. /**
  167. * Wrap a GQuery around an event's target element.
  168. */
  169. public static GQuery $(Event event) {
  170. return $((Element) event.getCurrentEventTarget().cast());
  171. }
  172. /**
  173. * Wrap a GQuery around an existing node.
  174. */
  175. public static GQuery $(Node n) {
  176. return new GQuery(JSArray.create(n));
  177. }
  178. /**
  179. * Wrap a GQuery around existing Elements.
  180. */
  181. public static GQuery $(NodeList<Element> elements) {
  182. return new GQuery(elements);
  183. }
  184. /**
  185. * Create a new GQuery given a list of objects.
  186. * Only node objects will be added;
  187. */
  188. public static GQuery $(@SuppressWarnings("rawtypes") ArrayList a) {
  189. JSArray elements = JSArray.create();
  190. for (Object o : a ) {
  191. if (o instanceof Node) {
  192. elements.addNode((Node)o);
  193. } else if (o instanceof Widget) {
  194. elements.addNode(((Widget)o).getElement());
  195. }
  196. }
  197. return new GQuery(elements);
  198. }
  199. /**
  200. * This function accepts a string containing a CSS selector which is then used
  201. * to match a set of elements, or it accepts raw HTML creating a GQuery
  202. * element containing those elements.
  203. */
  204. public static GQuery $(String selectorOrHtml) {
  205. if (selectorOrHtml == null || selectorOrHtml.trim().length() == 0) {
  206. return $();
  207. }
  208. if (selectorOrHtml.trim().charAt(0) == '<') {
  209. return innerHtml(selectorOrHtml);
  210. }
  211. return $(selectorOrHtml, document);
  212. }
  213. /**
  214. * This function accepts a string containing a CSS selector which is then used
  215. * to match a set of elements, or it accepts raw HTML creating a GQuery
  216. * element containing those elements. The second parameter is is a class
  217. * reference to a plugin to be used.
  218. */
  219. public static <T extends GQuery> T $(String selector, Class<T> plugin) {
  220. return $(selector, (Node) null, plugin);
  221. }
  222. /**
  223. * This function accepts a string containing a CSS selector which is then used
  224. * to match a set of elements, or it accepts raw HTML creating a GQuery
  225. * element containing those elements.
  226. * The second parameter is the context to use for the selector, or
  227. * the document where the new elements will be created.
  228. */
  229. public static GQuery $(String selectorOrHtml, Node ctx) {
  230. if (selectorOrHtml == null || selectorOrHtml.trim().length() == 0) {
  231. return $();
  232. }
  233. if (selectorOrHtml.trim().charAt(0) == '<') {
  234. Document doc = ctx instanceof Document ? ctx.<Document>cast() : ctx.getOwnerDocument();
  235. return $(cleanHtmlString(selectorOrHtml, doc));
  236. }
  237. return new GQuery(select(selectorOrHtml, ctx));
  238. }
  239. /**
  240. * This function accepts a string containing a CSS selector which is then used
  241. * to match a set of elements, or it accepts raw HTML creating a GQuery
  242. * element containing those elements. The second parameter is the context to
  243. * use for the selector. The third parameter is the class plugin to use.
  244. */
  245. @SuppressWarnings("unchecked")
  246. public static <T extends GQuery> T $(String selector, Node context,
  247. Class<T> plugin) {
  248. try {
  249. if (plugins != null) {
  250. T gquery = (T) plugins.get(plugin).init(new GQuery(select(selector, context)));
  251. return gquery;
  252. }
  253. throw new RuntimeException("No plugin for class " + plugin);
  254. } catch (Exception e) {
  255. throw new RuntimeException(e);
  256. }
  257. }
  258. /**
  259. * This function accepts a string containing a CSS selector which is then used
  260. * to match a set of elements, or it accepts raw HTML creating a GQuery
  261. * element containing those elements.
  262. * The second parameter is the context to use for the selector, or
  263. * the document where the new elements will be created.
  264. */
  265. public static GQuery $(String selectorOrHtml, Widget context) {
  266. return $(selectorOrHtml, context.getElement());
  267. }
  268. /**
  269. * This function accepts a string containing a CSS selector which is then used
  270. * to match a set of elements, or it accepts raw HTML creating a GQuery
  271. * element containing those elements. The second parameter is the context to
  272. * use for the selector. The third parameter is the class plugin to use.
  273. */
  274. public static <T extends GQuery> T $(String selector, Widget context,
  275. Class<T> plugin) {
  276. return $(selector, context.getElement(), plugin);
  277. }
  278. public static <T extends GQuery> T $(T gq) {
  279. return gq;
  280. }
  281. /**
  282. * Wrap a GQuery around an existing widget.
  283. */
  284. public static GQuery $(Widget w){
  285. return $(w.getElement());
  286. }
  287. /**
  288. * Wrap a GQuery around a array of existing widget.
  289. */
  290. public static GQuery $(Widget... widgetArray){
  291. JSArray elements = JSArray.create();
  292. for (Widget w : widgetArray){
  293. elements.addNode(w.getElement());
  294. }
  295. return $(elements);
  296. }
  297. /**
  298. * Wrap a JSON object.
  299. */
  300. public static Properties $$(String properties) {
  301. return Properties.create(properties);
  302. }
  303. @SuppressWarnings("unchecked")
  304. public static <T extends Node> T[] asArray(NodeList<T> nl) {
  305. if (GWT.isScript()) {
  306. return reinterpretCast(nl);
  307. } else {
  308. Node[] elts = new Node[nl.getLength()];
  309. for (int i = 0; i < elts.length; i++) {
  310. elts[i] = nl.getItem(i);
  311. }
  312. return (T[]) elts;
  313. }
  314. }
  315. /**
  316. * Return a lazy version of the GQuery interface. Lazy function calls are
  317. * simply queued up and not executed immediately.
  318. */
  319. public static LazyGQuery<?> lazy() {
  320. return $().createLazy();
  321. }
  322. public static void registerPlugin(Class<? extends GQuery> plugin,
  323. Plugin<? extends GQuery> pluginFactory) {
  324. if (plugins == null) {
  325. plugins = JsMap.createObject().cast();
  326. }
  327. plugins.put(plugin, pluginFactory);
  328. }
  329. @SuppressWarnings("unchecked")
  330. protected static GQuery cleanHtmlString(String elem, Document doc) {
  331. String tags = elem.trim().toLowerCase();
  332. String preWrap = "", postWrap = "";
  333. int wrapPos = 0;
  334. if (tags.contains("<opt")) {
  335. wrapPos = 1;
  336. preWrap = "<select multiple=\"multiple\">";
  337. postWrap = "</select>";
  338. } else if (tags.contains("<legend")) {
  339. wrapPos = 1;
  340. preWrap = "<fieldset>";
  341. postWrap = "</fieldset>";
  342. } else if (tags.matches("^<(thead|tbody|tfoot|colg|cap)")) {
  343. wrapPos = 1;
  344. preWrap = "<table>";
  345. postWrap = "</table>";
  346. } else if (tags.contains("<tr")) {
  347. wrapPos = 2;
  348. preWrap = "<table><tbody>";
  349. postWrap = "</tbody></table>";
  350. } else if (tags.contains("<td") || tags.contains("<th")) {
  351. wrapPos = 3;
  352. preWrap = "<table><tbody><tr>";
  353. postWrap = "</tr></tbody></table>";
  354. } else if (tags.contains("<col")) {
  355. wrapPos = 2;
  356. preWrap = "<table><tbody></tbody><colgroup>";
  357. postWrap = "</colgroup></table>";
  358. }
  359. // TODO: fix IE link tag serialization
  360. // TODO: fix IE <script> tag
  361. Element div = doc.createDivElement();
  362. div.setInnerHTML(preWrap + elem + postWrap);
  363. Node n = div;
  364. while (wrapPos-- != 0) {
  365. n = n.getLastChild();
  366. }
  367. // TODO: add fixes for IE TBODY issue
  368. return $((NodeList<Element>) n.getChildNodes().cast());
  369. }
  370. protected static <S> Object data(Element item, String name, S value) {
  371. if (dataCache == null) {
  372. windowData = JavaScriptObject.createObject().cast();
  373. dataCache = JavaScriptObject.createObject().cast();
  374. }
  375. item = item == window ? windowData : item;
  376. if (item == null) {
  377. return value;
  378. }
  379. int id = item.hashCode();
  380. if (name != null && !dataCache.exists(id)) {
  381. dataCache.put(id, DataCache.createObject().cast());
  382. }
  383. DataCache d = dataCache.get(id).cast();
  384. if (name != null && value != null) {
  385. d.put(name, value);
  386. }
  387. return name != null ? d.getObject(name) : id;
  388. }
  389. protected static String[] jsArrayToString(JsArrayString array) {
  390. if (GWT.isScript()) {
  391. return jsArrayToString0(array);
  392. } else {
  393. String result[] = new String[array.length()];
  394. for (int i = 0; i < result.length; i++) {
  395. result[i] = array.get(i);
  396. }
  397. return result;
  398. }
  399. }
  400. /**
  401. * We will use the fact as GWT use the widget itself as EventListener !
  402. * If no Widget associated with the element, this method returns null.
  403. * @param e
  404. * @return
  405. */
  406. protected static Widget getAssociatedWidget(Element e){
  407. EventListener listener = DOM.getEventListener((com.google.gwt.user.client.Element) e);
  408. //No listener attached to the element, so no widget exist for this element
  409. if (listener == null){
  410. return null;
  411. }
  412. if (listener instanceof Widget){
  413. //GWT uses the widget as event listener
  414. return (Widget) listener;
  415. }else if (listener instanceof EventsListener){
  416. EventsListener gQueryListener = (EventsListener)listener;
  417. if (gQueryListener.getOriginalEventListener() != null && gQueryListener.getOriginalEventListener() instanceof Widget){
  418. return (Widget) gQueryListener.getOriginalEventListener();
  419. }
  420. }
  421. // I think it's not a good idea to generate ourself a new widget wrapping the element...
  422. // To be discussed
  423. return null;
  424. }
  425. private static JSArray copyNodeList(NodeList<? extends Node> n) {
  426. JSArray res = JSArray.create();
  427. for (int i = 0; i < n.getLength(); i++) {
  428. res.addNode(n.getItem(i));
  429. }
  430. return res;
  431. }
  432. private static native void emptyDocument(Document d) /*-{
  433. d.open(); d.write("<head/><body/>"); d.close();
  434. }-*/;
  435. private native static Document getContentDocument(Node n) /*-{
  436. var d = n.contentDocument || n.contentWindow.document;
  437. if (!d.body) @com.google.gwt.query.client.GQuery::emptyDocument(Lcom/google/gwt/dom/client/Document;)(d);
  438. return d;
  439. }-*/;
  440. private static boolean hasClass(Element e, String clz) {
  441. return e.getClassName().matches("(^|.*\\s)" + clz + "(\\s.*|$)");
  442. }
  443. private static GQuery innerHtml(String html) {
  444. return $(cleanHtmlString(html, document));
  445. }
  446. private static native String[] jsArrayToString0(JsArrayString array) /*-{
  447. return array;
  448. }-*/;
  449. private static native <T extends Node> T[] reinterpretCast(NodeList<T> nl) /*-{
  450. return nl;
  451. }-*/;
  452. private static NodeList<Element> select(String selector, Node context) {
  453. if (engine == null) {
  454. engine = new SelectorEngine();
  455. }
  456. NodeList<Element> n = engine.select(selector, context);
  457. JSArray res = copyNodeList(n);
  458. return res;
  459. }
  460. private static native Element window() /*-{
  461. return $wnd;
  462. }-*/;
  463. protected NodeList<Element> elements = null;
  464. private String currentSelector;
  465. private GQuery previousObject;
  466. public GQuery() {
  467. elements = JavaScriptObject.createArray().cast();
  468. }
  469. public GQuery(Element element) {
  470. elements = JSArray.create(element);
  471. }
  472. public GQuery(GQuery gq) {
  473. this(gq.get());
  474. }
  475. public GQuery(JSArray elements) {
  476. this.elements = elements;
  477. }
  478. public GQuery(NodeList<Element> list) {
  479. elements = list;
  480. }
  481. /**
  482. * Add elements to the set of matched elements if they are not included yet.
  483. * It also update the selector appending the new one.
  484. */
  485. public GQuery add(GQuery previousObject) {
  486. return pushStack(unique(merge(elements, previousObject.elements)), "add",
  487. getSelector() + "," + previousObject.getSelector());
  488. }
  489. /**
  490. * Add elements to the set of matched elements if they are not included yet.
  491. */
  492. public GQuery add(String selector) {
  493. return add($(selector));
  494. }
  495. /**
  496. * Adds the specified classes to each matched element.
  497. */
  498. public GQuery addClass(String... classes) {
  499. for (Element e : elements()) {
  500. for (String clz : classes) {
  501. e.addClassName(clz);
  502. }
  503. }
  504. return this;
  505. }
  506. /**
  507. * Insert content after each of the matched elements. The elements must
  508. * already be inserted into the document (you can't insert an element after
  509. * another if it's not in the page).
  510. */
  511. public GQuery after(GQuery query) {
  512. return domManip(query, FUNC_AFTER);
  513. }
  514. /**
  515. * Insert content after each of the matched elements. The elements must
  516. * already be inserted into the document (you can't insert an element after
  517. * another if it's not in the page).
  518. */
  519. public GQuery after(Node n) {
  520. return domManip($(n), FUNC_AFTER);
  521. }
  522. /**
  523. * Insert content after each of the matched elements. The elements must
  524. * already be inserted into the document (you can't insert an element after
  525. * another if it's not in the page).
  526. */
  527. public GQuery after(String html) {
  528. return domManip(html, FUNC_AFTER);
  529. }
  530. /**
  531. * Add the previous selection to the current selection. Useful for traversing
  532. * elements, and then adding something that was matched before the last
  533. * traversal.
  534. */
  535. public GQuery andSelf() {
  536. return add(previousObject);
  537. }
  538. /**
  539. * Append content to the inside of every matched element. This operation is
  540. * similar to doing an appendChild to all the specified elements, adding them
  541. * into the document.
  542. */
  543. public GQuery append(GQuery query) {
  544. return domManip(query, FUNC_APPEND);
  545. }
  546. /**
  547. * Append content to the inside of every matched element. This operation is
  548. * similar to doing an appendChild to all the specified elements, adding them
  549. * into the document.
  550. */
  551. public GQuery append(Node n) {
  552. return domManip($(n), FUNC_APPEND);
  553. }
  554. /**
  555. * Append content to the inside of every matched element. This operation is
  556. * similar to doing an appendChild to all the specified elements, adding them
  557. * into the document.
  558. */
  559. public GQuery append(String html) {
  560. return domManip(html, FUNC_APPEND);
  561. }
  562. /**
  563. * All of the matched set of elements will be inserted at the end
  564. * of the element(s) specified by the parameter other.
  565. *
  566. * The operation $(A).appendTo(B) is, essentially, the reverse of doing a regular
  567. * $(A).append(B), instead of appending B to A, you're appending A to B.
  568. */
  569. public GQuery appendTo(GQuery other) {
  570. other.append(this);
  571. return this;
  572. }
  573. /**
  574. * All of the matched set of elements will be inserted at the end
  575. * of the element(s) specified by the parameter other.
  576. *
  577. * The operation $(A).appendTo(B) is, essentially, the reverse of doing a regular
  578. * $(A).append(B), instead of appending B to A, you're appending A to B.
  579. */
  580. public GQuery appendTo(Node n) {
  581. $(n).append(this);
  582. return this;
  583. }
  584. /**
  585. * All of the matched set of elements will be inserted at the end
  586. * of the element(s) specified by the parameter other.
  587. *
  588. * The operation $(A).appendTo(B) is, essentially, the reverse of doing a regular
  589. * $(A).append(B), instead of appending B to A, you're appending A to B.
  590. */
  591. public GQuery appendTo(String html) {
  592. $(html).append(this);
  593. return this;
  594. }
  595. /**
  596. * Convert to Plugin interface provided by Class literal.
  597. */
  598. @SuppressWarnings("unchecked")
  599. public <T extends GQuery> T as(Class<T> plugin) {
  600. // GQuery is not a plugin for itself
  601. if (plugin == GQUERY) {
  602. return (T) $(this);
  603. } else if (plugins != null) {
  604. Plugin<?> p = plugins.get(plugin);
  605. if (p != null) {
  606. return (T) p.init(this);
  607. }
  608. }
  609. throw new RuntimeException("No plugin registered for class " + plugin.getName());
  610. }
  611. /**
  612. * Return a GWT Widget containing the first matched element.
  613. *
  614. * If the element is already associated to a widget it returns the original widget,
  615. * otherwise a new GWT widget will be created depending on the tagName.
  616. *
  617. */
  618. public Widget asWidget() {
  619. return as(Widgets).widget();
  620. }
  621. /**
  622. * Set a key/value object as properties to all matched elements.
  623. *
  624. * Example: $("img").attr(new Properties("src: 'test.jpg', alt: 'Test Image'"))
  625. */
  626. public GQuery attr(Properties properties) {
  627. for (Element e : elements()) {
  628. for (String name : properties.keys()) {
  629. e.setAttribute(styleImpl.fixPropertyName(name), properties.get(name));
  630. }
  631. }
  632. return this;
  633. }
  634. /**
  635. * Access a property on the first matched element. This method makes it easy
  636. * to retrieve a property value from the first matched element. If the element
  637. * does not have an attribute with such a name, undefined is returned.
  638. * Attributes include title, alt, src, href, width, style, etc.
  639. */
  640. public String attr(String name) {
  641. return elements.getItem(0).getAttribute(styleImpl.fixPropertyName(name));
  642. }
  643. /**
  644. * Set a single property to a computed value, on all matched elements.
  645. */
  646. public GQuery attr(String key, Function closure) {
  647. for (int i = 0; i < elements.getLength(); i++) {
  648. Element e = elements.getItem(i);
  649. e.setAttribute(styleImpl.fixPropertyName(key), String.valueOf(closure.f(e, i)));
  650. }
  651. return this;
  652. }
  653. /**
  654. * Set a single property to a value, on all matched elements.
  655. */
  656. public GQuery attr(String key, String value) {
  657. key = styleImpl.fixPropertyName(key);
  658. for (Element e : elements()) {
  659. e.setAttribute(key, value);
  660. }
  661. return this;
  662. }
  663. /**
  664. * Insert content before each of the matched elements. The elements must
  665. * already be inserted into the document (you can't insert an element before
  666. * another if it's not in the page).
  667. */
  668. public GQuery before(GQuery query) {
  669. return domManip(query, FUNC_BEFORE);
  670. }
  671. /**
  672. * Insert content before each of the matched elements. The elements must
  673. * already be inserted into the document (you can't insert an element before
  674. * another if it's not in the page).
  675. */
  676. public GQuery before(Node n) {
  677. return domManip($(n), FUNC_BEFORE);
  678. }
  679. /**
  680. * Insert content before each of the matched elements. The elements must
  681. * already be inserted into the document (you can't insert an element before
  682. * another if it's not in the page).
  683. */
  684. public GQuery before(String html) {
  685. return domManip(html, FUNC_BEFORE);
  686. }
  687. /**
  688. * Binds a set of handlers to a particular Event for each matched element.
  689. *
  690. * The event handlers are passed as Functions that you can use to prevent
  691. * default behavior. To stop both default action and event bubbling, the
  692. * function event handler has to return false.
  693. *
  694. * You can pass an additional Object data to your Function as the second
  695. * parameter
  696. *
  697. */
  698. public GQuery bind(int eventbits, final Object data, final Function...funcs) {
  699. return as(Events).bind(eventbits, data, funcs);
  700. }
  701. /**
  702. * Bind a set of functions to the blur event of each matched element.
  703. * Or trigger the event if no functions are provided.
  704. */
  705. public GQuery blur(Function...f) {
  706. return bindOrFire(Event.ONBLUR, null, f);
  707. }
  708. /**
  709. * Bind a set of functions to the change event of each matched element.
  710. * Or trigger the event if no functions are provided.
  711. */
  712. public GQuery change(Function...f) {
  713. return bindOrFire(Event.ONCHANGE, null, f);
  714. }
  715. /**
  716. * Get a set of elements containing all of the unique immediate children of
  717. * each of the matched set of elements. Also note: while parents() will look
  718. * at all ancestors, children() will only consider immediate child elements.
  719. */
  720. public GQuery children() {
  721. JSArray result = JSArray.create();
  722. for (Element e : elements()) {
  723. allNextSiblingElements(e.getFirstChildElement(), result, null);
  724. }
  725. return new GQuery(unique(result));
  726. }
  727. /**
  728. * Get a set of elements containing all of the unique children of each of the
  729. * matched set of elements. This set is filtered with the expressions that
  730. * will cause only elements matching any of the selectors to be collected.
  731. */
  732. public GQuery children(String... filters) {
  733. return find(filters);
  734. }
  735. /**
  736. * Bind a set of functions to the click event of each matched element.
  737. * Or trigger the event if no functions are provided.
  738. */
  739. public GQuery click(Function...f) {
  740. return bindOrFire(Event.ONCLICK, null, f);
  741. }
  742. /**
  743. * Returns the inner height of the first matched element, including padding
  744. * but not the vertical scrollbar height, border, or margin.
  745. */
  746. public int clientHeight() {
  747. return get(0).getClientHeight();
  748. }
  749. /**
  750. * Returns the inner width of the first matched element, including padding
  751. * but not the vertical scrollbar width, border, or margin.
  752. */
  753. public int clientWidth() {
  754. return get(0).getClientWidth();
  755. }
  756. /**
  757. * Clone matched DOM Elements and select the clones. This is useful for moving
  758. * copies of the elements to another location in the DOM.
  759. */
  760. public GQuery clone() {
  761. JSArray result = JSArray.create();
  762. for (Element e : elements()) {
  763. result.addNode(e.cloneNode(true));
  764. }
  765. return new GQuery(result);
  766. }
  767. /**
  768. * Filter the set of elements to those that contain the specified text.
  769. */
  770. public GQuery contains(String text) {
  771. JSArray array = JSArray.create();
  772. for (Element e : elements()) {
  773. if ($(e).text().contains(text)) {
  774. array.addNode(e);
  775. }
  776. }
  777. return $(array);
  778. }
  779. /**
  780. * Find all the child nodes inside the matched elements (including text
  781. * nodes), or the content document, if the element is an iframe.
  782. */
  783. public GQuery contents() {
  784. JSArray result = JSArray.create();
  785. for (Element e : elements()) {
  786. if (IFrameElement.is(e)){
  787. result.addNode(getContentDocument(e));
  788. } else {
  789. NodeList<Node> children = e.getChildNodes();
  790. for (int i = 0; i < children.getLength(); i++) {
  791. result.addNode(children.getItem(i));
  792. }
  793. }
  794. }
  795. return new GQuery(unique(result));
  796. }
  797. public LazyGQuery<?> createLazy() {
  798. return GWT.create(GQuery.class);
  799. }
  800. /**
  801. * Set a key/value object as style properties to all matched elements. This
  802. * serves as the best way to set a large number of style properties on all
  803. * matched elements.
  804. *
  805. * Example: $(".item").css(Properties.create("color: 'red', background:
  806. * 'blue'"))
  807. */
  808. public GQuery css(Properties properties) {
  809. for (String property : properties.keys()) {
  810. css(property, properties.get(property));
  811. }
  812. return this;
  813. }
  814. /**
  815. * Return a style property on the first matched element.
  816. */
  817. public String css(String name) {
  818. return styleImpl.curCSS(get(0), name, false);
  819. }
  820. /**
  821. * Return a style property on the first matched element.
  822. *
  823. * The parameter force has a special meaning here:
  824. * - When force is false, returns the value of the css property defined
  825. * in the style attribute of the element.
  826. * - Otherwise it returns the real computed value.
  827. *
  828. * For instance if you define 'display=none' not in the element style
  829. * but in the css stylesheet, it returns an empty string unless you
  830. * pass the parameter force=true.
  831. */
  832. public String css(String name, boolean force) {
  833. return styleImpl.curCSS(get(0), name, force);
  834. }
  835. /**
  836. * Set a single style property to a value, on all matched elements.
  837. */
  838. public GQuery css(String prop, String val) {
  839. for (Element e : elements()) {
  840. styleImpl.setStyleProperty(e, prop, val);
  841. }
  842. return this;
  843. }
  844. /**
  845. * Set CSS property on every matched element using type-safe enumerations.
  846. */
  847. public <S, T extends CssProperty<S>> GQuery css(T cssProperty, S value) {
  848. for (Element e : elements()) {
  849. cssProperty.set(e.getStyle(), value);
  850. }
  851. return this;
  852. }
  853. /**
  854. * Set CSS property on every matched element using type-safe enumerations.
  855. */
  856. public GQuery css(TakesLength cssProperty, Length value) {
  857. for (Element e : elements()) {
  858. cssProperty.setLength(e.getStyle(), value);
  859. }
  860. return this;
  861. }
  862. /**
  863. * Set CSS property on every matched element using type-safe enumerations.
  864. */
  865. public GQuery css(TakesPercentage cssProperty, Percentage value) {
  866. for (Element e : elements()) {
  867. cssProperty.setPercentage(e.getStyle(), value);
  868. }
  869. return this;
  870. }
  871. /**
  872. * Returns value at named data store for the element, as set by data(name,
  873. * value).
  874. */
  875. public Object data(String name) {
  876. return data(elements.getItem(0), name, null);
  877. }
  878. /**
  879. * Returns value at named data store for the element, as set by data(name,
  880. * value) with desired return type.
  881. *
  882. * @param clz return type class literal
  883. */
  884. @SuppressWarnings("unchecked")
  885. public <T> T data(String name, Class<T> clz) {
  886. return (T) data(elements.getItem(0), name, null);
  887. }
  888. /**
  889. * Stores the value in the named spot with desired return type.
  890. */
  891. public GQuery data(String name, Object value) {
  892. for (Element e : elements()) {
  893. data(e, name, value);
  894. }
  895. return this;
  896. }
  897. /**
  898. * Bind a set of functions to the dblclick event of each matched element.
  899. * Or trigger the event if no functions are provided.
  900. */
  901. public GQuery dblclick(Function...f) {
  902. return bindOrFire(Event.ONDBLCLICK, null, f);
  903. }
  904. /**
  905. * Run one or more Functions over each element of the GQuery.
  906. * You have to override one of these funcions:
  907. * public void f(Element e)
  908. * public String f(Element e, int i)
  909. */
  910. public GQuery each(Function... f) {
  911. if (f != null) {
  912. for (Function f1 : f) {
  913. for (int i = 0; i < elements.getLength(); i++) {
  914. f1.f(elements.getItem(i), i);
  915. }
  916. }
  917. }
  918. return this;
  919. }
  920. /**
  921. * Returns the working set of nodes as a Java array. <b>Do NOT</b> attempt to
  922. * modify this array, e.g. assign to its elements, or call Arrays.sort()
  923. */
  924. public Element[] elements() {
  925. return asArray(elements);
  926. }
  927. /**
  928. * Remove all child nodes from the set of matched elements.
  929. * In the case of a document element, it removes all the content
  930. * You should call this method whenever you create a new iframe and you
  931. * want to add dynamic content to it.
  932. */
  933. public GQuery empty() {
  934. // TODO: add memory leak cleanup, remove event handlers, and
  935. // data caches
  936. for (Element e : elements()) {
  937. if (e.getNodeType() == Element.DOCUMENT_NODE) {
  938. emptyDocument(e.<Document>cast());
  939. } else {
  940. while (e.getFirstChild() != null) {
  941. e.removeChild(e.getFirstChild());
  942. }
  943. }
  944. }
  945. return this;
  946. }
  947. /**
  948. * Revert the most recent 'destructive' operation, changing the set of matched
  949. * elements to its previous state (right before the destructive operation).
  950. */
  951. public GQuery end() {
  952. return previousObject != null ? previousObject : new GQuery();
  953. }
  954. /**
  955. * Reduce GQuery to element in the specified position.
  956. */
  957. public GQuery eq(int pos) {
  958. return $(elements.getItem(pos));
  959. }
  960. /**
  961. * Bind a set of functions to the error event of each matched element.
  962. * Or trigger the event if no functions are provided.
  963. */
  964. public GQuery error(Function... f) {
  965. return bindOrFire(Event.ONERROR, null, f);
  966. }
  967. /**
  968. * Fade in all matched elements by adjusting their opacity. The effect will
  969. * take 1000 milliseconds to complete
  970. */
  971. public GQuery fadeIn(Function... f) {
  972. return $(as(Effects).fadeIn(f));
  973. }
  974. /**
  975. * Fade in all matched elements by adjusting their opacity.
  976. */
  977. public GQuery fadeIn(int millisecs, Function... f) {
  978. return $(as(Effects).fadeIn(millisecs, f));
  979. }
  980. /**
  981. * Fade out all matched elements by adjusting their opacity. The effect will
  982. * take 1000 milliseconds to complete
  983. */
  984. public GQuery fadeOut(Function... f) {
  985. return $(as(Effects).fadeOut(f));
  986. }
  987. /**
  988. * Fade out all matched elements by adjusting their opacity.
  989. */
  990. public GQuery fadeOut(int millisecs, Function... f) {
  991. return as(Effects).fadeOut(millisecs, f);
  992. }
  993. /**
  994. * Removes all elements from the set of matched elements that do not match the
  995. * specified function. The function is called with a context equal to the
  996. * current element. If the function returns false, then the element is removed
  997. * - anything else and the element is kept.
  998. */
  999. public GQuery filter(Predicate filterFn) {
  1000. JSArray result = JSArray.create();
  1001. for (int i = 0; i < elements.getLength(); i++) {
  1002. Element e = elements.getItem(i);
  1003. if (filterFn.f(e, i)) {
  1004. result.addNode(e);
  1005. }
  1006. }
  1007. return pushStack(result, "filter", currentSelector);
  1008. }
  1009. /**
  1010. * Removes all elements from the set of matched elements that do not pass the
  1011. * specified css expression. This method is used to narrow down the results of
  1012. * a search. Provide a comma-separated list of expressions to apply multiple
  1013. * filters at once.
  1014. */
  1015. public GQuery filter(String... filters) {
  1016. JSArray array = JSArray.create();
  1017. for (String f : filters) {
  1018. for (Element e : elements()) {
  1019. for (Element c : $(f, e.getParentNode()).elements()) {
  1020. if (c == e) {
  1021. array.addNode(c);
  1022. break;
  1023. }
  1024. }
  1025. }
  1026. }
  1027. return pushStack(unique(array), "filter", filters[0]);
  1028. }
  1029. /**
  1030. * Searches for all elements that match the specified css expression. This
  1031. * method is a good way to find additional descendant elements with which to
  1032. * process.
  1033. *
  1034. * Provide a comma-separated list of expressions to apply multiple filters at
  1035. * once.
  1036. */
  1037. public GQuery find(String... filters) {
  1038. JSArray array = JSArray.create();
  1039. for (String selector : filters) {
  1040. for (Element e : elements()) {
  1041. for (Element c : $(selector, e).elements()) {
  1042. array.addNode(c);
  1043. }
  1044. }
  1045. }
  1046. return pushStack(unique(array), "find", filters[0]);
  1047. }
  1048. /**
  1049. * Reduce the set of matched elements to the first in the set.
  1050. */
  1051. public GQuery first() {
  1052. return eq(0);
  1053. }
  1054. /**
  1055. * Bind a set of functions to the focus event of each matched element.
  1056. * Or trigger the event if no functions are provided.
  1057. */
  1058. public GQuery focus(Function...f) {
  1059. return bindOrFire(Event.ONFOCUS, null, f);
  1060. }
  1061. /**
  1062. * Return all elements matched in the GQuery as a NodeList. @see #elements()
  1063. * for a method which returns them as an immutable Java array.
  1064. */
  1065. public NodeList<Element> get() {
  1066. return elements;
  1067. }
  1068. /**
  1069. * Return the ith element matched.
  1070. */
  1071. public Element get(int i) {
  1072. return elements.getItem(i);
  1073. }
  1074. /**
  1075. * Return the previous set of matched elements prior to the last destructive
  1076. * operation (e.g. query)
  1077. */
  1078. public GQuery getPreviousObject() {
  1079. return previousObject;
  1080. }
  1081. /**
  1082. * Return the selector representing the current set of matched elements.
  1083. */
  1084. public String getSelector() {
  1085. return currentSelector;
  1086. }
  1087. /**
  1088. * Returns true any of the specified classes are present on any of the matched
  1089. * Reduce the set of matched elements to all elements after a given position.
  1090. * The position of the element in the set of matched elements starts at 0 and
  1091. * goes to length - 1.
  1092. */
  1093. public GQuery gt(int pos) {
  1094. return $(slice(pos + 1, -1));
  1095. }
  1096. /**
  1097. * Returns true any of the specified classes are present on any of the matched
  1098. * elements.
  1099. */
  1100. public boolean hasClass(String... classes) {
  1101. for (Element e : elements()) {
  1102. for (String clz : classes) {
  1103. if (hasClass(e, clz)) {
  1104. return true;
  1105. }
  1106. }
  1107. }
  1108. return false;
  1109. }
  1110. /**
  1111. * Get the current computed, pixel, height of the first matched element.
  1112. */
  1113. public int height() {
  1114. return get(0).getOffsetHeight();
  1115. }
  1116. /**
  1117. * Set the height of every element in the matched set.
  1118. */
  1119. public GQuery height(int height) {
  1120. for (Element e : elements()) {
  1121. e.getStyle().setPropertyPx("height", height);
  1122. }
  1123. return this;
  1124. }
  1125. /**
  1126. * Set the height style property of every matched element. It's useful for
  1127. * using 'percent' or 'em' units Example: $(".a").width("100%")
  1128. */
  1129. public GQuery height(String height) {
  1130. return css("height", height);
  1131. }
  1132. /**
  1133. * Make invisible all matched elements.
  1134. */
  1135. public GQuery hide() {
  1136. for (Element e : elements()) {
  1137. Object old = data(e, "oldDisplay", null);
  1138. if (old == null) {
  1139. data(e, "oldDisplay", styleImpl.curCSS(e, "display", false));
  1140. }
  1141. e.getStyle().setDisplay(Display.NONE);
  1142. }
  1143. return this;
  1144. }
  1145. /**
  1146. * Bind a function to the mouseover event of each matched element. A method
  1147. * for simulating hovering (moving the mouse on, and off, an object). This is
  1148. * a custom method which provides an 'in' to a frequent task. Whenever the
  1149. * mouse cursor is moved over a matched element, the first specified function
  1150. * is fired. Whenever the mouse moves off of the element, the second specified
  1151. * function fires.
  1152. */
  1153. public GQuery hover(Function fover, Function fout) {
  1154. return bind(Event.ONMOUSEOVER, null, fover).bind(Event.ONMOUSEOUT, null, fout);
  1155. }
  1156. /**
  1157. * Get the innerHTML of the first matched element.
  1158. */
  1159. public String html() {
  1160. return get(0).getInnerHTML();
  1161. }
  1162. /**
  1163. * Set the innerHTML of every matched element.
  1164. */
  1165. public GQuery html(String html) {
  1166. for (Element e : elements()) {
  1167. if (e.getNodeType() == Node.DOCUMENT_NODE) {
  1168. e = e.<Document>cast().getBody();
  1169. }
  1170. e.setInnerHTML(html);
  1171. }
  1172. return this;
  1173. }
  1174. /**
  1175. * Find the index of the specified Element.
  1176. */
  1177. public int index(Element element) {
  1178. for (int i = 0; i < elements.getLength(); i++) {
  1179. if (elements.getItem(i) == element) {
  1180. return i;
  1181. }
  1182. }
  1183. return -1;
  1184. }
  1185. /**
  1186. * Insert all of the matched elements after another, specified, set of
  1187. * elements.
  1188. */
  1189. public GQuery insertAfter(Element elem) {
  1190. return insertAfter($(elem));
  1191. }
  1192. /**
  1193. * Insert all of the matched elements after another, specified, set of
  1194. * elements.
  1195. */
  1196. public GQuery insertAfter(GQuery query) {
  1197. for (Element e : elements()) {
  1198. query.after(e);
  1199. }
  1200. return this;
  1201. }
  1202. /**
  1203. * Insert all of the matched elements after another, specified, set of
  1204. * elements.
  1205. */
  1206. public GQuery insertAfter(String selector) {
  1207. return insertAfter($(selector));
  1208. }
  1209. /**
  1210. * Insert all of the matched elements before another, specified, set of
  1211. * elements.
  1212. *
  1213. * The elements must already be inserted into the document (you can't insert
  1214. * an element after another if it's not in the page).
  1215. */
  1216. public GQuery insertBefore(Element item) {
  1217. return insertBefore($(item));
  1218. }
  1219. /**
  1220. * Insert all of the matched elements before another, specified, set of
  1221. * elements.
  1222. *
  1223. * The elements must already be inserted into the document (you can't insert
  1224. * an element after another if it's not in the page).
  1225. */
  1226. public GQuery insertBefore(GQuery query) {
  1227. for (Element e : elements()) {
  1228. query.before(e);
  1229. }
  1230. return this;
  1231. }
  1232. /**
  1233. * Insert all of the matched elements before another, specified, set of
  1234. * elements.
  1235. *
  1236. * The elements must already be inserted into the document (you can't insert
  1237. * an element after another if it's not in the page).
  1238. */
  1239. public GQuery insertBefore(String selector) {
  1240. return insertBefore($(selector));
  1241. }
  1242. /**
  1243. * Checks the current selection against an expression and returns true, if at
  1244. * least one element of the selection fits the given expression. Does return
  1245. * false, if no element fits or the expression is not valid.
  1246. */
  1247. public boolean is(String... filters) {
  1248. return filter(filters).size() > 0;
  1249. }
  1250. /**
  1251. * Bind a set of functions to the keydown event of each matched element.
  1252. * Or trigger the event if no functions are provided.
  1253. */
  1254. public GQuery keydown(Function...f) {
  1255. return bindOrFire(Event.ONKEYDOWN, null, f);
  1256. }
  1257. /**
  1258. * Trigger a keydown event passing the key pushed.
  1259. */
  1260. public GQuery keydown(int key) {
  1261. return trigger(Event.ONKEYDOWN, key);
  1262. }
  1263. /**
  1264. * Bind a set of functions to the keypress event of each matched element.
  1265. * Or trigger the event if no functions are provided.
  1266. */
  1267. public GQuery keypress(Function...f) {
  1268. return bindOrFire(Event.ONKEYPRESS, null, f);
  1269. }
  1270. /**
  1271. * Trigger a keypress event passing the key pushed.
  1272. */
  1273. public GQuery keypress(int key) {
  1274. return trigger(Event.ONKEYPRESS, key);
  1275. }
  1276. /**
  1277. * Bind a set of functions to the keyup event of each matched element.
  1278. * Or trigger the event if no functions are provided.
  1279. */
  1280. public GQuery keyup(Function...f) {
  1281. return bindOrFire(Event.ONKEYUP, null, f);
  1282. }
  1283. /**
  1284. * Trigger a keyup event passing the key pushed.
  1285. */
  1286. public GQuery keyup(int key) {
  1287. return trigger(Event.ONKEYUP, key);
  1288. }
  1289. /**
  1290. * Reduce the set of matched elements to the final one in the set.
  1291. */
  1292. public GQuery last() {
  1293. return eq(size() - 1);
  1294. }
  1295. /**
  1296. * Returns the computed left position of the first element matched.
  1297. */
  1298. public int left() {
  1299. return (int) GQUtils.cur(get(0), "left", true);
  1300. }
  1301. /**
  1302. * Returns the number of elements currently matched. The size function will
  1303. * return the same value.
  1304. */
  1305. public int length() {
  1306. return size();
  1307. }
  1308. /**
  1309. * Bind a function to the load event of each matched element.
  1310. */
  1311. public GQuery load(Function f) {
  1312. return bind(Event.ONLOAD, null, f);
  1313. }
  1314. /**
  1315. * Reduce the set of matched elements to all elements before a given position.
  1316. * The position of the element in the set of matched elements starts at 0 and
  1317. * goes to length - 1.
  1318. */
  1319. public GQuery lt(int pos) {
  1320. return $(slice(0, pos));
  1321. }
  1322. /**
  1323. * Pass each element in the current matched set through a function,
  1324. * producing a new array containing the return values.
  1325. */
  1326. @SuppressWarnings("unchecked")
  1327. public <W> ArrayList<W> map(Function f) {
  1328. @SuppressWarnings("rawtypes")
  1329. ArrayList ret = new ArrayList();
  1330. for (int i = 0; i < elements().length; i++) {
  1331. Object o = f.f(elements()[i], i);
  1332. if (o != null) {
  1333. ret.add(o);
  1334. }
  1335. }
  1336. return ret;
  1337. }
  1338. /**
  1339. * Bind a set of functions to the mousedown event of each matched element.
  1340. * Or trigger the event if no functions are provided.
  1341. */
  1342. public GQuery mousedown(Function...f) {
  1343. return bindOrFire(Event.ONMOUSEDOWN, null, f);
  1344. }
  1345. /**
  1346. * Bind a set of functions to the mousemove event of each matched element.
  1347. * Or trigger the event if no functions are provided.
  1348. */
  1349. public GQuery mousemove(Function...f) {
  1350. return bindOrFire(Event.ONMOUSEMOVE, null, f);
  1351. }
  1352. /**
  1353. * Bind a set of functions to the mouseout event of each matched element.
  1354. * Or trigger the event if no functions are provided.
  1355. */
  1356. public GQuery mouseout(Function...f) {
  1357. return bindOrFire(Event.ONMOUSEOUT, null, f);
  1358. }
  1359. /**
  1360. * Bind a set of functions to the mouseover event of each matched element.
  1361. * Or trigger the event if no functions are provided.
  1362. */
  1363. public GQuery mouseover(Function...f) {
  1364. return bindOrFire(Event.ONMOUSEOVER, null, f);
  1365. }
  1366. /**
  1367. * Bind a set of functions to the mouseup event of each matched element.
  1368. * Or trigger the event if no functions are provided.
  1369. */
  1370. public GQuery mouseup(Function...f) {
  1371. return bindOrFire(Event.ONMOUSEUP, null, f);
  1372. }
  1373. /**
  1374. * Get a set of elements containing the unique next siblings of each of the
  1375. * given set of elements. next only returns the very next sibling for each
  1376. * element, not all next siblings see {#nextAll}.
  1377. */
  1378. public GQuery next() {
  1379. JSArray result = JSArray.create();
  1380. for (Element e : elements()) {
  1381. Element next = e.getNextSiblingElement();
  1382. if (next != null) {
  1383. result.addNode(next);
  1384. }
  1385. }
  1386. return pushStack(unique(result), "next", getSelector());
  1387. }
  1388. /**
  1389. * Get a set of elements containing the unique next siblings of each of the
  1390. * given set of elements filtered by 1 or more selectors. next only returns
  1391. * the very next sibling for each element, not all next siblings see
  1392. * {#nextAll}.
  1393. */
  1394. public GQuery next(String... selectors) {
  1395. JSArray result = JSArray.create();
  1396. for (Element e : elements()) {
  1397. Element next = e.getNextSiblingElement();
  1398. if (next != null) {
  1399. result.addNode(next);
  1400. }
  1401. }
  1402. return pushStack(result, "next", selectors[0]).filter(selectors);
  1403. }
  1404. /**
  1405. * Find all sibling elements after the current element.
  1406. */
  1407. public GQuery nextAll() {
  1408. JSArray result = JSArray.create();
  1409. for (Element e : elements()) {
  1410. allNextSiblingElements(e.getNextSiblingElement(), result, null);
  1411. }
  1412. return pushStack(unique(result), "nextAll", getSelector());
  1413. }
  1414. /**
  1415. * Removes the specified Element from the set of matched elements. This method
  1416. * is used to remove a single Element from a jQuery object.
  1417. */
  1418. public GQuery not(Element elem) {
  1419. JSArray array = JSArray.create();
  1420. for (Element e : elements()) {
  1421. if (e != elem) {
  1422. array.addNode(e);
  1423. }
  1424. }
  1425. return $(array);
  1426. }
  1427. /**
  1428. * Removes any elements inside the passed set of elements from the set of
  1429. * matched elements.
  1430. */
  1431. public GQuery not(GQuery gq) {
  1432. GQuery ret = this;
  1433. for (Element e : gq.elements()) {
  1434. ret = ret.not(e);
  1435. }
  1436. return ret;
  1437. }
  1438. /**
  1439. * Removes elements matching the specified expression from the set of matched
  1440. * elements.
  1441. */
  1442. public GQuery not(String... filters) {
  1443. GQuery ret = this;
  1444. for (String f : filters) {
  1445. ret = ret.not($(f));
  1446. }
  1447. return ret;
  1448. }
  1449. /**
  1450. * Get the current offset of the first matched element, in pixels, relative to
  1451. * the document. The returned object contains two integer properties, top and
  1452. * left. The method works only with visible elements.
  1453. */
  1454. public com.google.gwt.query.client.GQuery.Offset offset() {
  1455. return new Offset(get(0).getAbsoluteLeft(), get(0).getAbsoluteTop());
  1456. }
  1457. /**
  1458. * Returns a GQuery collection with the positioned parent of the first matched
  1459. * element. This is the first parent of the element that has position (as in
  1460. * relative or absolute). This method only works with visible elements.
  1461. */
  1462. public GQuery offsetParent() {
  1463. Element offParent = GQUtils.or(elements.getItem(0).getOffsetParent(), body);
  1464. while (offParent != null
  1465. && !"body".equalsIgnoreCase(offParent.getTagName())
  1466. && !"html".equalsIgnoreCase(offParent.getTagName())
  1467. && "static".equals(styleImpl.curCSS(offParent, "position", true))) {
  1468. offParent = offParent.getOffsetParent();
  1469. }
  1470. return new GQuery(offParent);
  1471. }
  1472. /**
  1473. * Binds a handler to a particular Event (like Event.ONCLICK) for each matched
  1474. * element. The handler is executed only once for each element.
  1475. *
  1476. * The event handler is passed as a Function that you can use to prevent
  1477. * default behavior. To stop both default action and event bubbling, the
  1478. * function event handler has to return false.
  1479. *
  1480. * You can pass an additional Object data to your Function as the second
  1481. * parameter
  1482. */
  1483. public GQuery one(int eventbits, final Object data, final Function f) {
  1484. return as(Events).one(eventbits, data, f);
  1485. }
  1486. /**
  1487. * Get the current computed height for the first element in the set of matched elements,
  1488. * including padding, border, but not the margin.
  1489. */
  1490. public int outerHeight(){
  1491. return outerHeight(false);
  1492. }
  1493. /**
  1494. * Get the current computed height for the first element in the set of matched elements,
  1495. * including padding, border, and optionally margin.
  1496. */
  1497. public int outerHeight(boolean includeMargin){
  1498. int outerHeight = get(0).getOffsetHeight(); //height including padding and border
  1499. if (includeMargin){
  1500. outerHeight+=GQUtils.cur( get(0), "marginTop", true)+GQUtils.cur( get(0), "marginBottom", true);
  1501. }
  1502. return outerHeight;
  1503. }
  1504. /**
  1505. * Get the current computed width for the first element in the set of matched elements,
  1506. * including padding, border, but not the margin.
  1507. */
  1508. public int outerWidth(){
  1509. return outerWidth(false);
  1510. }
  1511. /**
  1512. * Get the current computed width for the first element in the set of matched elements,
  1513. * including padding and border and optionally margin.
  1514. */
  1515. public int outerWidth(boolean includeMargin){
  1516. int outerWidth = get(0).getOffsetWidth(); //width including padding and border
  1517. if (includeMargin){
  1518. outerWidth+=GQUtils.cur( get(0), "marginRight", true)+GQUtils.cur( get(0), "marginLeft", true);
  1519. }
  1520. return outerWidth;
  1521. }
  1522. /**
  1523. * Get a set of elements containing the unique parents of the matched set of
  1524. * elements.
  1525. */
  1526. public GQuery parent() {
  1527. JSArray result = JSArray.create();
  1528. for (Element e : elements()) {
  1529. Element p = e.getParentElement();
  1530. if (p != null) {
  1531. result.addNode(p);
  1532. }
  1533. }
  1534. return new GQuery(unique(result));
  1535. }
  1536. /**
  1537. * Get a set of elements containing the unique parents of the matched set of
  1538. * elements. You may use an optional expressions to filter the set of parent
  1539. * elements that will match one of them.
  1540. */
  1541. public GQuery parent(String... filters) {
  1542. return parent().filter(filters);
  1543. }
  1544. /**
  1545. * Get a set of elements containing the unique ancestors of the matched set of
  1546. * elements (except for the root element).
  1547. */
  1548. public GQuery parents() {
  1549. JSArray result = JSArray.create();
  1550. for (Element e : elements()) {
  1551. Node par = e.getParentNode();
  1552. while (par != null && par != document) {
  1553. result.addNode(par);
  1554. par = par.getParentNode();
  1555. }
  1556. }
  1557. return new GQuery(unique(result));
  1558. }
  1559. /**
  1560. * Get a set of elements containing the unique ancestors of the matched set of
  1561. * elements (except for the root element). The matched elements are filtered,
  1562. * returning those that match any of the filters.
  1563. */
  1564. public GQuery parents(String... filters) {
  1565. return parents().filter(filters);
  1566. }
  1567. /**
  1568. * Gets the top and left position of an element relative to its offset parent.
  1569. * The returned object contains two Integer properties, top and left. For
  1570. * accurate calculations make sure to use pixel values for margins, borders
  1571. * and padding. This method only works with visible elements.
  1572. */
  1573. public com.google.gwt.query.client.GQuery.Offset position() {
  1574. Element element = get(0);
  1575. if (element == null) {
  1576. return null;
  1577. }
  1578. // Get *real* offsetParent
  1579. Element offsetParent = element.getOffsetParent();
  1580. // Get correct offsets
  1581. Offset offset = offset();
  1582. Offset parentOffset = null;
  1583. if ("body".equalsIgnoreCase(offsetParent.getNodeName())
  1584. || "html".equalsIgnoreCase(offsetParent.getNodeName())) {
  1585. parentOffset = new Offset(0, 0);
  1586. } else {
  1587. parentOffset = $(offsetParent).offset();
  1588. }
  1589. // Subtract element margins
  1590. int topMargin = (int) GQUtils.cur(element, "marginTop", true);
  1591. // When margin-left = auto, Safari and chrome return a value while IE and
  1592. // Firefox return 0
  1593. // force the margin-left to 0 if margin-left = auto.
  1594. int leftMargin = 0;
  1595. if (!"auto".equals(element.getStyle().getMarginLeft())) {
  1596. leftMargin = (int) GQUtils.cur(element, "marginLeft", true);
  1597. }
  1598. offset = offset.add(-leftMargin, -topMargin);
  1599. // Add offsetParent borders
  1600. int parentOffsetBorderTop = (int) GQUtils.cur(offsetParent,
  1601. "borderTopWidth", true);
  1602. int parentOffsetBorderLeft = (int) GQUtils.cur(offsetParent,
  1603. "borderLeftWidth", true);
  1604. parentOffset = parentOffset.add(parentOffsetBorderLeft,
  1605. parentOffsetBorderTop);
  1606. // Subtract the two offsets
  1607. return offset.add(-parentOffset.left, -parentOffset.top);
  1608. }
  1609. /**
  1610. * Prepend content to the inside of every matched element. This operation is
  1611. * the best way to insert elements inside, at the beginning, of all matched
  1612. * elements.
  1613. */
  1614. public GQuery prepend(GQuery query) {
  1615. return domManip(query, FUNC_PREPEND);
  1616. }
  1617. /**
  1618. * Prepend content to the inside of every matched element. This operation is
  1619. * the best way to insert elements inside, at the beginning, of all matched
  1620. * elements.
  1621. */
  1622. public GQuery prepend(Node n) {
  1623. return domManip($(n), FUNC_PREPEND);
  1624. }
  1625. /**
  1626. * Prepend content to the inside of every matched element. This operation is
  1627. * the best way to insert elements inside, at the beginning, of all matched
  1628. * elements.
  1629. */
  1630. public GQuery prepend(String html) {
  1631. return domManip(html, FUNC_PREPEND);
  1632. }
  1633. /**
  1634. * All of the matched set of elements will be inserted at the beginning
  1635. * of the element(s) specified by the parameter other.
  1636. *
  1637. * The operation $(A).prependTo(B) is, essentially, the reverse of doing a regular
  1638. * $(A).prepend(B), instead of prepending B to A, you're prepending A to B.
  1639. */
  1640. public GQuery prependTo(GQuery other) {
  1641. other.prepend(this);
  1642. return this;
  1643. }
  1644. /**
  1645. * All of the matched set of elements will be inserted at the beginning
  1646. * of the element(s) specified by the parameter other.
  1647. *
  1648. * The operation $(A).prependTo(B) is, essentially, the reverse of doing a regular
  1649. * $(A).prepend(B), instead of prepending B to A, you're prepending A to B.
  1650. */
  1651. public GQuery prependTo(Node n) {
  1652. $(n).prepend(this);
  1653. return this;
  1654. }
  1655. /**
  1656. * All of the matched set of elements will be inserted at the beginning
  1657. * of the element(s) specified by the parameter other.
  1658. *
  1659. * The operation $(A).prependTo(B) is, essentially, the reverse of doing a regular
  1660. * $(A).prepend(B), instead of prepending B to A, you're prepending A to B.
  1661. */
  1662. public GQuery prependTo(String html) {
  1663. $(html).prepend(this);
  1664. return this;
  1665. }
  1666. /**
  1667. * Get a set of elements containing the unique previous siblings of each of
  1668. * the matched set of elements. Only the immediately previous sibling is
  1669. * returned, not all previous siblings.
  1670. */
  1671. public GQuery prev() {
  1672. JSArray result = JSArray.create();
  1673. for (Element e : elements()) {
  1674. Element next = getPreviousSiblingElement(e);
  1675. if (next != null) {
  1676. result.addNode(next);
  1677. }
  1678. }
  1679. return new GQuery(unique(result));
  1680. }
  1681. /**
  1682. * Get a set of elements containing the unique previous siblings of each of
  1683. * the matched set of elements filtered by selector. Only the immediately
  1684. * previous sibling is returned, not all previous siblings.
  1685. */
  1686. public GQuery prev(String... selectors) {
  1687. JSArray result = JSArray.create();
  1688. for (Element e : elements()) {
  1689. Element next = getPreviousSiblingElement(e);
  1690. if (next != null) {
  1691. result.addNode(next);
  1692. }
  1693. }
  1694. return new GQuery(unique(result)).filter(selectors);
  1695. }
  1696. /**
  1697. * Find all sibling elements in front of the current element.
  1698. */
  1699. public GQuery prevAll() {
  1700. JSArray result = JSArray.create();
  1701. for (Element e : elements()) {
  1702. allPreviousSiblingElements(getPreviousSiblingElement(e), result);
  1703. }
  1704. return pushStack(unique(result), "prevAll", getSelector());
  1705. }
  1706. /**
  1707. * Removes all matched elements from the DOM.
  1708. */
  1709. public GQuery remove() {
  1710. for (Element e : elements()) {
  1711. // TODO: cleanup event bindings
  1712. removeData(e, null);
  1713. if (e.getParentNode() != null) {
  1714. e.getParentNode().removeChild(e);
  1715. }
  1716. }
  1717. return this;
  1718. }
  1719. /**
  1720. * Remove the named attribute from every element in the matched set.
  1721. */
  1722. public GQuery removeAttr(String key) {
  1723. for (Element e : elements()) {
  1724. e.removeAttribute(key);
  1725. }
  1726. return this;
  1727. }
  1728. /**
  1729. * Removes the specified classes to each matched element.
  1730. */
  1731. public GQuery removeClass(String... classes) {
  1732. for (Element e : elements()) {
  1733. for (String clz : classes) {
  1734. e.removeClassName(clz);
  1735. }
  1736. }
  1737. return this;
  1738. }
  1739. /**
  1740. * Removes named data store from an element.
  1741. */
  1742. public GQuery removeData(String name) {
  1743. for (Element e : elements()) {
  1744. removeData(e, name);
  1745. }
  1746. return this;
  1747. }
  1748. /**
  1749. * Replaces the elements matched by the specified selector with the matched
  1750. * elements. This function is the complement to replaceWith() which does the
  1751. * same task with the parameters reversed.
  1752. */
  1753. public GQuery replaceAll(Element elem) {
  1754. return replaceAll($(elem));
  1755. }
  1756. /**
  1757. * Replaces the elements matched by the specified selector with the matched
  1758. * elements. This function is the complement to replaceWith() which does the
  1759. * same task with the parameters reversed.
  1760. */
  1761. public GQuery replaceAll(GQuery query) {
  1762. for (Element e : elements()) {
  1763. $(e).replaceWith(query);
  1764. }
  1765. return this;
  1766. }
  1767. /**
  1768. * Replaces the elements matched by the specified selector with the matched
  1769. * elements. This function is the complement to replaceWith() which does the
  1770. * same task with the parameters reversed.
  1771. */
  1772. public GQuery replaceAll(String html) {
  1773. return replaceAll($(html));
  1774. }
  1775. /**
  1776. * Replaces all matched elements with the specified HTML or DOM elements. This
  1777. * returns the GQuery element that was just replaced, which has been removed
  1778. * from the DOM.
  1779. */
  1780. public GQuery replaceWith(Element elem) {
  1781. return replaceWith($(elem));
  1782. }
  1783. /**
  1784. * Replaces all matched elements with the specified HTML or DOM elements. This
  1785. * returns the GQuery element that was just replaced, which has been removed
  1786. * from the DOM.
  1787. */
  1788. public GQuery replaceWith(GQuery query) {
  1789. return after(query).remove();
  1790. }
  1791. /**
  1792. * Replaces all matched elements with the specified HTML or DOM elements. This
  1793. * returns the GQuery element that was just replaced, which has been removed
  1794. * from the DOM.
  1795. */
  1796. public GQuery replaceWith(String html) {
  1797. return replaceWith($(html));
  1798. }
  1799. /**
  1800. * Save a set of Css properties of every matched element.
  1801. */
  1802. public void restoreCssAttrs(String... cssProps) {
  1803. for (Element e : elements()) {
  1804. for (String a : cssProps) {
  1805. styleImpl.setStyleProperty(e, a, (String) data(e, OLD_DATA_PREFIX + a, null));
  1806. }
  1807. }
  1808. }
  1809. /**
  1810. * Restore a set of previously saved Css properties in every matched element.
  1811. */
  1812. public void saveCssAttrs(String... cssProps) {
  1813. for (Element e : elements()) {
  1814. for (String a : cssProps) {
  1815. data(OLD_DATA_PREFIX + a, styleImpl.curCSS(e, a, false));
  1816. }
  1817. }
  1818. }
  1819. /**
  1820. * Bind a set of functions to the scroll event of each matched element.
  1821. * Or trigger the event if no functions are provided.
  1822. */
  1823. public GQuery scroll(Function...f) {
  1824. return bindOrFire(Event.ONSCROLL, null, f);
  1825. }
  1826. /**
  1827. * Gets the scroll left offset of the first matched element. This method works
  1828. * for both visible and hidden elements.
  1829. */
  1830. public int scrollLeft() {
  1831. Element e = get(0);
  1832. if (e == window) {
  1833. return Window.getScrollLeft();
  1834. } else if (e == (Node) document) {
  1835. return document.getScrollLeft();
  1836. } else {
  1837. return e.getScrollLeft();
  1838. }
  1839. }
  1840. /**
  1841. * When a value is passed in, the scroll left offset is set to that value on
  1842. * all matched elements. This method works for both visible and hidden
  1843. * elements.
  1844. */
  1845. public GQuery scrollLeft(int left) {
  1846. for (Element e : elements()) {
  1847. if (e == window || e == (Node) document) {
  1848. Window.scrollTo(left, $(e).scrollTop());
  1849. } else {
  1850. e.setPropertyInt("scrollLeft", left);
  1851. }
  1852. }
  1853. return this;
  1854. }
  1855. /**
  1856. * Gets the scroll top offset of the first matched element. This method works
  1857. * for both visible and hidden elements.
  1858. */
  1859. public int scrollTop() {
  1860. Element e = get(0);
  1861. if (e == window) {
  1862. return Window.getScrollTop();
  1863. } else if (e == (Node) document) {
  1864. return document.getScrollTop();
  1865. } else {
  1866. return e.getScrollTop();
  1867. }
  1868. }
  1869. /**
  1870. * When a value is passed in, the scroll top offset is set to that value on
  1871. * all matched elements. This method works for both visible and hidden
  1872. * elements.
  1873. */
  1874. public GQuery scrollTop(int top) {
  1875. for (Element e : elements()) {
  1876. if (e == window || e == (Node) document) {
  1877. Window.scrollTo($(e).scrollLeft(), top);
  1878. } else {
  1879. e.setPropertyInt("scrollTop", top);
  1880. }
  1881. }
  1882. return this;
  1883. }
  1884. public GQuery select() {
  1885. return as(Events).triggerHtmlEvent("select");
  1886. }
  1887. /**
  1888. * Force the current matched set of elements to become
  1889. * the specified array of elements.
  1890. */
  1891. public GQuery setArray(NodeList<Element> nodes) {
  1892. this.elements = nodes;
  1893. return this;
  1894. }
  1895. /**
  1896. * Set CSS property on the first element.
  1897. */
  1898. public <S, T extends CssProperty<S>> GQuery setCss(T cssProperty, S value) {
  1899. cssProperty.set(elements.getItem(0).getStyle(), value);
  1900. return this;
  1901. }
  1902. /**
  1903. * Set CSS property on first matched element using type-safe enumerations.
  1904. */
  1905. public GQuery setCss(TakesLength cssProperty, Length value) {
  1906. cssProperty.setLength(elements.getItem(0).getStyle(), value);
  1907. return this;
  1908. }
  1909. /**
  1910. * Set CSS property on first matched element using type-safe enumerations.
  1911. */
  1912. public GQuery setCss(TakesPercentage cssProperty, Percentage value) {
  1913. cssProperty.setPercentage(elements.getItem(0).getStyle(), value);
  1914. return this;
  1915. }
  1916. public void setPreviousObject(GQuery previousObject) {
  1917. this.previousObject = previousObject;
  1918. }
  1919. public void setSelector(String selector) {
  1920. this.currentSelector = selector;
  1921. }
  1922. /**
  1923. * Return the number of elements in the matched set. Make visible all mached
  1924. * elements
  1925. */
  1926. public GQuery show() {
  1927. for (Element e : elements()) {
  1928. styleImpl.setStyleProperty(e, "display",
  1929. GQUtils.or((String) data(e, "oldDisplay", null), ""));
  1930. // When the display=none is in the stylesheet.
  1931. if (!styleImpl.isVisible(e)) {
  1932. styleImpl.setStyleProperty(e, "display", "block");
  1933. }
  1934. }
  1935. return this;
  1936. }
  1937. /**
  1938. * Get a set of elements containing all of the unique siblings of each of the
  1939. * matched set of elements.
  1940. */
  1941. public GQuery siblings() {
  1942. JSArray result = JSArray.create();
  1943. for (Element e : elements()) {
  1944. allNextSiblingElements(e.getParentElement().getFirstChildElement(),
  1945. result, e);
  1946. }
  1947. return new GQuery(unique(result));
  1948. }
  1949. /**
  1950. * Get a set of elements containing all of the unique siblings of each of the
  1951. * matched set of elements filtered by the provided set of selectors.
  1952. */
  1953. public GQuery siblings(String... selectors) {
  1954. return siblings().filter(selectors);
  1955. }
  1956. /**
  1957. * Return the number of elements in the matched set.
  1958. */
  1959. public int size() {
  1960. return elements.getLength();
  1961. }
  1962. /**
  1963. * Selects a subset of the matched elements.
  1964. */
  1965. public GQuery slice(int start, int end) {
  1966. JSArray slice = JSArray.create();
  1967. if (end == -1 || end > elements.getLength()) {
  1968. end = elements.getLength();
  1969. }
  1970. for (int i = start; i < end; i++) {
  1971. slice.addNode(elements.getItem(i));
  1972. }
  1973. return new GQuery(slice);
  1974. }
  1975. public GQuery submit() {
  1976. return as(Events).trigger(EventsListener.ONSUBMIT);
  1977. }
  1978. /**
  1979. * Return the text contained in the first matched element.
  1980. */
  1981. public String text() {
  1982. String result = "";
  1983. for (Element e : elements()) {
  1984. result += e.getInnerText();
  1985. }
  1986. return result;
  1987. }
  1988. /**
  1989. * Set the innerText of every matched element.
  1990. */
  1991. public GQuery text(String txt) {
  1992. for (Element e : asArray(elements)) {
  1993. e.setInnerText(txt);
  1994. }
  1995. return this;
  1996. }
  1997. /**
  1998. * Toggle visibility of elements.
  1999. */
  2000. public GQuery toggle() {
  2001. for (Element e : elements()) {
  2002. if ($(e).visible()) {
  2003. $(e).hide();
  2004. } else {
  2005. $(e).show();
  2006. e.getStyle().setDisplay(Display.BLOCK);
  2007. }
  2008. }
  2009. return this;
  2010. }
  2011. /**
  2012. * Toggle among two or more function calls every other click.
  2013. */
  2014. public GQuery toggle(final Function... fn) {
  2015. return click(new Function() {
  2016. int click = 0;
  2017. public boolean f(Event e) {
  2018. int n = fn.length == 1 ? 0 : (click++ % fn.length);
  2019. return fn[n].f(e);
  2020. }
  2021. });
  2022. }
  2023. /**
  2024. * Adds or removes the specified classes to each matched element
  2025. * depending on the class's presence.
  2026. */
  2027. public GQuery toggleClass(String... classes) {
  2028. for (Element e : elements()) {
  2029. for (String clz : classes) {
  2030. if (hasClass(e, clz)) {
  2031. e.removeClassName(clz);
  2032. } else {
  2033. e.addClassName(clz);
  2034. }
  2035. }
  2036. }
  2037. return this;
  2038. }
  2039. /**
  2040. * Adds or removes the specified classes to each matched element
  2041. * depending on the value of the switch argument.
  2042. *
  2043. * if addOrRemove is true, the class is added and in the case of
  2044. * false it is removed.
  2045. */
  2046. public GQuery toggleClass(String clz, boolean addOrRemove) {
  2047. if (addOrRemove) {
  2048. addClass(clz);
  2049. } else {
  2050. removeClass(clz);
  2051. }
  2052. return this;
  2053. }
  2054. /**
  2055. * Returns the computed left position of the first element matched.
  2056. */
  2057. public int top() {
  2058. return (int) GQUtils.cur(get(0), "top", true);
  2059. }
  2060. /**
  2061. * Produces a string representation of the matched elements.
  2062. */
  2063. public String toString() {
  2064. return toString(false);
  2065. }
  2066. /**
  2067. * Produces a string representation of the matched elements.
  2068. */
  2069. public String toString(boolean pretty) {
  2070. String r = "";
  2071. for (Element e : elements()) {
  2072. if (window.equals(e)) {
  2073. continue;
  2074. }
  2075. r += (pretty && r.length() > 0 ? "\n " : "") + e.getString();
  2076. }
  2077. return r;
  2078. }
  2079. /**
  2080. * Trigger a set of events on each matched element.
  2081. *
  2082. * For keyboard events you can pass a second parameter which represents
  2083. * the key-code of the pushed key.
  2084. *
  2085. * Example: fire(Event.ONCLICK | Event.ONFOCUS)
  2086. * Example: fire(Event.ONKEYDOWN. 'a');
  2087. */
  2088. public GQuery trigger(int eventbits, int... keys) {
  2089. return as(Events).trigger(eventbits, keys);
  2090. }
  2091. /**
  2092. * Removes all events that match the eventbits.
  2093. */
  2094. public GQuery unbind(int eventbits) {
  2095. return as(Events).unbind(eventbits);
  2096. }
  2097. /**
  2098. * Remove all duplicate elements from an array of elements. Note that this
  2099. * only works on arrays of DOM elements, not strings or numbers.
  2100. */
  2101. public JSArray unique(JSArray result) {
  2102. return GQUtils.unique(result.<JsArray<Element>>cast()).cast();
  2103. }
  2104. /**
  2105. * Gets the content of the value attribute of the first matched element,
  2106. * returns only the first value even if it is a multivalued element. To get an
  2107. * array of all values in multivalues elements use vals()
  2108. *
  2109. * When the first element is a radio-button and is not checked, then it looks
  2110. * for a the first checked radio-button that has the same name in the list of
  2111. * matched elements.
  2112. */
  2113. public String val() {
  2114. String[] v = vals();
  2115. return (v != null && v.length > 0) ? v[0] : "";
  2116. }
  2117. /**
  2118. * Sets the value attribute of every matched element In the case of multivalue
  2119. * elements, all values are setted for other elements, only the first value is
  2120. * considered.
  2121. */
  2122. public GQuery val(String... values) {
  2123. for (Element e : elements()) {
  2124. String name = e.getNodeName();
  2125. if ("select".equalsIgnoreCase(name)) {
  2126. SelectElement s = SelectElement.as(e);
  2127. s.setSelectedIndex(-1);
  2128. if (values.length > 1 && s.isMultiple()) {
  2129. for (String v : values) {
  2130. for (int i = 0; i < s.getOptions().getLength(); i++) {
  2131. if (v.equals(s.getOptions().getItem(i).getValue())) {
  2132. s.getOptions().getItem(i).setSelected(true);
  2133. }
  2134. }
  2135. }
  2136. } else {
  2137. s.setValue(values[0]);
  2138. }
  2139. } else if ("input".equalsIgnoreCase(name)) {
  2140. InputElement ie = InputElement.as(e);
  2141. String type = ie.getType();
  2142. if ("radio".equalsIgnoreCase((type))
  2143. || "checkbox".equalsIgnoreCase(type)) {
  2144. if ("checkbox".equalsIgnoreCase(type)) {
  2145. for (String val : values) {
  2146. if (ie.getValue().equals(val)) {
  2147. ie.setChecked(true);
  2148. } else {
  2149. ie.setChecked(false);
  2150. }
  2151. }
  2152. } else if (ie.getValue().equals(values[0])) {
  2153. ie.setChecked(true);
  2154. } else {
  2155. ie.setChecked(false);
  2156. }
  2157. } else {
  2158. ie.setValue(values[0]);
  2159. }
  2160. } else if ("textarea".equalsIgnoreCase(name)) {
  2161. TextAreaElement.as(e).setValue(values[0]);
  2162. } else if ("button".equalsIgnoreCase(name)) {
  2163. ButtonElement.as(e).setValue(values[0]);
  2164. }
  2165. }
  2166. return this;
  2167. }
  2168. /**
  2169. * Gets the content of the value attribute of the first matched element,
  2170. * returns more than one value if it is a multiple select.
  2171. *
  2172. * When the first element is a radio-button and is not checked, then it looks
  2173. * for a the first checked radio-button that has the same name in the list of
  2174. * matched elements.
  2175. *
  2176. * This method always returns an array. If no valid value can be determined
  2177. * the array will be empty, otherwise it will contain one or more values.
  2178. */
  2179. public String[] vals() {
  2180. if (size() > 0) {
  2181. Element e = get(0);
  2182. if (e.getNodeName().equalsIgnoreCase("select")) {
  2183. SelectElement se = SelectElement.as(e);
  2184. if (se.isMultiple()) {
  2185. JsArrayString result = JsArrayString.createArray().cast();
  2186. for (OptionElement oe : asArray(se.getOptions())) {
  2187. if (oe.isSelected()) {
  2188. result.set(result.length(), oe.getValue());
  2189. }
  2190. }
  2191. return jsArrayToString(result);
  2192. } else if (se.getSelectedIndex() >= 0) {
  2193. return new String[]{
  2194. se.getOptions().getItem(se.getSelectedIndex()).getValue()};
  2195. }
  2196. } else if (e.getNodeName().equalsIgnoreCase("input")) {
  2197. InputElement ie = InputElement.as(e);
  2198. if ("radio".equalsIgnoreCase(ie.getType())) {
  2199. for (Element e2 : elements()) {
  2200. if ("input".equalsIgnoreCase(e2.getNodeName())) {
  2201. InputElement ie2 = InputElement.as(e2);
  2202. if ("radio".equalsIgnoreCase(ie2.getType()) && ie2.isChecked()
  2203. && ie.getName().equals(ie2.getName())) {
  2204. return new String[]{ie2.getValue()};
  2205. }
  2206. }
  2207. }
  2208. } else if ("checkbox".equalsIgnoreCase(ie.getType())) {
  2209. if (ie.isChecked()) {
  2210. return new String[]{ie.getValue()};
  2211. }
  2212. } else {
  2213. return new String[]{ie.getValue()};
  2214. }
  2215. } else if (e.getNodeName().equalsIgnoreCase("textarea")) {
  2216. return new String[]{TextAreaElement.as(e).getValue()};
  2217. } else if (e.getNodeName().equalsIgnoreCase("button")) {
  2218. return new String[]{ButtonElement.as(e).getValue()};
  2219. }
  2220. }
  2221. return new String[0];
  2222. }
  2223. /**
  2224. * Return true if the first element is visible.
  2225. */
  2226. public boolean visible() {
  2227. return styleImpl.isVisible(get(0));
  2228. }
  2229. /**
  2230. * Return the first non null attached widget from the matched elements
  2231. * or null if there isn't any.
  2232. */
  2233. public <W extends Widget> W widget(){
  2234. for (Element e : elements()){
  2235. @SuppressWarnings("unchecked")
  2236. W w = (W) getAssociatedWidget(e);
  2237. if (w != null){
  2238. return w;
  2239. }
  2240. }
  2241. return null;
  2242. }
  2243. /**
  2244. * return the list of attached widgets matching the query
  2245. */
  2246. public List<Widget> widgets(){
  2247. List<Widget> widgets = new ArrayList<Widget>();
  2248. for (Element e : elements()){
  2249. Widget w = getAssociatedWidget(e);
  2250. if (w != null){
  2251. widgets.add(w);
  2252. }
  2253. }
  2254. return widgets;
  2255. }
  2256. /**
  2257. * Get the current computed, pixel, width of the first matched element.
  2258. */
  2259. public int width() {
  2260. return get(0).getClientWidth();
  2261. }
  2262. /**
  2263. * Set the width of every matched element.
  2264. */
  2265. public GQuery width(int width) {
  2266. for (Element e : elements()) {
  2267. e.getStyle().setPropertyPx("width", width);
  2268. }
  2269. return this;
  2270. }
  2271. /**
  2272. * Wrap each matched element with the specified HTML content. This wrapping
  2273. * process is most useful for injecting additional structure into a document,
  2274. * without ruining the original semantic qualities of a document. This works
  2275. * by going through the first element provided (which is generated, on the
  2276. * fly, from the provided HTML) and finds the deepest descendant element
  2277. * within its structure -- it is that element that will enwrap everything
  2278. * else.
  2279. */
  2280. public GQuery wrap(Element elem) {
  2281. return wrap($(elem));
  2282. }
  2283. /**
  2284. * Wrap each matched element with the specified HTML content. This wrapping
  2285. * process is most useful for injecting additional structure into a document,
  2286. * without ruining the original semantic qualities of a document. This works
  2287. * by going through the first element provided (which is generated, on the
  2288. * fly, from the provided HTML) and finds the deepest descendant element
  2289. * within its structure -- it is that element that will enwrap everything
  2290. * else.
  2291. */
  2292. public GQuery wrap(GQuery query) {
  2293. for (Element e : elements()) {
  2294. $(e).wrapAll(query);
  2295. }
  2296. return this;
  2297. }
  2298. /**
  2299. * Wrap each matched element with the specified HTML content. This wrapping
  2300. * process is most useful for injecting additional structure into a document,
  2301. * without ruining the original semantic qualities of a document. This works
  2302. * by going through the first element provided (which is generated, on the
  2303. * fly, from the provided HTML) and finds the deepest descendant element
  2304. * within its structure -- it is that element that will enwrap everything
  2305. * else.
  2306. */
  2307. public GQuery wrap(String html) {
  2308. return wrap($(html));
  2309. }
  2310. /**
  2311. * Wrap all the elements in the matched set into a single wrapper element.
  2312. * This is different from .wrap() where each element in the matched set would
  2313. * get wrapped with an element. This wrapping process is most useful for
  2314. * injecting additional structure into a document, without ruining the
  2315. * original semantic qualities of a document.
  2316. *
  2317. * This works by going through the first element provided (which is generated,
  2318. * on the fly, from the provided HTML) and finds the deepest descendant
  2319. * element within its structure -- it is that element that will enwrap
  2320. * everything else.
  2321. */
  2322. public GQuery wrapAll(Element elem) {
  2323. return wrapAll($(elem));
  2324. }
  2325. /**
  2326. * Wrap all the elements in the matched set into a single wrapper element.
  2327. * This is different from .wrap() where each element in the matched set would
  2328. * get wrapped with an element. This wrapping process is most useful for
  2329. * injecting additional structure into a document, without ruining the
  2330. * original semantic qualities of a document.
  2331. *
  2332. * This works by going through the first element provided (which is generated,
  2333. * on the fly, from the provided HTML) and finds the deepest descendant
  2334. * element within its structure -- it is that element that will enwrap
  2335. * everything else.
  2336. */
  2337. public GQuery wrapAll(GQuery query) {
  2338. GQuery wrap = query.clone();
  2339. if (elements.getItem(0).getParentNode() != null) {
  2340. wrap.insertBefore(elements.getItem(0));
  2341. }
  2342. for (Element e : wrap.elements()) {
  2343. Node n = e;
  2344. while (n.getFirstChild() != null
  2345. && n.getFirstChild().getNodeType() == Node.ELEMENT_NODE) {
  2346. n = n.getFirstChild();
  2347. }
  2348. $((Element) n).append(this);
  2349. }
  2350. return this;
  2351. }
  2352. /**
  2353. * Wrap all the elements in the matched set into a single wrapper element.
  2354. * This is different from .wrap() where each element in the matched set would
  2355. * get wrapped with an element. This wrapping process is most useful for
  2356. * injecting additional structure into a document, without ruining the
  2357. * original semantic qualities of a document.
  2358. *
  2359. * This works by going through the first element provided (which is generated,
  2360. * on the fly, from the provided HTML) and finds the deepest descendant
  2361. * element within its structure -- it is that element that will enwrap
  2362. * everything else.
  2363. */
  2364. public GQuery wrapAll(String html) {
  2365. return wrapAll($(html));
  2366. }
  2367. /**
  2368. * Wrap the inner child contents of each matched element (including text
  2369. * nodes) with an HTML structure. This wrapping process is most useful for
  2370. * injecting additional structure into a document, without ruining the
  2371. * original semantic qualities of a document. This works by going through the
  2372. * first element provided (which is generated, on the fly, from the provided
  2373. * HTML) and finds the deepest ancestor element within its structure -- it is
  2374. * that element that will enwrap everything else.
  2375. */
  2376. public GQuery wrapInner(Element elem) {
  2377. return wrapInner($(elem));
  2378. }
  2379. /**
  2380. * Wrap the inner child contents of each matched element (including text
  2381. * nodes) with an HTML structure. This wrapping process is most useful for
  2382. * injecting additional structure into a document, without ruining the
  2383. * original semantic qualities of a document. This works by going through the
  2384. * first element provided (which is generated, on the fly, from the provided
  2385. * HTML) and finds the deepest ancestor element within its structure -- it is
  2386. * that element that will enwrap everything else.
  2387. */
  2388. public GQuery wrapInner(GQuery query) {
  2389. for (Element e : elements()) {
  2390. $(e).contents().wrapAll(query);
  2391. }
  2392. return this;
  2393. }
  2394. /**
  2395. * Wrap the inner child contents of each matched element (including text
  2396. * nodes) with an HTML structure. This wrapping process is most useful for
  2397. * injecting additional structure into a document, without ruining the
  2398. * original semantic qualities of a document. This works by going through the
  2399. * first element provided (which is generated, on the fly, from the provided
  2400. * HTML) and finds the deepest ancestor element within its structure -- it is
  2401. * that element that will enwrap everything else.
  2402. */
  2403. public GQuery wrapInner(String html) {
  2404. return wrapInner($(html));
  2405. }
  2406. protected GQuery pushStack(JSArray elts, String name, String selector) {
  2407. GQuery g = new GQuery(elts);
  2408. g.setPreviousObject(this);
  2409. g.setSelector(selector);
  2410. return g;
  2411. }
  2412. private void allNextSiblingElements(Element firstChildElement, JSArray result,
  2413. Element elem) {
  2414. while (firstChildElement != null) {
  2415. if (firstChildElement != elem) {
  2416. result.addNode(firstChildElement);
  2417. }
  2418. firstChildElement = firstChildElement.getNextSiblingElement();
  2419. }
  2420. }
  2421. private void allPreviousSiblingElements(Element firstChildElement,
  2422. JSArray result) {
  2423. while (firstChildElement != null) {
  2424. result.addNode(firstChildElement);
  2425. firstChildElement = getPreviousSiblingElement(firstChildElement);
  2426. }
  2427. }
  2428. /**
  2429. * Bind Handlers or fire Events for each matched element.
  2430. */
  2431. private GQuery bindOrFire(int eventbits, final Object data, final Function...funcs) {
  2432. if (funcs.length == 0) {
  2433. return trigger(eventbits);
  2434. } else {
  2435. return bind(eventbits, data, funcs);
  2436. }
  2437. }
  2438. private GQuery domManip(GQuery g, int func, Element...elms) {
  2439. JSArray newNodes = JSArray.create();
  2440. if (elms.length == 0) {
  2441. elms = elements();
  2442. }
  2443. for (Element e: elms) {
  2444. e.getOwnerDocument();
  2445. if (e.getNodeType() == Node.DOCUMENT_NODE) {
  2446. e = e.<Document>cast().getBody();
  2447. }
  2448. for (int j = 0; j < g.size(); j++) {
  2449. Node n = g.get(j);
  2450. if (g.size() > 1) {
  2451. n = n.cloneNode(true);
  2452. }
  2453. switch (func) {
  2454. case FUNC_PREPEND:
  2455. newNodes.addNode(e.insertBefore(n, e.getFirstChild()));
  2456. break;
  2457. case FUNC_APPEND:
  2458. newNodes.addNode(e.appendChild(n));
  2459. break;
  2460. case FUNC_AFTER:
  2461. newNodes.addNode(e.getParentNode().insertBefore(n, e.getNextSibling()));
  2462. break;
  2463. case FUNC_BEFORE:
  2464. newNodes.addNode(e.getParentNode().insertBefore(n, e));
  2465. break;
  2466. }
  2467. }
  2468. }
  2469. if (newNodes.size() > g.size()) {
  2470. g.setArray(newNodes);
  2471. }
  2472. return this;
  2473. }
  2474. private GQuery domManip(String htmlString, int func) {
  2475. HashMap<Document, GQuery> cache = new HashMap<Document, GQuery>();
  2476. for (Element e: elements()) {
  2477. Document d = e.getNodeType() == Node.DOCUMENT_NODE ? e.<Document>cast() : e.getOwnerDocument();
  2478. GQuery g = cache.get(d);
  2479. if (g == null) {
  2480. g = cleanHtmlString(htmlString, d);
  2481. cache.put(d, g);
  2482. }
  2483. domManip(g.clone(), func, e);
  2484. }
  2485. return this;
  2486. }
  2487. private native Element getPreviousSiblingElement(Element elem) /*-{
  2488. var sib = elem.previousSibling;
  2489. while (sib && sib.nodeType != 1)
  2490. sib = sib.previousSibling;
  2491. return sib;
  2492. }-*/;
  2493. private JSArray merge(NodeList<Element> first, NodeList<Element> second) {
  2494. JSArray res = copyNodeList(first);
  2495. for (int i = 0; i < second.getLength(); i++) {
  2496. res.addNode(second.getItem(i));
  2497. }
  2498. return res;
  2499. }
  2500. private void removeData(Element item, String name) {
  2501. if (dataCache == null) {
  2502. windowData = JavaScriptObject.createObject().cast();
  2503. dataCache = JavaScriptObject.createObject().cast();
  2504. }
  2505. item = item == window ? windowData : item;
  2506. int id = item.hashCode();
  2507. if (name != null) {
  2508. if (dataCache.exists(id)) {
  2509. dataCache.getCache(id).delete(name);
  2510. }
  2511. if (dataCache.getCache(id).isEmpty()) {
  2512. removeData(item, null);
  2513. }
  2514. } else {
  2515. dataCache.delete(id);
  2516. }
  2517. }
  2518. }