-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.js
executable file
·740 lines (625 loc) · 21.6 KB
/
Logger.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
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/*
* This file is part of Logger, a javascript-based logger.
* Copyright (C) 2008-2010 Daniel Bush
* This program is distributed under the terms of the GNU
* General Public License. A copy of the license should be
* enclosed with this project in the file LICENSE. If not
* see <http://www.gnu.org/licenses/>.
*
*/
var $web17_com_au$ = $web17_com_au$ || {};
$web17_com_au$.logger = function() {
var pp_module = $web17_com_au$.pretty_print;
var module={};
// Only set pp if we have it...
var pp = null;
if(pp_module) pp = pp_module.pp;
var ErrorMessages = {
'E1': "Body-tag not loaded yet - can't set up Logger!" ,
'E2': "Can't work out event handling interface."
};
var setProperty = function(obj,properties) {
if(!properties) return;
for(var i in properties) {
if(properties.hasOwnProperty(i)) {
obj[i] = properties[i];
}
}
}
// Logger object
//
// See README for usage.
//
// Note:
// - Logger is really a LogFrame which represents a frame
// that sits in the browser window and can display
// and handle multiple logs
module.LogFrame = module.Logger = function(logTitle,options) {
var me=this;
var title=logTitle;
var READY = false;
// Flag to indicate when logger can display itself.
// This is currently defined as when document.body is available.
var body = document.createDocumentFragment();
me.logs = {};
// The logs that this log frame handles
me.logger = null;
// The current Log instance that we are displaying.
var n=0,id;
// logFrame: the div containing logger.
// logHeader: title bar at the top.
// logTable: log entries are rows in the table.
var logFrame = document.createElement("div");
var logHeader = document.createElement("div");
var logBody = document.createElement("div");
var toolbars = {};
toolbars.buttons = document.createElement("div");
toolbars.logs = document.createElement("div");
var width='250px';
var store_width=width; // used by expandWidth()
var height='500px';
var zindex=1000;
var agt=navigator.userAgent.toLowerCase();
var storedPosition; // Place to store position.
var buttons = {}; // Stores buttons on logger menu (usually span nodes)
var minimized=false; // Put us in unminimized mode.
var wrapped=false; // Put us in wrapped mode.
var expandedWidth=false; // Width is not expanded to fit screen.
var dragServer = new DragServer();
// Throw error and alert user if body-tag not loaded yet.
// Replaced by ready_or_wait.
var ready_or_fail = function() {
//body=document.getElementsByTagName("body")[0];
// We need to use lowercase for getElementsByTagName when
// dealing with xml (xhtml etc). I can't check if this
// applies to IE at the moment so I will use document.body
// which will work regardless.
body=document.body;
if (!body) {
alert("E1: "+ErrorMessages['E1']);
throw new Error("E1: "+ErrorMessages['E1']);
}
}
// Keep checking if document.body is available.
// If it is, append document fragment in 'body'
// to document.body and update 'body'.
var ready_or_wait = function() {
var id;
var tries=0;
var interval=100; //ms
var fail_after=10000; //ms
var check_body = function() {
//me.log('checking for body...');
if(document.body) {
document.body.appendChild(body);
body = document.body;
READY=true;
window.clearInterval(id);
}
if(interval*tries>fail_after) {
window.clearInterval(id);
}
tries++;
}
id = window.setInterval(check_body,interval);
}
// Make a button
var makeButton = function(label,f,menuNode,style) {
var node = document.createElement('SPAN');
node.appendChild( document.createTextNode(' '+label+' ') );
node.style.marginRight='0.2em';
makeUnselectable(node);
menuNode.appendChild(node);
module.addEvent(node,'click',f);
if(style) setProperty(node.style,style);
return node;
}
// init()
// Called at the end - see below.
function init() {
//ready_or_fail();
ready_or_wait();
// Set ID according to how many loggers are on the
// page already.
while ( document.getElementById("Logger"+n) ) n++;
ID = 'Logger'+n;
setProperty(logFrame.style,{
color:'black', right:'0px', top:'0px',
visibility:'visible',
position:'absolute'
});
logFrame.setAttribute("id",ID);
setProperty(logHeader.style,{
backgroundColor:"black",
color:"white", fontFamily:"sans-serif",
fontWeight:"bold", fontSize:"9pt",
cursor:"move",
paddingBottom:"1px",
paddingLeft:"10px"
});
for(var i in toolbars) {
setProperty(toolbars[i].style,{
backgroundColor:"#333", color:"white",
fontFamily:"sans-serif",
fontSize:"7pt",
paddingBottom:"1px",
paddingRight:"1em",
paddingLeft:"10px",
cursor:"pointer",
textAlign:"right"
});
}
setProperty(toolbars.logs.style,{
textAlign:'left',
fontSize:'9pt'
});
makeUnselectable(logHeader);
makeUnselectable(toolbars.buttons);
makeUnselectable(toolbars.logs);
// Width, height, zindex
//
// We must set the width of logBody in order
// for the scroll setting to work. Otherwise
// ie6 will just expand logBody.
setProperty(logFrame.style,{
width:width,
height:height,
zIndex:zindex
});
setProperty(logBody.style,{
border:'solid black 1px',
width:width,
overflow:'scroll',
height:'100%',
backgroundColor:'white'
});
// Assemble Logger's html...
logFrame.appendChild(logHeader);
logFrame.appendChild(toolbars.buttons);
logFrame.appendChild(toolbars.logs);
logFrame.appendChild(logBody);
body.appendChild(logFrame);
me.add(title);
me.change(title);
// IE 7 and up generally handle position fixed.
if ( agt.indexOf("msie 6.")==-1 && agt.indexOf("msie 5.")==-1 ) {
logFrame.style.position='fixed';
//me.log('Using fixed positioning.');
} else {
//me.log('Using absolute positioning.');
}
buttons.minimize = makeButton('minimize',me.minimize,toolbars.buttons);
makeButton('wrap',me.wrap,toolbars.buttons);
makeButton('100%',me.expandWidth,toolbars.buttons);
makeButton('<',me.increaseWidth,toolbars.buttons);
makeButton('>',me.decreaseWidth,toolbars.buttons);
makeButton('\\/',me.increaseHeight,toolbars.buttons);
makeButton('/\\',me.decreaseHeight,toolbars.buttons);
makeButton('snap',me.snap,toolbars.buttons);
// Make logHeader a drag handle for dragging
// the logFrame.
dragServer.register(logFrame,logHeader,me.isDraggable);
// Hide log body when dragging.
//
// We override the element's style attribute
// for display to 'none'.
// Hiding the body may give better performance.
module.addEvent(logHeader,"mousedown",
function() {
if(!me.isDraggable()) return;
if(!minimized) me.minimize();
}
);
module.addEvent(logHeader,"mouseup",
function() {
if(!me.isDraggable()) return;
if(minimized) me.minimize();
}
);
// Process options passed into Logger at
// instantiation time.
//
// Should be run at the end of init().
if(options) {
options.minimized && me.minimize();
options.width && me.setWidth(options.width);
options.height && me.setHeight(options.height);
options.wrap && me.wrap();
}
} // Init()
// Add a Log instance to this frame but
// don't display it.
//
// Use `change` if you want to display it.
me.add = function(name) {
me.logs[name] = new module.Log(name);
var b = makeButton(
name,
function(){me.change(name)},
toolbars.logs,
{padding:'3px'}
);
me.logs[name].button = b;
me.logs[name].notify = function() {
b.innerHTML = name+'['+(me.logs[name].size()-1)+'] '
}
return me.logs[name];
}
// Switch to a new instance of Log to display.
me.change = function(name) {
if(me.logs[name]) {
logHeader.innerHTML = "log: "+name;
//logBody.innerHTML='';
// This upsets ie6; we seem to
// permanently lose whatever was there.
while(logBody.firstChild)
logBody.removeChild(logBody.firstChild);
logBody.appendChild(me.logs[name].node);
me.logger = me.logs[name];
me.repeatWrap();
}
for(var i in me.logs) {
if(me.logs[i].button) {
if(i==name) {
if(me.logs[i].button) {
me.logs[i].button.style.backgroundColor='#ccc';
me.logs[i].button.style.color='black';
}
} else {
if(me.logs[i].button) {
me.logs[i].button.style.backgroundColor='';
me.logs[i].button.style.color='';
}
}
}
}
}
// Disable text selection when using logHeader as a drag
// handle.
//
// Also see http://stackoverflow.com/questions/826782/css-rule-to-disable-text-selection-highlighting
var makeUnselectable = function(el) {
el.style.WebkitUserSelect="none"; // Chrome/safari.
el.style.MozUserSelect="none"; // Firefox.
el.unselectable="on"; // IE ???
}
// setWidth(): set width of logger; note the call
// to re-wrap text.
me.setWidth = function(w) {
logFrame.style.width=w;
logBody.style.width=w;
width=w;
me.repeatWrap();
}
me.setHeight = function(h) {
logFrame.style.height=h;
logBody.style.height='100%';
height=h;
}
// storePosition(): record our coordinates
var storePosition = function() {
storedPosition={
'top':logFrame.style.top,
'right':logFrame.style.right
}
}
// restorePosition(): move us to storedPosition.
var restorePosition = function() {
if (storedPosition) {
logFrame.style.right=storedPosition['right'];
logFrame.style.top=storedPosition['top'];
}
}
// Minimization
//
// minimize(): toggle hiding of logBody.
me.minimize = function() {
if(minimized) {
minimized=false;
buttons.minimize.innerHTML = ' minimize ';
logBody.style.display="";
}
else {
logBody.style.display="none";
buttons.minimize.innerHTML = ' maximize ';
minimized=true;
}
}
// Wrapping
//
// wrap(): toggle wrap or unwrap of lines in logTable.
// repeatWrap() Re-apply the wrap or unwrap;
// (we pretend we're in the other state, and run wrap())
me.wrap = function() {
if(wrapped) {
me.logger.node.style.width='2000px';
} else {
if(expandedWidth) {
me.logger.node.style.width='90%';
} else {
// 20 is for the scroll bar on the right.
me.logger.node.style.width=parseInt(width)-20+'px';
}
}
wrapped=!wrapped;
}
me.repeatWrap = function() {
wrapped=!wrapped;
me.wrap();
}
// Width expansion
//
// expandWidth(): Toggle logger's width to 100% or 'width'.
// When expanding, move logger to the top right corner.
// When unexpanding, restore the position we were in.
me.expandWidth = function() {
if(expandedWidth) {
expandedWidth=false; // Must call before setWidth.
me.setWidth(store_width);
restorePosition();
} else {
storePosition();
logFrame.style.right='0px';
logFrame.style.top='0px';
expandedWidth=true;
store_width = width;
me.setWidth('100%');
}
}
me.increaseWidth = function() {
if(expandedWidth) return;
width=parseInt(width)+20+'px';
me.setWidth(width);
}
me.decreaseWidth = function() {
if(expandedWidth) return;
if (parseInt(width)>20) width=parseInt(width)-20+'px';
me.setWidth(width);
}
// Height expansion
me.increaseHeight = function() {
height=parseInt(height)+30+'px';
me.setHeight(height);
}
me.decreaseHeight = function() {
if (parseInt(height)>30) height=parseInt(height)-30+'px';
me.setHeight(height);
}
// Snap
//
// Snap logger to top right corner of screen.
me.snap = function() {
logFrame.style.right='0px';
logFrame.style.top='0px';
storePosition();
}
// Prevent dragging of this frame in some situations.
me.isDraggable = function() {
if(expandedWidth) return false;
return true;
}
init();
return me;
} // LogFrame
// module.Log
// - represents an instance of a log
// - is essentially an html table that stores log entries in rows
module.Log = function(name) {
var me=this;
var title=name;
var logTable = document.createElement("table");
var tbody = document.createElement("tbody");
var logCount=1;
tbody.style.fontFamily="Courier,monospace";
tbody.style.fontSize="9pt";
logTable.appendChild(tbody);
me.node = logTable;
me.size = function() {
return logCount;
}
// Create a log entry
var makeLogEntry = function(node,styles) {
var tr = document.createElement("tr");
var td0 = document.createElement("td");
var td = document.createElement("td");
td0.appendChild(document.createTextNode(logCount+': '));
setProperty(td0.style,{width:'1%',color:'red',verticalAlign:'top'});
setProperty(td.style,{cursor:'pointer'});
setProperty(td.style,styles);
td.appendChild(node);
tr.appendChild(td0);
tr.appendChild(td);
var trs = tbody.getElementsByTagName("tr");
//tbody.appendChild(tr);
tbody.insertBefore(tr,(trs.length>0?trs[0]:null));
logCount++;
if(me.notify) me.notify();
}
// Concatentate and maybe process args passed to log()
// and functions of that ilk. Return resulting string
// which will then get logged.
var parseLogArgs = function() {
var msg='';
var p;
for(var i=0;i<arguments.length;i++) {
if(arguments[i] instanceof Array) {
if(pp) {
for(var j=0;j<arguments[i].length;j++) {
if(j!=0) msg+=',';
p = pp(arguments[i][j]);
msg+=(p);
}
} else msg+=arguments[i];
} else msg+=arguments[i];
}
return msg;
}
// Generates a function that logs.
//
// - We stash the original text of the entry in span._text
// and create a text node for it.
// - IE6 will remove extra whitespace characters like '\n'
// when using innerHTML (except perhaps in a pre-tag);
// other browsers tend to retain these characters but
// just not show them
// - also it's safer to use the dom to clear out a node
// rather than use innerHTML=''; ie6 can crash
// - we have to work around this fact
me.makeLogFunction = function(name,options) {
return me[name] = function() {
var span = document.createElement('SPAN');
span._text = parseLogArgs.apply(me,arguments);
span._tnode = document.createTextNode(span._text);
span.innerHTML = span._text;
makeLogEntry(span,options);
span.onclick = function() {
var pre;
if(span._pre) {
span._pre=false;
while(span.firstChild)
span.removeChild(span.firstChild);
span.innerHTML = span._text;
} else {
span._pre=true;
while(span.firstChild)
span.removeChild(span.firstChild);
span.appendChild(document.createElement('PRE'));
span.firstChild.appendChild(span._tnode);
}
}
};
}
// Basic logging
me.makeLogFunction( 'log' );
// Create strident log entry!
me.makeLogFunction(
'alert',
{backgroundColor:'red',color:'white',fontWeight:'bold'});
// Create angry, glowing log entry.
me.makeLogFunction(
'red',
{backgroundColor:'#fee',color:'red',fontWeight:'bold'});
// Create happy, green, contented log entry.
me.makeLogFunction(
'green',
{backgroundColor:'#afa',color:'green',fontWeight:'bold'});
me.makeLogFunction(
'blue',
{backgroundColor:'#eef',color:'blue',fontWeight:'bold'});
me.makeLogFunction(
'yellow',
{backgroundColor:'#ff8',color:'black',fontWeight:'bold'});
me.divider = function() {
makeLogEntry(document.createElement('HR'));
}
}
// Add / remove event handlers.
//
// 1) Handle non-standard IE DOM event handling.
// 2) We only handle the bubbling phase of events here.
// 3) For obj.attachEvent:
// The pointer 'this' apparently points
// to the 'window' object - so beware.
// See http://ejohn.org/projects/flexible-javascript-events/
// (John Resig).
// I'd like to do something like this:
// Object.prototype.addEvent = addEvent;
// and remove 'obj' from addEvent and use 'this'
// instead. But doesn't work in IE6.
module.addEvent = function(obj,eventType,fn) {
if( obj.addEventListener ) {
obj.addEventListener(eventType,fn,false);
}
else if( obj.attachEvent ) {
obj.attachEvent('on'+eventType , fn);
}
else {
throw new Error('E2: '+ErrorMessages['E2']);
}
}
module.removeEvent = function(obj,eventType,fn) {
if( obj.removeEventListener ) {
obj.removeEventListener(eventType,fn,false);
}
else if( obj.detachEvent ) {
obj.detachEvent('on'+eventType , fn);
}
else {
throw new Error('E2: '+ErrorMessages['E2']);
}
}
// DragServer (DS)
//
// Our Logger function has to be self-contained.
// So it is going to have its own drag code.
// This is a version of DragServer but it is
// not intended for general use. We include it
// here as part of the internals of Logger.
//
// We can only drag one thing at a time with our
// mouse. DS maintains an 'obj' variable which
// represents the thing we are dragging at the time
// we click down with the mouse.
//
// We register our draggable item and its drag handle
// using the 'register' function.
// The life cycle of each drag operation is handled
// by dragOn, drag and dragOff respectively.
//
// Issues:
// If we set up two event handlers using DS which both
// catch the same event, things could get interesting.
// Or: we register the same drag handle twice.
// These things are not dealt with in this implementation
// yet.
// We could impose a locking mechanism which would
// prevent the dragOn method being called if DS is
// already in use.
//
function DragServer() {
var me=this;
//var body = document.getElementsByTagName("body")[0];
// See not above regarding this invocation.
var body = document.body;
var mouseX,objX;
var mouseY,objY;
// Draggable element.
var obj=null;
var dragOn = function(e,draggable,canDrag) {
if(!canDrag()) return;
obj=draggable;
mouseX=parseInt(e.clientX);
mouseY=parseInt(e.clientY);
objX = parseInt(obj.style.right+0);
objY = parseInt(obj.style.top+0);
module.addEvent(document,"mousemove",drag);
}
var dragOff = function(e,draggable,canDrag) {
if(!canDrag()) return;
module.removeEvent(document,"mousemove",drag);
obj=null;
}
var drag = function(e) {
obj.style.right = objX-(e.clientX-mouseX)+'px';
obj.style.top = objY+e.clientY-mouseY+'px';
}
// Register a draggable item along with its drag handle.
//
// Draggable: a reference to the element we want to
// drag.
// DragHandle: a reference to the element which acts
// as our drag handle.
// canDrag(): is a function that determines whether
// we proceed with the dragging operation. Should return
// true or false.
//
var registrations={};
me.register = function(draggable,dragHandle,canDrag) {
module.addEvent(dragHandle,"mousedown",
function(e){dragOn(e,draggable,canDrag);});
module.addEvent(dragHandle,"mouseup",
function(e){dragOff(e,draggable,canDrag);});
}
}
return module;
}();