forked from jeresig/env-js
/
history.js
76 lines (71 loc) · 2.03 KB
/
history.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
/*
* history.js
*
*/
History = function(owner) {
var $current = 0,
$history = [null],
$owner = owner;
return {
go : function(target) {
if (typeof target == "number") {
target = $current + target;
if (target > -1 && target < $history.length){
if ($history[target].type == "hash") {
if ($owner.location) {
$owner.location.hash = $history[target].value;
}
} else {
if ($owner.location) {
$owner.location = $history[target].value;
}
}
$current = target;
}
} else {
//TODO: walk through the history and find the 'best match'?
}
},
get length() {
return $history.length;
},
back : function(count) {
if (count) {
this.go(-count);
} else {
this.go(-1);
}
},
get current() {
return this.item($current);
},
get previous() {
return this.item($current-1);
},
forward : function(count) {
if (count) {
this.go(count);
} else {
this.go(1);
}
},
item: function(index) {
if (index < history.length) {
return $history[index];
} else {
return null;
}
},
add: function(newLocation, type) {
//not a standard interface, we expose it to simplify
//history state modifications
if (newLocation !== $history[$current]) {
$history.slice(0, $current);
$history.push({
type: type||"href",
value: value
});
}
}
}; /* closes 'return {' */
};