Commits

Mark Lavin committed 2baf8c4

Add media definitions to widgets. Update example base to pull media from form. fixes #1.

Comments (0)

Files changed (2)

example/core/templates/base.html

 	<title>Django-Selectable Example</title>
         <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/themes/base/jquery-ui.css" type="text/css" media="screen">
         <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/themes/ui-lightness/jquery-ui.css" type="text/css" media="screen">
-        <link rel="stylesheet" href="{{ STATIC_URL }}css/dj.selectable.css" type="text/css" media="screen">
+        {{ form.media.css }}
         <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
         <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/jquery-ui.min.js"></script>
-        <script type="text/javascript" src="{{ STATIC_URL }}js/jquery.dj.selectable.js"></script>
+        {{ form.media.js }}
     </head>
     <body>
         <form action="{% url example-index %}" method="post" >

selectable/forms/widgets.py

 )
 
 
-class AutoCompleteWidget(forms.TextInput):
+MEDIA_URL = settings.MEDIA_URL
+STATIC_URL = getattr(settings, 'STATIC_URL', '')
+MEDIA_PREFIX = STATIC_URL or MEDIA_URL
+
+
+class SelectableMediaMixin(object):
+
+    class Media(object):
+        css = {
+            'all': ('%scss/dj.selectable.css' % MEDIA_PREFIX, )
+        }
+        js = ('%sjs/jquery.dj.selectable.js' % MEDIA_PREFIX, )
+
+
+class AutoCompleteWidget(forms.TextInput, SelectableMediaMixin):
 
     def __init__(self, lookup_class, *args, **kwargs):
         self.lookup_class = lookup_class
         attrs[u'data-selectable-allow-new'] = str(self.allow_new).lower()
         return attrs
 
-class AutoCompleteSelectWidget(forms.MultiWidget):
+
+class AutoCompleteSelectWidget(forms.MultiWidget, SelectableMediaMixin):
 
     def __init__(self, lookup_class, *args, **kwargs):
         self.lookup_class = lookup_class
         return [None, None]
 
 
-class AutoComboboxWidget(AutoCompleteWidget):
+class AutoComboboxWidget(AutoCompleteWidget, SelectableMediaMixin):
 
     def build_attrs(self, extra_attrs=None, **kwargs):
         attrs = super(AutoComboboxWidget, self).build_attrs(extra_attrs, **kwargs)
         return attrs
 
 
-class AutoComboboxSelectWidget(forms.MultiWidget):
+class AutoComboboxSelectWidget(forms.MultiWidget, SelectableMediaMixin):
 
     def __init__(self, lookup_class, *args, **kwargs):
         self.lookup_class = lookup_class
         return attrs
 
 
-class AutoCompleteSelectMultipleWidget(forms.MultiWidget):
+class AutoCompleteSelectMultipleWidget(forms.MultiWidget, SelectableMediaMixin):
 
     def __init__(self, lookup_class, *args, **kwargs):
         self.lookup_class = lookup_class
         return [None, None]
 
 
-class AutoComboboxSelectMultipleWidget(forms.MultiWidget):
+class AutoComboboxSelectMultipleWidget(forms.MultiWidget, SelectableMediaMixin):
 
     def __init__(self, lookup_class, *args, **kwargs):
         self.lookup_class = lookup_class
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.