-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-wizard.js
executable file
·125 lines (110 loc) · 4.49 KB
/
angular-wizard.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
angular
.module('ngWizard', [])
.service('WizardPage', function(){
return function(key, displayName, pageLinks, disabled) {
var page = {};
page.key = key;
page.displayName = displayName;
page.prevPage = pageLinks.prev;
page.nextPage = pageLinks.next;
page.canNextPage = angular.isFunction(pageLinks.canNext) ? pageLinks.canNext : (function() { return pageLinks.canNext; });
page.onNextPage = pageLinks.onNext;
// Disable page links by default until they have been navigated to
if (angular.isDefined(disabled)) {
page.disabled = disabled;
} else {
page.disabled = true;
}
return page;
}
})
.service('Wizard', [ '$interval', function($interval){
return function(initPages, initDelay) {
var wizard = {};
// specified (or empty, if no pages were given) pages list
wizard.pages = initPages || [];
wizard.percentage = '0';
wizard.success = false;
// Bind-able state for "Next" button timer
wizard.nextIsAllowed = true;
wizard.currentTimer = 0;
// Private members to manage our "Next" button timer
var delay = initDelay || 0;
var nextButtonTimeout = null;
var startTimer = function () {
stopTimer();
wizard.nextIsAllowed = false;
if (delay > 0) {
nextButtonTimeout = $interval(function() {
if (wizard.currentTimer === 0) {
wizard.nextIsAllowed = true;
wizard.stopTimer();
return;
}
wizard.currentTimer = wizard.currentTimer - 1;
}, 1000, wizard.currentTimer = delay);
}
};
var stopTimer = function () {
$interval.cancel(nextButtonTimeout);
nextButtonTimeout = null;
};
// given a key or display name, return the first page that corresponds to it (if such pages any exist)
wizard.getPageByName = function(name) {
var ret = null;
angular.forEach(wizard.pages, function(value) {
if (name === value.key || name === value.displayName) {
ret = value;
return;
}
});
return ret;
};
// Change to the specified page, which may either be a page name, or the page itself
wizard.setCurrentPage = function(page) {
if (angular.isString(page)) {
page = wizard.getPageByName(page);
}
if (!page) {
return;
}
wizard.currentPage = page;
wizard.percentage = (wizard.pages.indexOf(wizard.currentPage) / (wizard.pages.length - 1)) * 100;
wizard.currentPage.disabled = false;
};
// Define next / back button functionality here
wizard.canPrevPage = function() {
return wizard.currentPage.prevPage != null;
};
wizard.canNextPage = function() {
return wizard.currentPage.nextPage != null && wizard.currentPage.canNextPage();
};
wizard.prevPage = function() {
if (angular.isFunction(wizard.currentPage.prevPage)) {
wizard.setCurrentPage(wizard.currentPage.prevPage());
} else {
wizard.setCurrentPage(wizard.currentPage.prevPage);
}
};
wizard.nextPage = function() {
var onNextPage = wizard.currentPage.onNextPage;
if (onNextPage && angular.isFunction(onNextPage)) {
onNextPage();
}
if (angular.isFunction(wizard.currentPage.nextPage)) {
wizard.setCurrentPage(wizard.currentPage.nextPage());
} else {
wizard.setCurrentPage(wizard.currentPage.nextPage);
}
startTimer();
};
// the current page that we are on (initialized to the start page)
angular.forEach(wizard.pages, function (value) {
if (value.prevPage === null) {
// We have found our initial wizard page!
wizard.currentPage = value;
}
});
return wizard;
}
}]);