[PATCH 6/7] filters: Use jQuery UI autocomplete for submitters

Stephen Finucane stephen.finucane at intel.com
Wed Sep 9 22:59:13 AEST 2015


Extend use of the jQuery UI autocomplete widget to the submitters
field.

Signed-off-by: Stephen Finucane <stephen.finucane at intel.com>
---
 patchwork/filters.py                       |   9 +--
 patchwork/templates/patchwork/filters.html | 120 +++--------------------------
 patchwork/urls.py                          |   2 +-
 patchwork/views/base.py                    |  32 ++++----
 4 files changed, 25 insertions(+), 138 deletions(-)

diff --git a/patchwork/filters.py b/patchwork/filters.py
index 03a7616..dc90f89 100644
--- a/patchwork/filters.py
+++ b/patchwork/filters.py
@@ -137,14 +137,7 @@ class SubmitterFilter(Filter):
         return ''
 
     def _form(self):
-        name = ''
-        if self.person:
-            name = self.person.name
-        return mark_safe(
-            '<input onKeyUp="submitter_field_change(this)"'
-            ' name="submitter" id="submitter_input" value="%s"> '
-            '<select id="submitter_select" disabled="true"></select>'
-            % escape(name))
+        return mark_safe('<input id="submitter" name="submitter">')
 
     def key(self):
         if self.person:
diff --git a/patchwork/templates/patchwork/filters.html b/patchwork/templates/patchwork/filters.html
index ff1935c..fc2e0ec 100644
--- a/patchwork/templates/patchwork/filters.html
+++ b/patchwork/templates/patchwork/filters.html
@@ -5,8 +5,8 @@
 <script type="text/javascript" src="{% static "js/jquery-ui-1.11.4.min.js" %}"></script>
 <script type="text/javascript" language="JavaScript">
 var filterform_displayed = false;
-function filter_click()
-{
+
+function filter_click() {
     var form = document.getElementById('filterform');
     if (!form) {
         return;
@@ -19,131 +19,24 @@ function filter_click()
         form.style['display'] = 'block';
         filterform_displayed = true;
     }
-
-
-}
-function enable_selected_submitter(select, input)
-{
-    select.name = 'submitter';
-    input.name = '';
 }
+
 function filter_form_submit(form)
 {
     var i;
 
-    var submitter_select = document.getElementById("submitter_select");
-    var submitter_input = document.getElementById("submitter_input");
-    if (!submitter_select || !submitter_input) {
-        req = null;
-        return;
-    }
-
-    /* submitter handling. if possible, use the select box, otherwise leave
-     * as-is (and so the text box is used). */
-
-    if (submitter_select.options.length == 0) {
-        /* if there's no match, just use the input */
-
-    } else if (submitter_select.options.length == 1) {
-        /* if there's only one match, request by id */
-        submitter_select.selectedIndex = 0;
-        enable_selected_submitter(submitter_select, submitter_input);
-
-    } else if (submitter_select.selectedIndex != -1) {
-        /* if the user has explicitly selected, request by id */
-        enable_selected_submitter(submitter_select, submitter_input);
-
-    }
-
     for (i = 0; i < form.elements.length; i++) {
         var e = form.elements[i];
         if (e.type == 'submit') {
             continue;
         }
 
-        /* handle submitter data */
-        if (e.type == 'select-one') {
-            if (e.name == '') {
-                e.disabled = true;
-            }
-            if (e.selectedIndex != -1
-                    && e.options[e.selectedIndex].value == '') {
-                e.disabled = true;
-            }
-
-            continue;
-        }
-
         if (e.value == '') {
             e.disabled = true;
         }
     }
 }
 
-var req = null;
-
-function submitter_complete_response()
-{
-    if (req.readyState != 4) {
-        return
-    }
-
-    var completions;
-    eval("completions = " + req.responseText);
-
-    if (completions.length == 0) {
-        req = null;
-        return;
-    }
-
-    var submitter_select = document.getElementById("submitter_select");
-    var submitter_input = document.getElementById("submitter_input");
-    if (!submitter_select || !submitter_input) {
-        req = null;
-        return;
-    }
-
-    for (i = 0; i < completions.length; i++) {
-        name = completions[i]['fields']['name'];
-        if (name) {
-            name = completions[i]['fields']['name'] +
-                ' <' + completions[i]['fields']['email'] + '>';
-        } else {
-            name = completions[i]['fields']['email'];
-        }
-        o = new Option(name, completions[i]['pk']);
-        submitter_select.options[i] = o;
-    }
-
-    /* remove remaining options */
-    for (; i < submitter_select.length; i++) {
-        submitter_select.options[i] = null;
-    }
-
-    submitter_select.disabled = false;
-    req = null;
-}
-
-function submitter_field_change(field)
-{
-    var limit = 20;
-    var value = field.value;
-    if (value.length < 4) {
-        return;
-    }
-
-    if (req) {
-         return;
-    }
-
-    var url = '{% url 'patchwork.views.submitter_complete' %}?q=' + value +
-                        '&l=' + limit;
-    req = new XMLHttpRequest();
-    req.onreadystatechange = submitter_complete_response;
-    req.open("GET", url, true);
-    req.send('');
-}
-
 $(function() {
     $("#delegate").autocomplete({
         source: "{% url 'patchwork.views.get_delegates' %}",
@@ -159,6 +52,13 @@ $(function() {
         }
     });
 });
+
+$(function() {
+    $("#submitter").autocomplete({
+        source: "{% url 'patchwork.views.get_submitters' %}",
+        minLength: 3,
+    });
+});
 </script>
 
 <div class="filters">
diff --git a/patchwork/urls.py b/patchwork/urls.py
index 53ac6e5..026eb7a 100644
--- a/patchwork/urls.py
+++ b/patchwork/urls.py
@@ -73,7 +73,7 @@ urlpatterns = patterns('',
     (r'^confirm/(?P<key>[0-9a-f]+)/$', 'patchwork.views.confirm'),
 
     # submitter/delegate autocomplete
-    (r'^submitter/$', 'patchwork.views.submitter_complete'),
+    (r'^submitter/$', 'patchwork.views.get_submitters'),
     (r'^delegate/$', 'patchwork.views.get_delegates'),
 
     # email setup
diff --git a/patchwork/views/base.py b/patchwork/views/base.py
index 501e994..3e961ab 100644
--- a/patchwork/views/base.py
+++ b/patchwork/views/base.py
@@ -97,29 +97,23 @@ def confirm(request, key):
     return render_to_response('patchwork/confirm-error.html', context)
 
 
-def submitter_complete(request):
-    search = request.GET.get('q', '')
-    limit = request.GET.get('l', None)
-    response = HttpResponse(content_type='text/plain')
-
-    if len(search) <= 3:
-        return response
+def get_submitters(request):
+    if not request.is_ajax():
+        return HttpResponse('fail', 'application/json')
 
+    search = request.GET.get('term', '')
     queryset = Person.objects.filter(Q(name__icontains=search) |
                                      Q(email__icontains=search))
-    if limit is not None:
-        try:
-            limit = int(limit)
-        except ValueError:
-            limit = None
-
-    if limit is not None and limit > 0:
-            queryset = queryset[:limit]
-
-    json_serializer = serializers.get_serializer('json')()
-    json_serializer.serialize(queryset, ensure_ascii=False, stream=response)
+    results = []
+    for person in queryset:
+        result = {}
+        result['id'] = person.id
+        result['label'] = str(person)
+        result['value'] = person.id
+        results.append(result)
+    data = json.dumps(results)
 
-    return response
+    return HttpResponse(data, 'application/json')
 
 
 def get_delegates(request):
-- 
2.0.0



More information about the Patchwork mailing list