Эх сурвалжийг харах

[search] Adding collection settings tab

Romain Rigaux 11 жил өмнө
parent
commit
ef37713

+ 24 - 11
apps/search/src/search/templates/search.mako

@@ -34,11 +34,13 @@ ${ commonheader(_('Search'), "search", user, "80px") | n,unicode }
     % if user.is_superuser:
       <button type="button" title="${ _('Edit') }" rel="tooltip" data-placement="bottom" data-bind="click: toggleEditing, css: {'btn': true, 'btn-inverse': isEditing}"><i class="fa fa-pencil"></i></button>
       <button type="button" title="${ _('Save') }" rel="tooltip" data-placement="bottom" data-loading-text="${ _("Saving...") }" data-bind="click: save, css: {'btn': true}"><i class="fa fa-save"></i></button>
-      <button type="button" title="${ _('Save') }" rel="tooltip" data-placement="bottom" data-bind="css: {'btn': true}"><i class="fa fa-cog"></i></button>
+      <button type="button" title="${ _('Settings') }" rel="tooltip" data-placement="bottom" data-toggle="modal" data-target="#settingsDemiModal"
+          data-bind="css: {'btn': true}">
+        <i class="fa fa-cog"></i>
+      </button>
     % endif
       <button type="button" title="${ _('Share') }" rel="tooltip" data-placement="bottom" data-bind="click: showShareModal, css: {'btn': true}"><i class="fa fa-link"></i></button>
     % if user.is_superuser:
-      ## for enable, live search, max number of downloads, change solr
       &nbsp;&nbsp;&nbsp;
       <a class="btn" href="${ url('search:new_search') }" title="${ _('New') }" rel="tooltip" data-placement="bottom" data-bind="css: {'btn': true}"><i class="fa fa-file-o"></i></a>
       <a class="btn" href="${ url('search:admin_collections') }" title="${ _('Collections') }" rel="tooltip" data-placement="bottom" data-bind="css: {'btn': true}"><i class="fa fa-tags"></i></a>
@@ -46,8 +48,8 @@ ${ commonheader(_('Search'), "search", user, "80px") | n,unicode }
   </div>  
   
   <form data-bind="visible: columns().length == 0">  
-    ${ _('Search') }
-    <!-- ko if: $root.initial.collections -->
+    ${ _('Select a search index') }
+    <!-- ko if: columns().length == 0 -->
     <select data-bind="options: $root.initial.collections, value: $root.collection.name">
     </select>
     <!-- /ko -->
@@ -60,11 +62,6 @@ ${ commonheader(_('Search'), "search", user, "80px") | n,unicode }
         <span
             data-bind="editable: collection.label, editableOptions: {enabled: $root.isEditing(), placement: 'right'}">
         </span>
-        ##<i class="fa fa-edit" data-bind="visible: $root.isEditing() && ! $root.changeCollection(), click: function(){$root.changeCollection(true);}"></i>
-        <!-- ko if: $root.isEditing() && $root.changeCollection() -->
-        <select data-bind="options: $root.initial.collections, value: $root.collection.name">
-        </select>        
-        <!-- /ko -->
       </div>
 
       <span data-bind="foreach: query.qs">
@@ -192,12 +189,12 @@ ${ commonheader(_('Search'), "search", user, "80px") | n,unicode }
 </div>
 
 <div id="emptyDashboard" data-bind="fadeVisible: !isEditing() && columns().length == 0">
-  <div style="float:left; padding-top: 90px; margin-right: 20px; text-align: center; width: 260px">${_('Click on the pencil to get started with your dashboard!')}</div>
+  <div style="float:left; padding-top: 90px; margin-right: 20px; text-align: center; width: 260px">${ _('Click on the pencil to get started with your dashboard!') }</div>
   <img src="/search/static/art/hint_arrow.png" />
 </div>
 
 <div id="emptyDashboardEditing" data-bind="fadeVisible: isEditing() && columns().length == 0 && previewColumns() == ''">
-  <div style="float:right; padding-top: 90px; margin-left: 20px; text-align: center; width: 260px">${_('Click on a layout to get started with your dashboard!')}</div>
+  <div style="float:right; padding-top: 90px; margin-left: 20px; text-align: center; width: 260px">${ _('Click on a layout to start your dashboard!') }</div>
   <img src="/search/static/art/hint_arrow_horiz_flipped.png" />
 </div>
 
@@ -899,6 +896,22 @@ ${ commonheader(_('Search'), "search", user, "80px") | n,unicode }
   </div>
 </div>
 
+<div id="settingsDemiModal" class="demi-modal hide" data-backdrop="false">
+  <div class="modal-body">
+    <a href="javascript: void(0)" data-dismiss="modal" class="pull-right"><i class="fa fa-times"></i></a>
+    <div style="float: left; margin-right: 10px;text-align: center">
+      <!-- ko if: $root.initial.inited() -->
+      ${ _('Solr index') }
+      <select data-bind="options: $root.initial.collections, value: $root.collection.name">
+      </select>        
+      <!-- /ko -->
+    </div>
+    <div>
+      ${ _('Visible to everybody') } <input type="checkbox" data-bind="checked: $root.collection.enabled"/>
+    </div>
+  </div>
+</div>
+
 <div id="genericLoader" style="display: none">
 <!--[if !IE]> --><i class="fa fa-spinner fa-spin"></i><!-- <![endif]-->
 <!--[if IE]><img src="/static/art/spinner.gif" /><![endif]-->

+ 2 - 1
apps/search/src/search/urls.py

@@ -35,7 +35,8 @@ urlpatterns = patterns('search.views',
   url(r'^get_document$', 'get_document', name='get_document'),
   url(r'^get_range_facet$', 'get_range_facet', name='get_range_facet'),
   url(r'^get_timeline$', 'get_timeline', name='get_timeline'),
-  url(r'^get_collection', 'get_collection', name='get_collection'),
+  url(r'^get_collection$', 'get_collection', name='get_collection'),
+  url(r'^get_collections$', 'get_collections', name='get_collections'),
   
   # Admin
   url(r'^admin/collections_import$', 'admin_collections_import', name='admin_collections_import'),

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

@@ -58,7 +58,7 @@ def index(request):
   return render('search.mako', request, {
     'collection': collection,
     'query': query,
-    'initial': '{}',    
+    'initial': json.dumps({'collections': [], 'layout': []}),    
   })
 
 
@@ -160,7 +160,7 @@ def save(request):
     hue_collection.update_properties({'layout': layout})
     hue_collection.name = collection['name']
     hue_collection.label = collection['label']
-    hue_collection.enable = collection['enabled']
+    hue_collection.enabled = collection['enabled']
     hue_collection.save()
     response['status'] = 0
     response['id'] = hue_collection.id
@@ -486,7 +486,6 @@ def get_range_facet(request):
   return HttpResponse(json.dumps(result), mimetype="application/json")
 
 
-# TODO security
 def get_collection(request):  
   result = {'status': -1, 'message': 'Error'}
 
@@ -505,6 +504,19 @@ def get_collection(request):
   return HttpResponse(json.dumps(result), mimetype="application/json")
 
 
+def get_collections(request):  
+  result = {'status': -1, 'message': 'Error'}
+
+  try:           
+    result['collection'] = SearchController(request.user).get_solr_collection().keys()
+    result['status'] = 0      
+
+  except Exception, e:
+    result['message'] = unicode(str(e), "utf8")
+
+  return HttpResponse(json.dumps(result), mimetype="application/json")
+
+
 def install_examples(request):
   result = {'status': -1, 'message': ''}
 

+ 43 - 15
apps/search/static/js/search.ko.js

@@ -536,8 +536,8 @@ var Collection = function (vm, collection) {
     self.template.filteredModalFields(_fields);
   });
 
-  self.switchCollection = function() {
-    $.post("/search/get_collection/", {
+  self.switchCollection = function() { // Long term would be to reload the page
+    $.post("/search/get_collection", {
         name: self.name()
 	  }, function (data) {
 	    if (data.status == 0) {
@@ -594,7 +594,7 @@ var Collection = function (vm, collection) {
   }  
   
   self.syncFields = function() {
-    $.post("/search/get_collection/", {
+    $.post("/search/get_collection", {
         name: self.name()
 	  }, function (data) {
 	    if (data.status == 0) {
@@ -742,21 +742,50 @@ var NewTemplate = function (vm, initial) {
 
   self.collections = ko.mapping.fromJS(initial.collections);
   self.layout = initial.layout;
-
-  if (self.collections) {
-    vm.collection.name.subscribe(function (newValue) {
-	  vm.collection.label(newValue);
-	  vm.collection.switchCollection();
-	  vm.changeCollection(false);
-	  vm.search();
-    });
-  }
+  self.inited = ko.observable(self.collections().length > 0); // No collection if not a new dashboard
   
-  self.init = function() {
+  self.init = function() { 
+	if (self.inited()) {
+	  // If new dashboard
+	  vm.collection.name.subscribe(function(newValue) {
+		vm.collection.label(newValue);
+	    vm.collection.switchCollection();
+		vm.search();
+	  });
+	} else {
+	  self.syncCollections();
+	}	  
+	  
     if (initial.autoLoad) {
 	  magicLayout(vm);
-    }
+    }   
   };
+  
+  self.syncCollections = function () {
+    $.post("/search/get_collections", {
+    },function (data) {
+      if (data.status == 0) {
+    	// Sync new and old names
+        $.each(data.collection, function(index, name) {
+          if (self.collections.indexOf(name) == -1) {
+            self.collections.push(name);
+          }
+        });
+        $.each(self.collections(), function(index, collection) {
+          if (data.collection.indexOf(collection) == -1) {
+            self.collections.remove(collection);
+          }
+        });        
+      } 
+      else {
+        $(document).trigger("error", data.message);
+      }
+    }).fail(function (xhr, textStatus, errorThrown) {
+      $(document).trigger("error", xhr.responseText);
+    }).done(function() {
+      self.inited(true);
+    });
+  };   
 };
 
 
@@ -789,7 +818,6 @@ var SearchViewModel = function (collection_json, query_json, initial_json) {
     }
     return _facet;    
   };
-  self.changeCollection = ko.observable(false);
   self.toggledGridlayoutResultChevron = ko.observable(false);
   self.enableGridlayoutResultChevron = function() {
     self.toggledGridlayoutResultChevron(true);