class TextLineWidget(SchemaFieldWidget): template = getTemplate('textlinewidget.pt') defaultHtmlClass = ['field', 'field-textline'] defaultHtmlAttributes = set([ 'readonly', 'required', 'autocomplete', 'maxlength', 'pattern', 'placeholder', 'size', 'style', 'disabled' ])
class LinkFieldWidget(FieldWidget): template = getTemplate('linkfieldwidget.pt') def url(self): content = self.form.getContentData().getContent() return IURL(content, self.request)
class MultiChoiceFieldWidget(ChoiceFieldWidget): template = getTemplate('multichoicefieldwidget.pt') def __init__(self, field, value_field, form, request): super(MultiChoiceFieldWidget, self).__init__(field, form, request) self.source = value_field def prepareContentValue(self, value): form_value = [] if value is NO_VALUE: return {self.identifier: form_value} choices = self.choices() for entry in value: try: term = choices.getTerm(entry) form_value.append(term.token) except LookupError: pass return {self.identifier: form_value} def renderableChoice(self): current = self.inputValue() base_id = self.htmlId() for i, choice in enumerate(self.choices()): yield {'token': choice.token, 'title': choice.title, 'checked': choice.token in current, 'id': base_id + '-' + str(i)}
class ChoiceFieldWidget(SchemaFieldWidget): template = getTemplate('choicefieldwidget.pt') def __init__(self, field, form, request): super(ChoiceFieldWidget, self).__init__(field, form, request) self.source = field self.__choices = None def lookupTerm(self, value): choices = self.choices() try: return choices.getTerm(value) except LookupError: # the stored value is invalid. fallback on the default one. default = self.component.getDefaultValue(self.form) if default is not NO_VALUE: return choices.getTerm(default) return None def valueToUnicode(self, value): term = self.lookupTerm(value) if term is not None: return term.token return u'' def choices(self): if self.__choices is not None: return self.__choices # self.source is used instead of self.component in order to be # able to override it in subclasses. self.__choices = self.source.getChoices(self.form.context) return self.__choices
class ListObjectFieldWidget(MultiObjectFieldWidget): template = getTemplate('listobjectfieldwidget.pt') def __init__(self, field, value_field, form, request): super(ListObjectFieldWidget, self).__init__( field, value_field, form, request) self.allowOrdering = field.allowOrdering
class ChoiceDisplayWidget(ChoiceFieldWidget): template = getTemplate('choicedisplaywidget.pt') def valueToUnicode(self, value): term = self.lookupTerm(value) if term is not None: return term.title return u''
class MultiChoiceDisplayFieldWidget(MultiChoiceFieldWidget): template = getTemplate('multichoicedisplayfieldwidget.pt') def renderableChoice(self): current = self.inputValue() base_id = self.htmlId() for i, choice in enumerate(self.choices()): if choice.token in current: yield {'title': choice.title, 'id': base_id + '-' + str(i)}
class RadioFieldWidget(ChoiceFieldWidget): template = getTemplate('radiofieldwidget.pt') def renderableChoices(self): current = self.inputValue() base_id = self.htmlId() for i, choice in enumerate(self.choices()): yield {'token': choice.token, 'title': choice.title or choice.token, 'checked': choice.token == current and 'checked' or None, 'id': base_id + '-' + str(i)}
class ObjectFieldWidget(SchemaFieldWidget): template = getTemplate('objectfieldwidget.pt') def prepareContentValue(self, value): if value is NO_VALUE: return {self.identifier: []} return {self.identifier: value} def update(self): super(ObjectFieldWidget, self).update() value = self.component.dataManager(self.inputValue()) form = cloneFormData(self.form, value, self.identifier) self.objectWidgets = Widgets(form=form, request=self.request) self.objectWidgets.extend(self.component.objectFields) self.objectWidgets.update()
class PasswordWidget(SchemaFieldWidget): template = getTemplate('passwordwidget.pt') defaultHtmlClass = ['field', 'field-password'] defaultHtmlAttributes = ['readonly', 'required', 'autocomplete', 'maxlength', 'pattern', 'placeholder', 'size', 'style']
class MultiSelectFieldWidget(MultiChoiceFieldWidget): template = getTemplate('multiselectfieldwidget.pt')
class TextFieldWidget(SchemaFieldWidget): template = getTemplate('textfieldwidget.pt') defaultHtmlClass = ['field'] defaultHtmlAttributes = set(['maxlength', 'placeholder', 'required', 'rows', 'warp', 'readonly', 'cols', 'style'])
class TextDisplayWidget(SchemaFieldWidget): template = getTemplate('pre_text.pt') defaultHtmlAttributes = set(['style'])
class MultiObjectFieldWidget(MultiGenericFieldWidget): template = getTemplate('multiobjectfieldwidget.pt') def getFields(self): return self.valueField.objectFields
class MultiGenericDisplayFieldWidget(MultiGenericFieldWidget): template = getTemplate('multigenericdisplayfieldwidget.pt')
class MultiGenericFieldWidget(SchemaFieldWidget): allowAdding = True allowRemove = True template = getTemplate('multigenericfieldwidget.pt') def __init__(self, field, value_field, form, request): super(MultiGenericFieldWidget, self).__init__(field, form, request) self.allowAdding = field.allowAdding self.allowRemove = field.allowRemove self.valueField = value_field self.valueWidgets = Widgets() self.haveValues = False def createValueWidget(self, new_identifier, value): field = self.valueField.clone(new_identifier=str(new_identifier)) form = cloneFormData(self.form, prefix=self.identifier) if value is not None: form.ignoreContent = False form.setContentData(NoneDataManager(value)) else: form.ignoreRequest = False form.ignoreContent = True return createWidget(field, form, self.request) def addValueWidget(self, new_identifier, value): widget = self.createValueWidget(new_identifier, value) if widget is not None: self.valueWidgets.append(widget) return widget def prepareContentValue(self, values): if values is NO_VALUE: return {self.identifier: '0'} for position, value in enumerate(values): # Create new widgets for each value self.addValueWidget(position, value) count = len(values) if count: self.haveValues = True return {self.identifier: str(count)} def prepareRequestValue(self, values): value_count = 0 identifier_count = int(values.get(self.identifier, '0')) remove_something = self.identifier + '.remove' in values for position in range(0, identifier_count): value_marker = (self.identifier, position,) value_present = '%s.present.%d' % value_marker in values if not value_present: continue value_selected = '%s.checked.%d' % value_marker in values if remove_something and value_selected: continue self.addValueWidget(position, None) value_count += 1 if self.identifier + '.add' in values: self.addValueWidget(identifier_count, None) value_count += 1 values[self.identifier] = str(identifier_count + 1) if value_count: self.haveValues = True return values @property def jsonTemplateWidget(self): widgets = Widgets() widgets.append(self.createValueWidget('{identifier}', None)) widgets.update() return list(widgets)[0] def update(self): super(MultiGenericFieldWidget, self).update() self.valueWidgets.update() self.jsonAddIdentifier = None self.jsonAddTemplate = None self.includeEmptyMessage = self.allowRemove if self.allowAdding: self.jsonAddIdentifier = 'id' + md5hash(self.identifier) widgets = Widgets() widgets.append(self.createValueWidget( '{' + self.jsonAddIdentifier + '}', None)) widgets.update() self.jsonAddTemplate = list(widgets)[0]
class CheckBoxWidget(SchemaFieldWidget): template = getTemplate('checkboxwidget.pt')