-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNdeHistory.js
53 lines (41 loc) · 1.16 KB
/
NdeHistory.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
function NdeHistory() {
var history = [];
this.position = 0;
this.push = function(path) {
if ( path === history[this.position] ) {
// do nothing
} else if ( path === history[this.position-1] ) {
// set history to previous point
this.position = this.position - 1;
} else if ( path === history[this.position+1] ) {
// set history to next point
this.position = this.position + 1;
} else {
// add item to history
// set history to head
// remove all old history between ( 0 and currentPos )
if ( this.position !== 0 )
history = history.slice(this.position, history.length);
history.unshift(path);
this.position = 0;
}
}.bind(this);
this.getPrevious = function() {
var pos = Math.min(this.position+1, history.length -1);
return history[pos];
}.bind(this);
this.getNext = function() {
var pos = Math.max(this.position-1, 0);
return history[pos];
}.bind(this);
this.hasHistory = function() {
return history.length > 1;
};
this.hasNext = function() {
return this.position > 0;
}.bind(this);
this.hasPrevious = function() {
return this.position < history.length -1;
}.bind(this);
}
module.exports = NdeHistory;