SearchKit - Include default clauses in routbinding params
[civicrm-core.git] / ext / search_kit / ang / crmSearchAdmin / crmSearchAdmin.component.js
index 8cbc6177805a6fadd6c728c10ad06f324fcd1ca3..d0b8750bc3e31ea174a06b769d7b305f56b23556 100644 (file)
         this.groupExists = !!this.savedSearch.groups.length;
 
         if (!this.savedSearch.id) {
+          var defaults = {
+            version: 4,
+            select: getDefaultSelect(),
+            orderBy: {},
+            where: [],
+          };
+          _.each(['groupBy', 'join', 'having'], function(param) {
+            if (ctrl.paramExists(param)) {
+              defaults[param] = [];
+            }
+          });
+
           $scope.$bindToRoute({
             param: 'params',
             expr: '$ctrl.savedSearch.api_params',
             deep: true,
-            default: {
-              version: 4,
-              select: getDefaultSelect(),
-              orderBy: {},
-              where: [],
-            }
+            default: defaults
           });
         }
 
 
         $scope.$watchCollection('$ctrl.savedSearch.api_params.select', onChangeSelect);
 
-        if (this.paramExists('groupBy')) {
-          this.savedSearch.api_params.groupBy = this.savedSearch.api_params.groupBy || [];
-        }
-
-        if (this.paramExists('join')) {
-          this.savedSearch.api_params.join = this.savedSearch.api_params.join || [];
-        }
-
-        if (this.paramExists('having')) {
-          this.savedSearch.api_params.having = this.savedSearch.api_params.having || [];
-        }
-
         $scope.$watch('$ctrl.savedSearch', onChangeAnything, true);
 
         // After watcher runs for the first time and messes up the status, set it correctly