Explorar el Código

HUE-2332 [search] Display correct icon for selected exclusion facets

Romain Rigaux hace 11 años
padre
commit
051903d2e4

+ 21 - 9
apps/search/src/search/models.py

@@ -415,22 +415,30 @@ def get_facet_field(category, field, facets):
   else:
     return None
 
-def pairwise2(cat, selected_values, iterable):
+def pairwise2(cat, fq_filter, iterable):
   pairs = []
+  selected_values = [f['value'] for f in fq_filter]
   a, b = itertools.tee(iterable)
   for element in a:
-    pairs.append({'cat': cat, 'value': element, 'count': next(a), 'selected': element in selected_values})
+    pairs.append({
+        'cat': cat, 'value': element, 'count': next(a), 'selected': element in selected_values,
+        'exclude': all([f['exclude'] for f in fq_filter if f['value'] == element])
+    })
   return pairs
 
-def range_pair(cat, selected_values, iterable, end):
+def range_pair(cat, fq_filter, iterable, end):
   # e.g. counts":["0",17430,"1000",1949,"2000",671,"3000",404,"4000",243,"5000",165],"gap":1000,"start":0,"end":6000}
   pairs = []
+  selected_values = [f['value'] for f in fq_filter]
   a, to = itertools.tee(iterable)
   next(to, None)
   for element in a:
     next(to, None)
     to_value = next(to, end)
-    pairs.append({'field': cat, 'from': element, 'value': next(a), 'to': to_value, 'selected': element in selected_values})
+    pairs.append({
+        'field': cat, 'from': element, 'value': next(a), 'to': to_value, 'selected': element in selected_values,
+        'exclude': all([f['exclude'] for f in fq_filter if f['value'] == element])
+    })
   return pairs
 
 
@@ -440,7 +448,7 @@ def augment_solr_response(response, collection, query):
 
   normalized_facets = []
 
-  selected_values = dict([((fq['id'], fq['field'], fq['type']), [f['value'] for f in fq['filter']]) for fq in query['fqs']])
+  selected_values = dict([((fq['id'], fq['field'], fq['type']), fq['filter']) for fq in query['fqs']])
 
   if response and response.get('facet_counts'):
     # e.g. [{u'field': u'sun', u'type': u'query', u'id': u'67b43a63-ed22-747b-47e8-b31aad1431ea', u'label': u'sun'}
@@ -450,7 +458,6 @@ def augment_solr_response(response, collection, query):
       if category == 'field' and response['facet_counts']['facet_fields']:
         name = facet['field']
         collection_facet = get_facet_field(category, name, collection['facets'])
-        print selected_values.get((facet['id'], name, category))
         counts = pairwise2(name, selected_values.get((facet['id'], name, category), []), response['facet_counts']['facet_fields'][name])
         if collection_facet['properties']['sort'] == 'asc':
           counts.reverse()
@@ -494,7 +501,8 @@ def augment_solr_response(response, collection, query):
       elif category == 'pivot':
         name = ','.join([facet['field']] + [f['field'] for f in facet['properties']['facets']])
         if 'facet_pivot' in response['facet_counts'] and name in response['facet_counts']['facet_pivot']:
-          count = _augment_pivot_2d(response['facet_counts']['facet_pivot'][name])
+          _selected_values = selected_values.get((facet['id'], name, category), [])
+          count = _augment_pivot_2d(response['facet_counts']['facet_pivot'][name], _selected_values)
         else:
           count = []
         facet = {
@@ -535,8 +543,9 @@ def augment_solr_response(response, collection, query):
   return augmented
 
 
-def _augment_pivot_2d(counts):
+def _augment_pivot_2d(counts, fq_filter):
   values = set()
+  selected_values = [f['value'] for f in fq_filter]
 
   for dimension in counts:
     for pivot in dimension['pivot']:
@@ -551,7 +560,10 @@ def _augment_pivot_2d(counts):
     for pivot in dimension['pivot']:
       count[pivot['value']] = pivot['count']
     for val in values:
-      augmented.append({"count": count.get(val, 0), "value": val, "cat": dimension['value'], 'selected': False})
+      augmented.append({
+          "count": count.get(val, 0), "value": val, "cat": dimension['value'], 'selected': val in selected_values,
+          'exclude': all([f['exclude'] for f in fq_filter if f['value'] == val])
+      })
 
   return augmented
 

+ 17 - 15
apps/search/src/search/templates/search.mako

@@ -179,9 +179,9 @@ ${ commonheader(_('Search'), "search", user, "80px") | n,unicode }
                     draggable: {data: draggableTree(), isEnabled: true,
                     options: {'start': function(event, ui){lastWindowScrollPosition = $(window).scrollTop();$('.card-body').slideUp('fast');},
                               'stop': function(event, ui){$('.card-body').slideDown('fast', function(){$(window).scrollTop(lastWindowScrollPosition)});}}}"
-         title="${_('Tree')}" rel="tooltip" data-placement="top">
+         title="${_('Pivot')}" rel="tooltip" data-placement="top">
          <a data-bind="style: { cursor: $root.availableDraggableChart() ? 'move' : 'default' }">
-                       <i class="fa fa-sitemap fa-rotate-270"></i>
+                       <i class="fa fa-sitemap"></i>
          </a>
    </div>
     <div data-bind="css: { 'draggable-widget': true, 'disabled': !availableDraggableChart() },
@@ -354,8 +354,9 @@ ${ dashboard.layout_skeleton() }
                 <!-- /ko -->
                 <!-- ko if: $data.selected -->
                   <span class="pointer" data-bind="click: function(){ $root.query.toggleFacet({facet: $data, widget_id: $parent.id()}) }">
-                    <strong data-bind="text: ko.mapping.toJSON($data)"></strong>
-                    <a class="pointer"><i class="fa fa-times"></i></a>
+                    <strong data-bind="text: $data.value"></strong>
+                    <a class="pointer" data-bind="visible: ! exclude"><i class="fa fa-times"></i></a>
+                    <a class="pointer" data-bind="visible: exclude"><i class="fa fa-plus"></i></a>
                   </span>
                 <!-- /ko -->
               <!-- /ko -->
@@ -389,7 +390,8 @@ ${ dashboard.layout_skeleton() }
               <!-- ko if: selected -->
                 <span class="pointer" data-bind="click: function(){ $root.query.selectRangeFacet({count: $data.value, widget_id: $parent.id(), from: $data.from, to: $data.to, cat: $data.field}) }">
                   <strong data-bind="text: $data.from + ' - ' + $data.to"></strong>
-                  <a class="pointer"><i class="fa fa-times"></i></a>
+                  <a class="pointer" data-bind="visible: ! exclude"><i class="fa fa-times"></i></a>
+                  <a class="pointer" data-bind="visible: exclude"><i class="fa fa-plus"></i></a>
                 </span>
               <!-- /ko -->
           </div>
@@ -908,28 +910,28 @@ ${ dashboard.layout_skeleton() }
 <script type="text/html" id="filter-widget">
   <div data-bind="visible: $root.query.fqs().length == 0" style="margin-top: 10px">${_('There are currently no filters applied.')}</div>
   <div data-bind="foreach: { data: $root.query.fqs, afterRender: function(){ isLoading(false); } }">
-    <!-- ko if: $data.type() == 'field' -->    
-    <div class="filter-box">      
+    <!-- ko if: $data.type() == 'field' -->
+    <div class="filter-box">
       <div class="title">
         <a href="javascript:void(0)" class="pull-right" data-bind="click: function() { chartsUpdatingState(); $root.query.removeFilter($data); $root.search(); }">
           <i class="fa fa-times"></i>
         </a>
         <span data-bind="text: $data.field"></span>
         &nbsp;
-      </div>      
+      </div>
       <div class="content">
-        <strong>${_('value')}</strong>
+        <strong>${_('selected')}</strong>
         <span data-bind="text: $.map($.grep($data.filter(), function(f) { return ! f.exclude(); }), function(f) { return f.value(); }).join(', '); "></span>
         <br/>
-        <strong>${_('value')}</strong>:
-        <span data-bind="text: $.map($.grep($data.filter(), function(f) { return f.exclude(); }), function(f) { return f.value(); }).join(', ');"></span>        
+        <strong>${_('excluded')}</strong>:
+        <span data-bind="text: $.map($.grep($data.filter(), function(f) { return f.exclude(); }), function(f) { return f.value(); }).join(', ');"></span>
       </div>
-    </div>        
+    </div>
     <!-- /ko -->
 
     <!-- ko if: $data.type() == 'range' -->
     <div class="filter-box">
-      <div class="title">        
+      <div class="title">
         <a href="javascript:void(0)" class="pull-right" data-bind="click: function(){ chartsUpdatingState(); $root.query.removeFilter($data); $root.search() }">
           <i class="fa fa-times"></i>
         </a>
@@ -942,14 +944,14 @@ ${ dashboard.layout_skeleton() }
             <strong>${_('from')}</strong> <span data-bind="text: $data.from"></span>
             <strong>${_('to')}</strong> <span data-bind="text: $data.to"></span>
           <!-- /ko -->
-        </span>        
+        </span>
         <br/>
         <span data-bind="foreach: $data.properties" style="font-weight: normal"  class="excluded">
           <!-- ko if: $.grep($parent.filter(), function(f) { return f.value() == $data.from() && f.exclude() }).length > 0 -->
             <strong>${_('from')}</strong> <span data-bind="text: $data.from"></span>
             <strong>${_('to')}</strong> <span data-bind="text: $data.to"></span>
           <!-- /ko -->
-        </span>        
+        </span>
       </div>
     </div>
     <!-- /ko -->

+ 2 - 2
apps/search/static/js/search.ko.js

@@ -135,14 +135,14 @@ var Query = function (vm, query) {
 
     vm.search();
   }
-
+  
   function _toggleSingleTermFacet(data, exclude) {
     var fq = getFilterByField(data.val.cat);
     var id;
     if (fq) {
       id = fq.id();
     } else {
-      id = '***single' + UUID(); // Tag single terms like this
+      id = UUID();
     }
     self.toggleFacet({'widget_id': id, 'facet': {'cat': data.val.cat, 'value': data.val.value}, 'exclude': exclude});
     vm.search();

+ 0 - 3
desktop/libs/libsolr/src/libsolr/api.py

@@ -78,9 +78,6 @@ class SolrApi(object):
             f.append('%s%s:"%s"' % (exclude, fq['field'], value))
           else:
             f.append('%s{!field f=%s}%s' % (exclude, fq['field'], value))
-        #if fq['id'].startswith('***single'): # Do not tag Single term fq
-        #  _params = ' '.join(f)
-        #else:
         _params ='{!tag=%s}' % fq['field'] + ' '.join(f)
         params += (('fq', urllib.unquote(utf_quoter(_params))),)
       elif fq['type'] == 'range':