Commits

Moritz Schlarb committed 26a8e06

SingleSelectField and MultipleSelectField ready

All options usable

  • Participants
  • Parent commits 8ab116b

Comments (0)

Files changed (3)

File tw2/jqplugins/chosen/__init__.py

+from widgets import *

File tw2/jqplugins/chosen/samples.py

 See http://toscawidgets.org/documentation/WidgetBrowser for more information
 """
 
-from widgets import Chosen
+from widgets import *
 
 
-class DemoChosen(Chosen):
-    # Provide default parameters, value, etc... here
-    # default = <some-default-value>
-    pass
+class DemoChosenSingleSelectField(ChosenSingleSelectField):
+    options = [('Group 1', ['Item 1', 'Item 2']),
+        ('Group 2', ['Item 3', 'Item 4', 'Item 5', 'Item 6'])]
+    value = 'Item 2'
+    attrs = dict(style='width: 200px')
+
+
+class DemoChosenMultipleSelectField(ChosenMultipleSelectField):
+    options = [('Group 1', ['Item 1', 'Item 2']),
+        ('Group 2', ['Item 3', 'Item 4', 'Item 5', 'Item 6'])]
+    #value = ['Item 2', 'Item 5']
+    placeholder = 'Select an item, you won\'t regret it'
+    attrs = dict(style='width: 400px')

File tw2/jqplugins/chosen/widgets.py

 import tw2.forms as twf
 import tw2.jquery as twj
 
+__all__ = [
+    'chosen_img',
+    'chosen_js',
+    'chosen_css',
+    'ChosenSingleSelectField',
+    'ChosenMultipleSelectField']
 
+
+chosen_img = twc.Link(filename='static/chosen-sprite.png')
 chosen_js = twc.JSLink(
     filename='static/chosen.jquery.js',
     resources=[twj.jquery_js])
 chosen_css = twc.CSSLink(filename='static/chosen.css')
 
 
-class Chosen(twf.MultipleSelectionField):
-    #template = "mako:tw2.jqplugins.chosen.templates.chosen"
-
-    # declare static resources here
-    # you can remove either or both of these, if not needed
+class ChosenMixin(twc.Widget):
     resources = [chosen_js, chosen_css]
 
     selector = twc.Variable("Escaped id.  jQuery selector.")
+    opts = twc.Variable(
+        'Arguments for the javascript init function',
+        default=dict())
+
+    placeholder = twc.Param(
+        'Placeholder text, prompting user for selection',
+        default='')
+    no_results_text = twc.Param(
+        'Text shown when the search term returned no results',
+        default='')
 
     def prepare(self):
-        super(Chosen, self).prepare()
+        super(ChosenMixin, self).prepare()
         # put code here to run just before the widget is displayed
         if 'id' in self.attrs:
             self.selector = "#" + self.attrs['id'].replace(':', '\\:')
-        self.add_call(twj.jQuery(self.selector).chosen())
+
+        if self.placeholder:
+            self.attrs['data-placeholder'] = self.placeholder
+        if self.no_results_text:
+            self.opts['no_results_text'] = self.no_results_text
+
+        self.add_call(twj.jQuery(self.selector).chosen(self.opts))
+
+
+class ChosenSingleSelectField(ChosenMixin, twf.SingleSelectField):
+
+    def prepare(self):
+        # If field is not required, this adds a button for deselection
+        if not self.required:
+            self.opts['allow_single_deselect'] = True
+        super(ChosenSingleSelectField, self).prepare()
+
+
+class ChosenMultipleSelectField(ChosenMixin, twf.MultipleSelectField):
+    pass