Commits

Maciej Wiśniowski  committed c3abb24

small refactor of manufacturer autocomplete

  • Participants
  • Parent commits 4ec972f

Comments (0)

Files changed (4)

File lfs/manage/views/manufacturer.py

 @never_cache
 @permission_required("core.manage_shop", login_url="/login/")
 def manufacturers_ajax(request):
-    #[ { label: "Choice1", value: "value1" }, ... ]
     """ Returns list of manufacturers for autocomplete
     """
     term = request.GET.get('term', '')

File lfs/static/js/lfs.manage.product.js

+/* Handle autocomplete on manufacturer field at product management page
+ * Uses jquery UI autocomplete implementation
+ */
+$(function() {
+    var MANUFACTURER_AUTOCOMPLETE_SETTINGS = {
+         source: MANUFACTURERS_AJAX_URL,
+         select: function(event, ui){
+             $('#id_manufacturer').val(ui.item.value);
+             $('#id_manufacturer_autocomplete').val(ui.item.label);
+             return false;
+         },
+         change: function(event, ui){
+             if (ui.item === null){
+                 // only accept items selected from the list
+                 $('#id_manufacturer').val('');
+                 $('#id_manufacturer_autocomplete').val('');
+             }
+             return false;
+         },
+         minLength: 1
+    }
+
+    $('#id_manufacturer_autocomplete').autocomplete(MANUFACTURER_AUTOCOMPLETE_SETTINGS);
+    // when form is reloaded we have to reattach autocomplete
+    $('body').bind('form-save-end', function(evt){
+        if (evt.form_id == 'product-data-form'){
+            $('#id_manufacturer_autocomplete').autocomplete(MANUFACTURER_AUTOCOMPLETE_SETTINGS);
+        }
+    })
+})

File lfs/templates/manage/product/data.html

                 {% else %}
                     <div>
                         {% if product.is_variant %}
-                            {{ product.get_manufacturer }}
+                            {{ product.get_manufacturer|default_if_none:"-" }}
                         {% else %}
                             {{ form.manufacturer }}
                             <input type="text" id="id_manufacturer_autocomplete" value="{{ form.instance.manufacturer.name }}" />

File lfs/templates/manage/product/product.html

 {% block section %}manage-product{% endblock %}
 
 {% block javascript %}
+    <script type="text/javascript" src="{{ STATIC_URL }}js/lfs.manage.product.js"></script>
     <script type="text/javascript">
         var MANUFACTURERS_AJAX_URL = '{% url lfs_manufacturers_ajax %}';
-
-        var MANUFACTURER_AUTOCOMPLETE_SETTINGS = {
-                                     source: MANUFACTURERS_AJAX_URL,
-                                     select: function(event, ui){
-                                         $('#id_manufacturer').val(ui.item.value);
-                                         $('#id_manufacturer_autocomplete').val(ui.item.label);
-                                         return false;
-                                     },
-                                     change: function(event, ui){
-                                         if (ui.item === null){
-                                             // only accept items selected from the list
-                                             $('#id_manufacturer').val('');
-                                             $('#id_manufacturer_autocomplete').val('');
-                                         }
-                                         return false;
-                                     },
-                                     minLength: 1}
-
-        $(function() {
-            $('#id_manufacturer_autocomplete').autocomplete(MANUFACTURER_AUTOCOMPLETE_SETTINGS);
-            // when form is reloaded we have to reattach autocomplete
-            $('body').bind('form-save-end', function(evt){
-                if (evt.form_id == 'product-data-form'){
-                    $('#id_manufacturer_autocomplete').autocomplete(MANUFACTURER_AUTOCOMPLETE_SETTINGS);
-                }
-            })
-        })
     </script>
 {% endblock %}