1b2c45832d85c5b2ee7f56b0ff1510f46feb0281
[civicrm-core.git] / ext / search / ang / search / crmSearchFunction.component.js
1 (function(angular, $, _) {
2 "use strict";
3
4 angular.module('search').component('crmSearchFunction', {
5 bindings: {
6 expr: '=',
7 cat: '<'
8 },
9 templateUrl: '~/search/crmSearchFunction.html',
10 controller: function($scope, formatForSelect2, searchMeta) {
11 var ts = $scope.ts = CRM.ts(),
12 ctrl = this;
13
14 this.$onInit = function() {
15 ctrl.functions = formatForSelect2(_.where(CRM.vars.search.functions, {category: ctrl.cat}), 'name', 'title');
16 var fieldInfo = searchMeta.parseExpr(ctrl.expr);
17 ctrl.path = fieldInfo.path + fieldInfo.suffix;
18 ctrl.field = fieldInfo.field;
19 ctrl.fn = !fieldInfo.fn ? '' : fieldInfo.fn.name;
20 ctrl.modifier = fieldInfo.modifier || null;
21 initFunction();
22 };
23
24 function initFunction() {
25 ctrl.fnInfo = _.find(CRM.vars.search.functions, {name: ctrl.fn});
26 if (ctrl.fnInfo && _.includes(ctrl.fnInfo.params[0].prefix, 'DISTINCT')) {
27 ctrl.modifierAllowed = true;
28 }
29 else {
30 ctrl.modifierAllowed = false;
31 ctrl.modifier = null;
32 }
33 }
34
35 this.selectFunction = function() {
36 initFunction();
37 ctrl.writeExpr();
38 };
39
40 this.writeExpr = function() {
41 ctrl.expr = ctrl.fn ? (ctrl.fn + '(' + (ctrl.modifier ? ctrl.modifier + ' ' : '') + ctrl.path + ')') : ctrl.path;
42 };
43 }
44 });
45
46 })(angular, CRM.$, CRM._);