blocks.py :  » Web-Frameworks » Nevow » Nevow-0.10.0 » nevow » 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 » nevow » blocks.py
# Copyright (c) 2004 Divmod.
# See LICENSE for details.


"""
Easy to use box layout model. Works cross-browser on IE 6, Safari, Opera, and Mozilla (using Evil Hack(tm)).
Until a known bug in Mozilla is fixed, the Evil Hack will be in place. This evil hack prevents text inside of a Block from
being selected.

 - line: a horizontal box grouping
 - block: a vertical, flowing box grouping

Example::

  text = "Hello lots of text " * 20

  line(width='100%')[
    box(width='50%')[text],
    box(width='50%')[text]]
  # Note there is a mozilla bug right now where the second, flowing percentage is calculated using only
  # the empty space, so the second value here would have to be 100%; This module will soon add browser
  # sniffing to detect if mozilla is being used and adjust percentages accordingly. For now, use pixel, em, or
  # point values instead.

Experimental feature: Keyword arguments to the line and box protos are converted into css styles:
box(color='red') => <span style="color: red" />

Known Mozilla bugs
==================

If you use border, padding, or margin, mozilla shows weird rendering artifacts. background color and
background images generally appear to be safe.

It doesn't appear to be possible to set the vertical-align in some cases in mozilla.
"""

from zope.interface import implements

from nevow import static
from nevow import inevow
from nevow import tags

boxStyle = tags.xml("""
span.nevow-blocks-block {
    display: inline-block;
    -moz-binding: url('/mozbinding#inlineblock'); }

div.nevow-blocks-line {
    margin: 0px; padding: 0px }

.expanded {
    display: block; }

.collapsed {
    display: none; }

.visibilityImage {
    margin-right: 5px; }
""")

js = tags.xml( """
function collapse(node, collapsedText, expandedText) {
    for (var i = 0; i < node.childNodes.length; i++) {
        var childNode = node.childNodes[i]
        if (childNode.className == 'visibilityImage') {
            var img = childNode;
        } else if (childNode.className == 'headText') {
            var head = childNode;
        }
    }
    for (var i = 0; i < node.parentNode.childNodes.length; i++) {
        var childNode = node.parentNode.childNodes[i]
        if (childNode.className == 'collapsed' || childNode.className == 'expanded') {
            var next = childNode;
        }
    }
    if (next.className == 'collapsed') {
        img.src = '/images/outline-expanded.png';
        next.className = 'expanded';
        head.innerHTML = expandedText;
    } else {
        img.src = '/images/outline-collapsed.png';
        next.className = 'collapsed';
        head.innerHTML = collapsedText;
    }
}//""")

blocks_glue = [
    tags.style(type="text/css")[ boxStyle ],
    tags.script(type="text/javascript")[ tags.comment[js] ]]


mozBinding = """<?xml version="1.0"?>

<bindings xmlns="http://www.mozilla.org/xbl"
          xmlns:xbl="http://www.mozilla.org/xbl"
          xmlns:html="http://www.w3.org/1999/xhtml"
          xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

<binding id="inlineblock">
  <content>
  <html:style type="text/css">
    .nevow-inline-table { display: inline-table; }
    .nevow-inline-table-row { display: table-row; }
    .nevow-inline-table-cell { display: table-cell; }
  </html:style>
    <html:span class="nevow-inline-table">
    <html:span class="nevow-inline-table-row">
     <html:span class="nevow-inline-table-cell" xbl:inherits="style">
          <children/>
     </html:span>
     </html:span>
     </html:span>
  </content>
</binding>

</bindings>
"""

blocks_child = static.Data(mozBinding, 'text/xml; name="inline-block.xml"')


class _Blocks(object):
    def __init__(self, tag, className):
        self.tag = tag
        self.className = className

    def __call__(self, **kw):
        """Make and return a new block. If height or width is specified, they may be
        of any css-supported measurement format, such as '200px' or '50%'.
        
        If height or width is not specified, the block will "shrink wrap".
        
        Interesting experiment: kw arguments to __call__ are treated like css style key
        value pairs. For example, block(color='blue', background_color='red') will translate
        to style="color: blue; background-color: red". _ is mapped to -. Not sure if this will
        work but it will be interesting to see if it is useful to use.
        """
        return self.tag(
            _class=self.className,
            style='; '.join(
                [': '.join((k.replace('_', '-'), v))
                for (k, v) in kw.items()]))


block = _Blocks(tags.span, 'nevow-blocks-block')
line = _Blocks(tags.div, 'nevow-blocks-line')


class collapser(object):
    """Render a fragment of html with a head and a body.
    The body can be in two states, expanded and collapsed.
    When the body is in collapsed state, it is not visible in the browser.
    Clicking on the head area causes the visibility state of the body
    to toggle.

    TODO: This should be rewritten to check for patterns and slots so you
    could have it use table or paragraph tags or whatever instead of a span
    and a div, and you can omit the visibility image if desired (js would have 
    to change too)
    """
    implements(inevow.IRenderer)

    def __init__(self, headCollapsed, headExpanded, body, collapsed=True):
        self.headCollapsed = headCollapsed
        self.headExpanded = headExpanded
        self.body = body
        if collapsed:
            self.collapsed = 'collapsed'
        else:
            self.collapsed = 'expanded'

    def rend(self, ctx, data):
        return (tags.span(
            _class="collapser-line",
            onclick=(
                "collapse(this, '",
                self.headCollapsed,
                "', '",
                self.headExpanded,
                "');"))[
            tags.img(_class="visibilityImage", src="/images/outline-%s.png" % self.collapsed),
            tags.span(_class="headText", style="color: blue; text-decoration: underline; cursor: pointer;")[
                self.collapsed == 'collapsed' and self.headCollapsed or self.headExpanded ]
        ],
        tags.xml('&nbsp;'),
        tags.div(_class=self.collapsed)[
            self.body
        ])

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