1. Wicket
  2. WICKET-3422

Simplified visibility control of Enclosures in Ajax requests



      [Patch updated on 3/9/2011]

      The amount of boilerplate code needed to create partitions of a web page that take their
      visibility setting from a controlling component in Ajax requests.

      "Changing the visibility of a child component in Ajax callback method will not affect the entire
      enclosure but just the child component itself. This is because only the child component is added
      to the AjaxRequestTarget."

      1. InlineEnclosure tags, implemented as an attribute that can be attached to an arbitrary html tag.
      Works just like a regular Enclosure, but is Ajax ready.
      2. When handling Ajax requests, InlineEnclosures that are controlled by Components already in the Ajax
      request target are automatically added to the Ajax request target.

      Below we have an ajax link: 'toggleLink' that is used to
      toggle the visibility of the component 'label1'. Since the
      label1 component happens to be the controlling child component
      of the enclosure, the visibility of the whole enclosure is toggled
      at the same time.

      <a href="#" wicket:id="toggleLink">Toggle visibility</a>

      <div wicket:enclosure="controllingChild">
      The visibility of this text is also toggled.
      <div wicket:id="controllingChild">Test</div>

      Here we have the 'inline' Enclosure tag:
      <div wicket:enclosure="controllingChild">
      Notice that no boilerplate code for this is necessary on the
      Java side. The tag can be an arbitrary html tag (but cannot
      simultaneously be another wicket tag, like WebMarkupContainer).
      If it does not yet have a wicket id (as in this example), a
      page-unique id will be generated for it (ex. inlineEnclosure-1).
      The handling of the child id attribute is identical to the handling
      of child id:s in traditional Wicket Enclosure tags.

      On Java side we only need to introduce the child object and some kind
      of control for the toggling of the visibility of the child object.
      Here an ajax link is used. When user clicks the link, the visibility
      of the entire Enclosure is toggled. Developer must also remember to
      set the outputmarkupId=true for the label, as usual for Ajaxified code.
      Nothing else is needed, the patch handles everything from here.

      private Label label1 = new Label("controllingChild", "Test Label 1");
      private AjaxLink<Void> toggleLink;

      public MinimalAjaxEnclosurePage()
      toggleLink = new AjaxLink<Void>("toggleLink"){
      public void onClick(AjaxRequestTarget target)

      { label1.setVisible(!label1.isVisible()); target.addComponent(label1); }


      The patch can be tested with the new unit tests in

      • InlineEnclosureTest has tests for rendering of Inline enclosures in various situations:
      • chooses the correct child, whether or not child id is set
      • reuses existing wicket:id
      • works when nested with other inline Enclosures or traditional Wicket Enclosure tags
      • AjaxEnclosureTest has tests for toggling visibility of Inline enclosures by toggling
        the visibility of their controllers in Ajax requests:
      • only the correct child controls the enclosure, even when child id is not set
      • works with nested inline enclosures
      • TogglePageTests has tests for toggling visibility in ajax/non ajax situations, and where
        there are more than one targets in one ajax request

      Changes to Wicket code:

      • InlineEnclosureHandler:
      • Recognizes inline enclosures by their attribute (wicket:enclosure) and gives them
        a session-unique id. The id is also written directly to the underlying Xml-tag.
      • EnclosureResolver:
      • recognizes an inline Enclosure tag and creates an InlineEnclosure instance for it
      • Enclosure:
      • getEnclosureParent() visibility changed to protected, to enable access from extending
        InlineEnclosure class.
      • InlineEnclosure:
      • implements onAfterRenderChildren, notifying child objects stored in the onComponentTagBody phase
      • new method: updateVisibility(), used in Ajax calls to update the visibility of the enclosure with the
        visibility of it's controlling child object
      • new method: getChildId(), used in Ajax calls to get the child's id so it can be compared to the id of components
        added to the Ajax target
      • MarkupParser:
      • Appended InlineEnclosureHandler to the list of filters.
      • MarkupContainer
      • prevented removing InlineEnclosures at the time of detach.
      • WebApplication:
      • newAjaxRequestTarget: Adds a new listener: AjaxEnclosureListener
      • AjaxEnclosureListener (new class) (implementing AjaxRequestTarget.IListener)
      • onBeforeRespond: searches the Page for those InlineEnclosures that have their
        child controllers added to Ajax Request target, and adds them to the target as well.
      • Also sets these values to the found InlineEnclosures to help with ajax updates:
      • setOutputMarkupPlaceholderTag=true
      • setMarkupId=[id generated in InlineEnclosureHandler]
      • New tests:
      • src/test/java/org/apache/wicket/markup/html/internal:
      • AjaxEnclosurePage_*.html / .java : test pages for AjaxEnclosureTests.java
      • InlineEnclosurePage_*.html / .java : test pages for InlineEnclosureTest.java
      • SimplePanel2.html / .java : test page for InlineEnclosureTest.java
      • InlineEnclosurePanelPage.html / .java : test page for InlineEnclosureTest.java
      • FullReloadPage.html / .java, : test pages for TogglePageTests.java
        InlineEnclosureAjaxPage.html / .java
        InlineEnclosureWithAdditionalAjaxTargetPage.html / .java
        TraditionalEnclosureAjaxPage.html /.java
      • DefaultMarkupCacheKeyProvider: (Fixed a bug?)
      • Added a missing dot prefixing the file-extension.
      • Not directly related to the issue, but many wicket unit tests were failing without this change.

      Thanks for support:
      Martin Makundi
      Igor Vaynberg
      Jeremy Thomerson
      Juergen Donnerstag

      1. InlineAndAjaxifiedEnclosures_patch_R2.txt
        72 kB
        Joonas Hämäläinen
      2. wicket-3422.patch
        69 kB
        Juergen Donnerstag
      3. InlineAndAjaxifiedEnclosures_R3.patch
        92 kB
        Joonas Hämäläinen

        Issue Links


          No work has yet been logged on this issue.


            • Assignee:
              Juergen Donnerstag
              Joonas Hämäläinen
            • Votes:
              6 Vote for this issue
              4 Start watching this issue


              • Created: