Fix ts() namespace in searchKit & afform extensions
[civicrm-core.git] / ext / search / ang / crmSearchActions / crmSearchInput / crmSearchInputVal.component.js
1 (function(angular, $, _) {
2 "use strict";
3
4 angular.module('crmSearchActions').component('crmSearchInputVal', {
5 bindings: {
6 field: '<',
7 'multi': '<',
8 'optionKey': '<'
9 },
10 require: {ngModel: 'ngModel'},
11 template: '<div class="form-group" ng-include="$ctrl.getTemplate()"></div>',
12 controller: function($scope, formatForSelect2) {
13 var ts = $scope.ts = CRM.ts('org.civicrm.search'),
14 ctrl = this;
15
16 this.$onInit = function() {
17 var rendered = false;
18 ctrl.dateRanges = CRM.crmSearchActions.dateRanges;
19 ctrl.entity = ctrl.field.fk_entity || ctrl.field.entity;
20
21 this.ngModel.$render = function() {
22 ctrl.value = ctrl.ngModel.$viewValue;
23 if (!rendered && ctrl.field.input_type === 'Date') {
24 setDateType();
25 }
26 rendered = true;
27 };
28
29 $scope.$watch('$ctrl.value', function() {
30 ctrl.ngModel.$setViewValue(ctrl.value);
31 });
32
33 function setDateType() {
34 if (_.findWhere(ctrl.dateRanges, {id: ctrl.value})) {
35 ctrl.dateType = 'range';
36 } else if (ctrl.value === 'now') {
37 ctrl.dateType = 'now';
38 } else if (_.includes(ctrl.value, 'now -')) {
39 ctrl.dateType = 'now -';
40 } else if (_.includes(ctrl.value, 'now +')) {
41 ctrl.dateType = 'now +';
42 } else {
43 ctrl.dateType = 'fixed';
44 }
45 }
46 };
47
48 this.changeDateType = function() {
49 switch (ctrl.dateType) {
50 case 'fixed':
51 ctrl.value = '';
52 break;
53
54 case 'range':
55 ctrl.value = ctrl.dateRanges[0].id;
56 break;
57
58 case 'now':
59 ctrl.value = 'now';
60 break;
61
62 default:
63 ctrl.value = ctrl.dateType + ' 1 day';
64 }
65 };
66
67 this.dateUnits = function(setUnit) {
68 var vals = ctrl.value.split(' ');
69 if (arguments.length) {
70 vals[3] = setUnit;
71 ctrl.value = vals.join(' ');
72 } else {
73 return vals[3];
74 }
75 };
76
77 this.dateNumber = function(setNumber) {
78 var vals = ctrl.value.split(' ');
79 if (arguments.length) {
80 vals[2] = setNumber;
81 ctrl.value = vals.join(' ');
82 } else {
83 return parseInt(vals[2], 10);
84 }
85 };
86
87 this.getTemplate = function() {
88
89 if (ctrl.field.input_type === 'Date') {
90 return '~/crmSearchActions/crmSearchInput/date.html';
91 }
92
93 if (ctrl.field.data_type === 'Boolean') {
94 return '~/crmSearchActions/crmSearchInput/boolean.html';
95 }
96
97 if (ctrl.field.options) {
98 return '~/crmSearchActions/crmSearchInput/select.html';
99 }
100
101 if (ctrl.field.fk_entity || ctrl.field.name === 'id') {
102 return '~/crmSearchActions/crmSearchInput/entityRef.html';
103 }
104
105 if (ctrl.field.data_type === 'Integer') {
106 return '~/crmSearchActions/crmSearchInput/integer.html';
107 }
108
109 return '~/crmSearchActions/crmSearchInput/text.html';
110 };
111
112 this.getFieldOptions = function() {
113 return {results: formatForSelect2(ctrl.field.options, ctrl.optionKey || 'id', 'label', ['description', 'color', 'icon'])};
114 };
115
116 }
117 });
118
119 })(angular, CRM.$, CRM._);