|
| 1 | +/* globals _:false */ |
| 2 | +'use strict'; |
| 3 | +angular.module('angularify.semantic.wizard', []) |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +.controller('WizardController', ['$scope', |
| 8 | + function($scope) { |
| 9 | + $scope.steps = []; |
| 10 | + $scope.currentStep = null; |
| 11 | + $scope.stepsLength = ""; |
| 12 | + $scope.$watch('currentStep', function(step) { |
| 13 | + if (!step) return; |
| 14 | + var stepTitle = $scope.selectedStep.title || $scope.selectedStep.wzTitle; |
| 15 | + if ($scope.selectedStep && stepTitle !== $scope.currentStep) { |
| 16 | + $scope.goTo(_.findWhere($scope.steps, { |
| 17 | + title: $scope.currentStep |
| 18 | + })); |
| 19 | + } |
| 20 | + }); |
| 21 | + |
| 22 | + $scope.$watch('[editMode, steps.length]', function() { |
| 23 | + var editMode = $scope.editMode; |
| 24 | + if (_.isUndefined(editMode) || _.isNull(editMode)) return; |
| 25 | + |
| 26 | + if (editMode) { |
| 27 | + _.each($scope.steps, function(step) { |
| 28 | + step.completed = true; |
| 29 | + }); |
| 30 | + } |
| 31 | + }, true); |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + this.addStep = function(step) { |
| 37 | + $scope.steps.push(step); |
| 38 | + if ($scope.steps.length === 1) { |
| 39 | + $scope.goTo($scope.steps[0]); |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + $scope.goTo = function(step) { |
| 44 | + unselectAll(); |
| 45 | + $scope.selectedStep = step; |
| 46 | + if (!_.isUndefined($scope.currentStep)) { |
| 47 | + $scope.currentStep = step.title || step.wzTitle; |
| 48 | + } |
| 49 | + step.selected = true; |
| 50 | + $scope.$emit('wizard:stepChanged', { |
| 51 | + step: step, |
| 52 | + index: _.indexOf($scope.steps, step) |
| 53 | + }); |
| 54 | + }; |
| 55 | + |
| 56 | + function unselectAll() { |
| 57 | + _.each($scope.steps, function(step) { |
| 58 | + step.selected = false; |
| 59 | + }); |
| 60 | + $scope.selectedStep = null; |
| 61 | + } |
| 62 | + |
| 63 | + this.next = function(draft) { |
| 64 | + var index = _.indexOf($scope.steps, $scope.selectedStep); |
| 65 | + if (!draft) { |
| 66 | + $scope.selectedStep.completed = true; |
| 67 | + } |
| 68 | + if (index === $scope.steps.length - 1) { |
| 69 | + this.finish(); |
| 70 | + } else { |
| 71 | + $scope.goTo($scope.steps[index + 1]); |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + this.goTo = function(step) { |
| 76 | + var stepTo; |
| 77 | + if (_.isNumber(step)) { |
| 78 | + stepTo = $scope.steps[step]; |
| 79 | + } else { |
| 80 | + stepTo = _.findWhere($scope.steps, { |
| 81 | + title: step |
| 82 | + }); |
| 83 | + } |
| 84 | + $scope.goTo(stepTo); |
| 85 | + }; |
| 86 | + |
| 87 | + this.finish = function() { |
| 88 | + if ($scope.onFinish) { |
| 89 | + $scope.onFinish(); |
| 90 | + } |
| 91 | + }; |
| 92 | + |
| 93 | + this.cancel = this.previous = function() { |
| 94 | + var index = _.indexOf($scope.steps, $scope.selectedStep); |
| 95 | + if (index === 0) { |
| 96 | + throw new Error('Cant go back. Its already in step 0'); |
| 97 | + } else { |
| 98 | + $scope.goTo($scope.steps[index - 1]); |
| 99 | + } |
| 100 | + }; |
| 101 | + } |
| 102 | +]) |
| 103 | + .directive('wizard', function() { |
| 104 | + return { |
| 105 | + restrict: 'EA', |
| 106 | + replace: true, |
| 107 | + transclude: true, |
| 108 | + scope: { |
| 109 | + fullwidth: "@" |
| 110 | + }, |
| 111 | + controller: 'WizardController', |
| 112 | + template: '<div><div class="ui steps {{stepsLength}} small"><div class="ui step" ng-repeat="step in steps" ng-click="!step.completed || goTo(step)" ng-class="{disabled: !step.completed && !step.selected, active: step.selected && !step.completed, done: step.completed && !step.selected, editing: step.selected && step.completed}">{{step.title}}</div></div><div ng-transclude></div></div>', |
| 113 | + link: function(scope, element, attrs, WizardController) { |
| 114 | + if (scope.fullwidth === 'true') { |
| 115 | + var widthmatrix = { |
| 116 | + 0: '', |
| 117 | + 1: 'one', |
| 118 | + 2: 'two', |
| 119 | + 3: 'three', |
| 120 | + 4: 'four', |
| 121 | + 5: 'five', |
| 122 | + 6: 'six', |
| 123 | + 7: 'seven', |
| 124 | + 8: 'eight', |
| 125 | + 9: 'nine', |
| 126 | + 10: 'ten' |
| 127 | + }; |
| 128 | + scope.stepsLength = widthmatrix[scope.steps.length]; |
| 129 | + } |
| 130 | + |
| 131 | + } |
| 132 | + }; |
| 133 | + }) |
| 134 | + .directive('wizardPane', function() { |
| 135 | + return { |
| 136 | + restrict: 'EA', |
| 137 | + replace: true, |
| 138 | + transclude: true, |
| 139 | + require: '^wizard', |
| 140 | + controller: 'WizardController', |
| 141 | + scope: { |
| 142 | + title: '@' |
| 143 | + }, |
| 144 | + template: '<div class="ui segment" ng-transclude ng-show="selected"></div>', |
| 145 | + link: function(scope, element, attrs, WizardController) { |
| 146 | + |
| 147 | + WizardController.addStep(scope); |
| 148 | + } |
| 149 | + }; |
| 150 | + }); |
| 151 | + |
| 152 | + |
| 153 | +function wizardButtonDirective(action) { |
| 154 | + angular.module('angularify.semantic.wizard') |
| 155 | + .directive(action, function() { |
| 156 | + return { |
| 157 | + restrict: 'A', |
| 158 | + replace: false, |
| 159 | + require: '^wizard', |
| 160 | + link: function($scope, $element, $attrs, wizard) { |
| 161 | + |
| 162 | + $element.on('click', function(e) { |
| 163 | + e.preventDefault(); |
| 164 | + $scope.$apply(function() { |
| 165 | + $scope.$eval($attrs[action]); |
| 166 | + wizard[action.replace("wz", "").toLowerCase()](); |
| 167 | + }); |
| 168 | + }); |
| 169 | + } |
| 170 | + }; |
| 171 | + }); |
| 172 | +} |
| 173 | + |
| 174 | +wizardButtonDirective('wzNext'); |
| 175 | +wizardButtonDirective('wzPrevious'); |
| 176 | +wizardButtonDirective('wzFinish'); |
| 177 | +wizardButtonDirective('wzCancel'); |
0 commit comments