jquery_wev8.jsconsole_wev8.js
3.78 KB
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
/*************************************************************************
jquery.jsconsole.js
Convert a div tag in to a log window.
Copyright (c) 2012, Martin Wendt (http://wwWendt.de)
Dual licensed under the MIT or GPL Version 2 licenses.
http://code.google.com/p/dynatree/wiki/LicenseInfo
A current version and some documentation is available at
http://dynatree.googlecode.com/
${Version}
${Revision}
Usage:
logMsg("%o was toggled", this);
@depends: jquery.js
*************************************************************************/
/*jslint laxbreak: true, browser: true, indent: 0, white: false, onevar: false */
// Start of local namespace
(function($) {
var LOGLEVEL_Error = 3,
LOGLEVEL_Warn = 2,
LOGLEVEL_Info = 1,
LOGLEVEL_Debug = 0,
LEVEL_NAMES = {3: 'error', 2: 'warn', 1:'info', 0:'debug'},
wc = window.console;
var methods = {
init: function(options) {
// Create some defaults, extending them with any options that were provided
var opts = $.extend( {
logLevel: LOGLEVEL_Debug,
logLevelWC: LOGLEVEL_Debug
}, options);
opts.count = 0;
return this.each(function(){
// $(window).bind("keydown.jsconsole", methods._keydown);
var $this = $(this),
data = $this.data("jsconsole");
// If the plugin hasn't been initialized yet
if ( ! data ) {
$this.data("jsconsole", opts);
}
});
},
destroy: function( ) {
return this.each(function(){
$(window).unbind(".jsconsole");
$(this).removeData("jsconsole");
})
},
_log: function(args) {
var $this = $(this),
data = $this.data("jsconsole"),
level = arguments[0],
levelName = LEVEL_NAMES[level],
args = Array.prototype.slice.call(arguments), // Copy into a real array
prefix = levelName + " - ";
data.count += 1;
if(true){
// Prepend timestamp
var dt = new Date(),
tag = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds() + "." + dt.getMilliseconds();
prefix += tag;
}
// Replace the first argument (level), with prefix
args[0] = prefix;
// Log to window.console
if(wc && level >= data.logLevelWC){
try {
switch( level ) {
case LOGLEVEL_Info:
wc.info.apply(wc, args);
break;
case LOGLEVEL_Warn:
wc.warn.apply(wc, args);
break;
case LOGLEVEL_Error:
wc.error.apply(wc, args);
break;
default:
wc.log.apply(wc, args);
}
} catch(e) {
}
}
if(level >= data.logLevel){
// For html output we must concatenate single args as strings
for(var i=0; i<args.length; i++){
args[i] = "" + args[i];
}
$("<div/>", {
text: args.join(" "),
"class": "logEntry " + levelName
}).appendTo($this);
}
},
debug: function(msg) {
Array.prototype.unshift.call(arguments, LOGLEVEL_Debug); // prepend level
methods._log.apply(this, arguments);
},
info: function(msg) {
Array.prototype.unshift.call(arguments, LOGLEVEL_Info); // prepend level
methods._log.apply(this, arguments);
},
warn: function(msg) {
Array.prototype.unshift.call(arguments, LOGLEVEL_Warn); // prepend level
methods._log.apply(this, arguments);
},
error: function(msg) {
Array.prototype.unshift.call(arguments, LOGLEVEL_Error); // prepend level
methods._log.apply(this, arguments);
}
};
// Handle $('selector').jsconsole('method') calls
$.fn.jsconsole = function( method ) {
// Note: this is already a jQuery object
if ( methods[method] ) {
return methods[ method ].apply(this, Array.prototype.slice.call(arguments, 1));
} else if ( typeof method === "object" || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error("Method " + method + " does not exist on jQuery.jsconsole");
}
};
// -----------------------------------------------------------------------------
})(jQuery);