Commit | Line | Data |
---|---|---|
014174e7 CW |
1 | (function(angular, $, _) { |
2 | "use strict"; | |
3 | ||
1e313889 | 4 | angular.module('crmSearchTasks').component('crmSearchInputVal', { |
014174e7 CW |
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) { | |
33e81cf6 | 13 | var ts = $scope.ts = CRM.ts('org.civicrm.search_kit'), |
014174e7 CW |
14 | ctrl = this; |
15 | ||
16 | this.$onInit = function() { | |
17 | var rendered = false; | |
1e313889 | 18 | ctrl.dateRanges = CRM.crmSearchTasks.dateRanges; |
b1deaefc | 19 | ctrl.entity = ctrl.field.fk_entity || ctrl.field.entity; |
014174e7 CW |
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') { | |
1e313889 | 90 | return '~/crmSearchTasks/crmSearchInput/date.html'; |
014174e7 CW |
91 | } |
92 | ||
93 | if (ctrl.field.data_type === 'Boolean') { | |
1e313889 | 94 | return '~/crmSearchTasks/crmSearchInput/boolean.html'; |
014174e7 CW |
95 | } |
96 | ||
97 | if (ctrl.field.options) { | |
1e313889 | 98 | return '~/crmSearchTasks/crmSearchInput/select.html'; |
014174e7 CW |
99 | } |
100 | ||
b1deaefc | 101 | if (ctrl.field.fk_entity || ctrl.field.name === 'id') { |
1e313889 | 102 | return '~/crmSearchTasks/crmSearchInput/entityRef.html'; |
014174e7 CW |
103 | } |
104 | ||
105 | if (ctrl.field.data_type === 'Integer') { | |
1e313889 | 106 | return '~/crmSearchTasks/crmSearchInput/integer.html'; |
014174e7 CW |
107 | } |
108 | ||
6d3a6960 | 109 | if (ctrl.field.data_type === 'Float') { |
1e313889 | 110 | return '~/crmSearchTasks/crmSearchInput/float.html'; |
6d3a6960 CW |
111 | } |
112 | ||
1e313889 | 113 | return '~/crmSearchTasks/crmSearchInput/text.html'; |
014174e7 CW |
114 | }; |
115 | ||
116 | this.getFieldOptions = function() { | |
117 | return {results: formatForSelect2(ctrl.field.options, ctrl.optionKey || 'id', 'label', ['description', 'color', 'icon'])}; | |
118 | }; | |
119 | ||
120 | } | |
121 | }); | |
122 | ||
123 | })(angular, CRM.$, CRM._); |