Merge pull request #19235 from colemanw/flex
[civicrm-core.git] / ext / search / ang / crmSearchDisplayTable / crmSearchDisplayTable.component.js
1 (function(angular, $, _) {
2 "use strict";
3
4 angular.module('crmSearchDisplayTable').component('crmSearchDisplayTable', {
5 bindings: {
6 apiEntity: '<',
7 apiParams: '<',
8 settings: '<',
9 filters: '<'
10 },
11 templateUrl: '~/crmSearchDisplayTable/crmSearchDisplayTable.html',
12 controller: function($scope, crmApi4, searchDisplayUtils) {
13 var ts = $scope.ts = CRM.ts(),
14 ctrl = this;
15
16 this.page = 1;
17 this.selectedRows = [];
18 this.allRowsSelected = false;
19
20 this.$onInit = function() {
21 this.apiParams = _.cloneDeep(this.apiParams);
22 this.apiParams.limit = parseInt(this.settings.limit || 0, 10);
23 this.columns = searchDisplayUtils.prepareColumns(this.settings.columns, this.apiParams);
24 };
25
26 this.getResults = function() {
27 var params = searchDisplayUtils.prepareParams(ctrl.apiParams, ctrl.filters, ctrl.settings.pager ? ctrl.page : null);
28
29 crmApi4(ctrl.apiEntity, 'get', params).then(function(results) {
30 ctrl.results = results;
31 ctrl.rowCount = results.count;
32 });
33 };
34
35 $scope.$watch('$ctrl.filters', ctrl.getResults, true);
36
37 /**
38 * Returns crm-i icon class for a sortable column
39 * @param col
40 * @returns {string}
41 */
42 $scope.getOrderBy = function(col) {
43 var dir = ctrl.apiParams.orderBy && ctrl.apiParams.orderBy[col.key];
44 if (dir) {
45 return 'fa-sort-' + dir.toLowerCase();
46 }
47 return 'fa-sort disabled';
48 };
49
50 /**
51 * Called when clicking on a column header
52 * @param col
53 * @param $event
54 */
55 $scope.setOrderBy = function(col, $event) {
56 var dir = $scope.getOrderBy(col) === 'fa-sort-asc' ? 'DESC' : 'ASC';
57 if (!$event.shiftKey || !ctrl.apiParams.orderBy) {
58 ctrl.apiParams.orderBy = {};
59 }
60 ctrl.apiParams.orderBy[col.key] = dir;
61 ctrl.getResults();
62 };
63
64 $scope.formatResult = function(row, col) {
65 var value = row[col.key];
66 return searchDisplayUtils.formatSearchValue(row, col, value);
67 };
68
69 $scope.selectAllRows = function() {
70 // Deselect all
71 if (ctrl.allRowsSelected) {
72 ctrl.allRowsSelected = false;
73 ctrl.selectedRows.length = 0;
74 return;
75 }
76 // Select all
77 ctrl.allRowsSelected = true;
78 if (ctrl.page === 1 && ctrl.results[1].length < ctrl.apiParams.limit) {
79 ctrl.selectedRows = _.pluck(ctrl.results[1], 'id');
80 return;
81 }
82 // If more than one page of results, use ajax to fetch all ids
83 $scope.loadingAllRows = true;
84 var params = _.cloneDeep(ctrl.apiParams);
85 params.select = ['id'];
86 crmApi4(ctrl.apiEntity, 'get', params, ['id']).then(function(ids) {
87 $scope.loadingAllRows = false;
88 ctrl.selectedRows = _.toArray(ids);
89 });
90 };
91
92 $scope.selectRow = function(row) {
93 var index = ctrl.selectedRows.indexOf(row.id);
94 if (index < 0) {
95 ctrl.selectedRows.push(row.id);
96 ctrl.allRowsSelected = (ctrl.rowCount === ctrl.selectedRows.length);
97 } else {
98 ctrl.allRowsSelected = false;
99 ctrl.selectedRows.splice(index, 1);
100 }
101 };
102
103 $scope.isRowSelected = function(row) {
104 return ctrl.allRowsSelected || _.includes(ctrl.selectedRows, row.id);
105 };
106
107 }
108 });
109
110 })(angular, CRM.$, CRM._);