de5ce60e43d8c1b0e222941f0d179fa76c9063f2
[civicrm-core.git] / ext / search / ang / crmSearchAdmin / crmSearchClause.component.js
1 (function(angular, $, _) {
2 "use strict";
3
4 angular.module('crmSearchAdmin').component('crmSearchClause', {
5 bindings: {
6 fields: '<',
7 clauses: '<',
8 format: '@',
9 op: '@',
10 skip: '<',
11 label: '@',
12 deleteGroup: '&'
13 },
14 templateUrl: '~/crmSearchAdmin/crmSearchClause.html',
15 controller: function ($scope, $element, $timeout, searchMeta) {
16 var ts = $scope.ts = CRM.ts(),
17 ctrl = this,
18 meta = {};
19 this.conjunctions = {AND: ts('And'), OR: ts('Or'), NOT: ts('Not')};
20 this.operators = CRM.crmSearchAdmin.operators;
21 this.sortOptions = {
22 axis: 'y',
23 connectWith: '.api4-clause-group-sortable',
24 containment: $element.closest('.api4-clause-fieldset'),
25 over: onSortOver,
26 start: onSort,
27 stop: onSort
28 };
29
30 this.$onInit = function() {
31 ctrl.hasParent = !!$element.attr('delete-group');
32 };
33
34 this.getField = function(expr) {
35 if (!meta[expr]) {
36 meta[expr] = searchMeta.parseExpr(expr);
37 }
38 return meta[expr].field;
39 };
40
41 this.getOptionKey = function(expr) {
42 if (!meta[expr]) {
43 meta[expr] = searchMeta.parseExpr(expr);
44 }
45 return meta[expr].suffix ? meta[expr].suffix.slice(1) : 'id';
46 };
47
48 this.addGroup = function(op) {
49 ctrl.clauses.push([op, []]);
50 };
51
52 function onSort(event, ui) {
53 $($element).closest('.api4-clause-fieldset').toggleClass('api4-sorting', event.type === 'sortstart');
54 $('.api4-input.form-inline').css('margin-left', '');
55 }
56
57 // Indent clause while dragging between nested groups
58 function onSortOver(event, ui) {
59 var offset = 0;
60 if (ui.sender) {
61 offset = $(ui.placeholder).offset().left - $(ui.sender).offset().left;
62 }
63 $('.api4-input.form-inline.ui-sortable-helper').css('margin-left', '' + offset + 'px');
64 }
65
66 this.addClause = function() {
67 $timeout(function() {
68 if (ctrl.newClause) {
69 ctrl.clauses.push([ctrl.newClause, '=', '']);
70 ctrl.newClause = null;
71 }
72 });
73 };
74
75 this.deleteRow = function(index) {
76 ctrl.clauses.splice(index, 1);
77 };
78
79 // Remove empty values
80 this.changeClauseField = function(clause, index) {
81 if (clause[0] === '') {
82 ctrl.deleteRow(index);
83 }
84 };
85
86 this.changeClauseOperator = function(clause) {
87 // Add/remove value if operator allows for one
88 if (_.contains(clause[1], 'NULL')) {
89 clause.length = 2;
90 } else {
91 if (clause.length === 2) {
92 clause.push('');
93 }
94 // Change multi/single value to/from an array
95 var shouldBeArray = (clause[1] === 'IN' || clause[1] === 'NOT IN' || clause[1] === 'BETWEEN' || clause[1] === 'NOT BETWEEN');
96 if (!_.isArray(clause[2]) && shouldBeArray) {
97 clause[2] = [];
98 } else if (_.isArray(clause[2]) && !shouldBeArray) {
99 clause[2] = '';
100 }
101 if (clause[1] === 'BETWEEN' || clause[1] === 'NOT BETWEEN') {
102 clause[2].length = 2;
103 }
104 }
105 };
106
107 }
108 });
109
110 })(angular, CRM.$, CRM._);