Merge pull request #23283 from eileenmcnaughton/import_saved_map
[civicrm-core.git] / ext / search_kit / ang / crmSearchTasks / crmSearchInput / crmSearchInputVal.component.js
CommitLineData
014174e7
CW
1(function(angular, $, _) {
2 "use strict";
3
1e313889 4 angular.module('crmSearchTasks').component('crmSearchInputVal', {
014174e7
CW
5 bindings: {
6 field: '<',
aa0c5602 7 'op': '<',
014174e7
CW
8 'optionKey': '<'
9 },
10 require: {ngModel: 'ngModel'},
11 template: '<div class="form-group" ng-include="$ctrl.getTemplate()"></div>',
d2a29eba 12 controller: function($scope, formatForSelect2, crmApi4) {
33e81cf6 13 var ts = $scope.ts = CRM.ts('org.civicrm.search_kit'),
014174e7
CW
14 ctrl = this;
15
16 this.$onInit = function() {
df88f71d
CW
17 var rendered = false,
18 field = this.field || {};
1e313889 19 ctrl.dateRanges = CRM.crmSearchTasks.dateRanges;
df88f71d 20 ctrl.entity = field.fk_entity || field.entity;
014174e7
CW
21
22 this.ngModel.$render = function() {
23 ctrl.value = ctrl.ngModel.$viewValue;
40f6184e 24 if (!rendered && isDateField(field)) {
014174e7
CW
25 setDateType();
26 }
27 rendered = true;
28 };
29
30 $scope.$watch('$ctrl.value', function() {
31 ctrl.ngModel.$setViewValue(ctrl.value);
32 });
33
34 function setDateType() {
35 if (_.findWhere(ctrl.dateRanges, {id: ctrl.value})) {
36 ctrl.dateType = 'range';
37 } else if (ctrl.value === 'now') {
38 ctrl.dateType = 'now';
39 } else if (_.includes(ctrl.value, 'now -')) {
40 ctrl.dateType = 'now -';
41 } else if (_.includes(ctrl.value, 'now +')) {
42 ctrl.dateType = 'now +';
43 } else {
44 ctrl.dateType = 'fixed';
45 }
46 }
47 };
48
aa0c5602
CW
49 this.isMulti = function() {
50 // If there's a search operator, return `true` if the operator takes multiple values, else `false`
51 if (ctrl.op) {
52 return ctrl.op === 'IN' || ctrl.op === 'NOT IN';
53 }
54 // If no search operator this is an input for e.g. the bulk update action
55 // Return `true` if the field is multi-valued, else `null`
56 return ctrl.field && (ctrl.field.serialize || ctrl.field.data_type === 'Array') ? true : null;
57 };
58
014174e7
CW
59 this.changeDateType = function() {
60 switch (ctrl.dateType) {
61 case 'fixed':
62 ctrl.value = '';
63 break;
64
65 case 'range':
66 ctrl.value = ctrl.dateRanges[0].id;
67 break;
68
69 case 'now':
70 ctrl.value = 'now';
71 break;
72
73 default:
74 ctrl.value = ctrl.dateType + ' 1 day';
75 }
76 };
77
78 this.dateUnits = function(setUnit) {
79 var vals = ctrl.value.split(' ');
80 if (arguments.length) {
81 vals[3] = setUnit;
82 ctrl.value = vals.join(' ');
83 } else {
84 return vals[3];
85 }
86 };
87
88 this.dateNumber = function(setNumber) {
89 var vals = ctrl.value.split(' ');
90 if (arguments.length) {
91 vals[2] = setNumber;
92 ctrl.value = vals.join(' ');
93 } else {
94 return parseInt(vals[2], 10);
95 }
96 };
97
d2a29eba
CW
98 this.lookupAddress = function() {
99 ctrl.value.geo_code_1 = null;
100 ctrl.value.geo_code_2 = null;
101 if (ctrl.value.address) {
102 crmApi4('Address', 'getCoordinates', {
103 address: ctrl.value.address
104 }).then(function(coordinates) {
105 if (coordinates[0]) {
106 ctrl.value.geo_code_1 = coordinates[0].geo_code_1;
107 ctrl.value.geo_code_2 = coordinates[0].geo_code_2;
108 }
109 });
110 }
111 };
112
014174e7 113 this.getTemplate = function() {
f27b9421 114 var field = ctrl.field || {};
014174e7 115
aa0c5602
CW
116 if (_.includes(['LIKE', 'NOT LIKE', 'REGEXP', 'NOT REGEXP'], ctrl.op)) {
117 return '~/crmSearchTasks/crmSearchInput/text.html';
118 }
119
d2a29eba
CW
120 if (field.input_type === 'Location') {
121 ctrl.value = ctrl.value || {distance_unit: CRM.crmSearchAdmin.defaultDistanceUnit};
122 return '~/crmSearchTasks/crmSearchInput/location.html';
123 }
124
40f6184e 125 if (isDateField(field)) {
1e313889 126 return '~/crmSearchTasks/crmSearchInput/date.html';
014174e7
CW
127 }
128
f27b9421 129 if (field.data_type === 'Boolean') {
1e313889 130 return '~/crmSearchTasks/crmSearchInput/boolean.html';
014174e7
CW
131 }
132
f27b9421 133 if (field.options) {
1e313889 134 return '~/crmSearchTasks/crmSearchInput/select.html';
014174e7
CW
135 }
136
aa0c5602 137 if ((field.fk_entity || field.name === 'id') && !_.includes(['>', '<', '>=', '<='], ctrl.op)) {
1e313889 138 return '~/crmSearchTasks/crmSearchInput/entityRef.html';
014174e7
CW
139 }
140
f27b9421 141 if (field.data_type === 'Integer') {
1e313889 142 return '~/crmSearchTasks/crmSearchInput/integer.html';
014174e7
CW
143 }
144
f27b9421 145 if (field.data_type === 'Float') {
1e313889 146 return '~/crmSearchTasks/crmSearchInput/float.html';
6d3a6960
CW
147 }
148
1e313889 149 return '~/crmSearchTasks/crmSearchInput/text.html';
014174e7
CW
150 };
151
152 this.getFieldOptions = function() {
f27b9421
CW
153 var field = ctrl.field || {};
154 return {results: formatForSelect2(field.options || [], ctrl.optionKey || 'id', 'label', ['description', 'color', 'icon'])};
014174e7
CW
155 };
156
40f6184e
CW
157 function isDateField(field) {
158 return field.data_type === 'Date' || field.data_type === 'Timestamp';
159 }
160
014174e7
CW
161 }
162 });
163
164})(angular, CRM.$, CRM._);