You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by webchick <am...@vegas.com> on 2008/04/01 20:15:35 UTC

Calling a JS function from onNodeLinkClicked in a Tree?

Hi all,
I'm using a Tree for navigation and I have a need to load an external page
in a new window when a link is clicked in the tree.  I was hoping to put
something in the onNodeLinkClicked function for my tree that could
create/call a JS function to load the URL in a new window as required.  This
does not need to be done for the entire tree structure, so I need to be able
to set up this functionality for specified nodes only.  Is there a way to do
this?  

I have read the JS related article on the wiki (
http://cwiki.apache.org/WICKET/calling-javascript-function-on-wicket-components-onclick.html
http://cwiki.apache.org/WICKET/calling-javascript-function-on-wicket-components-onclick.html
), but I don't see how to add this to only a few nodes in a tree.

Here's my tree definition:

private Tree getTree()
{
  final Tree linkTree = new Tree("navTree", createNavigationTreeModel())
  {
    private static final long serialVersionUID = -4646756742893751770L;

    @Override
    protected void onNodeLinkClicked(AjaxRequestTarget target, TreeNode
node)
    {
      // If this is a MenuItem, reload content.
      if (((DefaultMutableTreeNode) node).getUserObject() instanceof
MenuItem)
      {
        MenuItem menuItem = (MenuItem) ((DefaultMutableTreeNode)
node).getUserObject();
        Panel contentPanel = menuItem.getContent();
        contentPanel.setOutputMarkupId(true);

        MenuPanel.this.getParent().replace(contentPanel);
        target.addComponent(contentPanel);
      }
      else if (((DefaultMutableTreeNode) node).getUserObject() instanceof
Menu)
      {
        Menu menu = (Menu) ((DefaultMutableTreeNode) node).getUserObject();

        // Use Javascript to open new window?  Get URL from menu object.      
      }

      getTreeState().selectNode(node, true);
      super.onNodeLinkClicked(target, node);
    }
  };

  linkTree.setRootLess(true);
  linkTree.setOutputMarkupId(true);
  return linkTree;
}

The if(MenuItem) replace portion works great, it's the else if(Menu) part
that has me stuck.  Thanks in advance for any suggestions or comments.
-- Amanda
-- 
View this message in context: http://www.nabble.com/Calling-a-JS-function-from-onNodeLinkClicked-in-a-Tree--tp16420093p16420093.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Calling a JS function from onNodeLinkClicked in a Tree?

Posted by Al Maw <wi...@almaw.com>.
This isn't as easy to tweak as it could be, I'm afraid. :-(

You're better off using target on the href than doing this with JavaScript,
I'd have thought.

That said, most people think that choosing to open a new window or not is
pretty evil - the user has the option to do this if they want to, it breaks
the expected functionality of the back button, etc. etc.

I'd probably extend BaseTree in core and write my own little Panel which
just displays a link, which obviously you can tweak as you like. If you want
to pop things up using JavaScript, you can use Link#setPopupSettings(...) to
achieve this nicely.

Regards,

Al

On Tue, Apr 1, 2008 at 7:15 PM, webchick <am...@vegas.com> wrote:

>
> Hi all,
> I'm using a Tree for navigation and I have a need to load an external page
> in a new window when a link is clicked in the tree.  I was hoping to put
> something in the onNodeLinkClicked function for my tree that could
> create/call a JS function to load the URL in a new window as required.
>  This
> does not need to be done for the entire tree structure, so I need to be
> able
> to set up this functionality for specified nodes only.  Is there a way to
> do
> this?
>
> I have read the JS related article on the wiki (
>
> http://cwiki.apache.org/WICKET/calling-javascript-function-on-wicket-components-onclick.html
>
> http://cwiki.apache.org/WICKET/calling-javascript-function-on-wicket-components-onclick.html
> ), but I don't see how to add this to only a few nodes in a tree.
>
> Here's my tree definition:
>
> private Tree getTree()
> {
>  final Tree linkTree = new Tree("navTree", createNavigationTreeModel())
>  {
>    private static final long serialVersionUID = -4646756742893751770L;
>
>    @Override
>    protected void onNodeLinkClicked(AjaxRequestTarget target, TreeNode
> node)
>    {
>      // If this is a MenuItem, reload content.
>      if (((DefaultMutableTreeNode) node).getUserObject() instanceof
> MenuItem)
>      {
>        MenuItem menuItem = (MenuItem) ((DefaultMutableTreeNode)
> node).getUserObject();
>        Panel contentPanel = menuItem.getContent();
>        contentPanel.setOutputMarkupId(true);
>
>        MenuPanel.this.getParent().replace(contentPanel);
>        target.addComponent(contentPanel);
>      }
>      else if (((DefaultMutableTreeNode) node).getUserObject() instanceof
> Menu)
>      {
>        Menu menu = (Menu) ((DefaultMutableTreeNode) node).getUserObject();
>
>        // Use Javascript to open new window?  Get URL from menu object.
>      }
>
>      getTreeState().selectNode(node, true);
>      super.onNodeLinkClicked(target, node);
>    }
>  };
>
>  linkTree.setRootLess(true);
>  linkTree.setOutputMarkupId(true);
>  return linkTree;
> }
>
> The if(MenuItem) replace portion works great, it's the else if(Menu) part
> that has me stuck.  Thanks in advance for any suggestions or comments.
> -- Amanda
> --
> View this message in context:
> http://www.nabble.com/Calling-a-JS-function-from-onNodeLinkClicked-in-a-Tree--tp16420093p16420093.html
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>