Merge pull request #19116 from eileenmcnaughton/pay_edit
[civicrm-core.git] / ext / search / ang / crmSearchActions / crmSearchInput / crmSearchInput.component.js
1 (function(angular, $, _) {
2 "use strict";
3
4 angular.module('crmSearchActions').component('crmSearchInput', {
5 bindings: {
6 field: '<',
7 'op': '<',
8 'format': '<',
9 'optionKey': '<'
10 },
11 require: {ngModel: 'ngModel'},
12 templateUrl: '~/crmSearchActions/crmSearchInput/crmSearchInput.html',
13 controller: function($scope) {
14 var ts = $scope.ts = CRM.ts(),
15 ctrl = this;
16
17 this.isMulti = function() {
18 // If there's a search operator, return `true` if the operator takes multiple values, else `false`
19 if (ctrl.op) {
20 return ctrl.op === 'IN' || ctrl.op === 'NOT IN';
21 }
22 // If no search operator this is an input for e.g. the bulk update action
23 // Return `true` if the field is multi-valued, else `null`
24 return ctrl.field.serialize || ctrl.field.data_type === 'Array' ? true : null;
25 };
26
27 this.$onInit = function() {
28
29 $scope.$watch('$ctrl.value', function() {
30 ctrl.ngModel.$setViewValue(ctrl.value);
31 });
32
33 // For the ON clause, string values must be quoted
34 ctrl.ngModel.$parsers.push(function(viewValue) {
35 return ctrl.format === 'json' && _.isString(viewValue) && viewValue.length ? JSON.stringify(viewValue) : viewValue;
36 });
37
38 // For the ON clause, unquote string values
39 ctrl.ngModel.$formatters.push(function(value) {
40 return ctrl.format === 'json' && _.isString(value) && value.length ? JSON.parse(value) : value;
41 });
42
43 this.ngModel.$render = function() {
44 ctrl.value = ctrl.ngModel.$viewValue;
45 };
46
47 };
48 }
49 });
50
51 })(angular, CRM.$, CRM._);