forked from smsohan/angular_wizard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
98 lines (72 loc) · 2.09 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
var myApp = angular.module('myApp', []);
myApp.controller('WizardController', ['$scope', function($scope){
$scope.log = function(event){
console.log(event);
}
$scope.user = {};
}]);
myApp.directive('wizard', function(){
return {
restrict: 'E',
transclude: true,
scope: {
onBeforeStepChange: '&',
onStepChanging: '&',
onAfterStepChange: '&',
},
templateUrl: 'wizard.html',
replace: true,
link: function(scope){
scope.currentStepIndex = 0;
scope.steps[scope.currentStepIndex].currentStep = true;
},
controller: function($scope){
$scope.steps = [];
this.registerStep = function(step){
$scope.steps.push(step);
}
var toggleSteps = function(showIndex){
var event = {event: {fromStep: $scope.currentStepIndex, toStep: showIndex}};
if($scope.onBeforeStepChange){
$scope.onBeforeStepChange(event);
}
$scope.steps[$scope.currentStepIndex].currentStep = false;
if($scope.onStepChanging){
$scope.onStepChanging(event);
}
$scope.currentStepIndex = showIndex;
$scope.steps[$scope.currentStepIndex].currentStep = true;
if($scope.onAfterStepChange){
$scope.onAfterStepChange(event);
}
}
$scope.showNextStep = function(){
toggleSteps($scope.currentStepIndex + 1);
}
$scope.showPreviousStep = function(){
toggleSteps($scope.currentStepIndex - 1);
}
$scope.hasNext = function(){
return $scope.currentStepIndex < ($scope.steps.length - 1);
}
$scope.hasPrevious = function(){
return $scope.currentStepIndex > 0;
}
}
};
});
myApp.directive('step', function(){
return {
require: "^wizard",
restrict: 'E',
transclude: true,
scope: {
title: '@'
},
template: '<div class="step" ng-show="currentStep"><h2>{{title}}</h2> <div ng-transclude></div> </div>',
replace: true,
link: function(scope, element, attrs, wizardController){
wizardController.registerStep(scope);
}
};
});