Skip to content

Commit

Permalink
Search ext: misc cleanup & fixes
Browse files Browse the repository at this point in the history
- Fixes white on white button text by adding btn-default class
- Fixes missing field names in the bulk-update action
- Decouples searchActions from the main search controller
- Misc cleanup
  • Loading branch information
colemanw committed Oct 9, 2020
1 parent 1d1cf83 commit dc1043a
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 33 deletions.
28 changes: 14 additions & 14 deletions ext/search/ang/search.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,31 +81,29 @@
.factory('searchMeta', function() {
function getEntity(entityName) {
if (entityName) {
entityName = entityName === true ? searchEntity : entityName;
return _.find(CRM.vars.search.schema, {name: entityName});
}
}
function getField(name) {
var dotSplit = name.split('.'),
function getField(fieldName, entityName) {
var dotSplit = fieldName.split('.'),
joinEntity = dotSplit.length > 1 ? dotSplit[0] : null,
fieldName = _.last(dotSplit).split(':')[0],
entityName = searchEntity;
name = _.last(dotSplit).split(':')[0];
// Custom fields contain a dot in their fieldname
// If 3 segments, the first is the joinEntity and the last 2 are the custom field
if (dotSplit.length === 3) {
fieldName = dotSplit[1] + '.' + fieldName;
name = dotSplit[1] + '.' + name;
}
// If 2 segments, it's ambiguous whether this is a custom field or joined field. Search the main entity first.
if (dotSplit.length === 2) {
var field = _.find(getEntity(true).fields, {name: dotSplit[0] + '.' + fieldName});
var field = _.find(getEntity(entityName).fields, {name: dotSplit[0] + '.' + name});
if (field) {
return field;
}
}
if (joinEntity) {
entityName = _.find(CRM.vars.search.links[entityName], {alias: joinEntity}).entity;
}
return _.find(getEntity(entityName).fields, {name: fieldName});
return _.find(getEntity(entityName).fields, {name: name});
}
return {
getEntity: getEntity,
Expand All @@ -120,12 +118,14 @@
result.fn = _.find(CRM.vars.search.functions, {name: expr.substring(0, bracketPos)});
result.modifier = _.trim(parsed[1]);
}
result.field = getField(fieldName);
var split = fieldName.split(':'),
prefixPos = split[0].lastIndexOf(result.field.name);
result.path = split[0];
result.prefix = prefixPos > 0 ? result.path.substring(0, prefixPos) : '';
result.suffix = !split[1] ? '' : ':' + split[1];
result.field = expr ? getField(fieldName, searchEntity) : undefined;
if (result.field) {
var split = fieldName.split(':'),
prefixPos = split[0].lastIndexOf(result.field.name);
result.path = split[0];
result.prefix = prefixPos > 0 ? result.path.substring(0, prefixPos) : '';
result.suffix = !split[1] ? '' : ':' + split[1];
}
return result;
}
};
Expand Down
13 changes: 4 additions & 9 deletions ext/search/ang/search/crmSearch.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
this.page = 1;
this.params = {};
// After a search this.results is an object of result arrays keyed by page,
// Prior to searching it's an empty string because 1: falsey and 2: doesn't throw an error if you try to access undefined properties
// Initially this.results is an empty string because 1: it's falsey (unlike an empty object) and 2: it doesn't throw an error if you try to access undefined properties (unlike null)
this.results = '';
this.rowCount = false;
// Have the filters (WHERE, HAVING, GROUP BY, JOIN) changed?
Expand Down Expand Up @@ -381,13 +381,6 @@
return value;
}

function getOption(field, value) {
return _.find(field.options, function(option) {
// Type coersion is intentional
return option.id == value;
});
}

$scope.fieldsForGroupBy = function() {
return {results: getAllFields('', function(key) {
return _.contains(ctrl.params.groupBy, key);
Expand All @@ -413,7 +406,9 @@
};

function getDefaultSelect() {
return _.filter(['id', 'display_name', 'label', 'title', 'location_type_id:label'], searchMeta.getField);
return _.filter(['id', 'display_name', 'label', 'title', 'location_type_id:label'], function(field) {
return !!searchMeta.getField(field, ctrl.entity);
});
}

function getAllFields(suffix, disabledIf) {
Expand Down
4 changes: 2 additions & 2 deletions ext/search/ang/search/crmSearch/controls.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
{{:: ts('Auto') }}
</button>
</div>
<crm-search-actions entity="$ctrl.entity" ids="$ctrl.selectedRows"></crm-search-actions>
<crm-search-actions entity="$ctrl.entity" ids="$ctrl.selectedRows" refresh="$ctrl.refreshPage()"></crm-search-actions>
<div class="btn-group pull-right">
<button type="button" class="btn form-control dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<button type="button" class="btn btn-default form-control dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="crm-i fa-save"></i> {{:: ts('Create')}}
<span class="caret"></span>
</button>
Expand Down
8 changes: 3 additions & 5 deletions ext/search/ang/search/crmSearchActions.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
angular.module('search').component('crmSearchActions', {
bindings: {
entity: '<',
refresh: '&',
ids: '<'
},
require: {
search: '^crmSearch'
},
templateUrl: '~/search/crmSearchActions.html',
controller: function($scope, crmApi4, dialogService, searchMeta) {
var ts = $scope.ts = CRM.ts(),
Expand Down Expand Up @@ -44,7 +42,7 @@
var path = $scope.$eval(action.crmPopup.path, data),
query = action.crmPopup.query && $scope.$eval(action.crmPopup.query, data);
CRM.loadForm(CRM.url(path, query))
.on('crmFormSuccess', ctrl.search.refreshPage);
.on('crmFormSuccess', ctrl.refresh);
}
// If action uses dialogService
else if (action.uiDialog) {
Expand All @@ -53,7 +51,7 @@
title: action.title
});
dialogService.open('crmSearchAction', action.uiDialog.templateUrl, data, options)
.then(ctrl.search.refreshPage);
.then(ctrl.refresh);
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion ext/search/ang/search/crmSearchActions.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="btn-group" title="{{:: ts('Perform action on selected items.') }}">
<button type="button" ng-disabled="!$ctrl.ids.length" ng-click="$ctrl.init()" class="btn form-control dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<button type="button" ng-disabled="!$ctrl.ids.length" ng-click="$ctrl.init()" class="btn form-control dropdown-toggle btn-default" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{:: ts('Action') }} <span class="caret"></span>
</button>
<ul class="dropdown-menu" ng-if=":: $ctrl.actions">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

this.availableFields = function() {
var results = _.transform(ctrl.entity.fields, function(result, item) {
var formatted = {id: item.name, text: item.title, description: item.description};
var formatted = {id: item.name, text: item.label, description: item.description};
if (fieldInUse(item.name)) {
formatted.disabled = true;
}
Expand Down
3 changes: 2 additions & 1 deletion ext/search/ang/search/crmSearchFunction.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
angular.module('search').component('crmSearchFunction', {
bindings: {
expr: '=',
cat: '<'
cat: '<',
entity: '<'
},
templateUrl: '~/search/crmSearchFunction.html',
controller: function($scope, formatForSelect2, searchMeta) {
Expand Down

0 comments on commit dc1043a

Please sign in to comment.