Changeset 829 for trunk


Ignore:
Timestamp:
Feb 27, 2011, 10:27:38 PM (8 years ago)
Author:
gav
Message:

Create custom taglib of helpBalloon to allow their usage in ajax rendered templates.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/taglib/CustomTagLib.groovy

    r817 r829  
    99class CustomTagLib {
    1010    static namespace = 'custom'
     11
     12    private static final Object helpBalloonLockable = new Object();
     13    private static long helpBalloonCount = 0L;
    1114
    1215    def resources = { attrs ->
     
    295298
    296299    /**
     300    * Customised version of helpBalloon as found in help-balloon plugin.
     301    * This version can be used in ajax rendered templates where the original fails.
     302    *
     303    * Fields added:
     304    * iconId - Optional to specify the anchor elements id, by default an id is generated if iconSrc is supplied.
     305    * iconSrc - Optional to specify anchor image src, if not supplied no anchor image is output by the taglib.
     306    *
     307    * Example:
     308    * <!--
     309    * <custom:helpBalloon code="entry.date.done" iconSrc="${resource(plugin:'help-balloons', dir:'images', file:'balloon-icon.gif')}"/>
     310    * or
     311    * <a href="#" id="mynewanchor" onclick="return false;">this</a>
     312    * <custom:helpBalloon code="entry.date.done" iconId="mynewanchor" />
     313    * -->
     314    */
     315    def helpBalloon = {attrs, body ->
     316        def mkp = new groovy.xml.MarkupBuilder(out) //this line will be unnecessary in versions of Grails after version 1.2
     317
     318        def title = attrs["title"]
     319        def content = attrs["content"]
     320        def code = attrs["code"]
     321        def suffix = attrs["suffix"] ?: ".help"
     322        def encodeAs = attrs["encodeAs"]
     323        def iconId = attrs["iconId"]
     324        def iconSrc =  attrs["iconSrc"]
     325
     326        if (!title && code) title = g.message(code: code)
     327        if (!content && code) content = g.message(code: code + suffix)
     328
     329        title = title ?: ""
     330        content = content ?: ""
     331
     332        if (encodeAs) {
     333            switch (encodeAs.toUpperCase()) {
     334
     335                case "HTML":
     336                    title = title.encodeAsHTML()
     337                    content = content.encodeAsHTML()
     338                    break
     339
     340                case "XML":
     341                    title = title.encodeAsXML()
     342                    content = content.encodeAsXML()
     343                    break
     344            }
     345        }
     346
     347        def num
     348        synchronized (helpBalloonLockable) {
     349            num = helpBalloonCount++;
     350        }
     351
     352        if(iconSrc) {
     353            iconId = iconId ?: "customHb$num"
     354            mkp.img( id: iconId, src: iconSrc)
     355        }
     356
     357        def javascript = """var customHb$num = new HelpBalloon({
     358    title: '${title.encodeAsJavaScript()}',
     359    content: '${content.encodeAsJavaScript()}'"""
     360
     361        if(iconId) {
     362        javascript +=""",
     363    icon: \$('$iconId')"""
     364        }
     365
     366        javascript += """
     367});"""
     368
     369        mkp.script(type: "text/javascript") {
     370            yieldUnescaped(javascript)
     371        }
     372
     373    }
     374
     375    /**
    297376    * Determine if a supplied string is considered a url or not.
    298377    * The scheme/protocol can be adjusted, file:// has been excluded here.
Note: See TracChangeset for help on using the changeset viewer.