From c7e966548d6fc7b97c65c419fd2a426f825e6bd5 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Mon, 21 Dec 2020 21:45:45 -0500 Subject: [PATCH] Search kit: Add token selector in display admin UI, allow tokens in tooltips Also includes some cleanup of the crmSearchAdminLinkSelect component, making it more self-contained --- .../crmSearchAdminDisplay.component.js | 15 ------ ... => crmSearchAdminLinkSelect.component.js} | 22 +++++++- .../crmSearchAdminLinkSelect.html | 5 +- .../crmSearchAdminTokenSelect.component.js | 51 +++++++++++++++++++ .../crmSearchAdminTokenSelect.html | 10 ++++ .../searchAdminDisplayList.component.js | 1 - .../displays/searchAdminDisplayList.html | 3 +- .../searchAdminDisplayTable.component.js | 1 - .../displays/searchAdminDisplayTable.html | 3 +- ext/search/ang/crmSearchDisplay.module.js | 21 +++++--- .../crmSearchDisplayList.component.js | 1 + .../crmSearchDisplayListItems.html | 2 +- .../crmSearchDisplayTable.component.js | 1 + .../crmSearchDisplayTable.html | 2 +- 14 files changed, 106 insertions(+), 32 deletions(-) rename ext/search/ang/crmSearchAdmin/{crmSearchAdminLinkSelect.directive.js => crmSearchAdminLinkSelect.component.js} (60%) create mode 100644 ext/search/ang/crmSearchAdmin/crmSearchAdminTokenSelect.component.js create mode 100644 ext/search/ang/crmSearchAdmin/crmSearchAdminTokenSelect.html diff --git a/ext/search/ang/crmSearchAdmin/crmSearchAdminDisplay.component.js b/ext/search/ang/crmSearchAdmin/crmSearchAdminDisplay.component.js index 0def0e2770..8ab7a1cd30 100644 --- a/ext/search/ang/crmSearchAdmin/crmSearchAdminDisplay.component.js +++ b/ext/search/ang/crmSearchAdmin/crmSearchAdminDisplay.component.js @@ -65,21 +65,6 @@ } }; - // Return all possible links to main entity or join entities - this.getLinks = function() { - var links = _.cloneDeep(searchMeta.getEntity(ctrl.savedSearch.api_entity).paths || []); - _.each(ctrl.savedSearch.api_params.join, function(join) { - var joinName = join[0].split(' AS '), - joinEntity = searchMeta.getEntity(joinName[0]); - _.each(joinEntity.paths, function(path) { - var link = _.cloneDeep(path); - link.path = link.path.replace(/\[/g, '[' + joinName[1] + '.'); - links.push(link); - }); - }); - return links; - }; - this.preview = this.stale = false; this.previewDisplay = function() { diff --git a/ext/search/ang/crmSearchAdmin/crmSearchAdminLinkSelect.directive.js b/ext/search/ang/crmSearchAdmin/crmSearchAdminLinkSelect.component.js similarity index 60% rename from ext/search/ang/crmSearchAdmin/crmSearchAdminLinkSelect.directive.js rename to ext/search/ang/crmSearchAdmin/crmSearchAdminLinkSelect.component.js index 6676055464..1a63886985 100644 --- a/ext/search/ang/crmSearchAdmin/crmSearchAdminLinkSelect.directive.js +++ b/ext/search/ang/crmSearchAdmin/crmSearchAdminLinkSelect.component.js @@ -4,13 +4,29 @@ angular.module('crmSearchAdmin').component('crmSearchAdminLinkSelect', { bindings: { column: '<', - links: '<' + apiEntity: '<', + apiParams: '<' }, templateUrl: '~/crmSearchAdmin/crmSearchAdminLinkSelect.html', - controller: function ($scope, $element, $timeout) { + controller: function ($scope, $element, $timeout, searchMeta) { var ts = $scope.ts = CRM.ts(), ctrl = this; + // Return all possible links to main entity or join entities + function getLinks() { + var links = _.cloneDeep(searchMeta.getEntity(ctrl.apiEntity).paths || []); + _.each(ctrl.apiParams.join, function(join) { + var joinName = join[0].split(' AS '), + joinEntity = searchMeta.getEntity(joinName[0]); + _.each(joinEntity.paths, function(path) { + var link = _.cloneDeep(path); + link.path = link.path.replace(/\[/g, '[' + joinName[1] + '.'); + links.push(link); + }); + }); + return links; + } + function onChange() { var val = $('select', $element).val(); if (val !== ctrl.column.link) { @@ -31,6 +47,8 @@ } this.$onInit = function() { + this.links = getLinks(); + $('select', $element).on('change', function() { $scope.$apply(onChange); }); diff --git a/ext/search/ang/crmSearchAdmin/crmSearchAdminLinkSelect.html b/ext/search/ang/crmSearchAdmin/crmSearchAdminLinkSelect.html index 47648e7aa4..74bc729130 100644 --- a/ext/search/ang/crmSearchAdmin/crmSearchAdminLinkSelect.html +++ b/ext/search/ang/crmSearchAdmin/crmSearchAdminLinkSelect.html @@ -7,4 +7,7 @@ {{ ts('Other...') }} - +
+ + +
diff --git a/ext/search/ang/crmSearchAdmin/crmSearchAdminTokenSelect.component.js b/ext/search/ang/crmSearchAdmin/crmSearchAdminTokenSelect.component.js new file mode 100644 index 0000000000..c2c4b2ea12 --- /dev/null +++ b/ext/search/ang/crmSearchAdmin/crmSearchAdminTokenSelect.component.js @@ -0,0 +1,51 @@ +(function(angular, $, _) { + "use strict"; + + angular.module('crmSearchAdmin').component('crmSearchAdminTokenSelect', { + bindings: { + apiEntity: '<', + apiParams: '<', + model: '<', + field: '@' + }, + templateUrl: '~/crmSearchAdmin/crmSearchAdminTokenSelect.html', + controller: function ($scope, $element, searchMeta) { + var ts = $scope.ts = CRM.ts(), + ctrl = this; + + this.initTokens = function() { + ctrl.tokens = ctrl.tokens || getTokens(); + }; + + this.insertToken = function(key) { + ctrl.model[ctrl.field] = (ctrl.model[ctrl.field] || '') + ctrl.tokens[key].token; + }; + + function getTokens() { + var tokens = { + id: { + token: '[id]', + label: searchMeta.getField('id', ctrl.apiEntity).label + } + }; + _.each(ctrl.apiParams.join, function(joinParams) { + var info = searchMeta.parseExpr(joinParams[0].split(' AS ')[1] + '.id'); + tokens[info.alias] = { + token: '[' + info.alias + ']', + label: info.field ? info.field.label : info.alias + }; + }); + _.each(ctrl.apiParams.select, function(expr) { + var info = searchMeta.parseExpr(expr); + tokens[info.alias] = { + token: '[' + info.alias + ']', + label: info.field ? info.field.label : info.alias + }; + }); + return tokens; + } + + } + }); + +})(angular, CRM.$, CRM._); diff --git a/ext/search/ang/crmSearchAdmin/crmSearchAdminTokenSelect.html b/ext/search/ang/crmSearchAdmin/crmSearchAdminTokenSelect.html new file mode 100644 index 0000000000..e30fc5d1e8 --- /dev/null +++ b/ext/search/ang/crmSearchAdmin/crmSearchAdminTokenSelect.html @@ -0,0 +1,10 @@ +
+ + +
diff --git a/ext/search/ang/crmSearchAdmin/displays/searchAdminDisplayList.component.js b/ext/search/ang/crmSearchAdmin/displays/searchAdminDisplayList.component.js index 8517f149b7..d3080f6ed8 100644 --- a/ext/search/ang/crmSearchAdmin/displays/searchAdminDisplayList.component.js +++ b/ext/search/ang/crmSearchAdmin/displays/searchAdminDisplayList.component.js @@ -55,7 +55,6 @@ }; } ctrl.hiddenColumns = ctrl.crmSearchAdminDisplay.initColumns(); - ctrl.links = ctrl.crmSearchAdminDisplay.getLinks(); }; } diff --git a/ext/search/ang/crmSearchAdmin/displays/searchAdminDisplayList.html b/ext/search/ang/crmSearchAdmin/displays/searchAdminDisplayList.html index de7e87eb75..e877777e81 100644 --- a/ext/search/ang/crmSearchAdmin/displays/searchAdminDisplayList.html +++ b/ext/search/ang/crmSearchAdmin/displays/searchAdminDisplayList.html @@ -45,11 +45,12 @@
- +
+
diff --git a/ext/search/ang/crmSearchAdmin/displays/searchAdminDisplayTable.component.js b/ext/search/ang/crmSearchAdmin/displays/searchAdminDisplayTable.component.js index 3ef0cd702a..3dc937b70d 100644 --- a/ext/search/ang/crmSearchAdmin/displays/searchAdminDisplayTable.component.js +++ b/ext/search/ang/crmSearchAdmin/displays/searchAdminDisplayTable.component.js @@ -39,7 +39,6 @@ }; } ctrl.hiddenColumns = ctrl.crmSearchAdminDisplay.initColumns(); - ctrl.links = ctrl.crmSearchAdminDisplay.getLinks(); }; } diff --git a/ext/search/ang/crmSearchAdmin/displays/searchAdminDisplayTable.html b/ext/search/ang/crmSearchAdmin/displays/searchAdminDisplayTable.html index b6d5092ac5..3589b56c03 100644 --- a/ext/search/ang/crmSearchAdmin/displays/searchAdminDisplayTable.html +++ b/ext/search/ang/crmSearchAdmin/displays/searchAdminDisplayTable.html @@ -31,11 +31,12 @@
- +
+
diff --git a/ext/search/ang/crmSearchDisplay.module.js b/ext/search/ang/crmSearchDisplay.module.js index d9f5bf2152..8d9ed43d58 100644 --- a/ext/search/ang/crmSearchDisplay.module.js +++ b/ext/search/ang/crmSearchDisplay.module.js @@ -5,21 +5,25 @@ angular.module('crmSearchDisplay', CRM.angRequires('crmSearchDisplay')) .factory('searchDisplayUtils', function() { - function getUrl(link, row) { - var url = replaceTokens(link, row); - if (url.slice(0, 1) !== '/' && url.slice(0, 4) !== 'http') { - url = CRM.url(url); - } - return _.escape(url); - } function replaceTokens(str, data) { + if (!str) { + return ''; + } _.each(data, function(value, key) { str = str.replace('[' + key + ']', value); }); return str; } + function getUrl(link, row) { + var url = replaceTokens(link, row); + if (url.slice(0, 1) !== '/' && url.slice(0, 4) !== 'http') { + url = CRM.url(url); + } + return _.escape(url); + } + function formatSearchValue(row, col, value) { var type = col.dataType, result = value; @@ -99,7 +103,8 @@ formatSearchValue: formatSearchValue, canAggregate: canAggregate, prepareColumns: prepareColumns, - prepareParams: prepareParams + prepareParams: prepareParams, + replaceTokens: replaceTokens }; }); diff --git a/ext/search/ang/crmSearchDisplayList/crmSearchDisplayList.component.js b/ext/search/ang/crmSearchDisplayList/crmSearchDisplayList.component.js index caf250af11..50157877bc 100644 --- a/ext/search/ang/crmSearchDisplayList/crmSearchDisplayList.component.js +++ b/ext/search/ang/crmSearchDisplayList/crmSearchDisplayList.component.js @@ -18,6 +18,7 @@ this.apiParams = _.cloneDeep(this.apiParams); this.apiParams.limit = parseInt(this.settings.limit || 0, 10); this.columns = searchDisplayUtils.prepareColumns(this.settings.columns, this.apiParams); + $scope.displayUtils = searchDisplayUtils; }; this.getResults = function() { diff --git a/ext/search/ang/crmSearchDisplayList/crmSearchDisplayListItems.html b/ext/search/ang/crmSearchDisplayList/crmSearchDisplayListItems.html index 15b8b1674d..8a6ae49367 100644 --- a/ext/search/ang/crmSearchDisplayList/crmSearchDisplayListItems.html +++ b/ext/search/ang/crmSearchDisplayList/crmSearchDisplayListItems.html @@ -1,4 +1,4 @@
  • -
    +
  • diff --git a/ext/search/ang/crmSearchDisplayTable/crmSearchDisplayTable.component.js b/ext/search/ang/crmSearchDisplayTable/crmSearchDisplayTable.component.js index 8ea65c7c9e..127d5d9c13 100644 --- a/ext/search/ang/crmSearchDisplayTable/crmSearchDisplayTable.component.js +++ b/ext/search/ang/crmSearchDisplayTable/crmSearchDisplayTable.component.js @@ -21,6 +21,7 @@ this.apiParams = _.cloneDeep(this.apiParams); this.apiParams.limit = parseInt(this.settings.limit || 0, 10); this.columns = searchDisplayUtils.prepareColumns(this.settings.columns, this.apiParams); + $scope.displayUtils = searchDisplayUtils; }; this.getResults = function() { diff --git a/ext/search/ang/crmSearchDisplayTable/crmSearchDisplayTable.html b/ext/search/ang/crmSearchDisplayTable/crmSearchDisplayTable.html index 59d55f2d5f..d23d5efe9c 100644 --- a/ext/search/ang/crmSearchDisplayTable/crmSearchDisplayTable.html +++ b/ext/search/ang/crmSearchDisplayTable/crmSearchDisplayTable.html @@ -18,7 +18,7 @@ - + -- 2.25.1