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