forked from piranna/demo-console
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
162 lines (124 loc) · 3.61 KB
/
index.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
(function(window){
/* jshint node: true */
'use strict';
const reSpace = /\s/;
/**
# demo-console
Show a demo console when working with [requirebin](http://requirebin.com/?gist=6079475). Just
include it in one of your require bin demos like this:
## Example Usage
<<< demo/index.js
## Reference
**/
function Console(console)
{
if(!(this instanceof Console))
return new Console(console)
console = console || window.console
// ensure we have items
var items = initConsole();
this.log = log.bind(items, console, 'log')
this.info = log.bind(items, console, 'info')
this.error = log.bind(items, console, 'error')
this.warn = log.bind(items, console, 'warn')
this.trace = function()
{
var stack = (new Error).stack
log.apply(items, arguments.unshift(console, 'trace').push(stack));
};
}
/**
### console.log()
As per the browser `console.log` statement
**/
function log(console, level) {
var items = this
var item = document.createElement('li');
var argv = [].slice.call(arguments, 2)
// initialise the item
item.innerHTML = argv.map(renderData).join(' ');
// add the class
item.classList.add(level);
// add to the list
items.appendChild(item);
setTimeout(function() {
items.parentNode.scrollTop = items.offsetHeight;
}, 100);
// pass the call through to the original window console
console[level].apply(console, argv);
};
/* internals */
function initConsole() {
// look for the container list
var container;
var list = document.querySelector('.democonsole ul');
// if we found the list return it
if (list) {
return list;
}
// otherwise, go ahead and create it
container = document.createElement('div');
list = document.createElement('ul');
// initialise stuff
container.className = 'democonsole';
container.appendChild(list);
// add the console to the dom
document.body.appendChild(container);
return list;
}
function renderData(data, index) {
var initialItem = index === 0;
function extractData(key) {
var hasSpace = reSpace.test(key);
var quoteChar = hasSpace ? '\'' : '';
var content = span(quoteChar + key + quoteChar, 'key') + span(': ')
+ renderData(data[key])
return '<div data-type="object-key">' + content + '</div>';
}
if (typeof data == 'undefined') {
return span('undefined', 'undefined');
}
if (data === true || data === false) {
return span(data, 'boolean');
}
if (Array.isArray(data)) {
return span('[') + data.map(function(entry) {
return renderData(entry);
}).join(', ') + span(']');
}
if (typeof data == 'string' || (data instanceof String)) {
return span(initialItem ? data : ('\'' + data + '\''), 'string');
}
if (data === null) {
return span('null', 'null');
}
if (data instanceof Error) {
return span(data.toString(), 'error');
}
else if (data instanceof Window) {
return span('window', 'window');
}
if (data instanceof DocumentType) {
return 'doctype: ' + data.name;
}
if (data instanceof HTMLCollection) {
return span('[]', 'html-collection');
}
if (data instanceof HTMLDocument) {
return [].slice.call(document.childNodes).map(renderData);
}
if (data instanceof HTMLElement) {
return data.tagName;
}
if (typeof data == 'object') {
return '<div data-type="object">' + span('{') +
Object.keys(data).map(extractData).join('<div class="comma-float">,</div>') +
span('}') + '</div>';
}
return span(data, typeof data);
}
function span(content, dataType) {
return '<span data-type="' + (dataType || '') + '">' + content + '</span>';
}
window.Console = Console
})(this)