Jelajahi Sumber

HUE-2165 [search] syncFields() can corrupt the model

Field that does not exist anymore should be remove in bulk in both the
collection and template field list.
New fields can be added only to the list of selected fields.
Romain Rigaux 11 tahun lalu
induk
melakukan
22e1401
1 mengubah file dengan 24 tambahan dan 23 penghapusan
  1. 24 23
      apps/search/static/js/search.ko.js

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

@@ -291,7 +291,7 @@ var Query = function (vm, query) {
     }
     }
 
 
     var fq = self.getFacetFilter(data.widget_id);
     var fq = self.getFacetFilter(data.widget_id);
-  
+
     if (fq == null) {
     if (fq == null) {
       self.fqs.push(ko.mapping.fromJS({
       self.fqs.push(ko.mapping.fromJS({
           'id': data.widget_id,
           'id': data.widget_id,
@@ -299,7 +299,7 @@ var Query = function (vm, query) {
           'filter': [data.from],
           'filter': [data.from],
           'properties': [{'from': data.from, 'to': data.to}],
           'properties': [{'from': data.from, 'to': data.to}],
           'type': 'range'
           'type': 'range'
-      }));      
+      }));
     } else {
     } else {
       if (fq.filter().indexOf(data.from) > -1) { // Unselect
       if (fq.filter().indexOf(data.from) > -1) { // Unselect
         fq.filter.remove(data.from);
         fq.filter.remove(data.from);
@@ -310,7 +310,7 @@ var Query = function (vm, query) {
         });
         });
         if (fq.filter().length == 0) {
         if (fq.filter().length == 0) {
           self.removeFilter(ko.mapping.fromJS({'id': data.widget_id}));
           self.removeFilter(ko.mapping.fromJS({'id': data.widget_id}));
-        }      
+        }
       } else {
       } else {
        fq.filter.push(data.from);
        fq.filter.push(data.from);
        fq.properties.push(ko.mapping.fromJS({'from': data.from, 'to': data.to}));
        fq.properties.push(ko.mapping.fromJS({'from': data.from, 'to': data.to}));
@@ -329,7 +329,7 @@ var Query = function (vm, query) {
         // Also re-init range select widget
         // Also re-init range select widget
         var rangeWidget = vm.collection.getFacetById(fq.id());
         var rangeWidget = vm.collection.getFacetById(fq.id());
         if (rangeWidget != null && RANGE_SELECTABLE_WIDGETS.indexOf(rangeWidget.widgetType()) != -1 && fq.type() == 'range') {
         if (rangeWidget != null && RANGE_SELECTABLE_WIDGETS.indexOf(rangeWidget.widgetType()) != -1 && fq.type() == 'range') {
-          vm.collection.timeLineZoom({'id': rangeWidget.id()});  
+          vm.collection.timeLineZoom({'id': rangeWidget.id()});
         }
         }
         return false;
         return false;
       }
       }
@@ -422,7 +422,7 @@ var Collection = function (vm, collection) {
 
 
   self.addFacet = function (facet_json) {
   self.addFacet = function (facet_json) {
     self.removeFacet(function(){return facet_json.widget_id});
     self.removeFacet(function(){return facet_json.widget_id});
-  
+
     $.post("/search/template/new_facet", {
     $.post("/search/template/new_facet", {
       "collection": ko.mapping.toJSON(self),
       "collection": ko.mapping.toJSON(self),
         "id": facet_json.widget_id,
         "id": facet_json.widget_id,
@@ -493,7 +493,7 @@ var Collection = function (vm, collection) {
     var _field = null;
     var _field = null;
     $.each(self.template.fields(), function (index, field) {
     $.each(self.template.fields(), function (index, field) {
       if (field.name() == name) {
       if (field.name() == name) {
-      _field = field;
+        _field = field;
         return false;
         return false;
       }
       }
     });
     });
@@ -523,7 +523,7 @@ var Collection = function (vm, collection) {
   });
   });
   self.template.availableWidgetFields = ko.computed(function() {
   self.template.availableWidgetFields = ko.computed(function() {
     if (self.template.fieldsModalType() == 'histogram-widget') {
     if (self.template.fieldsModalType() == 'histogram-widget') {
-      return vm.availableDateFields();  
+      return vm.availableDateFields();
     }
     }
     else if (self.template.fieldsModalType() == 'line-widget') {
     else if (self.template.fieldsModalType() == 'line-widget') {
       return vm.availableNumberFields();
       return vm.availableNumberFields();
@@ -555,12 +555,12 @@ var Collection = function (vm, collection) {
         name: self.name()
         name: self.name()
     }, function (data) {
     }, function (data) {
       if (data.status == 0) {
       if (data.status == 0) {
-        self.idField(data.collection.collection.idField);  
+        self.idField(data.collection.collection.idField);
         self.template.template(data.collection.collection.template.template);
         self.template.template(data.collection.collection.template.template);
         self.template.fieldsAttributes.removeAll();
         self.template.fieldsAttributes.removeAll();
         $.each(data.collection.collection.template.fieldsAttributes, function(index, field) {
         $.each(data.collection.collection.template.fieldsAttributes, function(index, field) {
           self.template.fieldsAttributes.push(ko.mapping.fromJS(field));
           self.template.fieldsAttributes.push(ko.mapping.fromJS(field));
-        });  
+        });
         self.fields.removeAll();
         self.fields.removeAll();
         $.each(data.collection.collection.fields, function(index, field) {
         $.each(data.collection.collection.fields, function(index, field) {
           self.fields.push(ko.mapping.fromJS(field));
           self.fields.push(ko.mapping.fromJS(field));
@@ -594,16 +594,17 @@ var Collection = function (vm, collection) {
     var _toAdd = diff(_newFieldsNames, _currentFieldsNames);
     var _toAdd = diff(_newFieldsNames, _currentFieldsNames);
 
 
     // Deleted fields
     // Deleted fields
-    $.each(currentObservable(), function(index, field) {
-      if (_toDelete.indexOf(field.name()) != -1) {
-        currentObservable.remove(field);
-      }
-    });
+    self.template.fieldsSelected.removeAll(_toDelete);
+    var bulk = $.grep(currentObservable(), function(field) {
+      return (_toDelete.indexOf(field.name()) != -1)
+    });     	
+    currentObservable.removeAll(bulk);
+
     // New fields
     // New fields
     $.each(newJson, function(index, field) {
     $.each(newJson, function(index, field) {
-     if (_toAdd.indexOf(field.name) != -1) {
-       currentObservable.push(ko.mapping.fromJS(field));
-     }
+      if (_toAdd.indexOf(field.name) != -1) {
+        currentObservable.push(ko.mapping.fromJS(field));
+      }
     });
     });
   }
   }
 
 
@@ -613,7 +614,7 @@ var Collection = function (vm, collection) {
       }, function (data) {
       }, function (data) {
         if (data.status == 0) {
         if (data.status == 0) {
           self.idField(data.collection.collection.idField);
           self.idField(data.collection.collection.idField);
-          syncArray(self.template.fieldsAttributes, data.collection.collection.template.fieldsAttributes, false);  
+          syncArray(self.template.fieldsAttributes, data.collection.collection.template.fieldsAttributes, false);
           syncArray(self.fields, data.collection.collection.fields, false);
           syncArray(self.fields, data.collection.collection.fields, false);
         }
         }
         // After sync the dynamic fields
         // After sync the dynamic fields
@@ -626,7 +627,7 @@ var Collection = function (vm, collection) {
         name: self.name()
         name: self.name()
       }, function (data) {
       }, function (data) {
         if (data.status == 0) {
         if (data.status == 0) {
-          syncArray(self.template.fieldsAttributes, data.gridlayout_header_fields, true);  
+          syncArray(self.template.fieldsAttributes, data.gridlayout_header_fields, true);
           syncArray(self.fields, data.fields, true);
           syncArray(self.fields, data.fields, true);
         }
         }
     }).fail(function (xhr, textStatus, errorThrown) {});
     }).fail(function (xhr, textStatus, errorThrown) {});
@@ -772,7 +773,7 @@ var NewTemplate = function (vm, initial) {
       });
       });
     } else {
     } else {
       self.syncCollections();
       self.syncCollections();
-    }  
+    }
 
 
     if (initial.autoLoad) {
     if (initial.autoLoad) {
       magicLayout(vm);
       magicLayout(vm);
@@ -895,8 +896,8 @@ var SearchViewModel = function (collection_json, query_json, initial_json) {
   });
   });
 
 
   self.init = function (callback) {
   self.init = function (callback) {
-  self.initial.init();
-  self.collection.syncFields();
+    self.initial.init();
+    self.collection.syncFields();
     self.search(callback);
     self.search(callback);
   }
   }
 
 
@@ -1049,7 +1050,7 @@ var SearchViewModel = function (collection_json, query_json, initial_json) {
             doc['details'].push(ko.mapping.fromJS({
             doc['details'].push(ko.mapping.fromJS({
               key: key,
               key: key,
               value: val
               value: val
-          }));                
+          }));
         });
         });
       }
       }
       else if (data.status == 1) {
       else if (data.status == 1) {