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.

Collapser.java 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 gwtquery.plugins.collapser;
  17. import com.google.gwt.dom.client.Element;
  18. import com.google.gwt.dom.client.NodeList;
  19. import com.google.gwt.query.client.Function;
  20. import com.google.gwt.query.client.GQuery;
  21. import com.google.gwt.query.client.Plugin;
  22. import com.google.gwt.query.client.plugins.Effects;
  23. import com.google.gwt.user.client.Event;
  24. /**
  25. * Collapser sample plugin. For all matched elements, adds a clickable [X] next
  26. * to the element which toggles its visibility.
  27. */
  28. public class Collapser extends GQuery {
  29. /**
  30. * Used to register the plugin.
  31. */
  32. private static class CollapserPlugin implements Plugin<Collapser> {
  33. public Collapser init(GQuery gq) {
  34. return new Collapser(gq.get());
  35. }
  36. }
  37. /**
  38. * Plugin key for Collapser.
  39. */
  40. public static final Class<Collapser> Collapser = Collapser.class;
  41. static {
  42. GQuery.registerPlugin(Collapser.class, new CollapserPlugin());
  43. }
  44. public Collapser(NodeList<Element> list) {
  45. super(list);
  46. }
  47. /**
  48. * Adds a [X] link button before each matched element with a bound click
  49. * handler that toggles visibility of the element.
  50. */
  51. public Collapser apply() {
  52. for (final Element e : elements()) {
  53. GQuery button = $("<a href='#'>[X]</a>");
  54. $(e).before(button);
  55. button.click(new Function() {
  56. public boolean f(Event evt) {
  57. $(e).as(Effects.Effects).toggle();
  58. return true;
  59. }
  60. });
  61. }
  62. return this;
  63. }
  64. }