~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/app/browser/lazrjs.py

  • Committer: Abel Deuring
  • Date: 2011-07-27 16:22:36 UTC
  • mfrom: (13539 devel)
  • mto: This revision was merged to the branch mainline in revision 13587.
  • Revision ID: abel.deuring@canonical.com-20110727162236-m5e4e45257ehq65r
devel merged

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
    'BooleanChoiceWidget',
9
9
    'EnumChoiceWidget',
10
10
    'InlineEditPickerWidget',
 
11
    'InlinePersonEditPickerWidget',
11
12
    'InlineMultiCheckboxWidget',
12
13
    'standard_text_html_representation',
13
14
    'TextAreaEditorWidget',
35
36
from canonical.launchpad.webapp.publisher import canonical_url
36
37
from canonical.launchpad.webapp.vocabulary import IHugeVocabulary
37
38
from lp.app.browser.stringformatter import FormattersAPI
 
39
from lp.app.browser.vocabulary import get_person_picker_entry_metadata
38
40
from lp.services.propertycache import cachedproperty
39
41
 
40
42
 
238
240
 
239
241
    def __init__(self, context, exported_field, default_html,
240
242
                 content_box_id=None, header='Select an item',
241
 
                 step_title='Search', assign_button_text='Pick Me',
242
 
                 remove_button_text='Remove Person',
 
243
                 step_title='Search',
243
244
                 null_display_value='None',
244
245
                 edit_view="+edit", edit_url=None, edit_title=''):
245
246
        """Create a widget wrapper.
252
253
            Automatically generated if this is not provided.
253
254
        :param header: The large text at the top of the picker.
254
255
        :param step_title: Smaller line of text below the header.
255
 
        :param assign_button_text: Override default button text: "Pick Me"
256
 
        :param remove_button_text: Override default button text: "Remove"
257
256
        :param null_display_value: This will be shown for a missing value
258
257
        :param edit_view: The view name to use to generate the edit_url if
259
258
            one is not specified.
267
266
        self.default_html = default_html
268
267
        self.header = header
269
268
        self.step_title = step_title
270
 
        self.assign_button_text = assign_button_text
271
 
        self.remove_button_text = remove_button_text
272
269
        self.null_display_value = null_display_value
273
270
 
274
271
        # JSON encoded attributes.
278
275
            self.exported_field.vocabularyName)
279
276
 
280
277
    @property
 
278
    def picker_type(self):
 
279
        return 'default'
 
280
 
 
281
    @property
 
282
    def selected_value_metadata(self):
 
283
        return None
 
284
 
 
285
    @property
 
286
    def selected_value(self):
 
287
        """ String representation of field value associated with the picker.
 
288
 
 
289
        Default implementation is to return the 'name' attribute.
 
290
        """
 
291
        if self.context is None:
 
292
            return None
 
293
        val = getattr(self.context, self.exported_field.__name__)
 
294
        if val is not None and safe_hasattr(val, 'name'):
 
295
            return getattr(val, 'name')
 
296
        return None
 
297
 
 
298
    @property
281
299
    def config(self):
 
300
        return self.getConfig()
 
301
 
 
302
    def getConfig(self):
282
303
        return dict(
 
304
            picker_type=self.picker_type,
283
305
            header=self.header, step_title=self.step_title,
284
 
            assign_button_text=self.assign_button_text,
285
 
            remove_button_text=self.remove_button_text,
 
306
            selected_value=self.selected_value,
 
307
            selected_value_metadata=self.selected_value_metadata,
286
308
            null_display_value=self.null_display_value,
287
 
            show_remove_button=self.optional_field,
288
 
            show_assign_me_button=self.show_assign_me_button,
289
309
            show_search_box=self.show_search_box)
290
310
 
291
311
    @property
302
322
    def show_search_box(self):
303
323
        return IHugeVocabulary.providedBy(self.vocabulary)
304
324
 
 
325
 
 
326
class InlinePersonEditPickerWidget(InlineEditPickerWidget):
 
327
    def __init__(self, context, exported_field, default_html,
 
328
                 content_box_id=None, header='Select an item',
 
329
                 step_title='Search', assign_me_text='Pick me',
 
330
                 remove_person_text='Remove person',
 
331
                 remove_team_text='Remove team',
 
332
                 null_display_value='None',
 
333
                 edit_view="+edit", edit_url=None, edit_title=''):
 
334
        """Create a widget wrapper.
 
335
 
 
336
        :param context: The object that is being edited.
 
337
        :param exported_field: The attribute being edited. This should be
 
338
            a field from an interface of the form ISomeInterface['fieldname']
 
339
        :param default_html: Default display of attribute.
 
340
        :param content_box_id: The HTML id to use for this widget.
 
341
            Automatically generated if this is not provided.
 
342
        :param header: The large text at the top of the picker.
 
343
        :param step_title: Smaller line of text below the header.
 
344
        :param assign_me_text: Override default button text: "Pick me"
 
345
        :param remove_person_text: Override default link text: "Remove person"
 
346
        :param remove_team_text: Override default link text: "Remove team"
 
347
        :param null_display_value: This will be shown for a missing value
 
348
        :param edit_view: The view name to use to generate the edit_url if
 
349
            one is not specified.
 
350
        :param edit_url: The URL to use for editing when the user isn't logged
 
351
            in and when JS is off.  Defaults to the edit_view on the context.
 
352
        :param edit_title: Used to set the title attribute of the anchor.
 
353
        """
 
354
        super(InlinePersonEditPickerWidget, self).__init__(
 
355
            context, exported_field, default_html, content_box_id, header,
 
356
            step_title, null_display_value,
 
357
            edit_view, edit_url, edit_title)
 
358
 
 
359
        self.assign_me_text = assign_me_text
 
360
        self.remove_person_text = remove_person_text
 
361
        self.remove_team_text = remove_team_text
 
362
 
 
363
    @property
 
364
    def picker_type(self):
 
365
        return 'person'
 
366
 
 
367
    @property
 
368
    def selected_value_metadata(self):
 
369
        val = getattr(self.context, self.exported_field.__name__)
 
370
        return get_person_picker_entry_metadata(val)
 
371
 
305
372
    @property
306
373
    def show_assign_me_button(self):
307
374
        # show_assign_me_button is true if user is in the vocabulary.
309
376
        user = getUtility(ILaunchBag).user
310
377
        return user and user in vocabulary
311
378
 
 
379
    def getConfig(self):
 
380
        config = super(InlinePersonEditPickerWidget, self).getConfig()
 
381
        config.update(dict(
 
382
            show_remove_button=self.optional_field,
 
383
            show_assign_me_button=self.show_assign_me_button,
 
384
            assign_me_text=self.assign_me_text,
 
385
            remove_person_text=self.remove_person_text,
 
386
            remove_team_text=self.remove_team_text))
 
387
        return config
 
388
 
312
389
 
313
390
class InlineMultiCheckboxWidget(WidgetBase):
314
391
    """Wrapper for the lazr-js multicheckbox widget."""
358
435
        linkify_items = attribute_type == "reference"
359
436
 
360
437
        if header is None:
361
 
            header = self.exported_field.title+":"
 
438
            header = self.exported_field.title + ":"
362
439
        self.header = header,
363
440
        self.empty_display_value = empty_display_value
364
441
        self.label = label
366
443
        self.label_close_tag = "</%s>" % label_tag
367
444
        self.items = selected_items
368
445
        self.items_open_tag = ("<%s id='%s'>" %
369
 
            (items_tag, self.content_box_id+"-items"))
 
446
            (items_tag, self.content_box_id + "-items"))
370
447
        self.items_close_tag = "</%s>" % items_tag
371
448
        self.linkify_items = linkify_items
372
449
 
376
453
            else:
377
454
                vocabulary = exported_field.vocabularyName
378
455
 
379
 
 
380
456
        if isinstance(vocabulary, basestring):
381
457
            vocabulary = getVocabularyRegistry().get(context, vocabulary)
382
458
 
522
598
    @property
523
599
    def config(self):
524
600
        return dict(
525
 
            contentBox='#'+self.content_box_id,
 
601
            contentBox='#' + self.content_box_id,
526
602
            value=self.current_value,
527
603
            title=self.header,
528
604
            items=[
578
654
    @property
579
655
    def config(self):
580
656
        return dict(
581
 
            contentBox='#'+self.content_box_id,
 
657
            contentBox='#' + self.content_box_id,
582
658
            value=self.value,
583
659
            title=self.header,
584
660
            items=self.items)