|
| 1 | +/** |
| 2 | + * angular-steps |
| 3 | + * @version v0.1.0 - 2014-07-10 |
| 4 | + * @link https://github.com/omichelsen/angular-steps |
| 5 | + * @author Ole Michelsen <[email protected]> |
| 6 | + * @license MIT License, http://www.opensource.org/licenses/MIT |
| 7 | + */ |
| 8 | + (function () { |
| 9 | + |
| 10 | + angular.module('templates-angular-steps', ['step.html', 'steps.html']); |
| 11 | + |
| 12 | + angular.module('step.html', []).run(['$templateCache', |
| 13 | + function($templateCache) { |
| 14 | + $templateCache.put('step.html', '<div ng-show=\"selected\" class=\"step ng-hide\" ng-transclude></div>'); |
| 15 | + } |
| 16 | + ]); |
| 17 | + |
| 18 | + angular.module('steps.html', []).run(['$templateCache', |
| 19 | + function($templateCache) { |
| 20 | + $templateCache.put('steps.html', |
| 21 | + '<div class=\"angular-steps\">\n' + |
| 22 | + ' <div class=\"steps\" ng-transclude></div>\n' + |
| 23 | + '</div>'); |
| 24 | + } |
| 25 | + ]); |
| 26 | + |
| 27 | + angular.module('angular-steps', ['templates-angular-steps']); |
| 28 | + |
| 29 | + angular.module('angular-steps').directive('step', function() { |
| 30 | + return { |
| 31 | + restrict: 'EA', |
| 32 | + replace: true, |
| 33 | + transclude: true, |
| 34 | + scope: { |
| 35 | + name: '@' |
| 36 | + }, |
| 37 | + require: '^steps', |
| 38 | + templateUrl: function(element, attributes) { |
| 39 | + return attributes.template || 'step.html'; |
| 40 | + }, |
| 41 | + link: function($scope, $element, $attrs, steps) { |
| 42 | + steps.addStep($scope); |
| 43 | + } |
| 44 | + }; |
| 45 | + }); |
| 46 | + |
| 47 | + angular.module('angular-steps').directive('steps', function() { |
| 48 | + return { |
| 49 | + restrict: 'EA', |
| 50 | + replace: true, |
| 51 | + transclude: true, |
| 52 | + scope: { |
| 53 | + currentStep: '=', |
| 54 | + onFinish: '&', |
| 55 | + name: '@' |
| 56 | + }, |
| 57 | + templateUrl: function(element, attributes) { |
| 58 | + return attributes.template || 'steps.html'; |
| 59 | + }, |
| 60 | + controller: ['$scope', '$element', 'StepsService', |
| 61 | + function($scope, $element, StepsService) { |
| 62 | + |
| 63 | + StepsService.addSteps($scope.name || StepsService.defaultName, this); |
| 64 | + $scope.$on('$destroy', function() { |
| 65 | + StepsService.removeSteps($scope.name || StepsService.defaultName); |
| 66 | + }); |
| 67 | + |
| 68 | + $scope.steps = []; |
| 69 | + |
| 70 | + $scope.$watch('currentStep', function(step) { |
| 71 | + if (!step) return; |
| 72 | + var stepName = $scope.selectedStep.name; |
| 73 | + if ($scope.selectedStep && stepName !== $scope.currentStep) { |
| 74 | + var found = $scope.steps.filter(function (elm) { |
| 75 | + return elm.name === $scope.currentStep; |
| 76 | + })[0]; |
| 77 | + $scope.goTo(found); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + this.addStep = function(step) { |
| 82 | + $scope.steps.push(step); |
| 83 | + if ($scope.steps.length === 1) { |
| 84 | + $scope.goTo($scope.steps[0]); |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + $scope.goTo = function(step) { |
| 89 | + unselectAll(); |
| 90 | + $scope.selectedStep = step; |
| 91 | + if ($scope.currentStep !== void 0) { |
| 92 | + $scope.currentStep = step.name; |
| 93 | + } |
| 94 | + step.selected = true; |
| 95 | + }; |
| 96 | + |
| 97 | + function unselectAll() { |
| 98 | + $scope.steps.forEach(function(step) { |
| 99 | + step.selected = false; |
| 100 | + }); |
| 101 | + $scope.selectedStep = null; |
| 102 | + } |
| 103 | + |
| 104 | + this.next = function() { |
| 105 | + var index = $scope.steps.indexOf($scope.selectedStep); |
| 106 | + if (index === $scope.steps.length - 1) { |
| 107 | + this.finish(); |
| 108 | + } else { |
| 109 | + $scope.goTo($scope.steps[index + 1]); |
| 110 | + } |
| 111 | + }; |
| 112 | + |
| 113 | + this.goTo = function(step) { |
| 114 | + var stepTo; |
| 115 | + if (isNaN(step)) { |
| 116 | + stepTo = $scope.steps.filter(function (elm) { |
| 117 | + return elm.name === step; |
| 118 | + })[0]; |
| 119 | + } else { |
| 120 | + stepTo = $scope.steps[step]; |
| 121 | + } |
| 122 | + $scope.goTo(stepTo); |
| 123 | + }; |
| 124 | + |
| 125 | + this.finish = function() { |
| 126 | + if ($scope.onFinish) { |
| 127 | + $scope.onFinish(); |
| 128 | + } |
| 129 | + }; |
| 130 | + |
| 131 | + this.cancel = this.previous = function() { |
| 132 | + var index = $scope.steps.indexOf($scope.selectedStep); |
| 133 | + if (index === 0) { |
| 134 | + throw new Error('Already at step 0'); |
| 135 | + } else { |
| 136 | + $scope.goTo($scope.steps[index - 1]); |
| 137 | + } |
| 138 | + }; |
| 139 | + } |
| 140 | + ] |
| 141 | + }; |
| 142 | + }); |
| 143 | + |
| 144 | + function stepsButtonDirective(action) { |
| 145 | + angular.module('angular-steps') |
| 146 | + .directive(action, function() { |
| 147 | + return { |
| 148 | + restrict: 'A', |
| 149 | + replace: false, |
| 150 | + require: '^steps', |
| 151 | + link: function($scope, $element, $attrs, steps) { |
| 152 | + $element.on('click', function(e) { |
| 153 | + e.preventDefault(); |
| 154 | + $scope.$apply(function() { |
| 155 | + $scope.$eval($attrs[action]); |
| 156 | + steps[action.replace('step', '').toLowerCase()](); |
| 157 | + }); |
| 158 | + }); |
| 159 | + } |
| 160 | + }; |
| 161 | + }); |
| 162 | + } |
| 163 | + |
| 164 | + stepsButtonDirective('stepNext'); |
| 165 | + stepsButtonDirective('stepPrevious'); |
| 166 | + stepsButtonDirective('stepFinish'); |
| 167 | + stepsButtonDirective('stepCancel'); |
| 168 | + |
| 169 | + angular.module('angular-steps').factory('StepsService', function() { |
| 170 | + var service = {}; |
| 171 | + |
| 172 | + var instances = {}; |
| 173 | + |
| 174 | + service.defaultName = 'default'; |
| 175 | + |
| 176 | + service.addSteps = function(name, steps) { |
| 177 | + instances[name] = steps; |
| 178 | + }; |
| 179 | + |
| 180 | + service.removeSteps = function(name) { |
| 181 | + delete instances[name]; |
| 182 | + }; |
| 183 | + |
| 184 | + service.steps = function(name) { |
| 185 | + return instances[name || service.defaultName]; |
| 186 | + }; |
| 187 | + |
| 188 | + return service; |
| 189 | + }); |
| 190 | + |
| 191 | +})(); |
0 commit comments