Mike Orr avatar Mike Orr committed 4b693ac

Unfinished code for group fields.

Comments (0)

Files changed (1)

unfinished/group.py

+def checkbox_group(name, selected_values, options, ncol, 
+    direction="horizontal"):
+    """Return a group of checkboxes arranged in columns.  See ``group()``.
+    """
+    return group(name, selected_values, options, ncol, direction, "checkbox")
+
+def radio_group(name, selected_values, options, ncol, 
+    direction="horizontal"):
+    """Return a group of radio buttons  arranged in columns.  See ``group()``.
+    """
+    return group(name, selected_values, options, ncol, direction, "radio")
+
+
+def group(name, selected_values, options, ncol, direction="horizontal",
+    input_type="checkbox"):
+    """Return a group of checkboxes or radio buttons arranged in columns.
+
+    Arguments:
+
+      ``name`` -- name of the widget group (<input name=>).
+
+      ``selected_values`` -- list or tuple of previously-selected values.
+          Pass ``None`` to not select any.
+
+      ``options`` -- a list of ``Option`` objects or ``(value, label)``
+          pairs.  See ``select()`` for the range of legal values.
+
+      ``ncol`` -- number of columns.
+
+      ``direction`` -- "horizontal" or "vertical" (or any string starting
+          with "h" or "v", case insensitive).  In horizontal groups, the second
+          element is to the right of the first.  In vertical groups, the
+          second element is below it.
+
+      ``input_type`` -- "checkbox" or "radio".
+    """
+    if input_type not in ["checkbox", "radio"]:
+        raise ValueError("input type must be 'checkbox' or 'radio'")
+    if not isinstance(options, Options):
+        options = Options(options)
+    options_html = [
+        HTML.label(
+            HTML.input(type=input_type, name=name, value=x.value),
+            x.label)
+        for x in options]
+    # Distribute the elements horizontally or vertically into a 2D list
+    # in row-major order.
+    array = containers.distribute(options_html, ncol, direction)
+    # Turn the list sideways to make it column-major.
+    columns = containers.transpose(array)
+    # Format each column into a <div>.
+    for i, col in enumerate(columns):
+        col = BR.join(columns[i])
+        columns[i] = HTML.div(c=col, style="float:left; margin-right: 2em")
+    return HTML.div(NL, NL.join(columns), NL)
+            
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.