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