Search ext: Reorganize into searchAdmin and searchActions modules
[civicrm-core.git] / ext / search / ang / searchActions / crmSearchActions.component.js
similarity index 66%
rename from ext/search/ang/search/crmSearchActions.component.js
rename to ext/search/ang/searchActions/crmSearchActions.component.js
index 212b8d914f85b8acf26638da53fe8501cc756f49..a7bfb3cef55f7a9d922724b5494b0a150bce312f 100644 (file)
@@ -1,29 +1,44 @@
 (function(angular, $, _) {
   "use strict";
 
-  angular.module('search').component('crmSearchActions', {
+  angular.module('searchActions').component('crmSearchActions', {
     bindings: {
       entity: '<',
       refresh: '&',
       ids: '<'
     },
-    templateUrl: '~/search/crmSearchActions.html',
+    templateUrl: '~/searchActions/crmSearchActions.html',
     controller: function($scope, crmApi4, dialogService, searchMeta) {
       var ts = $scope.ts = CRM.ts(),
-        ctrl = this;
+        ctrl = this,
+        initialized = false,
+        unwatchIDs = $scope.$watch('$ctrl.ids.length', watchIDs);
 
-      this.$onInit = function() {
+      function watchIDs() {
+        if (ctrl.ids && ctrl.ids.length && !initialized) {
+          unwatchIDs();
+          initialized = true;
+          initialize();
+        }
+      }
+
+      function initialize() {
         var entityTitle = searchMeta.getEntity(ctrl.entity).titlePlural;
-        if (!ctrl.actions) {
-          var actions = _.transform(_.cloneDeep(CRM.vars.search.actions), function (actions, action) {
+        crmApi4(ctrl.entity, 'getActions', {
+          where: [['name', 'IN', ['update', 'delete']]],
+        }, ['name']).then(function(allowed) {
+          _.each(allowed, function(action) {
+            CRM.searchActions.tasks[action].entities.push(ctrl.entity);
+          });
+          var actions = _.transform(_.cloneDeep(CRM.searchActions.tasks), function(actions, action) {
             if (_.includes(action.entities, ctrl.entity)) {
               action.title = action.title.replace('%1', entityTitle);
               actions.push(action);
             }
           }, []);
           ctrl.actions = _.sortBy(actions, 'title');
-        }
-      };
+        });
+      }
 
       this.isActionAllowed = function(action) {
         return !action.number || $scope.eval('' + $ctrl.ids.length + action.number);