From d0aeca745999eb359d520b5b09b88a2686b39866 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sun, 18 Jul 2021 23:58:21 -0400 Subject: [PATCH] Refactor SearchKit displays to inherit traits from a common base This refactors searchDisplayUtils to searchDisplayBaseTrait service, which acts like a PHP trait, controllers use it via angular.extend(). --- ext/search_kit/ang/crmSearchDisplay.module.js | 116 +++++++++++++----- .../ang/crmSearchDisplay/Pager.html | 2 +- .../ang/crmSearchDisplay/colType/buttons.html | 2 +- .../ang/crmSearchDisplay/colType/links.html | 2 +- .../ang/crmSearchDisplay/colType/menu.html | 2 +- .../crmSearchDisplayList.component.js | 53 +------- .../crmSearchDisplayListItems.html | 4 +- .../crmSearchDisplayTable.component.js | 59 ++------- .../crmSearchDisplayTable.html | 4 +- 9 files changed, 105 insertions(+), 139 deletions(-) diff --git a/ext/search_kit/ang/crmSearchDisplay.module.js b/ext/search_kit/ang/crmSearchDisplay.module.js index 64b6bd3c86..14b863f67b 100644 --- a/ext/search_kit/ang/crmSearchDisplay.module.js +++ b/ext/search_kit/ang/crmSearchDisplay.module.js @@ -4,7 +4,9 @@ // Declare module angular.module('crmSearchDisplay', CRM.angRequires('crmSearchDisplay')) - .factory('searchDisplayUtils', function(crmApi4) { + // Provides base methods and properties common to all search display types + .factory('searchDisplayBaseTrait', function(crmApi4) { + var ts = CRM.ts('org.civicrm.search_kit'); // Replace tokens keyed to rowData. // If rowMeta is provided, values will be formatted; if omitted, raw values will be provided. @@ -74,41 +76,91 @@ return result; } - function getApiParams(ctrl, mode) { - return { - return: mode || 'page:' + ctrl.page, - savedSearch: ctrl.search, - display: ctrl.display, - sort: ctrl.sort, - filters: _.assign({}, (ctrl.afFieldset ? ctrl.afFieldset.getFieldData() : {}), ctrl.filters), - afform: ctrl.afFieldset ? ctrl.afFieldset.getFormName() : null - }; - } + // Return a base trait shared by all search display controllers + // Gets mixed in using angular.extend() + return { + page: 1, + rowCount: null, + getUrl: getUrl, + + // Called by the controller's $onInit function + initializeDisplay: function($scope, $element) { + var ctrl = this; + this.sort = this.settings.sort ? _.cloneDeep(this.settings.sort) : []; + + $scope.getResults = _.debounce(function() { + ctrl.getResults(); + }, 100); - function getResults(ctrl) { - return crmApi4('SearchDisplay', 'run', getApiParams(ctrl)).then(function(results) { - ctrl.results = results; - ctrl.editing = false; - if (!ctrl.rowCount) { - if (!ctrl.settings.limit || results.length < ctrl.settings.limit) { - ctrl.rowCount = results.length; - } else if (ctrl.settings.pager) { - var params = getApiParams(ctrl, 'row_count'); - crmApi4('SearchDisplay', 'run', params).then(function(result) { - ctrl.rowCount = result.count; - }); + // If search is embedded in contact summary tab, display count in tab-header + var contactTab = $element.closest('.crm-contact-page .ui-tabs-panel').attr('id'); + if (contactTab) { + var unwatchCount = $scope.$watch('$ctrl.rowCount', function(rowCount) { + if (typeof rowCount === 'number') { + unwatchCount(); + CRM.tabHeader.updateCount(contactTab.replace('contact-', '#tab_'), rowCount); + } + }); + } + + function onChangeFilters() { + ctrl.page = 1; + ctrl.rowCount = null; + if (ctrl.onChangeFilters) { + ctrl.onChangeFilters(); } + $scope.getResults(); } - }); - } - return { - formatDisplayValue: formatDisplayValue, - formatLinks: formatLinks, - getApiParams: getApiParams, - getResults: getResults, - replaceTokens: replaceTokens, - getUrl: getUrl + if (this.afFieldset) { + $scope.$watch(this.afFieldset.getFieldData, onChangeFilters, true); + } + $scope.$watch('$ctrl.filters', onChangeFilters, true); + }, + + // Generate params for the SearchDisplay.run api + getApiParams: function(mode) { + return { + return: mode || 'page:' + this.page, + savedSearch: this.search, + display: this.display, + sort: this.sort, + filters: _.assign({}, (this.afFieldset ? this.afFieldset.getFieldData() : {}), this.filters), + afform: this.afFieldset ? this.afFieldset.getFormName() : null + }; + }, + + // Call SearchDisplay.run and update ctrl.results and ctrl.rowCount + getResults: function() { + var ctrl = this; + return crmApi4('SearchDisplay', 'run', ctrl.getApiParams()).then(function(results) { + ctrl.results = results; + ctrl.editing = false; + if (!ctrl.rowCount) { + if (!ctrl.settings.limit || results.length < ctrl.settings.limit) { + ctrl.rowCount = results.length; + } else if (ctrl.settings.pager) { + var params = ctrl.getApiParams('row_count'); + crmApi4('SearchDisplay', 'run', params).then(function(result) { + ctrl.rowCount = result.count; + }); + } + } + }); + }, + replaceTokens: function(value, row) { + return replaceTokens(value, row, this.settings.columns); + }, + getLinks: function(rowData, col) { + rowData._links = rowData._links || {}; + if (!(col.key in rowData._links)) { + rowData._links[col.key] = formatLinks(rowData, col.key, this.settings.columns); + } + return rowData._links[col.key]; + }, + formatFieldValue: function(rowData, col) { + return formatDisplayValue(rowData, col.key, this.settings.columns); + } }; }); diff --git a/ext/search_kit/ang/crmSearchDisplay/Pager.html b/ext/search_kit/ang/crmSearchDisplay/Pager.html index d760bec287..868bc8b855 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="$ctrl.getResults()" + ng-change="getResults()" items-per-page="$ctrl.settings.limit" max-size="6" force-ellipses="true" diff --git a/ext/search_kit/ang/crmSearchDisplay/colType/buttons.html b/ext/search_kit/ang/crmSearchDisplay/colType/buttons.html index 4909f20dbf..1c178db16d 100644 --- a/ext/search_kit/ang/crmSearchDisplay/colType/buttons.html +++ b/ext/search_kit/ang/crmSearchDisplay/colType/buttons.html @@ -1,5 +1,5 @@ - + {{:: $ctrl.replaceTokens(item.text, row) }} diff --git a/ext/search_kit/ang/crmSearchDisplay/colType/links.html b/ext/search_kit/ang/crmSearchDisplay/colType/links.html index b4a6dea220..5d59a7960d 100644 --- a/ext/search_kit/ang/crmSearchDisplay/colType/links.html +++ b/ext/search_kit/ang/crmSearchDisplay/colType/links.html @@ -1,5 +1,5 @@ - + {{:: $ctrl.replaceTokens(item.text, row) }} diff --git a/ext/search_kit/ang/crmSearchDisplay/colType/menu.html b/ext/search_kit/ang/crmSearchDisplay/colType/menu.html index f4f9afb340..fa961dcf37 100644 --- a/ext/search_kit/ang/crmSearchDisplay/colType/menu.html +++ b/ext/search_kit/ang/crmSearchDisplay/colType/menu.html @@ -5,7 +5,7 @@