1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
import json |
4
|
|
|
from bika.lims import _ |
5
|
|
|
from Products.Archetypes.Registry import registerWidget |
6
|
|
|
from Products.Archetypes.Widget import StringWidget |
7
|
|
|
from senaite.core.i18n import translate as t |
8
|
|
|
from senaite.core.z3cform.widgets.selectother.widget import OTHER_OPTION_VALUE |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class SelectOtherWidget(StringWidget): |
12
|
|
|
"""Select Other Widget for AT fields |
13
|
|
|
""" |
14
|
|
|
# CSS class that is picked up by the ReactJS component |
15
|
|
|
klass = u"senaite-selectother-widget-input" |
16
|
|
|
|
17
|
|
|
_properties = StringWidget._properties.copy() |
18
|
|
|
_properties.update({ |
19
|
|
|
"macro": "senaite_widgets/selectotherwidget", |
20
|
|
|
}) |
21
|
|
|
|
22
|
|
|
def process_form(self, instance, field, form, empty_marker=None, |
23
|
|
|
emptyReturnsMarker=False, validating=True): |
24
|
|
|
value = form.get(field.getName(), "") |
25
|
|
|
if isinstance(value, list): |
26
|
|
|
if value[0] == OTHER_OPTION_VALUE: |
27
|
|
|
return value[1], {} |
28
|
|
|
value = value[0] |
29
|
|
|
|
30
|
|
|
return value, {} |
31
|
|
|
|
32
|
|
View Code Duplication |
def get_input_widget_attributes(self, context, field, value): |
|
|
|
|
33
|
|
|
"""Return input widget attributes for the ReactJS component |
34
|
|
|
|
35
|
|
|
This method get called from the page template to populate the |
36
|
|
|
attributes that are used by the ReactJS widget component. |
37
|
|
|
|
38
|
|
|
:param context: The current context of the field |
39
|
|
|
:param field: The current field of the widget |
40
|
|
|
:param value: The current field value |
41
|
|
|
""" |
42
|
|
|
option = "" |
43
|
|
|
other = "" |
44
|
|
|
|
45
|
|
|
# find out if the value is a predefined option |
46
|
|
|
choices = self.get_choices(context, field) |
47
|
|
|
options = dict(choices).keys() |
48
|
|
|
if value in options: |
49
|
|
|
option = value |
50
|
|
|
elif value: |
51
|
|
|
option = OTHER_OPTION_VALUE |
52
|
|
|
other = value |
53
|
|
|
|
54
|
|
|
attributes = { |
55
|
|
|
"data-id": field.getName(), |
56
|
|
|
"data-name": field.getName(), |
57
|
|
|
"data-choices": choices, |
58
|
|
|
"data-option": option, |
59
|
|
|
"data-option_other": OTHER_OPTION_VALUE, |
60
|
|
|
"data-other": other, |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
# convert all attributes to JSON |
64
|
|
|
for key, value in attributes.items(): |
65
|
|
|
attributes[key] = json.dumps(value) |
66
|
|
|
|
67
|
|
|
return attributes |
68
|
|
|
|
69
|
|
|
def get_vocabulary(self, context, field): |
70
|
|
|
func = getattr(field, "Vocabulary", None) |
71
|
|
|
if callable(func): |
72
|
|
|
return func(context) |
73
|
|
|
return None |
74
|
|
|
|
75
|
|
|
def get_choices(self, context, field): |
76
|
|
|
"""Returns the predefined options for this field |
77
|
|
|
""" |
78
|
|
|
# generate a list of tuples (value, text) from vocabulary |
79
|
|
|
vocabulary = self.get_vocabulary(context, field) |
80
|
|
|
choices = list(vocabulary.items()) if vocabulary else [] |
81
|
|
|
|
82
|
|
|
# insert the empty option |
83
|
|
|
choices.insert(0, ("", "")) |
84
|
|
|
|
85
|
|
|
# append the "Other..." choice |
86
|
|
|
other = (OTHER_OPTION_VALUE, t(_("Other..."))) |
87
|
|
|
choices.append(other) |
88
|
|
|
|
89
|
|
|
return choices |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
registerWidget(SelectOtherWidget, title="Select Other Widget") |
93
|
|
|
|