forked from tinyfox266/ydiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnav.js
213 lines (174 loc) · 5.68 KB
/
nav.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/////////////////////// debug flag ////////////////////////
var debug = false;
/////////////////////// adjustable parameters //////////////////
var minStep = 10;
var nSteps = 30;
var stepInterval = 10;
var blockRange = 15; // how far consider one page blocked
var nodeHLColor = 'lightgrey';
var lineHLColor = '#FFFF66';
var lineBlockedColor = '#E9AB17';
var bgColor = '';
var bodyBlockedColor = '#FAF0E6';
///////////////////////// globals ////////////////////////
var eventCount = 0;
var moving = false;
var matchId = -1;
var matchLineId = -1;
var cTimeout;
///////////////////////// utilities ///////////////////////
// No Math.sign() in JS?
function sign(x) {
if (x > 0) {
return 1;
} else if (x < 0) {
return -1;
} else {
return 0;
}
}
function log(msg) {
if (debug) {
console.log("[ " + window.name + " ] " + msg);
}
}
///////////////////// window scrolling stuff ////////////////////
// accessing other window
if (window.name == 'left') {
otherside = parent.right;
} else {
otherside = parent.left;
}
function elementPosition(id) {
obj = document.getElementById(id);
var curleft = 0, curtop = 0;
if (obj && obj.offsetParent) {
curleft = obj.offsetLeft;
curtop = obj.offsetTop;
while (obj = obj.offsetParent) {
curleft += obj.offsetLeft;
curtop += obj.offsetTop;
}
}
return { x: curleft, y: curtop };
}
/*
* Scroll the window to relative position, detecting blocking positions.
*/
function scrollWithBlockCheck(distX, distY) {
var oldTop = document.body.scrollTop;
var oldLeft = document.body.scrollLeft;
eventCount++;
window.scrollBy(distX, distY); // the ONLY place for actual scrolling
var actualX = document.body.scrollLeft - oldLeft;
var actualY = document.body.scrollTop - oldTop;
log("distY=" + distY + ", actualY=" + actualY);
// extra leewaw here because Chrome scrolling is horribly inacurate
if ((Math.abs(distX) > blockRange && actualX == 0)
|| Math.abs(distY) > blockRange && actualY == 0) {
log("blocked");
eventCount--;
document.body.style.backgroundColor = bodyBlockedColor;
putHighlight(matchLineId, lineBlockedColor);
with (otherside) {
putHighlight(matchLineId, lineBlockedColor);
}
return true;
} else {
document.body.style.backgroundColor = bgColor;
otherside.document.body.style.backgroundColor = bgColor;
putHighlight(matchLineId, lineHLColor);
with (otherside) {
putHighlight(matchLineId, lineHLColor);
}
return false;
}
}
/*
* timed animation function for scrolling the current window
*/
function matchWindow(n)
{
moving = true;
var linkPos = otherside.elementPosition(otherside.matchId).y;
var linkOffset = linkPos - otherside.document.body.scrollTop;
var targetPos = document.body.scrollTop + linkOffset
var curPos = elementPosition(matchId).y;
var distY = curPos - targetPos;
var distX = otherside.document.body.scrollLeft
- document.body.scrollLeft;
log("matching window... " + n + " distY=" + distY);
if (distY == 0 && distX == 0) {
moving = false;
} else if (n <= 1) {
scrollWithBlockCheck(distX, distY);
moving = false;
} else{
var stepSize = Math.floor(Math.abs(distY) / n);
actualMinStep = Math.min(minStep, Math.abs(distY));
if (Math.abs(stepSize) < minStep) {
var step = actualMinStep * sign(distY);
} else {
var step = stepSize * sign(distY);
}
var blocked = scrollWithBlockCheck(distX, step);
var rest = Math.floor(distY / step) - 1;
log("blocked?" + blocked + ", rest steps=" + rest);
if (!blocked) {
cTimeout = setTimeout("matchWindow(" + rest + ")", stepInterval);
} else {
moving = false;
}
}
}
////////////////////////// highlighting /////////////////////////////
function putHighlight(id, color) {
var elm = document.getElementById(id);
if (elm != null) {
elm.style.backgroundColor = color;
}
}
/*
* Highlight the link, target nodes and their lines,
* then start animation to move the other window to match.
*/
function highlight(linkId, targetId, linkLineId, targetLineId)
{
putHighlight(matchId, bgColor);
putHighlight(matchLineId, bgColor);
putHighlight(linkId, nodeHLColor);
putHighlight(linkLineId, lineHLColor);
matchId = linkId;
matchLineId = linkLineId;
with (otherside) {
putHighlight(matchId, bgColor);
putHighlight(matchLineId, bgColor);
putHighlight(targetId, nodeHLColor);
putHighlight(targetLineId, lineHLColor);
matchId = targetId;
matchLineId = targetLineId;
cTimeout = setTimeout("matchWindow(" + nSteps + ")" , stepInterval);
}
}
//////////////////////////// event handling ////////////////////////////
/*
* Making other side move instantly. Move other side only if:
* - I am not in an animation initiated by the other side (moving)
* - I do not have pending program-generated scroll events.
*
* Theoretically eventCount alone should work, but this is not the
* case with Safari, so the 'moving' flag is necessary!
*/
function instantMoveOtherWindow (e) {
log("moving=" + moving + ", eventCount=" + eventCount);
if (!moving && eventCount == 0) {
otherside.matchWindow(1);
}
if (eventCount > 0) {
eventCount--;
}
log("eventCount change=" + eventCount);
}
// Scroll and Resize event handlers
window.onscroll = instantMoveOtherWindow
window.onresize = instantMoveOtherWindow