/
index.js
84 lines (83 loc) · 1.77 KB
/
index.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
var Stepx = function(options) {
var stepx = this;
options = options || {};
stepx.onStart = options.onStart || function(){};
stepx.onFinish = options.onFinish || function(){};
stepx.index = -1;
stepx.steps = [];
stepx.getById = function(id) {
for (var i = 0; i < stepx.steps.length; i++) {
if (stepx.steps[i].id == id) {
return stepx.steps[i];
}
}
return false;
};
stepx.getAt = function(index) {
if (index < stepx.steps.length) {
return stepx.steps[index];
}
return false;
};
stepx.add = function(fn, id) {
var step;
if (id && (step = stepx.getById(id))) {
step.fn = fn;
} else {
step = {
id: id || 'step_'+stepx.steps.length,
index: stepx.steps.length,
fn: fn,
run: function() {
stepx.index = this.index;
if (typeof this.fn === 'function') {
this.fn.apply(this, arguments);
}
}
};
stepx.steps.push(step);
}
return this;
};
stepx.goto = function(index) {
if (typeof index == 'string') {
return stepx.gotoId(index);
}
var step;
if (step = stepx.getAt(index)) {
step.run();
}
return this;
};
stepx.gotoId = function(id) {
var step;
if (step = stepx.getById(id)) {
step.run();
}
return this;
};
stepx.next = function() {
if (stepx.index < stepx.steps.length-1){
var step = stepx.getAt(stepx.index+1);
step.run.apply(step, arguments);
} else {
stepx.finish.apply(stepx, arguments);
}
return this;
};
stepx.finish = function() {
if (typeof stepx.onFinish === 'function') {
stepx.onFinish.apply(stepx, arguments);
}
return;
};
stepx.start = function() {
if (typeof stepx.onStart === 'function') {
stepx.onStart.apply(stepx, arguments);
}
stepx.index = -1;
stepx.next.apply(stepx, arguments);
};
return stepx;
};
module.exports = Stepx;