Browse Source

[search] Customize hightlighting

Romain Rigaux 12 năm trước cách đây
mục cha
commit
29e5087

+ 10 - 3
apps/search/src/search/models.py

@@ -102,12 +102,15 @@ class Facet(models.Model):
 
 
 class Result(models.Model):
-  _META_TEMPLATE_ATTRS = ['template', 'highlighting', 'css']
+  _META_TEMPLATE_ATTRS = ['properties', 'template', 'highlighting', 'css']
 
   data = models.TextField()
 
   def update_from_post(self, post_data):
     data_dict = json.loads(self.data)
+    print data_dict
+    if post_data.get('properties'):
+      data_dict['properties'] = json.loads(post_data['properties'])
 
     if post_data.get('template'):
       data_dict['template'] = json.loads(post_data['template'])
@@ -135,7 +138,7 @@ class Result(models.Model):
 
     if data_dict.get('highlighting'):
       params += (
-        ('hl', 'true'),
+        ('hl', data_dict.get('properties', {}).get('highlighting_enabled') and 'true' or 'false'),
         ('hl.fl', ','.join(data_dict.get('highlighting'))),
       )
 
@@ -189,7 +192,11 @@ class CoreManager(models.Manager):
                    'fields': [],
                    'dates': []
                 }))
-      result = Result.objects.create(data=json.dumps({'template': 'To customize!<br/>', 'highlighting': []}))
+      result = Result.objects.create(data=json.dumps({
+                  'template': '{{id}} To customize!<br/>',
+                  'highlighting': [],
+                  'properties': {'highlighting_enabled': False},
+              }))
       sorting = Sorting.objects.create(data=json.dumps({'properties': {'is_enabled': False}, 'fields': []}))
 
       return Core.objects.create(name=name, label=name, facets=facets, result=result, sorting=sorting)

+ 9 - 5
apps/search/src/search/templates/admin_core_highlighting.mako

@@ -38,11 +38,14 @@ ${ commonheader(_('Search'), "search", user) | n,unicode }
       <div class="section">
         <div class="alert alert-info">
           <div class="pull-right">
-            <label><input type='checkbox' data-bind="checked: isEnabled" style="margin-top: -2px; margin-right: 4px"/> ${_('Enabled') }</label>
+            <label>
+              <input type='checkbox' data-bind="checked: isEnabled" style="margin-top: -2px; margin-right: 4px"/> ${_('Enabled') }
+            </label>
           </div>
-          <h4>${_('Highlighting')}</h4>
+          <h4>${_('Highlighting')}</h4>          
         </div>
         <div style="padding-left: 10px;margin-bottom: 20px">
+          <p>${_('Highlights the query keywords matching some of the fields below.')}</p>
           <em>${_('Please select some fields to highlight in the result below.')}</em>
         </div>
 
@@ -92,12 +95,13 @@ ${ commonheader(_('Search'), "search", user) | n,unicode }
 
     var resultData = ${ hue_core.result.data | n,unicode };
 
-    self.highlightedFields = ko.observableArray(resultData!=null?resultData.highlighting:[]);
-    self.isEnabled = ko.observable(true);
+    self.highlightedFields = ko.observableArray(resultData != null ? resultData.highlighting : []);
+    self.isEnabled = ko.observable(${ hue_core.result.data | n,unicode }.properties.highlighting_enabled);
 
     self.submit = function () {
       $.ajax("${ url('search:admin_core_highlighting', core=hue_core.name) }", {
         data: {
+          'properties': ko.utils.stringifyJson({'highlighting_enabled': self.isEnabled()}),
           'highlighting': ko.utils.stringifyJson(self.highlightedFields)
         },
         contentType: 'application/json',
@@ -131,7 +135,7 @@ ${ commonheader(_('Search'), "search", user) | n,unicode }
       searchPlaceholder: "${_('Search')}",
       noChoicesFound: "${_('No fields found.')}",
       width:$(".selector").width(),
-      height:240,
+      height:340,
       onChange: function(){
         viewModel.highlightedFields($("#fields").val());
       }