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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
|
/* -----------------------------------------------------------------------------
* STARTUP
*/
/* TODO: Check setup variables */
/* TODO: Loading indicator */
var categoryCurrent = null;
var categoryArea = document.getElementById("headers");
var xmlData = null;
var gdoc = null;
var gwindow = null;
function init()
{
/* Set by frame.html */
if(!window._frameLoaded)
{
window.setTimeout("init();", 100);
return;
}
setupFrame();
loadXml(makeEndpointURI(), loadedGraphData);
}
init();
function loadedGraphData(doc)
{
if(!doc)
{
displayError("Couldn't load graph data from server", categoryArea);
return;
}
xmlData = doc;
displayCategories();
displayCurrentPage();
}
/* -----------------------------------------------------------------------------
* GRAPHS
*/
function setupFrame()
{
var frame = document.getElementById("main-frame");
/* COMPAT: IE and Mozilla access the content of frames differently */
gdoc = frame.contentDocument ? frame.contentDocument : frame.contentWindow.document;
gwindow = frame.contentWindow ? frame.contentWindow : frame.contentDocument.window;
}
function displayCurrentPage()
{
var area = gdoc.getElementById("graphs");
/* Hide all images not from this category */
var i, images = area.getElementsByTagName("img");
for(i = 0; i < images.length; i++)
{
if(images.item(i)._category == categoryCurrent)
images.item(i).style.display = "inline";
else
images.item(i).style.display = "none";
}
var data = findCategoryData(categoryCurrent);
if(!xmlData || !data)
{
displayError("Graph data isn't loaded from server", area);
return;
}
var tend = nowTime();
var tbeg = tend - 86400; /* One day by default */
var children = data.childNodes;
for(i = 0; i < children.length; i++)
{
var child = children.item(i);
if(child.nodeType != 1 || child.nodeName != "graph")
continue;
var name = child.getAttribute("name");
if(!name || !name.length)
continue;
/* Make sure we don't already have this graph */
var id = "graph-" + categoryCurrent + "-" + name;
var img = gdoc.getElementById(id);
if(img != null)
continue;
img = gdoc.createElement("img");
var title = child.getAttribute("title");
if(title && title.length)
{
img.setAttribute("title", child.getAttribute("title"));
img.setAttribute("alt", child.getAttribute("title"));
}
else
img.setAttribute("alt", "Graph");
img.className = "graph";
img.id = id;
area.appendChild(img);
img._category = categoryCurrent;
img._name = name;
img._tbeg = tbeg;
img._tend = tend;
reloadGraph(img);
img.onmousedown = function(evt)
{ actionGotoCancel(); return zoomGraphStart(evt || gwindow.event); }
img.onmouseover = function(evt)
{ return actionsDisplay(gwindow.event ? gwindow.event : evt); }
}
}
function reloadGraph(img)
{
/* TODO: Move these into settings */
/* TODO: We should encode colons properly */
var GRAPH_COLOR_BACK = ":252424";
var GRAPH_COLOR_FONT = ":E7E1CC";
var GRAPH_WIDTH = 450;
var GRAPH_HEIGHT = 120
var uri = makeEndpointURI() + "/graph?category=" + img._category + "&name=" + img._name;
/* Add date options */
uri += "&start=" + img._tbeg + "&end=" + img._tend;
/* And colors */
uri += "&color=BACK" + GRAPH_COLOR_BACK + "&color=FONT" + GRAPH_COLOR_FONT +
"&color=SHADEA" + GRAPH_COLOR_BACK + "&color=SHADEB" + GRAPH_COLOR_BACK;
/* Size */
uri += "&height=" + GRAPH_HEIGHT + "&width=" + GRAPH_WIDTH;
img.setAttribute("src", uri);
}
function zoomGraphStart(evt)
{
/* COMPAT: In IE the left button is 1, Mozilla is 0 */
var lbutton = gwindow.event ? 1 : 0;
if(evt.button != lbutton)
return;
/* COMPAT: Target is handled differently in Internet nastiness */
var img = evt.target || evt.srcElement;
if(img.className != "graph")
return;
var zoom = gdoc.getElementById("zoom");
zoom._img = img;
zoom._zooming = true;
zoom._from = evt.clientX + gdoc.body.scrollLeft;
zoom._start = zoom._from - img.offsetLeft;
zoom._end = zoom._start;
zoom.style.width = 1;
zoom.style.left = zoom._from;
zoom.style.height = img.height;
zoom.style.top = img.offsetTop;
zoom.style.display = "block";
zoom.onmousemove = zoom._img.onmousemove = function(evt)
{ return zoomGraphUpdate(gwindow.event ? gwindow.event : evt); }
zoom.onmouseup = zoom._img.onmouseup = function(evt)
{ return zoomGraphDone(gwindow.event ? gwindow.event : evt); }
document.onmouseup = gdoc.onmouseup = function(evt)
{ return zoomGraphDone(gwindow.event ? gwindow.event : evt); }
cancelEvent(evt);
return false;
}
function zoomGraphUpdate(evt)
{
var zoom = gdoc.getElementById("zoom");
if(!zoom._zooming || !zoom._img)
return;
var to = evt.clientX + gdoc.body.scrollLeft;
var width = to - zoom._from;
zoom._end = to - zoom._img.offsetLeft;
if(width == 0)
width = 1;
if(width < 0)
{
zoom.style.left = to;
zoom.style.width = -width;
}
else
{
zoom.style.left = zoom._from;
zoom.style.width = width;
}
zoom.style.height = zoom._img.height;
zoom.style.top = zoom._img.offsetTop;
zoom.style.display = "block";
cancelEvent(evt);
return false;
}
function zoomGraphDone(evt)
{
var zoom = gdoc.getElementById("zoom");
if(!zoom._zooming)
return;
/* COMPAT: Target is handled differently in Internet nastiness */
var targ = evt.target || evt.srcElement;
if(targ == zoom || targ == zoom._img)
zoomGraphSelect(zoom);
zoomGraphCancel(evt);
}
function zoomGraphSelect(zoom)
{
// Arbitrary values (TODO: Move these to CONFIG)
var GRAPH_MARGIN_LEFT = 67;
var GRAPH_MARGIN_RIGHT = 27;
var left = GRAPH_MARGIN_LEFT;
var right = zoom._img.width - GRAPH_MARGIN_RIGHT;
var start = zoom._start > zoom._end ? zoom._end : zoom._start;
var end = zoom._start > zoom._end ? zoom._start : zoom._end;
/* If too small selected, then punt */
if((end - start) <= 1)
return;
/* If entire thing selected, then punt */
if(start < left && end > right)
return;
/* If only margins selected, then punt */
if(!(start < right && end > left))
return;
/* Limit it to the right range */
var range = zoom._img.width - GRAPH_MARGIN_RIGHT - GRAPH_MARGIN_LEFT;
start = start < left ? 0 : start - left;
end = end - left > range ? range : end - left;
/* Compute the ratio pixels:time */
var ratio = (zoom._img._tend - zoom._img._tbeg) / range;
var obeg = zoom._img._tbeg;
var oend = zoom._img._tend;
zoom._img._tend = Math.floor((end * ratio) + zoom._img._tbeg);
zoom._img._tbeg = Math.floor((start * ratio) + zoom._img._tbeg);
reloadGraph(zoom._img);
}
function zoomGraphCancel(evt)
{
var zoom = gdoc.getElementById("zoom");
if(zoom._zooming) {
if(zoom._img)
zoom._img.onmousemove = zoom._img.onmouseup = zoom._img.onmouseleave = null;
zoom._img = null;
zoom.onmousemove = zoom.onmouseup = null;
zoom._zooming = false;
}
zoom.style.display = "none";
}
function zoomGraphOut(img)
{
var factor = Math.floor((img._tend - img._tbeg) / 2.5);
var now = nowTime();
/* If near the end we have special behavior */
if(img._tend >= now - 300 || img._tend <= now)
{
img._tbeg -= factor;
img._tend = now;
}
else
{
factor = Math.floor(factor / 2);
img._tbeg -= factor
img._tend += factor;
}
reloadGraph(img);
}
function zoomGraphMove(img, dir)
{
var factor = Math.floor((img._tend - img._tbeg) / 3);
if(!dir)
factor = -factor;
img._tbeg += factor;
img._tend += factor;
reloadGraph(img);
}
function actionsDisplay(evt)
{
/* COMPAT: Target is handled differently in Internet nastiness */
var img = evt.target || evt.srcElement;
if(img.className != "graph")
return;
var actions = gdoc.getElementById("actions");
actions._img = img;
actions._display = true;
actions.style.top = img.offsetTop;
actions.style.left = img.offsetLeft + (img.width - 16);
actions.style.display = "block";
actions.onmouseout = img.onmouseout = function(evt)
{ return actionsHide(gwindow.event ? gwindow.event : evt); }
/* TODO: We only actually have to do this once */
var i, acts = actions.getElementsByTagName("img");
for(i = 0; i < acts.length; i++)
{
acts.item(i).onclick = function(evt)
{ return actionRun(gwindow.event ? gwindow.event : evt); }
}
}
function actionsHide(evt)
{
var actions = gdoc.getElementById("actions");
if(!actions._display)
return;
var x = evt.clientX + gdoc.body.scrollLeft;
var y = evt.clientY + gdoc.body.scrollTop;
var img = actions._img;
/* See if we're actually still within the image */
if(x >= img.offsetLeft && x <= img.offsetLeft + img.width &&
y >= img.offsetTop && y <= img.offsetTop + img.height)
return;
actions.style.display = "none";
img.onmouseout = actions.onmouseout = null;
actions._img = null;
actions._display = false;
}
function actionRun(evt)
{
/* COMPAT: Target is handled differently in Internet nastiness */
var act = evt.target || evt.srcElement;
if(act.className != "action")
return;
var actions = gdoc.getElementById("actions");
if(!actions._display)
return;
switch(act.getAttribute("id"))
{
case "action-zoom":
zoomGraphOut(actions._img);
break;
case "action-next":
zoomGraphMove(actions._img, true);
break;
case "action-prev":
zoomGraphMove(actions._img, false);
break;
case "action-goto":
actionGoto(actions._img, act);
break;
}
}
function actionGoto(img, act)
{
/* Display the selection thingy */
var got = gdoc.getElementById("goto");
/* TODO: Make 120/170 a constant */
got.style.top = img.offsetTop + 40;
got.style.left = (img.offsetLeft + img.width) - 160;
got.style.width = 120;
got.style.display = "block";
var sel = gdoc.getElementById("goto-select");
sel.onchange = actionGotoChange;
sel._img = img;
}
function actionGotoCancel()
{
var got = gdoc.getElementById("goto");
got.style.display = "none";
}
function actionGotoChange()
{
var sel = gdoc.getElementById("goto-select");
var img = sel._img;
sel.onchange = null;
sel._img = null;
times = sel.value.split(":");
img._tbeg = nowTime() - times[0];
img._tend = nowTime() - times[1];
reloadGraph(img);
actionGotoCancel();
}
/* -----------------------------------------------------------------------------
* GROUP CATEGORIES
*/
function findCategoryData(cat)
{
var i;
if(!xmlData)
return null;
var cats = xmlData.getElementsByTagName("category");
for(i = 0; i < cats.length; i++)
{
if(cats.item(i).getAttribute("name") == cat)
return cats.item(i);
}
return null;
}
function changeCategory(evt)
{
var header = evt.target || evt.srcElement;
categoryCurrent = header.getAttribute("category");
displayCurrentPage();
}
function displayCategories()
{
var i;
/* Get the template and clean it up a bit */
var template = document.getElementById("header-template");
var groups = xmlData.getElementsByTagName("category");
for(i = 0; i < groups.length; i++)
{
var name = groups.item(i).getAttribute("name");
if(!categoryCurrent)
categoryCurrent = name;
var el = template.cloneNode(false);
el.removeAttribute("id");
el.appendChild(document.createTextNode(name));
el.setAttribute("category", name);
el.onclick = function(evt)
{ return changeCategory(evt || window.event); }
categoryArea.insertBefore(el, template);
}
}
/* -----------------------------------------------------------------------------
* HELPERS
*/
function loadXml(uri, callback)
{
var xmlhttp;
var xmlHttpChange = function() {
if(xmlhttp.readyState == 4)
{
if(xmlhttp.status == 200)
callback(xmlhttp.responseXML);
else
callback(null);
}
}
/* Mozilla code */
if(window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = xmlHttpChange;
xmlhttp.open("GET", uri, true);
xmlhttp.send(null);
}
/* And now for the Nasty */
else if (window.ActiveXObject)
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
/* TODO: need to do something when this fails (can it fail?) */
if(xmlhttp)
{
xmlhttp.onreadystatechange = xmlHttpChange;
xmlhttp.open("GET", uri, true);
xmlhttp.send()
}
}
}
function displayError(message, place)
{
var el = document.createElement("span");
el.className = "error";
var text = document.createTextNode(message);
el.appendChild(text);
if(place)
place.appendChild(el);
return el;
}
function nowTime()
{
return Math.floor((new Date()).getTime() / 1000);
}
function makeEndpointURI()
{
/* TODO: This should use the scheme of the current URI */
return "http://" + document.location.host + ENDPOINT;
}
function cancelEvent(evt)
{
/* COMPAT: Totally different for IE and mozilla */
if(evt.preventDefault)
evt.preventDefault();
if(evt.stopPropagation)
evt.stopPropagation();
if("cancelBubble" in evt)
evt.cancelBubble = true;
if("returnValue" in evt)
evt.returnValue = false;
}
|