Procházet zdrojové kódy

[search] Create a new template based on any solr collection

Romain Rigaux před 11 roky
rodič
revize
286f05c

+ 1 - 1
apps/search/src/search/models.py

@@ -277,7 +277,7 @@ class Collection(models.Model):
       props['collection'] = self.get_default(user)
     if 'layout' not in props:
       props['layout'] = []    
-      
+  
     if self.id:
       props['collection']['id'] = self.id
     if self.name:

+ 3 - 0
apps/search/src/search/search_controller.py

@@ -127,3 +127,6 @@ class SearchController(object):
   def is_core(self, core_name):
     solr_cores = SolrApi(SOLR_URL.get(), self.user).cores()
     return core_name in solr_cores
+
+  def get_solr_collection(self):
+    return SolrApi(SOLR_URL.get(), self.user).collections()

+ 17 - 7
apps/search/src/search/templates/search2.mako

@@ -38,11 +38,20 @@ ${ commonheader(_('Search'), "search", user, "60px") | n,unicode }
     </div>
   % endif
   
-  <form class="form-search" style="margin: 0" data-bind="submit: search">
+  <form data-bind="visible: columns().length == 0">  
+    ${ _('Search') }
+    <!-- ko if: $root.initial.collections -->
+    <select data-bind="options: $root.initial.collections, value: $root.collection.name">
+    </select>
+    <!-- /ko -->
+  </form>
+  
+  <form class="form-search" style="margin: 0" data-bind="submit: search, visible: columns().length != 0"">
     <strong>${_("Search")}</strong>
     <div class="input-append">
       <div class="selectMask">
-        <span class="current-collection">${ collection.label }</span>        
+        <span class="current-collection" data-bind="text: collection.label"></span>
+        ## click: show also collection list + edit label box        
       </div>
 
       <span data-bind="foreach: query.qs">
@@ -79,7 +88,7 @@ ${ commonheader(_('Search'), "search", user, "60px") | n,unicode }
         <div class="layout-box" style="width: 100px;"></div>
       </div>
     </a>
-    <a data-bind="visible: columns().length == 0" href="javascript: magicLayout()" onmouseover="viewModel.previewColumns('magic')" onmouseout="viewModel.previewColumns('')">
+    <a data-bind="visible: columns().length == 0" href="javascript: magicLayout(viewModel)" onmouseover="viewModel.previewColumns('magic')" onmouseout="viewModel.previewColumns('')">
       <div class="layout-container">
         <div class="layout-box" style="width: 100px;"><i class="fa fa-magic"></i></div>
       </div>
@@ -124,7 +133,7 @@ ${ commonheader(_('Search'), "search", user, "60px") | n,unicode }
     </div>
     <div class="row-fluid" data-bind="visible: previewColumns() == 'magic'">
       <div class="span12 preview-row">
-        Hue logo picks a timeline, filter, result, pie bar, widgets...: big magic icon? could preview but too complicated for now.
+        ${ _('Template predefined with some widgets.') }
       </div>
     </div>
   </div>
@@ -149,7 +158,7 @@ ${ commonheader(_('Search'), "search", user, "60px") | n,unicode }
 </script>
 
 <script type="text/html" id="row-template">
-  <div class="emptyRow" data-bind="visible: widgets().length == 0 && $index()==0 && $root.isEditing() && $parent.size() > 4 && $parent.rows().length == 1">
+  <div class="emptyRow" data-bind="visible: widgets().length == 0 && $index() == 0 && $root.isEditing() && $parent.size() > 4 && $parent.rows().length == 1">
     <img src="/search/static/art/hint_arrow_flipped.png" style="float:left; margin-right: 10px"/>
     <div style="float:left; text-align: center; width: 260px">${_('Drag any of the widgets inside your empty row')}</div>
     <div class="clearfix"></div>
@@ -865,8 +874,9 @@ function rangePieChartDataTransformer(data) {
     item.widget_id = data.widget_id;
     _data.push({
       label: item.from + ' - ' + item.to,
-      value: item.value,
+      from: item.from,
       to: item.to,
+      value: item.value,
       obj: item
     });
   });
@@ -1040,7 +1050,7 @@ $(document).ready(function () {
   };
 
 
-  viewModel = new SearchViewModel(${ collection.get_c(user) | n,unicode }, ${ query | n,unicode });
+  viewModel = new SearchViewModel(${ collection.get_c(user) | n,unicode }, ${ query | n,unicode }, ${ initial | n,unicode });
   ko.applyBindings(viewModel);
 
   viewModel.init();

+ 15 - 1
apps/search/src/search/views.py

@@ -68,16 +68,30 @@ def index(request):
   return render('search2.mako', request, {
     'collection': collection,
     'query': query,
+    'initial': '{}',    
   })
 
 
 def new_search(request):
-  collection = Collection(name='twitter_demo', label='New Twitter Template')
+  collections = SearchController(request.user).get_solr_collection().keys()
+  if not collections:
+    return no_collections(request)
+
+  collection = Collection(name=collections[0], label=collections[0])
   query = {'qs': [{'q': ''}], 'fqs': []}
 
   return render('search2.mako', request, {
     'collection': collection,
     'query': query,
+    'initial': json.dumps({
+         'collections': collections,
+         'layout': [
+              {"size":3,"rows":[
+                  {"widgets":[]}],"klass":"card card-home card-column span3"},
+              {"size":9,"rows":[
+                  {"widgets":[{"size":12,"name":"Results","id":"8897bfbd-053f-ab56-dbfb-432f7b28aceb","widgetType":"resultset-widget","properties":{},"offset":0,"isLoading":False,"klass":"card card-widget span12"}]}],"klass":"card card-home card-column span9"}
+          ]         
+     }),
   })
 
 

+ 23 - 6
apps/search/static/js/search.ko.js

@@ -128,9 +128,8 @@ function oneThirdRightLayout() {
   setLayout([9, 3]);
 }
 
-function magicLayout() {
-  setLayout([3, 9]);
-  alert('Hue picked a timeline, filter, result, pie bar, widgets...');
+function magicLayout(vm) {
+  loadLayout(vm, vm.initial.layout);
 }
 
 function setLayout(colSizes) {
@@ -327,8 +326,8 @@ var Collection = function (vm, collection) {
   var self = this;
 
   self.id = collection.id;
-  self.name = collection.name;
-  self.label = collection.label;
+  self.name = ko.mapping.fromJS(collection.name);
+  self.label = ko.mapping.fromJS(collection.label);
   self.idField = collection.idField;
   self.template = ko.mapping.fromJS(collection.template);
   self.template.fieldsSelected.subscribe(function () {
@@ -554,14 +553,32 @@ var Collection = function (vm, collection) {
   };
 };
 
+var NewTemplate = function (vm, initial) {
+  var self = this;
+
+  self.collections = ko.mapping.fromJS(initial.collections);
+  self.collections.subscribe(function (newValue) {
+    vm.collection.name(newValue);
+  });
+  
+  self.layout = initial.layout;
+};
+
 
-var SearchViewModel = function (collection_json, query_json) {
+var SearchViewModel = function (collection_json, query_json, initial_json) {
   var self = this;
 
   // Models
   self.collection = new Collection(self, collection_json.collection);
   self.query = new Query(self, query_json);
+  self.initial = new NewTemplate(self, initial_json);
 
+  if (initial_json.collections) {
+    self.collection.name.subscribe(function (newValue) {
+	  self.collection.label(newValue);alert(newValue);
+    });
+  }
+  
   // UI
   self.response = ko.observable({});
   self.results = ko.observableArray([]);