Commit | Line | Data |
---|---|---|
25523059 CW |
1 | (function(angular, $, _) { |
2 | "use strict"; | |
3 | ||
4 | angular.module('search').component('crmSearchActions', { | |
5 | bindings: { | |
6 | entity: '<', | |
7 | ids: '<' | |
8 | }, | |
9 | require: { | |
b0422f12 | 10 | search: '^crmSearch' |
25523059 CW |
11 | }, |
12 | templateUrl: '~/search/crmSearchActions.html', | |
13 | controller: function($scope, crmApi4, dialogService, searchMeta) { | |
14 | var ts = $scope.ts = CRM.ts(), | |
15 | entityTitle = searchMeta.getEntity(this.entity).title, | |
16 | ctrl = this; | |
17 | ||
18 | this.init = function() { | |
19 | if (!ctrl.actions) { | |
20 | var actions = _.transform(_.cloneDeep(CRM.vars.search.actions), function (actions, action) { | |
21 | if (_.includes(action.entities, ctrl.entity)) { | |
22 | action.title = action.title.replace('%1', entityTitle); | |
23 | actions.push(action); | |
24 | } | |
25 | }, []); | |
26 | ctrl.actions = _.sortBy(actions, 'title'); | |
27 | } | |
28 | }; | |
29 | ||
30 | this.isActionAllowed = function(action) { | |
31 | return !action.number || $scope.eval('' + $ctrl.ids.length + action.number); | |
32 | }; | |
33 | ||
34 | this.doAction = function(action) { | |
35 | if (!ctrl.isActionAllowed(action) || !ctrl.ids.length) { | |
36 | return; | |
37 | } | |
38 | var data = { | |
39 | ids: ctrl.ids, | |
40 | entity: ctrl.entity | |
41 | }; | |
42 | // If action uses a crmPopup form | |
43 | if (action.crmPopup) { | |
44 | var path = $scope.$eval(action.crmPopup.path, data), | |
45 | query = action.crmPopup.query && $scope.$eval(action.crmPopup.query, data); | |
46 | CRM.loadForm(CRM.url(path, query)) | |
47 | .on('crmFormSuccess', ctrl.search.refreshPage); | |
48 | } | |
49 | // If action uses dialogService | |
50 | else if (action.uiDialog) { | |
51 | var options = CRM.utils.adjustDialogDefaults({ | |
52 | autoOpen: false, | |
53 | title: action.title | |
54 | }); | |
55 | dialogService.open('crmSearchAction', action.uiDialog.templateUrl, data, options) | |
56 | .then(ctrl.search.refreshPage); | |
57 | } | |
58 | }; | |
59 | } | |
60 | }); | |
61 | ||
62 | })(angular, CRM.$, CRM._); |