Commit | Line | Data |
---|---|---|
ab4e6f8a CW |
1 | (function(angular, $, _) { |
2 | "use strict"; | |
3 | ||
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'); | |
7 | ||
8 | // Trait properties get mixed into display controller using angular.extend() | |
9 | return { | |
10 | ||
11 | selectedRows: [], | |
12 | allRowsSelected: false, | |
13 | ||
14 | // Toggle the "select all" checkbox | |
15 | selectAllRows: function() { | |
16 | var ctrl = this; | |
17 | // Deselect all | |
18 | if (ctrl.allRowsSelected) { | |
19 | ctrl.allRowsSelected = false; | |
20 | ctrl.selectedRows.length = 0; | |
21 | return; | |
22 | } | |
23 | // Select all | |
24 | ctrl.allRowsSelected = true; | |
10cd9d37 | 25 | if (ctrl.page === 1 && ctrl.results.length < ctrl.limit) { |
ab4e6f8a CW |
26 | ctrl.selectedRows = _.pluck(ctrl.results, 'id'); |
27 | return; | |
28 | } | |
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); | |
35 | }); | |
36 | }, | |
37 | ||
38 | // Toggle row selection | |
39 | selectRow: function(row) { | |
40 | var index = this.selectedRows.indexOf(row.id); | |
41 | if (index < 0) { | |
42 | this.selectedRows.push(row.id); | |
43 | this.allRowsSelected = (this.rowCount === this.selectedRows.length); | |
44 | } else { | |
45 | this.allRowsSelected = false; | |
46 | this.selectedRows.splice(index, 1); | |
47 | } | |
48 | }, | |
49 | ||
50 | // @return bool | |
51 | isRowSelected: function(row) { | |
52 | return this.allRowsSelected || _.includes(this.selectedRows, row.id); | |
53 | }, | |
54 | ||
957358aa CW |
55 | // Overwrite empty onChangeFilters array from searchDisplayBaseTrait |
56 | onChangeFilters: [function() { | |
57 | // Reset selection when filters are changed | |
ab4e6f8a CW |
58 | this.selectedRows.length = 0; |
59 | this.allRowsSelected = false; | |
957358aa | 60 | }] |
ab4e6f8a CW |
61 | |
62 | }; | |
63 | }); | |
64 | ||
65 | })(angular, CRM.$, CRM._); |