typeahead.py :  » Web-Frameworks » Nevow » Nevow-0.10.0 » examples » athenademo » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Nevow 
Nevow » Nevow 0.10.0 » examples » athenademo » typeahead.py
# vi:ft=python
from nevow import tags
from formless import annotate,webform
from twisted.python import util

animals = {u'elf' : u'Pointy ears.  Bad attitude regarding trees.',
           u'chipmunk': u'Cute.  Fuzzy.  Sings horribly.',
           u'chupacabra': u'It sucks goats.',
           u'ninja': u'Stealthy and invisible, and technically an animal.',
           }


class TypeAheadPage(athena.LivePage):
    _tmpl = util.sibpath(__file__, "typeahead.html")
    docFactory = loaders.xmlfile(_tmpl)
    def render_typehereField(self, ctx, data):
        frag = TypeAheadFieldFragment()
        frag.page = self
        return frag

class TypeAheadFieldFragment(athena.LiveFragment):
    docFactory = loaders.stan(
            T.span(render=T.directive('liveFragment'))[ '\n',
                T.input(type="text", _class="typehere"), '\n',
                T.h3(_class="description"),
                ])

    def loadDescription(self, typed):
        if typed == u'':
            return None, u'--'
        matches = []
        for key in animals:
            if key.startswith(typed):
                 matches.append(key)
        if len(matches) == 1:
            return matches[0], animals[matches[0]]
        elif len(matches) > 1:
            return None, u"(Multiple found)"
        else:
            return None, u'--'
    athena.expose(loadDescription)

class DataEntry(rend.Page):
    """Add Animal"""
    addSlash = 1

    docFactory = loaders.stan(
            T.html[T.body[T.h1[
                "First, a Setup Form."],
                T.h2["Enter some animals as data.  Click 'Done' to test looking up these animals."],
                T.h3["The neat stuff happens when you hit 'Done'."],
                webform.renderForms(),
                T.ol(data=T.directive("animals"), render=rend.sequence)[
                        T.li(pattern="item", render=T.directive("string")),
                                                                        ],
                T.h1[T.a(href=url.here.child('typeahead'))["Done"]],
                          ]
                    ]
                              )
    def bind_animals(self, ctx, ):
        """Add Animal"""
        return annotate.MethodBinding(
                'animals',
                annotate.Method(arguments=
                    [annotate.Argument('animal', annotate.String()),
                     annotate.Argument('description', annotate.Text())]),
                action="Add Animal",
                                      )

    def animals(self, animal, description):
        """Add Animal"""
        if not (animal and description):
            return
        animals[animal.decode('utf-8')] = description.decode('utf-8')
        return url.here

    def data_animals(self, ctx, data):
        return animals.keys()

    def child_typeahead(self, ctx):
        return TypeAheadPage(None, None)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.