[PATCH v2 2/4] filters: Pre-populate delegate, submitter filters

Stephen Finucane stephen at that.guru
Sun Sep 30 07:26:08 AEST 2018


This appears to have got lost in the transition to 'selectize.js'. In
brief, this will ensure that a previously selected delegate or submitter
is still enabled post-filtering. For more information, see [1].

[1] https://stackoverflow.com/a/45124779

Signed-off-by: Stephen Finucane <stephen at that.guru>
Cc: Thomas Monjalon <thomas at monjalon.net>
Closes: #78
---
v2:
- Handle non-specific searches
---
 patchwork/templates/patchwork/filters.html | 71 +++++++++-------------
 1 file changed, 30 insertions(+), 41 deletions(-)

diff --git a/patchwork/templates/patchwork/filters.html b/patchwork/templates/patchwork/filters.html
index e760310b..9b0c4cb2 100644
--- a/patchwork/templates/patchwork/filters.html
+++ b/patchwork/templates/patchwork/filters.html
@@ -16,8 +16,6 @@ function filter_click()
         form.style['display'] = 'block';
         filterform_displayed = true;
     }
-
-
 }
 
 Selectize.define('enter_key_submit', function (options) {
@@ -39,9 +37,6 @@ Selectize.define('enter_key_submit', function (options) {
 
 $(document).ready(function() {
     $('#submitter_input').selectize({
-        valueField: 'pk',
-        labelField: 'name',
-        searchField: ['name', 'email'],
         plugins: ['enter_key_submit'],
         maxItems: 1,
         persist: false,
@@ -52,43 +47,37 @@ $(document).ready(function() {
                 this.$input.closest('form').submit();
             }, this);
         },
-        render: {
-            option: function(item, escape) {
-                if (item.name)
-                    return '<div>' + escape(item.name) + ' <' +
-                                     escape(item.email) + '>' + '</div>';
-                return '<div>' + escape(item.email) + '</div>';
+{% if "submitter" in filters.applied_filters %}
+{% with submitter_filter=filters.applied_filters.submitter %}
+        options: [
+            {
+                value: "{{ submitter_filter.key }}",
+                text: "{{ submitter_filter.condition }}",
             },
-            item: function(item, escape) {
-                if (item.name)
-                    return '<div>' + escape(item.name) + '</div>';
-                return '<div>' + escape(item.email) + '</div>';
-            }
-        },
+        ],
+        items: ["{{ submitter_filter.key }}"],
+{% endwith %}
+{% endif %}
         load: function(query, callback) {
             if (query.length < 4)
                 return callback();
 
             req = $.ajax({
-                url: '{% url 'api-submitters' %}?q=' +
-                      encodeURIComponent(query) + '&l=10',
+                url: "{% url 'api-submitters' %}",
+                data: {q: query, l: 10},
                 error: function() {
                     callback();
                 },
                 success: function(res) {
-                    callback(res);
+                    callback($.map(res, function (obj) {
+                        return {value: obj.pk, text: `${obj.name} <${obj.email}>`};
+                    }));
                 }
             });
         }
     });
-});
-
 
-$(document).ready(function() {
     $('#delegate_input').selectize({
-        valueField: 'pk',
-        labelField: 'name',
-        searchField: ['name'],
         plugins: ['enter_key_submit'],
         maxItems: 1,
         persist: false,
@@ -99,26 +88,28 @@ $(document).ready(function() {
                 this.$input.closest('form').submit();
             }, this);
         },
-        render: {
-            option: function(item, escape) {
-                if (item.email)
-                    return '<div>' + escape(item.name) + ' <' +
-                                     escape(item.email) + '>' + '</div>';
-                return '<div>' + escape(item.name) + '</div>';
+{% if "delegate" in filters.applied_filters %}
+{% with delegate_filter=filters.applied_filters.delegate %}
+        options: [
+            {
+                value: "{{ delegate_filter.key }}",
+                text: "{{ delegate_filter.condition }}",
             },
-            item: function(item, escape) {
-                return '<div>' + escape(item.name) + '</div>';
-            }
-        },
+        ],
+        items: ["{{ delegate_filter.key }}"],
+{% endwith %}
+{% endif %}
         load: function(query, callback) {
             req = $.ajax({
-                url: '{% url 'api-delegates' %}?q=' +
-                      encodeURIComponent(query) + '&l=10',
+                url: "{% url 'api-delegates' %}",
+                data: {q: query, l: 10},
                 error: function() {
                     callback();
                 },
                 success: function(res) {
-                    callback(res);
+                    callback($.map(res, function (obj) {
+                        return {value: obj.pk, text: obj.name};
+                    }));
                 }
             });
         }
@@ -167,5 +158,3 @@ $(document).ready(function() {
   </form>
  </div>
 </div>
-
-
-- 
2.17.1



More information about the Patchwork mailing list