Merge pull request #18748 from eileenmcnaughton/log
[civicrm-core.git] / ext / search / ang / crmSearchActions / crmSearchActions.component.js
1 (function(angular, $, _) {
2 "use strict";
3
4 angular.module('crmSearchActions').component('crmSearchActions', {
5 bindings: {
6 entity: '<',
7 refresh: '&',
8 ids: '<'
9 },
10 templateUrl: '~/crmSearchActions/crmSearchActions.html',
11 controller: function($scope, crmApi4, dialogService) {
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 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) {
32 CRM.crmSearchActions.tasks[action].entities.push(ctrl.entity);
33 });
34 var actions = _.transform(_.cloneDeep(CRM.crmSearchActions.tasks), function(actions, action) {
35 if (_.includes(action.entities, ctrl.entity)) {
36 action.title = action.title.replace('%1', ctrl.entityInfo.title_plural);
37 actions.push(action);
38 }
39 }, []);
40 ctrl.actions = _.sortBy(actions, 'title');
41 });
42 }
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,
54 entity: ctrl.entity,
55 entityInfo: ctrl.entityInfo
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))
62 .on('crmFormSuccess', ctrl.refresh);
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)
71 .then(ctrl.refresh);
72 }
73 };
74 }
75 });
76
77 })(angular, CRM.$, CRM._);