-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimefork.js
107 lines (80 loc) · 2.55 KB
/
timefork.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
// require: statenode.js deltaree.js
//(function(){
var body;
addEventListener('DOMContentLoaded', function(){
body = document.getElementsByTagName('body')[0];
}, false);
var defs = {
idleTime: 1000,
lastTime: new Date().getTime(),
canvas : undefined,
context : undefined,
style : {
TWO_PI : Math.PI*2,
diam : 5,
spacing : 4,
}
};
// TimeFork Constructor
var TimeFork = function( aName, callback ){
// Copy defaults to new instance
for(var i in defs){ this[i] = defs[i]; }
// Back reference to object, used in event calls to maintain scope
var origin = this;
this.name = aName;
this.callback = callback;
// Set first leaf in statetree
this.stateTree.propState = DeltaTree(null, {});
this.pointInHistory = this.stateTree;
return this;
};
TimeFork.prototype.stateTree = new StateNode({
parent : null,
ancestor : 0,
branches : [],
eventTime : defs.lastTime,
eventType : 'History began.',
eventData : null,
propState : null,
});
TimeFork.prototype.filter = function( event ){
clearTimeout( this.timeout );
var origin = this;
this.timeout = setTimeout( function(){ origin.recordState( event ); }, this.idleTime );
};
TimeFork.prototype.recordState = function( obj ){
var now = new Date().getTime();
this.pointInHistory = this.pointInHistory.branches[ this.pointInHistory.branches.length ] = new StateNode({
parent : this.pointInHistory,
ancestor : this.lastTime,
branches : [],
eventTime : now,
propState : this.pointInHistory.propState.fork(obj),
});
this.lastTime = now;
this.callback();
};
TimeFork.prototype.currentState = function() {
return this.pointInHistory.propState.toObj();
};
TimeFork.prototype.go = function(point){
this.pointInHistory = point;
this.callback();
}
TimeFork.prototype.back = function(){
this.go(this.pointInHistory.parent);
};
TimeFork.prototype.forward = function(fork){
fork = fork || this.pointInHistory.branches.length - 1;
if (fork < this.pointInHistory.branches.length){
this.go(this.pointInHistory.branches[fork]);
}
};
TimeFork.prototype.activeNodes = function(){
return this.pointInHistory.ancestors().concat(this.pointInHistory);
};
// Model instatiator
this.timeFork = function( aName, elem ){
return new TimeFork( aName, elem );
};
//})();