Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClickEvent support for tags #4

Open
GoogleCodeExporter opened this issue Mar 13, 2015 · 2 comments
Open

ClickEvent support for tags #4

GoogleCodeExporter opened this issue Mar 13, 2015 · 2 comments

Comments

@GoogleCodeExporter
Copy link

I like your tag cloud, but I would like to define an action to be performed 
when a tag (in my case a word tag) is click, instead of just link.

Adding this, I think, is relatively simple - a WordTag (maybe ImageTag too) 
could have an additional parameter - ClickHandler - in the constructor (or, 
ideally a method addClickHandler(...)) and a getter for this handler (or 
handlers). Then, while rendering the tag cloud, insert a call 
inlineHTML.addClickHandler(wordtag.getClickHandler()) [InlineHTML implements 
the GWT HasClickHandlers interface], and remove the 'href' property from the 
<a> tag in the html content (the <a> tag is no longer needed then, but it 
spares work needed to style this bit). 

Original issue reported on code.google.com by joanna.k...@gmail.com on 4 Feb 2011 at 9:30

@GoogleCodeExporter
Copy link
Author

I agree, the fact that it only accepts a String and return a String is a bit 
restrictive and against good OO practices. I need to add a tool-tip and I have 
no object to deal with. 

Original comment by br...@109forest.com on 5 Dec 2014 at 8:13

@GoogleCodeExporter
Copy link
Author

I was able to find a fairly simple workaround for this. Luckily, it doesn't 
require having to extend the existing API either. You can add a DomHandler to 
the tag cloud itself that will be triggered for every click event. 
Unfortunately, this will be triggered if the user clicks anywhere within the 
tag cloud, not just on one of the generated anchor tags. But from using the GWT 
ClickEvent, you can figure out which part of the tag cloud was clicked. And if 
it was on a tag, retrieve its text if needed.

Here's an example snippet of how I was able to achieve this:

TagCloud cloud = new TagCloud();

// Add words to tag cloud

cloud.addDomHandler(new ClickHandler() {

    @Override
    public void onClick(ClickEvent event) {
        event.preventDefault();   // Ignore URL link
        Element clickedElement = Element.as(event.getNativeEvent().getEventTarget());

        // Check if user clicked on anchor tag
        if (clickedElement.getNodeName().equalsIgnoreCase("a")) {

           String selectedTag = clickedElement.getInnerText();

           // Do whatever you need with the selected tag

        }
    }
}, ClickEvent.getType());

Original comment by jkaltenb...@gmail.com on 30 Jan 2015 at 6:21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant