-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathdebug.coffee
251 lines (194 loc) · 5.34 KB
/
debug.coffee
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
# ```
# debug.js 1.0.0
#
# ! CopyRight: binnng http://github.com/binnng/debug.js
# Licensed under: MIT
# http://binnng.github.io/debug.js
# ```
# ### Fork me on [Github](https://github.com/binnng/debug.js)!
# ### [Homepage](http://binnng.github.io/debug.js)
((WIN, DOC) ->
"use strict"
UNDEFINED = undefined
NULL = null
LOG = "log"
DANGER = "danger"
WARN = "warn"
SUCCESS = "success"
ERROR = "error"
CLICK = "click"
isTouch = "ontouchend" of WIN
noop = ->
dom = DOC.querySelectorAll
toString = {}.toString
# 绑定事件
bind = (el, evt, callback) ->
el.addEventListener evt, callback, no
# 解绑事件
unbind = (el, evt, fn) ->
el.removeEventListener evt, fn, no
isNull = (val) -> val is NULL
isArray = Array.isArray or (val) ->
val and "[object Array]" is toString.call(val)
isObejct = (val) ->
typeof val is "object" and not isArray(val) and not isNull(val)
# 不能直接定义,可能取不到
getBody = -> DOC["body"] or dom("body")?[0] or dom("html")?[0]
class Debug
# 种类和颜色
debugMap =
log: "0074cc"
danger: "da4f49"
warn: "faa732"
success: "5bb75b"
error: "bd362f"
# 将不同类型的msg转换为可读的String
render = (msg) ->
text = ""
arr = []
if isArray msg
for item in msg
if typeof(item) is "object"
arr.push render(item)
text = "[" + arr.join(',') + "]"
else
arr.push "#{item}"
text = "[" + arr.join(',') + "]"
else if isObejct msg
for item of msg
if typeof(msg[item]) is "object"
arr.push "#{item}: " + render(msg[item])
text = "{" + arr.join(',') + "}"
else
arr.push "#{item}: #{msg[item]}"
text = "{" + arr.join(',') + "}"
else
text = String(msg)
text
# 设置translate
translate = (el, y) ->
el.style.webkitTransform = "translate3d(0,#{y},0)"
el.style.transform = "translate3d(0,#{y},0)"
joinCss = (css) -> css.join ";"
# debug 容器底部预留的高度
parentBottom = 6
publicCss = [
"-webkit-transition: all .3s ease"
"transition: all .3s ease"
]
# 每个debug信息样式
childCss = [
"margin-top:-1px"
"padding:10px"
"border-top:1px solid rgba(255,255,255,.1)"
"margin:0"
"max-width:" + ( WIN.outerWidth - 20 ) + "px"
].concat publicCss
# debug容器的样式
parentCss = [
"-webkit-overflow-scrolling:touch"
"overflow:auto"
"line-height:1.5"
"z-index:5000"
"position:fixed"
"left:0"
"top:0"
"font-size:11px"
"background:rgba(0,0,0,.8)"
"color:#fff"
"width:100%"
"padding-bottom:#{parentBottom}px"
"max-height:100%"
].concat publicCss
constructor: ->
# 是否初始化,是否收起
@isInit = @isHide = no
# 当前消息,当前执行函数,当前颜色
@msg = @fn = @color = ""
# 外层元素
@el = NULL
init: ->
el = @el = DOC.createElement "div"
el.setAttribute "style", joinCss(parentCss)
body = getBody()
body.appendChild(el)
# 初始位置
translate el, 0
# 绑定事件
bind el, CLICK, => @toggle()
@isInit = yes
@
# `debug.print()`
# 核心的输出debug信息方法
print: ->
@init() unless @isInit
css = childCss.concat ["color:##{@color}"]
child = DOC.createElement "p"
child.setAttribute "style", joinCss(css)
child.innerHTML = @msg
@el.appendChild child
@
# `debug.toggle()`
# 隐藏和收起
toggle: (event) =>
(if @isHide then @show else @hide).call @, event
show: (event) ->
translate @el, 0
@isHide = no
@
hide: (event) ->
translate @el, "-#{@el.offsetHeight - parentBottom}px"
@isHide = yes
@
# 添加所有debug方法
# ```
# debug.log()
# debug.danger()
# debug.warn()
# debug.error()
# debug.success()
# ```
for fn of debugMap
@::[fn] = ((fn) ->
(msg) ->
@fn = fn
@msg = render msg
@color = debugMap[fn]
@print()
) fn
# 开放一个实例
entry = new Debug()
# 绑定window,捕捉js报错信息
# 可以通过debug.silence()禁用
errListener = (error) ->
# 只输出有用的错误信息
msg = [
"Error:"
"filename: #{error.filename}"
"lineno: #{error.lineno}"
"message: #{error.message}"
"type: #{error.type}"
]
entry.error msg.join("<br/>")
bind WIN, ERROR, errListener
# 取消绑定window的错误捕捉
entry.silence = ->
unbind WIN, ERROR, errListener
# 如果是移动设备
# 接管`console.log`
# 使用uglify压缩可以去掉所有console代码
if isTouch
_log = window.console.log
console.log = ->
entry.log arguments[0]
_log.apply window.console, arguments
if typeof exports isnt "undefined" and module.exports
module.exports = exports = entry
else if typeof define is "function"
define "debug", (require, exports, module) ->
module.exports = exports = entry
else if typeof angular is "object"
angular.module("binnng/debug", []).factory "$debug", -> entry
else
WIN["debug"] = entry
) window, document