1 (function(angular
, $, _
) {
4 angular
.module('crmSearchDisplayTable').component('crmSearchDisplayTable', {
13 afFieldset
: '?^^afFieldset'
15 templateUrl
: '~/crmSearchDisplayTable/crmSearchDisplayTable.html',
16 controller: function($scope
, crmApi4
, searchDisplayUtils
) {
17 var ts
= $scope
.ts
= CRM
.ts('org.civicrm.search'),
22 this.selectedRows
= [];
23 this.allRowsSelected
= false;
25 this.$onInit = function() {
26 this.sort
= this.settings
.sort
? _
.cloneDeep(this.settings
.sort
) : [];
27 $scope
.displayUtils
= searchDisplayUtils
;
29 if (this.afFieldset
) {
30 $scope
.$watch(this.afFieldset
.getFieldData
, refresh
, true);
32 $scope
.$watch('$ctrl.filters', refresh
, true);
35 this.getResults
= _
.debounce(function() {
36 searchDisplayUtils
.getResults(ctrl
);
46 * Returns crm-i icon class for a sortable column
50 $scope
.getSort = function(col
) {
51 var dir
= _
.reduce(ctrl
.sort
, function(dir
, item
) {
52 return item
[0] === col
.key
? item
[1] : dir
;
55 return 'fa-sort-' + dir
.toLowerCase();
57 return 'fa-sort disabled';
61 * Called when clicking on a column header
65 $scope
.setSort = function(col
, $event
) {
66 if (col
.type
!== 'field') {
69 var dir
= $scope
.getSort(col
) === 'fa-sort-asc' ? 'DESC' : 'ASC';
70 if (!$event
.shiftKey
|| !ctrl
.sort
) {
73 var index
= _
.findIndex(ctrl
.sort
, [col
.key
]);
75 ctrl
.sort
[index
][1] = dir
;
77 ctrl
.sort
.push([col
.key
, dir
]);
82 this.formatFieldValue = function(rowData
, col
) {
83 return searchDisplayUtils
.formatDisplayValue(rowData
, col
.key
, ctrl
.settings
.columns
);
86 $scope
.selectAllRows = function() {
88 if (ctrl
.allRowsSelected
) {
89 ctrl
.allRowsSelected
= false;
90 ctrl
.selectedRows
.length
= 0;
94 ctrl
.allRowsSelected
= true;
95 if (ctrl
.page
=== 1 && ctrl
.results
.length
< ctrl
.settings
.limit
) {
96 ctrl
.selectedRows
= _
.pluck(ctrl
.results
, 'id');
99 // If more than one page of results, use ajax to fetch all ids
100 $scope
.loadingAllRows
= true;
101 var params
= searchDisplayUtils
.getApiParams(ctrl
, 'id');
102 crmApi4('SearchDisplay', 'run', params
, ['id']).then(function(ids
) {
103 $scope
.loadingAllRows
= false;
104 ctrl
.selectedRows
= _
.toArray(ids
);
108 $scope
.selectRow = function(row
) {
109 var index
= ctrl
.selectedRows
.indexOf(row
.id
);
111 ctrl
.selectedRows
.push(row
.id
);
112 ctrl
.allRowsSelected
= (ctrl
.rowCount
=== ctrl
.selectedRows
.length
);
114 ctrl
.allRowsSelected
= false;
115 ctrl
.selectedRows
.splice(index
, 1);
119 $scope
.isRowSelected = function(row
) {
120 return ctrl
.allRowsSelected
|| _
.includes(ctrl
.selectedRows
, row
.id
);
126 })(angular
, CRM
.$, CRM
._
);