1 |
/* |
2 |
jquery logging plugin |
3 |
|
4 |
2006-08-18 Dobrica Pavlinusic <dpavlin@rot13.org> |
5 |
*/ |
6 |
|
7 |
var _jql; |
8 |
|
9 |
$.log = { |
10 |
|
11 |
/* semi-private functions */ |
12 |
|
13 |
join: function(a) { |
14 |
var m = ''; |
15 |
for (var i=0; i<a.length; i++) |
16 |
m += a[i] + '<br/>'; |
17 |
return m; |
18 |
}, |
19 |
|
20 |
insert: function(what, m) { |
21 |
if (! _jql) this.create(); |
22 |
if (! _jql.on[what]) return; |
23 |
|
24 |
_jql.count[ what ]++; |
25 |
|
26 |
var html = '<div class="jql-' + what + '"' |
27 |
if ( ! _jql.on[what] ) html += ' style="display:none"'; |
28 |
html += '>' + m + '</div>'; |
29 |
$('#jql-log').append( html ); |
30 |
//$('#jql-log').prepend( html ); |
31 |
|
32 |
// refresh count |
33 |
$('#jql-counter').html( |
34 |
'<span class="jql-info">'+_jql.count.info+'</span> ' + |
35 |
'<span class="jql-debug">'+_jql.count.debug+'</span> ' + |
36 |
'<span class="jql-error">'+_jql.count.error+'</span>' |
37 |
); |
38 |
|
39 |
this.scroll(); |
40 |
}, |
41 |
|
42 |
scroll: function() { |
43 |
// poor man's scroll to bottom |
44 |
var e = document.getElementById('jql-log'); |
45 |
e.scrollTop = e.scrollHeight - e.clientHeight; |
46 |
}, |
47 |
|
48 |
/* public functions */ |
49 |
|
50 |
create: function() { |
51 |
|
52 |
$('body').append( |
53 |
'<div id="jql" class="jql"><div id="jql-bar">' + |
54 |
'<a href="#" onclick="javascript:return $.log.clean();">clean</a> | ' + |
55 |
'<a href="#" class="jql-info" onclick="javascript:return $.log.toggle(\'info\');">info</a> ' + |
56 |
'<a href="#" class="jql-debug" onclick="javascript:return $.log.toggle(\'debug\');">debug</a> ' + |
57 |
'<a href="#" class="jql-error" onclick="javascript:return $.log.toggle(\'error\');">error</a> | ' + |
58 |
'<a href="#" onclick="javascript:return $.log.toggle();">close</a>' + |
59 |
'</div><div id="jql-log"></div></div>' + |
60 |
'<div id="jql-show" class="jql">' + |
61 |
'<a href="#" onclick="javascript:return $.log.toggle();"><span id="jql-counter">?</span></a>' + |
62 |
'</div>' |
63 |
); |
64 |
|
65 |
_jql = { |
66 |
count: { |
67 |
info: 0, |
68 |
debug: 0, |
69 |
error: 0 |
70 |
}, |
71 |
on: { |
72 |
info: true, |
73 |
debug: true, |
74 |
error: true |
75 |
} |
76 |
}; |
77 |
|
78 |
this.debug('created logger'); |
79 |
}, |
80 |
|
81 |
/* onclick events */ |
82 |
|
83 |
toggle: function( what ) { |
84 |
if (what) { |
85 |
$.log.debug('toggle '+what); |
86 |
$('#jql-log .jql-'+what).toggle(); |
87 |
_jql.on[what] = ! _jql.on[what]; |
88 |
if (_jql.on[what]) { |
89 |
$('#jql-bar .jql-'+what).removeClass('jql-disabled'); |
90 |
} else { |
91 |
$('#jql-bar .jql-'+what).addClass('jql-disabled'); |
92 |
} |
93 |
} else { |
94 |
$.log.debug('toggle logger'); |
95 |
$('#jql').toggle(); |
96 |
$('#jql-show').toggle(); |
97 |
} |
98 |
this.scroll(); |
99 |
return false; |
100 |
}, |
101 |
|
102 |
clean: function() { |
103 |
$('#jql-log').html(''); |
104 |
}, |
105 |
|
106 |
/* public methods to call from your code */ |
107 |
|
108 |
info: function() { |
109 |
if (arguments) this.insert( 'info', this.join(arguments) ); |
110 |
}, |
111 |
debug: function() { |
112 |
if (arguments) this.insert( 'debug', this.join(arguments) ); |
113 |
}, |
114 |
error: function() { |
115 |
if (arguments) this.insert( 'error', this.join(arguments) ); |
116 |
} |
117 |
}; |
118 |
|