def linepath(facet, id, link):
    output = cls('facet', db.name(facet), ': ')
    output += link('any', term=(facet, 0, 0))
    for step in db.path(facet, id):
        text = cls('value', db.name(facet, step))
        if step != id: text = link(text, term=(facet, step, 0))
        output += [' > ', text]
    return output
def userfacets(username):
    #print userfacets
    facets = db.db.select(['facets'], 'user', "name='%s'" % username)
    result = []
    for facetname in facets[0][0].split(', '):
        result.append(db.name(facetname))
    return result
def facetpath(f, id, link=None, tips=0):
    path = []
    for step in db.path(f, id):
        sel = (step == id) and ' sel' or ''
        text = cls('value' + sel, db.name(f, step))
        if not sel and link:
            text = link(text, term=(f, step, 0))
            if tips:
                text = tip(
                    'include items classified under "%s"' % db.name(f, step),
                    text)
        if sel and tips:
            text = tip(
                'all current results are classified under "%s"' %
                db.name(f, step), text)
        path += [cls('arrow', ' > '), text]
    return path[1:]
def termbox(term, link, c='termbox', path=1, button=1, **attrs):
    facet, value, leaf = term
    name = cls('facet', db.name(facet))
    xbutton = tip(coll.X_ALT, div('×', c='removebox'))
    xbutton = button and link(xbutton, remove=term) or ''

    #xbutton = button and tip(coll.X_ALT, div(link('×', remove=term), c='removebox')) or ''
    if path:
        path = facetpath(facet, value, link, tips=1)
        if path and not button:
            all = link('all', term=(facet, 0, 0))
            label = [name, ': ', all, cls('arrow', ' > '), path]
        else:
            label = [name, ': ', path]
    else:
        label = [name, ': ', cls('value sel', db.name(facet, value))]
    return div(table(tr(td(label), td(xbutton)), c=c, **attrs),
               c='facet_' + facet)
def indentpath(facet, id, query, link, links=0, buttons=0):
    path = db.path(facet, id)
    steps = [(0, small(db.name(facet).upper()))
             ] + [(step, esc(db.name(facet, step))) for step in path]
    output = []
    indent = 0
    for step, text in steps:
        sel = (step == id) and ' sel' or ''
        term = (facet, step, 0)
        if links and not sel: text = link(text, term=term)
        if indent > 20: output += space(indent - 20)
        if indent > 0: output += img(coll.ANGLE_SRC)
        output += [cls('value' + sel, text)]
        if step and buttons:
            count = db.count(query + term)
            if count > 1:
                output += [nbsp] + cls('count', '(%d)' % count)
                tip = 'look only within these %d items' % count
                icon = img(coll.DETAIL_SRC, alt=tip, align='absmiddle')
                output += [nbsp, link(icon, term=term)]
        output += [br]
        indent += 20
    return output
def groupselect(query, facet, group):
    count = db.count(query)
    if not count: return ''
    select, option = mktag('select'), mktag('option')
    options = []
    for f in [''] + db.facetlist:
        name = f and db.name(f) or '(none)'
        if f == group: options += option(name, value=f, selected=1)
        else: options += option(name, value=f)
    return form('%d item%s selected.  Group by: ' % (count, plural(count)),
                input(type='hidden', name='q', value=query.serialize()),
                input(type='hidden', name='facet', value=facet),
                select(options, name='group'), input(type='submit',
                                                     value='go'))
def facetheading(f, path, link):
    return [link(cls('facet', db.name(f)), facet=f, term=(f, 0, 0)), path]
def facetlist(link, facet):
    links = []
    for f in db.facetlist:
        links.append(cls('facet' + (f == facet and ' sel' or ''), db.name(f)))
    return join(' | ', links)