DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Frameworks
  4. Integrating Bootstrap JS with Tapestry5

Integrating Bootstrap JS with Tapestry5

Taha Siddiqi user avatar by
Taha Siddiqi
·
Oct. 31, 11 · Interview
Like (0)
Save
Tweet
Share
5.22K Views

Join the DZone community and get the full member experience.

Join For Free

adding bootstrap js twispy to your template is very easy

<a href='#' class='twipsy' data-title='my twipsy'>hover over me</a>
<script type='text/javascript'>
   $('.twipsy').twipsy();
</a>

and you can always use property-expressions to display some dynamic data. but what if you want to have block rendered as title. this was exactly what i was trying to accomplish in one of my recent projects. (we are using tapestry-jquery for jquery support in that project.)

this involves a javascript trick and ofcourse a tapestry mixin. bootstrap allows you to change the title dynamically by setting title property to a function. so if we are able to render the block in a hidden element, we can always use this function to display it

$('#mytwipsy').twipsy({title:function(){
  return $('myhiddenblock').html();
});

before we render the block we need to hide it by wrapping it in a hidden div. the key is what thiago always says

tapestry only renders rendercommands

we add three rendercommands to the renderqueue. the first one and last one to wrap the block in a hidden div and the middle one to render the title block. the problem with renderqueue is that it is a stack(not a queue). to “negate this stack effect”, we create a wrapper around it.

public class compositerendercommand implements rendercommand {
    private stack commands = collectionfactory.newstack();

    public void add(rendercommand command){
        commands.push(command);
    }

    public void render(markupwriter writer, renderqueue queue) {
        while(!commands.isempty()){
            queue.push(commands.pop());
        }
    }
}

it adds the commands to a stack and the pushes it on the queue(which is a stack :) ). sometimes two wrongs do make a right :)

as this functionality will be better served with a mixin so here it is

@import(library = {"bootstrap-twipsy.js", "twipsy.js"})
public class twipsy {

    @parameter(required = true, allownull = false, defaultprefix = bindingconstants.block)
    private block title;

    @inject
    private javascriptsupport javascriptsupport;

    @inject
    private componentresources resources;

    @symbol(bootstrapsymbolconstants.bootstrap_prefix)
    @inject
    private string bootstrapprefix;

    @injectcontainer
    private clientelement container;

    public string gettitleid() {
        return container.getclientid() + "-title";
    }

    @afterrender
    rendercommand render() {
        addjavascript();

        compositerendercommand commands = new compositerendercommand();

        commands.add(new rendercommand() {
            public void render(markupwriter writer, renderqueue queue) {
                writetitlebegintag(writer);
            }
        });

        commands.add((rendercommand) title);

        commands.add(new rendercommand() {
            public void render(markupwriter writer, renderqueue queue) {
                writetitleendtag(writer);
            }
        });

        return commands;
    }

    private void writetitlebegintag(markupwriter writer) {
        writer.element("div",
            "style", "display:none",
            "id", gettitleid());
    }

    private void writetitleendtag(markupwriter writer) {
        writer.end();
    }

    private void addjavascript() {
        jsonobject arguments = bootstraputils.convertinformalparameterstojson(resources,
            bootstrapprefix);

        jsonobject mainparams = new jsonobject();
        mainparams.put("arguments", arguments);
        mainparams.put("id", container.getclientid());
        mainparams.put("title", gettitleid());
        javascriptsupport.addinitializercall("bootstraptwipsy", mainparams);
    }

}

public class bootstraputils {

    public static jsonobject convertinformalparameterstojson(componentresources resources,
        string prefix) {
        jsonobject json = new jsonobject();

        for(string parameter : resources.getinformalparameternames()) {
            if(parameter.startswith(prefix)) {
                json.put(parameter.substring(prefix.length()),
                    resources.getinformalparameter
                        (parameter,
                            string.class));
            }
        }

        return json;
    }

}

note we use the informal parameters prefixed with bootstrapprefix(‘bp-’) as arguments to the twipsy function call.

we add a simple javascript to accomplish our javascript part of the trick

(function($){
  $.extend(tapestry.initializer, {
    bootstraptwipsy: function(specs){
      var element = $("#" + specs.id);
      var title = $("#" + specs.title);

      $.extend(specs.arguments, {
        title: function(){
          return title.html();
        },
        html:true
      });

      element.twipsy(specs.arguments);
    }
  });
})(jquery);

that’s it. a simple usage will be

this is some content which contains twipsy
    <a>content</a>

some tooltip <strong>demo</strong> with a lot of <em>html formatting!!</em>

from http://tawus.wordpress.com/2011/10/28/integrating-bootstrap-js-with-tapestry5/

Bootstrap (front-end framework)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Microservices Testing
  • Assessment of Scalability Constraints (and Solutions)
  • What Is Advertised Kafka Address?
  • Implementing PEG in Java

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: