Search ext: group tab
[civicrm-core.git] / ext / search / ang / searchActions / crmSearchActionUpdate.ctrl.js
1 (function(angular, $, _) {
2 "use strict";
3
4 angular.module('searchActions').controller('crmSearchActionUpdate', function ($scope, $timeout, crmApi4, dialogService, searchMeta) {
5 var ts = $scope.ts = CRM.ts(),
6 model = $scope.model,
7 ctrl = $scope.$ctrl = this;
8
9 this.entity = searchMeta.getEntity(model.entity);
10 this.entityTitle = model.ids.length === 1 ? this.entity.title : this.entity.titlePlural;
11 this.values = [];
12 this.add = null;
13
14 function fieldInUse(fieldName) {
15 return _.includes(_.collect(ctrl.values, 0), fieldName);
16 }
17
18 this.updateField = function(index) {
19 // Debounce the onchange event using timeout
20 $timeout(function() {
21 if (!ctrl.values[index][0]) {
22 ctrl.values.splice(index, 1);
23 }
24 });
25 };
26
27 this.addField = function() {
28 // Debounce the onchange event using timeout
29 $timeout(function() {
30 if (ctrl.add) {
31 ctrl.values.push([ctrl.add, '']);
32 }
33 ctrl.add = null;
34 });
35 };
36
37 this.availableFields = function() {
38 var results = _.transform(ctrl.entity.fields, function(result, item) {
39 var formatted = {id: item.name, text: item.label, description: item.description};
40 if (fieldInUse(item.name)) {
41 formatted.disabled = true;
42 }
43 if (item.name !== 'id') {
44 result.push(formatted);
45 }
46 }, []);
47 return {results: results};
48 };
49
50 this.cancel = function() {
51 dialogService.cancel('crmSearchAction');
52 };
53
54 this.save = function() {
55 crmApi4(model.entity, 'Update', {
56 where: [['id', 'IN', model.ids]],
57 values: _.zipObject(ctrl.values)
58 }).then(function() {
59 dialogService.close('crmSearchAction');
60 });
61 };
62
63 });
64 })(angular, CRM.$, CRM._);