Commit | Line | Data |
---|---|---|
e78d6a2d CW |
1 | (function(angular, $, _) { |
2 | "use strict"; | |
3 | ||
4 | // Declare module | |
03b55607 CW |
5 | angular.module('crmSearchDisplay', CRM.angRequires('crmSearchDisplay')) |
6 | ||
406f1014 | 7 | .factory('searchDisplayUtils', function(crmApi4) { |
03b55607 | 8 | |
69f0bd2b CW |
9 | // Replace tokens keyed to rowData. |
10 | // If rowMeta is provided, values will be formatted; if omiited, raw values will be provided. | |
11 | function replaceTokens(str, rowData, rowMeta) { | |
c7e96654 CW |
12 | if (!str) { |
13 | return ''; | |
14 | } | |
69f0bd2b CW |
15 | _.each(rowData, function(value, key) { |
16 | if (str.indexOf('[' + key + ']') >= 0) { | |
17 | var column = rowMeta && _.findWhere(rowMeta, {key: key}), | |
18 | replacement = column ? formatRawValue(column, value) : value; | |
19 | str = str.replace(new RegExp(_.escapeRegExp('[' + key + ']', 'g')), replacement); | |
20 | } | |
03b55607 CW |
21 | }); |
22 | return str; | |
23 | } | |
24 | ||
69f0bd2b CW |
25 | function getUrl(link, rowData) { |
26 | var url = replaceTokens(link, rowData); | |
c7e96654 CW |
27 | if (url.slice(0, 1) !== '/' && url.slice(0, 4) !== 'http') { |
28 | url = CRM.url(url); | |
29 | } | |
daa4e55a | 30 | return url; |
c7e96654 CW |
31 | } |
32 | ||
69f0bd2b CW |
33 | // Returns html-escaped display value for a single column in a row |
34 | function formatDisplayValue(rowData, key, rowMeta) { | |
35 | var column = _.findWhere(rowMeta, {key: key}), | |
36 | displayValue = column.rewrite ? replaceTokens(column.rewrite, rowData, rowMeta) : formatRawValue(column, rowData[key]), | |
37 | result = _.escape(displayValue); | |
38 | if (column.link) { | |
2dbf2d72 CW |
39 | var target = ''; |
40 | if (column.link.target) { | |
41 | target = column.link.target === 'crm-popup' ? 'class="crm-popup" ' : 'target="' + column.link.target + '" '; | |
42 | } | |
43 | result = '<a ' + target + 'href="' + _.escape(getUrl(column.link.path, rowData)) + '">' + result + '</a>'; | |
69f0bd2b CW |
44 | } |
45 | return result; | |
46 | } | |
47 | ||
48 | // Formats raw field value according to data type | |
49 | function formatRawValue(column, value) { | |
50 | var type = column && column.dataType, | |
03b55607 CW |
51 | result = value; |
52 | if (_.isArray(value)) { | |
53 | return _.map(value, function(val) { | |
69f0bd2b | 54 | return formatRawValue(column, val); |
03b55607 CW |
55 | }).join(', '); |
56 | } | |
57 | if (value && (type === 'Date' || type === 'Timestamp') && /^\d{4}-\d{2}-\d{2}/.test(value)) { | |
58 | result = CRM.utils.formatDate(value, null, type === 'Timestamp'); | |
59 | } | |
60 | else if (type === 'Boolean' && typeof value === 'boolean') { | |
61 | result = value ? ts('Yes') : ts('No'); | |
62 | } | |
63 | else if (type === 'Money' && typeof value === 'number') { | |
64 | result = CRM.formatMoney(value); | |
65 | } | |
03b55607 | 66 | return result; |
b1603dbd | 67 | } |
03b55607 | 68 | |
406f1014 CW |
69 | function getApiParams(ctrl, mode) { |
70 | return { | |
71 | return: mode || 'page:' + ctrl.page, | |
72 | savedSearch: ctrl.search, | |
73 | display: ctrl.display, | |
74 | sort: ctrl.sort, | |
82bf6674 CW |
75 | filters: _.assign({}, (ctrl.afFieldset ? ctrl.afFieldset.getFieldData() : {}), ctrl.filters), |
76 | afform: ctrl.afFieldset ? ctrl.afFieldset.getFormName() : null | |
406f1014 CW |
77 | }; |
78 | } | |
79 | ||
80 | function getResults(ctrl) { | |
b2ee26f0 | 81 | return crmApi4('SearchDisplay', 'run', getApiParams(ctrl)).then(function(results) { |
406f1014 | 82 | ctrl.results = results; |
b2ee26f0 | 83 | ctrl.editing = false; |
406f1014 CW |
84 | if (ctrl.settings.pager && !ctrl.rowCount) { |
85 | if (results.length < ctrl.settings.limit) { | |
86 | ctrl.rowCount = results.length; | |
87 | } else { | |
88 | var params = getApiParams(ctrl, 'row_count'); | |
89 | crmApi4('SearchDisplay', 'run', params).then(function(result) { | |
90 | ctrl.rowCount = result.count; | |
91 | }); | |
92 | } | |
b1603dbd CW |
93 | } |
94 | }); | |
b1603dbd CW |
95 | } |
96 | ||
97 | return { | |
69f0bd2b | 98 | formatDisplayValue: formatDisplayValue, |
406f1014 CW |
99 | getApiParams: getApiParams, |
100 | getResults: getResults, | |
daa4e55a CW |
101 | replaceTokens: replaceTokens, |
102 | getUrl: getUrl | |
03b55607 CW |
103 | }; |
104 | }); | |
e78d6a2d CW |
105 | |
106 | })(angular, CRM.$, CRM._); |