19
18
from zope.schema.interfaces import IChoice
21
from lp.app.browser.stringformatter import FormattersAPI
22
from lp.app.browser.vocabulary import get_person_picker_entry_metadata
20
from canonical.launchpad.webapp import canonical_url
23
21
from lp.services.propertycache import cachedproperty
24
from lp.services.webapp import canonical_url
25
from lp.services.webapp.vocabulary import IHugeVocabulary
28
24
class VocabularyPickerWidget(SingleDataHelper, ItemsWidgetBase):
31
27
__call__ = ViewPageTemplateFile('templates/form-picker.pt')
33
picker_type = 'default'
34
# Provide default values for the following properties in case someone
35
# creates a vocab picker for a person instead of using the derived
37
show_assign_me_button = False
38
show_remove_button = False
39
assign_me_text = 'Pick me'
40
remove_person_text = 'Remove person'
41
remove_team_text = 'Remove team'
43
29
popup_name = 'popup-vocabulary-picker'
45
31
# Override inherited attributes for the form field.
93
79
def inputField(self):
95
81
'formToken': cgi.escape(self.formToken, quote=True),
96
'name': self.input_id,
97
83
'displayWidth': self.displayWidth,
98
84
'displayMaxWidth': self.displayMaxWidth,
99
85
'onKeyPress': self.onKeyPress,
107
93
class="%(cssClass)s" />""" % d
110
def selected_value(self):
111
""" String representation of field value associated with the picker.
113
Default implementation is to return the 'name' attribute.
115
val = self._getFormValue()
116
if val is not None and safe_hasattr(val, 'name'):
117
return getattr(val, 'name')
121
def selected_value_metadata(self):
97
return self.name.replace('.', '-')
125
100
def show_widget_id(self):
126
return 'show-widget-%s' % self.input_id.replace('.', '-')
131
picker_type=self.picker_type,
132
selected_value=self.selected_value,
133
selected_value_metadata=self.selected_value_metadata,
134
header=self.header_text, step_title=self.step_title_text,
135
extra_no_results_message=self.extra_no_results_message,
136
assign_me_text=self.assign_me_text,
137
remove_person_text=self.remove_person_text,
138
remove_team_text=self.remove_team_text,
139
show_remove_button=self.show_remove_button,
140
show_assign_me_button=self.show_assign_me_button,
141
vocabulary_name=self.vocabulary_name,
142
vocabulary_filters=self.vocabulary_filters,
143
input_element=self.input_id)
146
def json_config(self):
147
return simplejson.dumps(self.config)
101
return 'show-widget-%s' % self.suffix
150
104
def extra_no_results_message(self):
155
109
:return: A string that will be passed to Y.Node.create()
156
110
so it needs to be contained in a single HTML element.
161
def vocabulary_filters(self):
162
"""The name of the field's vocabulary."""
163
choice = IChoice(self.context)
164
if choice.vocabulary is None:
165
# We need the vocabulary to get the supported filters.
167
"The %r.%s interface attribute doesn't have its "
168
"vocabulary specified."
169
% (choice.context, choice.__name__))
170
# Only IHugeVocabulary's have filters.
171
if not IHugeVocabulary.providedBy(choice.vocabulary):
173
supported_filters = choice.vocabulary.supportedFilters()
174
# If we have no filters or just the ALL filter, then no filtering
175
# support is required.
177
if (len(supported_filters) == 0 or
178
(len(supported_filters) == 1
179
and supported_filters[0].name == 'ALL')):
181
for filter in supported_filters:
184
'title': filter.title,
185
'description': filter.description,
112
return simplejson.dumps(None)
190
115
def vocabulary_name(self):
205
130
def header_text(self):
206
return self.header or self.vocabulary.displayname
131
return simplejson.dumps(self.header or self.vocabulary.displayname)
209
134
def step_title_text(self):
210
return self.step_title or self.vocabulary.step_title
135
return simplejson.dumps(self.step_title or self.vocabulary.step_title)
213
138
def input_id(self):
214
"""This is used to ensure the widget id contains only valid chars."""
215
return FormattersAPI(self.name).zope_css_id()
217
141
def chooseLink(self):
218
142
if self.nonajax_uri is None:
235
159
class PersonPickerWidget(VocabularyPickerWidget):
237
160
include_create_team_link = False
238
show_assign_me_button = True
239
show_remove_button = False
240
picker_type = 'person'
243
def selected_value_metadata(self):
244
val = self._getFormValue()
245
return get_person_picker_entry_metadata(val)
247
162
def chooseLink(self):
248
163
link = super(PersonPickerWidget, self).chooseLink()
287
203
def extra_no_results_message(self):
288
return ("<strong>Didn't find the project you were "
204
return simplejson.dumps("<strong>Didn't find the project you were "
290
206
'<a href="%s/+affects-new-product">Register it</a>.</strong>'
291
207
% canonical_url(self.context.context))