CRM-15578 - crmUiWizard - Allow conditional steps
authorTim Otten <totten@civicrm.org>
Sun, 14 Dec 2014 07:25:44 +0000 (23:25 -0800)
committerTim Otten <totten@civicrm.org>
Sun, 14 Dec 2014 22:55:40 +0000 (14:55 -0800)
js/angular-crm-ui.js

index 72c57d33c65a52fccd824bbfb3886cfb6bd60e7a..8bd92becbb58e203cfdf8ccd87b259bbb1d6719c 100644 (file)
               selectedIndex = 0;
             }
             steps.push(step);
+            steps.sort(function(a,b){
+              return a.crmUiWizardStep - b.crmUiWizardStep;
+            });
+            selectedIndex = findIndex();
+          };
+          this.remove = function(step) {
+            var key = null;
+            angular.forEach(steps, function(otherStep, otherKey) {
+              if (otherStep === step) key = otherKey;
+            });
+            if (key != null) {
+              steps.splice(key, 1);
+            }
           };
           this.goto = function(index) {
             if (index < 0) index = 0;
       };
     })
 
-    // example <div crm-ui-wizard-step crm-title="ts('My Title')">...content...</div>
+    // example: <div crm-ui-wizard-step crm-title="ts('My Title')">...content...</div>
+    // If there are any conditional steps, then be sure to set a weight explicitly on *all* steps to maintain ordering.
+    // example: <div crm-ui-wizard-step="100" crm-title="..." ng-if="...">...content...</div>
     .directive('crmUiWizardStep', function() {
+      var nextWeight = 1;
       return {
         require: '^crmUiWizard',
         restrict: 'EA',
         scope: {
-          crmTitle: '@'
+          crmTitle: '@', // expression, evaluates to a printable string
+          crmUiWizardStep: '@' // int, a weight which determines the ordering of the steps
         },
         template: '<div class="crm-wizard-step" ng-show="selected" ng-transclude/></div>',
         transclude: true,
         link: function (scope, element, attrs, crmUiWizardCtrl) {
+          if (scope.crmUiWizardStep) {
+            scope.crmUiWizardStep = parseInt(scope.crmUiWizardStep);
+          } else {
+            scope.crmUiWizardStep = nextWeight++;
+          }
           crmUiWizardCtrl.add(scope);
+          element.on('$destroy', function(){
+            crmUiWizardCtrl.remove(scope);
+          });
         }
       };
     })