| 1 |
from zope.interface import implementer |
| 2 |
from twisted.web.template import tags as T |
| 3 |
from .. import iformal |
| 4 |
from ..util import render_cssid |
| 5 |
|
| 6 |
_UNSET = object() |
| 7 |
|
| 8 |
class MultichoiceBase(object): |
| 9 |
""" |
| 10 |
A base class for widgets that provide the UI to select one or more items |
| 11 |
from a list. |
| 12 |
|
| 13 |
Based on ChoiceBase |
| 14 |
|
| 15 |
options: |
| 16 |
A sequence of objects adaptable to IKey and ILabel. IKey is used as the |
| 17 |
<option>'s value attribute; ILabel is used as the <option>'s child. |
| 18 |
IKey and ILabel adapters for tuple are provided. |
| 19 |
noneOption: |
| 20 |
An object adaptable to IKey and ILabel that is used to identify when |
| 21 |
nothing has been selected. Defaults to ('', '') |
| 22 |
|
| 23 |
default can be whitespace-separated items. |
| 24 |
""" |
| 25 |
|
| 26 |
options = None |
| 27 |
noneOption = None |
| 28 |
|
| 29 |
def __init__(self, original, options=None, noneOption=_UNSET): |
| 30 |
self.original = original |
| 31 |
if options is not None: |
| 32 |
self.options = options |
| 33 |
if noneOption is not _UNSET: |
| 34 |
self.noneOption = noneOption |
| 35 |
|
| 36 |
def processInput(self, request, key, args, default=''): |
| 37 |
values = args.get(key, default.split()) |
| 38 |
rv = [] |
| 39 |
for value in values: |
| 40 |
value = iformal.IStringConvertible(self.original).toType(value) |
| 41 |
if self.noneOption is not None and value == self.noneOption[0]: |
| 42 |
value = None |
| 43 |
rv.append(self.original.validate(value)) |
| 44 |
return rv |
| 45 |
|
| 46 |
@implementer( iformal.IWidget ) |
| 47 |
class MultiselectChoice(MultichoiceBase): |
| 48 |
""" |
| 49 |
A drop-down list of options. |
| 50 |
""" |
| 51 |
|
| 52 |
noneOption = ('', '') |
| 53 |
|
| 54 |
def _renderTag(self, request, key, value, converter, disabled): |
| 55 |
|
| 56 |
def renderOptions(request, data): |
| 57 |
if self.noneOption is not None: |
| 58 |
yield T.option(value=iformal.IKey(self.noneOption).key())[iformal.ILabel(self.noneOption).label()] |
| 59 |
if data is None: |
| 60 |
return |
| 61 |
for item in data: |
| 62 |
optValue = iformal.IKey(item).key() |
| 63 |
optLabel = iformal.ILabel(item).label() |
| 64 |
optValue = converter.fromType(optValue) |
| 65 |
option = T.option(value=str(optValue))[str(optLabel)] |
| 66 |
|
| 67 |
if value and optValue in value: |
| 68 |
option = option(selected='selected') |
| 69 |
|
| 70 |
yield option |
| 71 |
|
| 72 |
tag=T.select(name=key, id=render_cssid(key), data=self.options, multiple="multiple")[renderOptions] |
| 73 |
|
| 74 |
if disabled: |
| 75 |
tag(class_='disabled', disabled='disabled') |
| 76 |
return tag |
| 77 |
|
| 78 |
def render(self, request, key, args, errors): |
| 79 |
converter = iformal.IStringConvertible(self.original) |
| 80 |
if errors: |
| 81 |
value = args.get(key, ['']) |
| 82 |
else: |
| 83 |
value = converter.fromType(args.get(key)) |
| 84 |
return self._renderTag(request, key, value, converter, False) |
| 85 |
|
| 86 |
def renderImmutable(self, request, key, args, errors): |
| 87 |
converter = iformal.IStringConvertible(self.original) |
| 88 |
value = converter.fromType(args.get(key)) |
| 89 |
return self._renderTag(request, key, value, converter, True) |
| 90 |
|
| 91 |
|
| 92 |
__all__ = ["MultichoiceBase", "MultiselectChoice"] |