CRM-14789 - crmCaseType - Allow enable/disable/delete
authorTim Otten <totten@civicrm.org>
Thu, 10 Jul 2014 07:53:38 +0000 (00:53 -0700)
committerTim Otten <totten@civicrm.org>
Thu, 10 Jul 2014 07:53:38 +0000 (00:53 -0700)
js/angular-crm-ui.js
js/angular-crmCaseType.js
partials/crmCaseType/list.html

index db8272200780c304ed0086d93a840c36020a2f0b..d144c377159e495a2ad455847c9dcec726f2c35b 100644 (file)
         }
       };
     })
+    // Example: <button crm-confirm="{message: ts('Frobincation is a footastical operation')}" on-yes="frobnicate(123)">Frobincate</button>
+    // Example: <button crm-confirm="{type: 'disable', obj: myObject}" on-yes="myObject.is_active=0; myObject.save()">Disable</button>
+    .directive('crmConfirm', function () {
+      // Helpers to calculate default options from CRM.confirm()
+      var defaultFuncs = {
+        'disable': function (options) {
+          return {
+            message: ts('Are you sure you want to disable this?'),
+            options: {no: ts('Cancel'), yes: ts('Disable')},
+            width: 300,
+            title: ts('Disable %1?', {
+              1: options.obj.title || options.obj.label || options.obj.name || ts('the record')
+            })
+          };
+        },
+        'delete': function (options) {
+          return {
+            message: ts('Are you sure you want to delete this?'),
+            options: {no: ts('Cancel'), yes: ts('Delete')},
+            width: 300,
+            title: ts('Delete %1?', {
+              1: options.obj.title || options.obj.label || options.obj.name || ts('the record')
+            })
+          };
+        }
+      };
+      return {
+        template: '',
+        link: function (scope, element, attrs) {
+          $(element).click(function () {
+            var options = scope.$eval(attrs['crmConfirm']);
+            var defaults = (options.type) ? defaultFuncs[options.type](options) : {};
+            CRM.confirm(_.extend(defaults, options))
+              .on('crmConfirm:yes', function () { scope.$apply(attrs['onYes']); })
+              .on('crmConfirm:no', function () { scope.$apply(attrs['onNo']); });
+          });
+        }
+      };
+    })
     .run(function($rootScope, $location) {
       /// Example: <button ng-click="goto('home')">Go home!</button>
       $rootScope.goto = function(path) {
         $location.path(path);
       };
+      // useful for debugging: $rootScope.log = console.log || function() {};
     })
   ;
 
index e617064d2b83e91f53ed2a45cd603f680697c9c6..131dd62f8767c75354c09ab88bf5c01988a0df1f 100644 (file)
 
   crmCaseType.controller('CaseTypeListCtrl', function($scope, crmApi, caseTypes) {
     $scope.caseTypes = caseTypes.values;
+    $scope.toggleCaseType = function (caseType) {
+      caseType.is_active = (caseType.is_active == '1') ? '0' : '1';
+      crmApi('CaseType', 'create', caseType, true)
+        .then(function (data) {
+          if (data.is_error) {
+            caseType.is_active = (caseType.is_active == '1') ? '0' : '1'; // revert
+            $scope.$digest();
+          }
+        });
+    };
+    $scope.deleteCaseType = function (caseType) {
+      crmApi('CaseType', 'delete', {id: caseType.id}, true)
+        .then(function (data) {
+          if (!data.is_error) {
+            delete caseTypes.values[caseType.id];
+            $scope.$digest();
+          }
+        });
+    };
   });
 
 })(angular, CRM.$, CRM._);
\ No newline at end of file
index ee4fc37df3a4cd7606f41ab200acff46659561f4..316d9a5eaa818b24d213c7fe4b8bfea1be2bf864 100644 (file)
@@ -17,13 +17,37 @@ Required vars: caseTypes
          </tr>
   </thead>
        <tbody>
-         <tr ng-repeat="caseType in caseTypes" ng-class-even="'crm-entity even-row even'" ng-class-odd="'crm-entity odd-row odd'">
+         <tr ng-repeat="caseType in caseTypes"
+        class="crm-entity"
+        ng-class-even="'even-row even'"
+        ng-class-odd="'odd-row odd'"
+        ng-class="{disabled: 0==caseType.is_active}">
            <td>{{caseType.title}}</td>
            <td>{{caseType.name}}</td>
            <td>{{caseType.description}}</td>
            <td>{{caseType.is_active == 1 ? 'Yes' : 'No'}}</td>
-           <td><a ng-href="#/caseType/{{caseType.id}}">Edit</a></td>
-       </tr>
+           <!-- FIXME: Can't figure out how styling in other tables gets the nowrap effect... in absence of a consistent fix, KISS -->
+           <td style="white-space: nowrap">
+             <span>
+               <a class="action-item crm-hover-button" ng-href="#/caseType/{{caseType.id}}">Edit</a>
+
+               <span class="btn-slide crm-hover-button">
+                 more
+                 <ul class="panel" style="display: none;">
+                   <li ng-hide="caseType.is_active">
+                     <a class="action-item crm-hover-button" ng-click="toggleCaseType(caseType)">Enable</a>
+                   </li>
+                   <li ng-show="caseType.is_active">
+                <a class="action-item crm-hover-button" crm-confirm="{type: 'disable', obj: caseType}" on-yes="toggleCaseType(caseType)">Disable</a>
+                   </li>
+                   <li>
+                <a class="action-item crm-hover-button" crm-confirm="{type: 'delete', obj: caseType}" on-yes="deleteCaseType(caseType)">Delete</a>
+                   </li>
+                 </ul>
+               </span>
+             </span>
+           </td>
+         </tr>
        </tbody>
 </table>