Search ext & APIv4 Explorer: Refactor clause from directive to component
[civicrm-core.git] / ext / search / ang / search / crmSearchClause.component.js
CommitLineData
9b057f1e
CW
1(function(angular, $, _) {
2 "use strict";
3
4 angular.module('search').component('crmSearchClause', {
5 bindings: {
6 fields: '<',
7 clauses: '<',
8 format: '@',
9 op: '@',
10 skip: '<',
11 label: '@',
12 deleteGroup: '&'
13 },
14 templateUrl: '~/search/crmSearchClause.html',
15 controller: function ($scope, $element, $timeout) {
16 var ts = $scope.ts = CRM.ts(),
17 ctrl = this;
18 this.conjunctions = {AND: ts('And'), OR: ts('Or'), NOT: ts('Not')};
19 this.operators = CRM.vars.search.operators;
20 this.sortOptions = {
21 axis: 'y',
22 connectWith: '.api4-clause-group-sortable',
23 containment: $element.closest('.api4-clause-fieldset'),
24 over: onSortOver,
25 start: onSort,
26 stop: onSort
27 };
28
29 this.$onInit = function() {
30 ctrl.hasParent = !!$element.attr('delete-group');
31 };
32
33 this.addGroup = function(op) {
34 ctrl.clauses.push([op, []]);
35 };
36
37 function onSort(event, ui) {
38 $($element).closest('.api4-clause-fieldset').toggleClass('api4-sorting', event.type === 'sortstart');
39 $('.api4-input.form-inline').css('margin-left', '');
40 }
41
42 // Indent clause while dragging between nested groups
43 function onSortOver(event, ui) {
44 var offset = 0;
45 if (ui.sender) {
46 offset = $(ui.placeholder).offset().left - $(ui.sender).offset().left;
47 }
48 $('.api4-input.form-inline.ui-sortable-helper').css('margin-left', '' + offset + 'px');
49 }
50
51 this.addClause = function() {
52 $timeout(function() {
53 if (ctrl.newClause) {
54 ctrl.clauses.push([ctrl.newClause, '=', '']);
55 ctrl.newClause = null;
56 }
57 });
58 };
59
60 this.deleteRow = function(index) {
61 ctrl.clauses.splice(index, 1);
62 };
63
64 // Remove empty values
65 this.changeClauseField = function(clause, index) {
66 if (clause[0] === '') {
67 ctrl.deleteRow(index);
68 }
69 };
70
71 // Add/remove value if operator allows for one
72 this.changeClauseOperator = function(clause) {
73 if (_.contains(clause[1], 'NULL')) {
74 clause.length = 2;
75 } else if (clause.length === 2) {
76 clause.push('');
77 }
78 };
79
80 }
81 });
82
83})(angular, CRM.$, CRM._);