1 (function(angular
, $, _
) {
4 // Trait shared by any search display controllers which use tasks
5 angular
.module('crmSearchDisplay').factory('searchDisplayTasksTrait', function(crmApi4
) {
6 var ts
= CRM
.ts('org.civicrm.search_kit');
8 // Trait properties get mixed into display controller using angular.extend()
12 allRowsSelected
: false,
14 // Toggle the "select all" checkbox
15 selectAllRows: function() {
18 if (ctrl
.allRowsSelected
) {
19 ctrl
.allRowsSelected
= false;
20 ctrl
.selectedRows
.length
= 0;
24 ctrl
.allRowsSelected
= true;
25 if (ctrl
.page
=== 1 && ctrl
.results
.length
< ctrl
.limit
) {
26 ctrl
.selectedRows
= _
.pluck(ctrl
.results
, 'id');
29 // If more than one page of results, use ajax to fetch all ids
30 ctrl
.loadingAllRows
= true;
31 var params
= ctrl
.getApiParams('id');
32 crmApi4('SearchDisplay', 'run', params
, ['id']).then(function(ids
) {
33 ctrl
.loadingAllRows
= false;
34 ctrl
.selectedRows
= _
.toArray(ids
);
38 // Toggle row selection
39 selectRow: function(row
) {
40 var index
= this.selectedRows
.indexOf(row
.id
);
42 this.selectedRows
.push(row
.id
);
43 this.allRowsSelected
= (this.rowCount
=== this.selectedRows
.length
);
45 this.allRowsSelected
= false;
46 this.selectedRows
.splice(index
, 1);
51 isRowSelected: function(row
) {
52 return this.allRowsSelected
|| _
.includes(this.selectedRows
, row
.id
);
55 // Reset selection when filters are changed
56 onChangeFilters: function() {
57 this.selectedRows
.length
= 0;
58 this.allRowsSelected
= false;
64 })(angular
, CRM
.$, CRM
._
);