Commit | Line | Data |
---|---|---|
44402a2e CW |
1 | (function(angular, $, _) { |
2 | "use strict"; | |
3 | ||
4 | angular.module('crmSearchDisplay').component('crmSearchDisplayTable', { | |
5 | bindings: { | |
6 | apiEntity: '<', | |
7 | apiParams: '<', | |
590c0e3f CW |
8 | settings: '<', |
9 | filters: '<' | |
44402a2e CW |
10 | }, |
11 | templateUrl: '~/crmSearchDisplay/crmSearchDisplayTable.html', | |
12 | controller: function($scope, crmApi4) { | |
13 | var ts = $scope.ts = CRM.ts(), | |
14 | ctrl = this; | |
15 | ||
16 | this.page = 1; | |
17 | ||
18 | this.$onInit = function() { | |
590c0e3f | 19 | this.orderBy = _.cloneDeep(this.apiParams.orderBy || {}); |
44402a2e | 20 | this.limit = parseInt(ctrl.settings.limit || 0, 10); |
590c0e3f CW |
21 | this.columns = _.cloneDeep(ctrl.settings.columns); |
22 | _.each(ctrl.columns, function(col, num) { | |
44402a2e CW |
23 | var index = ctrl.apiParams.select.indexOf(col.expr); |
24 | if (_.includes(col.expr, '(') && !_.includes(col.expr, ' AS ')) { | |
25 | col.expr += ' AS column_' + num; | |
26 | ctrl.apiParams.select[index] += ' AS column_' + num; | |
27 | } | |
28 | col.key = _.last(col.expr.split(' AS ')); | |
29 | }); | |
44402a2e CW |
30 | }; |
31 | ||
32 | function getResults() { | |
33 | var params = _.merge(_.cloneDeep(ctrl.apiParams), {limit: ctrl.limit, offset: (ctrl.page - 1) * ctrl.limit, orderBy: ctrl.orderBy}); | |
590c0e3f CW |
34 | if (_.isEmpty(params.where)) { |
35 | params.where = []; | |
36 | } | |
37 | _.each(ctrl.filters, function(value, key) { | |
38 | if (value) { | |
39 | params.where.push([key, 'CONTAINS', value]); | |
40 | } | |
41 | }); | |
44402a2e CW |
42 | if (ctrl.settings.pager) { |
43 | params.select.push('row_count'); | |
44 | } | |
45 | crmApi4(ctrl.apiEntity, 'get', params).then(function(results) { | |
46 | ctrl.results = results; | |
47 | ctrl.rowCount = results.count; | |
48 | }); | |
49 | } | |
50 | ||
51 | this.changePage = function() { | |
52 | getResults(); | |
53 | }; | |
54 | ||
590c0e3f CW |
55 | $scope.$watch('$ctrl.filters', getResults, true); |
56 | ||
44402a2e CW |
57 | /** |
58 | * Returns crm-i icon class for a sortable column | |
59 | * @param col | |
60 | * @returns {string} | |
61 | */ | |
62 | $scope.getOrderBy = function(col) { | |
63 | var dir = ctrl.orderBy && ctrl.orderBy[col.key]; | |
64 | if (dir) { | |
65 | return 'fa-sort-' + dir.toLowerCase(); | |
66 | } | |
67 | return 'fa-sort disabled'; | |
68 | }; | |
69 | ||
70 | /** | |
71 | * Called when clicking on a column header | |
72 | * @param col | |
73 | * @param $event | |
74 | */ | |
75 | $scope.setOrderBy = function(col, $event) { | |
76 | var dir = $scope.getOrderBy(col) === 'fa-sort-asc' ? 'DESC' : 'ASC'; | |
77 | if (!$event.shiftKey) { | |
78 | ctrl.orderBy = {}; | |
79 | } | |
80 | ctrl.orderBy[col.key] = dir; | |
81 | getResults(); | |
82 | }; | |
83 | ||
84 | $scope.formatResult = function(row, col) { | |
85 | var value = row[col.key]; | |
86 | return formatFieldValue(col, value); | |
87 | }; | |
88 | ||
89 | function formatFieldValue(col, value) { | |
90 | var type = col.dataType; | |
91 | if (_.isArray(value)) { | |
92 | return _.map(value, function(val) { | |
93 | return formatFieldValue(col, val); | |
94 | }).join(', '); | |
95 | } | |
96 | if (value && (type === 'Date' || type === 'Timestamp') && /^\d{4}-\d{2}-\d{2}/.test(value)) { | |
97 | return CRM.utils.formatDate(value, null, type === 'Timestamp'); | |
98 | } | |
99 | else if (type === 'Boolean' && typeof value === 'boolean') { | |
100 | return value ? ts('Yes') : ts('No'); | |
101 | } | |
102 | else if (type === 'Money' && typeof value === 'number') { | |
103 | return CRM.formatMoney(value); | |
104 | } | |
105 | return value; | |
106 | } | |
107 | ||
108 | } | |
109 | }); | |
110 | ||
111 | })(angular, CRM.$, CRM._); |