From b7fd7a81e9100572079aa6d73208439a340c1b42 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 5 Aug 2021 13:30:58 -0400 Subject: [PATCH] SearchKit - Refactor sort-related functions into a trait --- .../ang/crmSearchDisplay/Pager.html | 2 +- .../traits/searchDisplayBaseTrait.service.js | 8 ++-- .../searchDisplaySortableTrait.service.js | 43 +++++++++++++++++++ .../crmSearchDisplayList.component.js | 2 +- .../crmSearchDisplayTable.component.js | 43 ++----------------- .../crmSearchDisplayTable.html | 6 +-- 6 files changed, 55 insertions(+), 49 deletions(-) create mode 100644 ext/search_kit/ang/crmSearchDisplay/traits/searchDisplaySortableTrait.service.js diff --git a/ext/search_kit/ang/crmSearchDisplay/Pager.html b/ext/search_kit/ang/crmSearchDisplay/Pager.html index 868bc8b855..d760bec287 100644 --- a/ext/search_kit/ang/crmSearchDisplay/Pager.html +++ b/ext/search_kit/ang/crmSearchDisplay/Pager.html @@ -4,7 +4,7 @@ boundary-links="true" total-items="$ctrl.rowCount" ng-model="$ctrl.page" - ng-change="getResults()" + ng-change="$ctrl.getResults()" items-per-page="$ctrl.settings.limit" max-size="6" force-ellipses="true" diff --git a/ext/search_kit/ang/crmSearchDisplay/traits/searchDisplayBaseTrait.service.js b/ext/search_kit/ang/crmSearchDisplay/traits/searchDisplayBaseTrait.service.js index 68d45d5029..d7b2da89c1 100644 --- a/ext/search_kit/ang/crmSearchDisplay/traits/searchDisplayBaseTrait.service.js +++ b/ext/search_kit/ang/crmSearchDisplay/traits/searchDisplayBaseTrait.service.js @@ -85,8 +85,8 @@ var ctrl = this; this.sort = this.settings.sort ? _.cloneDeep(this.settings.sort) : []; - $scope.getResults = _.debounce(function() { - ctrl.getResults(); + this.getResults = _.debounce(function() { + ctrl.runSearch(); }, 100); // If search is embedded in contact summary tab, display count in tab-header @@ -106,7 +106,7 @@ if (ctrl.onChangeFilters) { ctrl.onChangeFilters(); } - $scope.getResults(); + ctrl.getResults(); } if (this.afFieldset) { @@ -128,7 +128,7 @@ }, // Call SearchDisplay.run and update ctrl.results and ctrl.rowCount - getResults: function() { + runSearch: function() { var ctrl = this; return crmApi4('SearchDisplay', 'run', ctrl.getApiParams()).then(function(results) { ctrl.results = results; diff --git a/ext/search_kit/ang/crmSearchDisplay/traits/searchDisplaySortableTrait.service.js b/ext/search_kit/ang/crmSearchDisplay/traits/searchDisplaySortableTrait.service.js new file mode 100644 index 0000000000..15a89d8cb4 --- /dev/null +++ b/ext/search_kit/ang/crmSearchDisplay/traits/searchDisplaySortableTrait.service.js @@ -0,0 +1,43 @@ +(function(angular, $, _) { + "use strict"; + + // Trait shared by any search display controllers which allow sorting + angular.module('crmSearchDisplay').factory('searchDisplaySortableTrait', function() { + var ts = CRM.ts('org.civicrm.search_kit'); + + // Trait properties get mixed into display controller using angular.extend() + return { + + sort: [], + + getSort: function(col) { + var dir = _.reduce(this.sort, function(dir, item) { + return item[0] === col.key ? item[1] : dir; + }, null); + if (dir) { + return 'fa-sort-' + dir.toLowerCase(); + } + return 'fa-sort disabled'; + }, + + setSort: function(col, $event) { + if (col.type !== 'field') { + return; + } + var dir = this.getSort(col) === 'fa-sort-asc' ? 'DESC' : 'ASC'; + if (!$event.shiftKey || !this.sort) { + this.sort = []; + } + var index = _.findIndex(this.sort, [col.key]); + if (index > -1) { + this.sort[index][1] = dir; + } else { + this.sort.push([col.key, dir]); + } + this.getResults(); + } + + }; + }); + +})(angular, CRM.$, CRM._); diff --git a/ext/search_kit/ang/crmSearchDisplayList/crmSearchDisplayList.component.js b/ext/search_kit/ang/crmSearchDisplayList/crmSearchDisplayList.component.js index 718452d35e..0f726367c5 100644 --- a/ext/search_kit/ang/crmSearchDisplayList/crmSearchDisplayList.component.js +++ b/ext/search_kit/ang/crmSearchDisplayList/crmSearchDisplayList.component.js @@ -25,7 +25,7 @@ // Refresh current page this.refresh = function(row) { - ctrl.getResults(); + ctrl.runSearch(); }; } diff --git a/ext/search_kit/ang/crmSearchDisplayTable/crmSearchDisplayTable.component.js b/ext/search_kit/ang/crmSearchDisplayTable/crmSearchDisplayTable.component.js index 7801a41b0b..c1adf84e18 100644 --- a/ext/search_kit/ang/crmSearchDisplayTable/crmSearchDisplayTable.component.js +++ b/ext/search_kit/ang/crmSearchDisplayTable/crmSearchDisplayTable.component.js @@ -13,10 +13,10 @@ afFieldset: '?^^afFieldset' }, templateUrl: '~/crmSearchDisplayTable/crmSearchDisplayTable.html', - controller: function($scope, $element, crmApi4, searchDisplayBaseTrait, searchDisplayTasksTrait) { + controller: function($scope, $element, searchDisplayBaseTrait, searchDisplayTasksTrait, searchDisplaySortableTrait) { var ts = $scope.ts = CRM.ts('org.civicrm.search_kit'), // Mix in traits to this controller - ctrl = angular.extend(this, searchDisplayBaseTrait, searchDisplayTasksTrait); + ctrl = angular.extend(this, searchDisplayBaseTrait, searchDisplayTasksTrait, searchDisplaySortableTrait); this.$onInit = function() { this.initializeDisplay($scope, $element); @@ -25,7 +25,7 @@ // Refresh page after inline-editing a row this.refresh = function(row) { var rowId = row.id; - ctrl.getResults() + ctrl.runSearch() .then(function() { // If edited row disappears (because edits cause it to not meet search criteria), deselect it var index = ctrl.selectedRows.indexOf(rowId); @@ -35,43 +35,6 @@ }); }; - /** - * Returns crm-i icon class for a sortable column - * @param col - * @returns {string} - */ - $scope.getSort = function(col) { - var dir = _.reduce(ctrl.sort, function(dir, item) { - return item[0] === col.key ? item[1] : dir; - }, null); - if (dir) { - return 'fa-sort-' + dir.toLowerCase(); - } - return 'fa-sort disabled'; - }; - - /** - * Called when clicking on a column header - * @param col - * @param $event - */ - $scope.setSort = function(col, $event) { - if (col.type !== 'field') { - return; - } - var dir = $scope.getSort(col) === 'fa-sort-asc' ? 'DESC' : 'ASC'; - if (!$event.shiftKey || !ctrl.sort) { - ctrl.sort = []; - } - var index = _.findIndex(ctrl.sort, [col.key]); - if (index > -1) { - ctrl.sort[index][1] = dir; - } else { - ctrl.sort.push([col.key, dir]); - } - $scope.getResults(); - }; - } }); diff --git a/ext/search_kit/ang/crmSearchDisplayTable/crmSearchDisplayTable.html b/ext/search_kit/ang/crmSearchDisplayTable/crmSearchDisplayTable.html index 9917eb082f..dd4c502ef9 100644 --- a/ext/search_kit/ang/crmSearchDisplayTable/crmSearchDisplayTable.html +++ b/ext/search_kit/ang/crmSearchDisplayTable/crmSearchDisplayTable.html @@ -1,6 +1,6 @@
- +
@@ -8,8 +8,8 @@ - -- 2.25.1
- + + {{ col.label }}