Merge branch '5.31' into master
[civicrm-core.git] / ext / search / ang / searchActions / crmSearchActions.component.js
1 (function(angular, $, _) {
2 "use strict";
3
4 angular.module('searchActions').component('crmSearchActions', {
5 bindings: {
6 entity: '<',
7 refresh: '&',
8 ids: '<'
9 },
10 templateUrl: '~/searchActions/crmSearchActions.html',
11 controller: function($scope, crmApi4, dialogService, searchMeta) {
12 var ts = $scope.ts = CRM.ts(),
13 ctrl = this,
14 initialized = false,
15 unwatchIDs = $scope.$watch('$ctrl.ids.length', watchIDs);
16
17 function watchIDs() {
18 if (ctrl.ids && ctrl.ids.length && !initialized) {
19 unwatchIDs();
20 initialized = true;
21 initialize();
22 }
23 }
24
25 function initialize() {
26 var entityTitle = searchMeta.getEntity(ctrl.entity).title_plural;
27 crmApi4(ctrl.entity, 'getActions', {
28 where: [['name', 'IN', ['update', 'delete']]],
29 }, ['name']).then(function(allowed) {
30 _.each(allowed, function(action) {
31 CRM.searchActions.tasks[action].entities.push(ctrl.entity);
32 });
33 var actions = _.transform(_.cloneDeep(CRM.searchActions.tasks), function(actions, action) {
34 if (_.includes(action.entities, ctrl.entity)) {
35 action.title = action.title.replace('%1', entityTitle);
36 actions.push(action);
37 }
38 }, []);
39 ctrl.actions = _.sortBy(actions, 'title');
40 });
41 }
42
43 this.isActionAllowed = function(action) {
44 return !action.number || $scope.eval('' + $ctrl.ids.length + action.number);
45 };
46
47 this.doAction = function(action) {
48 if (!ctrl.isActionAllowed(action) || !ctrl.ids.length) {
49 return;
50 }
51 var data = {
52 ids: ctrl.ids,
53 entity: ctrl.entity
54 };
55 // If action uses a crmPopup form
56 if (action.crmPopup) {
57 var path = $scope.$eval(action.crmPopup.path, data),
58 query = action.crmPopup.query && $scope.$eval(action.crmPopup.query, data);
59 CRM.loadForm(CRM.url(path, query))
60 .on('crmFormSuccess', ctrl.refresh);
61 }
62 // If action uses dialogService
63 else if (action.uiDialog) {
64 var options = CRM.utils.adjustDialogDefaults({
65 autoOpen: false,
66 title: action.title
67 });
68 dialogService.open('crmSearchAction', action.uiDialog.templateUrl, data, options)
69 .then(ctrl.refresh);
70 }
71 };
72 }
73 });
74
75 })(angular, CRM.$, CRM._);