This can probably be improved (and I've done little testing on it),
but I've come up with something to satisfy my description above:

    def group(template: NodeSeq): NodeSeq = {
        val default = S.attr("default").openOr("")
        val active_attrs = S.prefixedAttrsToMetaData("active")

        val names = template \\ "bind" filter(_.prefix == "menu")
flatMap(_.attributes.get("name"))

        def bind(xml: NodeSeq, currentPage: String): NodeSeq = {
            xml.flatMap {
                node => node match {
                    case s : Elem if (node.prefix == "menu" &&
node.label == "bind") => {
                            node.attributes.get("name") match {
                                case None => bind(node.child,
currentPage)
                                case Some(ns) => {
                                        SiteMap.findLoc(ns.text) match
{
                                            case Full(location) =>
                                                val link: Elem = <a
href={location.createDefaultLink}>{location.linkText.openOr("")}</a> //
location.createDefaultLink.toList.firstOption.getOrElse(<a /
>).asInstanceOf[Elem]
                                                if (ns == currentPage)
                                                    link %
anchorAttributes(node.attributes) % active_attrs
                                                else
                                                    link %
anchorAttributes(node.attributes)
                                             case _ => bind
(node.child, currentPage)
                                        }
                                    }
                            }
                        }
                    case Group(nodes) => Group(bind(nodes,
currentPage))
                    case s : Elem => Elem(node.prefix, node.label,
node.attributes,node.scope, bind(node.child, currentPage) : _*)
                    case n => node
                }
            }
        }

        for (request <- S.request.toList;
            loc <- request.location.toList)
        yield {
            val currentPage: String = if (names.contains(loc.name))
loc.name else default
            println("currentPage" + currentPage)
            Group(bind(template, currentPage))
       }
    }

    private def anchorAttributes(attributes : MetaData) : MetaData = {
        val amap = Map() ++ attributes.flatMap(attr => attr match {
                case ua : PrefixedAttribute =>
                    ua.pre match {
                        case "a" => List(ua.key ->
ua.value.first.toString)
                        case _ => Nil
                    }
                case _ => Nil
            })
        S.mapToAttrs(amap)
    }

On Mar 28, 11:59 am, bradford <fingerm...@gmail.com> wrote:
> Hi Derek,
>
> Cool.  I will still use your changes, but for this task I will need
> what I have described above.  This will give me the ability to set
> attributes to the "selected" item or default to a selected item if
> there is no match.  For example, let's say I have archives, music, and
> movies.  If I am on music, I want to set a selected class or style on
> it.  But if I embed movies into home, there will be no matches so I
> want it to default to movies.
>
> Thanks for your help and work,
> Bradford
>
> On Mar 28, 11:53 am, Derek Chen-Becker <dchenbec...@gmail.com> wrote:
>
> > Argh, stupid mistake on not capturing attrs. I'm pushing a fix up to git in
> > about 10 minutes. With this fix, is there still a reason that you need to
> > build your own MyMenu.group snippet?
>
> > Derek
>
> > On Fri, Mar 27, 2009 at 7:02 PM, bradford <fingerm...@gmail.com> wrote:
>
> > > Derek, I saw the changes and noticed that it doesn't capture the a
> > > attributes anymore.  I just wanted to point it out, because I wasn't
> > > sure if that was intended or not.
>
> > > Ok, I finally came up with a design for what I'm looking for:
>
> > > <lift:MyMenu.group active:class="selected" default="home">
> > >  <li><menu:bind name="home" a:id="home" /></li>
> > >  <li><menu:bind name="archives" a:id="archives" /></li>
> > >  <li><menu:bind name="music" a:id="music" a:style="margin-left:
> > > 10px;" /></li>
> > > </lift:MyMenu.group>
>
> > > I originally was going to try <menu:home /> and <menu:archives />
> > > instead since I could probably just call bind straight up on them, but
> > > I couldn't figure out how to extract these sub-elements since they are
> > > prefixed.
>
> > > Regards,
> > > Bradford
>
> > > On Mar 27, 1:57 am, Derek Chen-Becker <dchenbec...@gmail.com> wrote:
> > > > It's in trunk now. Add the "donthide" attribute to your Menu.item tags
> > > and
> > > > you'll get the same text as you would normally, just not in link form.
>
> > > > Derek
>
> > > > On Wed, Mar 25, 2009 at 4:31 PM, bradford <fingerm...@gmail.com> wrote:
>
> > > > > Derek, that'll work :)
>
> > > > > Thanks,
> > > > > Bradford
>
> > > > > On Mar 25, 6:13 pm, Derek Chen-Becker <dchenbec...@gmail.com> wrote:
> > > > > > That seems reasonable to me. That's actually what the Menu.builder
> > > does,
> > > > > > essentially. I don't think we want to modify the default behavior,
> > > but I
> > > > > > could make it a "donthide" attribute instead of "always" to do what
> > > > > you're
> > > > > > saying. Bradford, would that work for you?
>
> > > > > > Derek
>
> > > > > > On Wed, Mar 25, 2009 at 3:54 PM, Charles F. Munat <c...@munat.com>
> > > > > wrote:
>
> > > > > > > Actually, my feeling on item is that if you're on that page, item
> > > > > should
> > > > > > > return placeholder text (i.e. the same text without the link). For
> > > me,
> > > > > > > at least, that's the most common scenario.
>
> > > > > > > Chas.
>
> > > > > > > bradford wrote:
> > > > > > > > David, you're right that needing to surround the element text of
> > > a
> > > > > > > > with span is a unique case and should be a custom snippet.  I've
> > > > > > > > removed the span now and think that Derek's addition of "always"
> > > > > would
> > > > > > > > be just what I need.  Adding group="foo" to Menu.builder would
> > > > > suffice
> > > > > > > > as well.
>
> > > > > > > > Thanks for the tips, Chas.
>
> > > > > > > > Derek, if you do add "always" can you please let me know so that
> > > I
> > > > > can
> > > > > > > > update my code.
>
> > > > > > > > Thanks,
> > > > > > > > Bradford
>
> > > > > > > > On Mar 24, 10:37 pm, Derek Chen-Becker <dchenbec...@gmail.com>
> > > > > wrote:
> > > > > > > >> The general case is that a page won't link to itself, I think,
> > > which
> > > > > is
> > > > > > > why
> > > > > > > >> the default isn't to show it when the page matches. Unless
> > > anyone
> > > > > has
> > > > > > > >> objections I can add an "always" attribute. As for #1, the
> > > Menu.item
> > > > > > > makes a
> > > > > > > >> link using whatever the contents of the Menu.item tag are for
> > > the
> > > > > link
> > > > > > > text:
>
> > > > > > > >> <lift:Menu.item name="foo"><span>Go 
> > > > > > > >> here</span></lift:Menu.item>
>
> > > > > > > >> should become
>
> > > > > > > >> <a href={foo location}><span>Go here</span></a>
>
> > > > > > > >> Am I misunderstanding what you're looking for there? As for #2,
> > > you
> > > > > > > should
> > > > > > > >> be able to add a class using the prefixed attribute:
>
> > > > > > > >> <lift:Menu.builder li_item:class="bar" />
>
> > > > > > > >> In this context, li_item is the menu item that matches the
> > > current
> > > > > page.
> > > > > > > >> With Menu.group, you can specify the binding template:
>
> > > > > > > >> <ul>
> > > > > > > >> <lift:Menu.group group="help">
> > > > > > > >>   <li class="bar"><menu:bind /></li>
> > > > > > > >> </lift:Menu.group>
> > > > > > > >> </ul>
>
> > > > > > > >> But there's no provision to do anything special for the current
> > > > > page.
>
> > > > > > > >> Let me know if that's not sufficient or if I'm misunderstanding
> > > your
> > > > > > > >> requirement.
>
> > > > > > > >> Derek
>
> > > > > > > >> On Tue, Mar 24, 2009 at 3:50 PM, bradford <fingerm...@gmail.com
>
> > > > > wrote:
>
> > > > > > > >>> Thanks for the clarification, David, and for your snippet,
> > > Derek.
> > > > > > > >>> I think adding an "always" attribute to Menu.item would be 
> > > > > > > >>> very
> > > > > > > >>> beneficial.  I still don't understand why that's not its
> > > default
> > > > > > > >>> behavior.
> > > > > > > >>> It looks like I will not be able to use any of lift's Menu 
> > > > > > > >>> tags
> > > at
> > > > > > > >>> this time, because 1) I need to surround the item text with
> > > span
> > > > > and
> > > > > > > >>> 2) I need a way to add class="active" to the li_item.  Both 
> > > > > > > >>> are
> > > not
> > > > > > > >>> possible with Menu.item, Menu.group, or Menu.builder.  Let me
> > > know
> > > > > if
> > > > > > > >>> I am mistaken.  If I am not not, may I put in a feature 
> > > > > > > >>> request
> > > for
> > > > > > > >>> these items.  For the time being I will just hard code it as
> > > > > follows
> > > > > > > >>> (which is not a big deal to me at this time):
> > > > > > > >>> <ul class="menu">
> > > > > > > >>> <li><a href="/foo1" class="active"><span>Foo1</span></foo>
> > > > > > > >>> <li><a href="/foo2"><span>Foo2</span></foo>
> > > > > > > >>> <li><a href="/foo3"><span>Foo3</span></foo>
> > > > > > > >>> </ul>
> > > > > > > >>> Thanks again for the great support :)
> > > > > > > >>> Bradford
> > > > > > > >>> On Mar 24, 12:08 pm, David Pollak <
> > > feeder.of.the.be...@gmail.com>
> > > > > > > >>> wrote:
> > > > > > > >>>> On Tue, Mar 24, 2009 at 9:02 AM, Charles F. Munat <
> > > c...@munat.com
>
> > > > > > > >>> wrote:
> > > > > > > >>>>> David Pollak wrote:
> > > > > > > >>>>>>     What's the best practice:
> > > > > > > >>>>>>     For more information about <lift:Menu.item name="foo1"
> > > />.
> > > > >  For
> > > > > > > >>> more
> > > > > > > >>>>>>     information about <lift:Menu.item name="foo2" />.
> > > > > > > >>>>>>     Or
> > > > > > > >>>>>>     For more information about <a href="/foo1">foo1</a>.
> > >  For
> > > > > more
> > > > > > > >>>>>>     information about <a href="/foo2">foo2</a>.
> > > > > > > >>>>>> The latter.  This allows you to move the pages around on 
> > > > > > > >>>>>> the
> > > > > > > >>> filesystem
> > > > > > > >>>>>> without having to grep through all you source files looking
> > > for
> > > > > what
> > > > > > > >>>>>> needs to be changed.
> > > > > > > >>>>> Am I missing something, or did you mean the former?
> > > > > > > >>>> D'oh!  That brain-finger connection is always getting messed
> > > up.
> > > > >  I
> > > > > > > meant
> > > > > > > >>>> the former.  Thanks for correcting me!
> > > > > > > >>>>> Chas.
> > > > > > > >>>> --
> > > > > > > >>>> Lift, the simply functional web frameworkhttp://liftweb.net
> > > > > > > >>>> Beginning Scalahttp://www.apress.com/book/view/1430219890
> > > > > > > >>>> Follow me:http://twitter.com/dpp
> > > > > > > >>>> Git some:http://github.com/dpp

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Lift" group.
To post to this group, send email to liftweb@googlegroups.com
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to