From eab8ba76619cc62da29b11b5007e7f4f08e83cd5 Mon Sep 17 00:00:00 2001 From: colemanw Date: Sun, 17 Dec 2023 21:34:14 -0500 Subject: [PATCH] SearchKit - Convert 'Field Transformations' accordion into 'Select Fields' tab --- .../ang/crmSearchAdmin/crmSearch-fields.html | 9 +-- .../crmSearchAdmin.component.js | 16 +++--- .../crmSearchAdminFields.component.js | 56 +++++++++++++++++++ .../crmSearchAdmin/crmSearchAdminFields.html | 13 +++++ .../crmSearchAdminResultsTable.component.js | 7 +-- .../crmSearchAdminResultsTable.html | 2 +- 6 files changed, 81 insertions(+), 22 deletions(-) create mode 100644 ext/search_kit/ang/crmSearchAdmin/crmSearchAdminFields.component.js create mode 100644 ext/search_kit/ang/crmSearchAdmin/crmSearchAdminFields.html diff --git a/ext/search_kit/ang/crmSearchAdmin/crmSearch-fields.html b/ext/search_kit/ang/crmSearchAdmin/crmSearch-fields.html index c02e30b140..e699ea56e2 100644 --- a/ext/search_kit/ang/crmSearchAdmin/crmSearch-fields.html +++ b/ext/search_kit/ang/crmSearchAdmin/crmSearch-fields.html @@ -1,8 +1,3 @@ -
-
- -
- -
-
+
+
diff --git a/ext/search_kit/ang/crmSearchAdmin/crmSearchAdmin.component.js b/ext/search_kit/ang/crmSearchAdmin/crmSearchAdmin.component.js index 1414ba4bd2..48e8f31f7b 100644 --- a/ext/search_kit/ang/crmSearchAdmin/crmSearchAdmin.component.js +++ b/ext/search_kit/ang/crmSearchAdmin/crmSearchAdmin.component.js @@ -508,17 +508,9 @@ // Deletes an item from an array param this.clearParam = function(name, idx) { - if (name === 'select') { - // Function selectors use `ng-repeat` with `track by $index` so must be refreshed when splicing the array - ctrl.hideFuncitons(); - } ctrl.savedSearch.api_params[name].splice(idx, 1); }; - this.hideFuncitons = function() { - $scope.controls.showFunctions = false; - }; - function onChangeSelect(newSelect, oldSelect) { // When removing a column from SELECT, also remove from ORDER BY & HAVING _.each(_.difference(oldSelect, newSelect), function(col) { @@ -579,6 +571,14 @@ return {results: ctrl.getSelectFields()}; }; + this.fieldsForSelect = function() { + return { + results: ctrl.getAllFields(':label', ['Field', 'Custom', 'Extra', 'Pseudo'], (key) => { + ctrl.savedSearch.api_params.select.includes(key); + }) + }; + }; + this.getAllFields = function(suffix, allowedTypes, disabledIf, topJoin) { disabledIf = disabledIf || _.noop; allowedTypes = allowedTypes || ['Field', 'Custom', 'Extra', 'Filter']; diff --git a/ext/search_kit/ang/crmSearchAdmin/crmSearchAdminFields.component.js b/ext/search_kit/ang/crmSearchAdmin/crmSearchAdminFields.component.js new file mode 100644 index 0000000000..9b8422353f --- /dev/null +++ b/ext/search_kit/ang/crmSearchAdmin/crmSearchAdminFields.component.js @@ -0,0 +1,56 @@ +(function(angular, $, _) { + "use strict"; + + angular.module('crmSearchAdmin').component('crmSearchAdminFields', { + bindings: { + }, + require: { + crmSearchAdmin: '^crmSearchAdmin' + }, + templateUrl: '~/crmSearchAdmin/crmSearchAdminFields.html', + controller: function ($scope, $element) { + var ts = $scope.ts = CRM.ts('org.civicrm.search_kit'), + ctrl = this; + + // savedSearch.api_params.select is an array of strings. + // ui-sortable (and angularJs loops in general) don't work well with primitives + // So this controller converts the strings into objects and maintains 2-way sync between + // the two arrays. + this.select = []; + + $scope.$watchCollection('$ctrl.crmSearchAdmin.savedSearch.api_params.select', function(flatSelect) { + ctrl.select.length = flatSelect.length; + flatSelect.forEach((key, index) => { + ctrl.select[index] = ctrl.select[index] || {}; + ctrl.select[index].key = key; + ctrl.select[index].label = ctrl.crmSearchAdmin.getFieldLabel(key); + ctrl.select[index].isPseudoField = ctrl.crmSearchAdmin.isPseudoField(key); + }); + }); + + $scope.$watch('$ctrl.select', function(selectObject, oldSelect) { + if (oldSelect && oldSelect.length && selectObject) { + ctrl.crmSearchAdmin.savedSearch.api_params.select.length = selectObject.length; + selectObject.forEach((item, index) => { + ctrl.crmSearchAdmin.savedSearch.api_params.select[index] = item.key; + }); + } + }, true); + + // Drag-n-drop settings for reordering search fields + this.sortableOptions = { + containment: '.crm-search-select-fields', + axis: 'y', + forcePlaceholderSize: true, + update: function(e, ui) { + // Don't allow items to be moved to position 0 if locked + if (!ui.item.sortable.dropindex && ctrl.crmSearchAdmin.groupExists) { + ui.item.sortable.cancel(); + } + } + }; + + } + }); + +})(angular, CRM.$, CRM._); diff --git a/ext/search_kit/ang/crmSearchAdmin/crmSearchAdminFields.html b/ext/search_kit/ang/crmSearchAdmin/crmSearchAdminFields.html new file mode 100644 index 0000000000..9bb43b853d --- /dev/null +++ b/ext/search_kit/ang/crmSearchAdmin/crmSearchAdminFields.html @@ -0,0 +1,13 @@ +
+
+ + + + +
+
+ diff --git a/ext/search_kit/ang/crmSearchAdmin/resultsTable/crmSearchAdminResultsTable.component.js b/ext/search_kit/ang/crmSearchAdmin/resultsTable/crmSearchAdminResultsTable.component.js index b55816ee87..b44da215ee 100644 --- a/ext/search_kit/ang/crmSearchAdmin/resultsTable/crmSearchAdminResultsTable.component.js +++ b/ext/search_kit/ang/crmSearchAdmin/resultsTable/crmSearchAdminResultsTable.component.js @@ -59,16 +59,11 @@ if (!ui.item.sortable.dropindex && ctrl.crmSearchAdmin.groupExists) { ui.item.sortable.cancel(); } - // Function selectors use `ng-repeat` with `track by $index` so must be refreshed when rearranging the array - ctrl.crmSearchAdmin.hideFuncitons(); } }; $scope.fieldsForSelect = function() { - return {results: ctrl.crmSearchAdmin.getAllFields(':label', ['Field', 'Custom', 'Extra', 'Pseudo'], function(key) { - return _.contains(ctrl.search.api_params.select, key); - }) - }; + return ctrl.crmSearchAdmin.fieldsForSelect(); }; $scope.addColumn = function(col) { diff --git a/ext/search_kit/ang/crmSearchAdmin/resultsTable/crmSearchAdminResultsTable.html b/ext/search_kit/ang/crmSearchAdmin/resultsTable/crmSearchAdminResultsTable.html index f4a1c86fdf..b6b0e653cb 100644 --- a/ext/search_kit/ang/crmSearchAdmin/resultsTable/crmSearchAdminResultsTable.html +++ b/ext/search_kit/ang/crmSearchAdmin/resultsTable/crmSearchAdminResultsTable.html @@ -19,7 +19,7 @@ -- 2.25.1