-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.coffee
170 lines (143 loc) · 4.13 KB
/
utils.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
m = require 'mithril'
# get event's target
getTarget = (event) -> elem = event.target || event.srcElement
# get even's target's data-xxx
getTargetData = (event, dataStr) ->
elem = event.target || event.srcElement
if elem.dataset? then elem.dataset[dataStr]
else elem.getAttribute('data-'+ dataStr)
# get even's current target's data-xxx
getCurrentTargetData = (event, dataStr) ->
elem = event.currentTarget
if elem.dataset? then elem.dataset[dataStr]
else elem.getAttribute('data-'+ dataStr)
targetHasClass = (elem, str) ->
(elem.className.indexOf str) != -1
# cancelBubble
cancelBubble = (e) ->
# for IE
if e.cancelBubble == false
e.cancelBubble = true
# others
e.stopPropagation?()
false
# clear Date's hour minute and second
clearDateHMS = (date) ->
date.setHours 0
date.setMinutes 0
date.setSeconds 0
date
# format Date to yyyy-mm-dd
formatDate = (date) ->
yyyy = date.getFullYear()
mm = (date.getMonth() + 1)
dd = date.getDate()
yyyy + '-' + (formatXX mm) + '-' + (formatXX dd)
# format Date to yyyy-mm-dd
formatHMS = (date) ->
hh = date.getHours()
mm = date.getMinutes()
ss = date.getSeconds()
(formatXX hh) + ':' + (formatXX mm) + ':' + (formatXX ss)
# format Date to yyyy-mm-dd hh:mm:ss
formatDateWithHMS = (date) ->
hh = date.getHours()
mm = date.getMinutes()
ss = date.getSeconds()
(formatDate date) + ' ' + (formatHMS date)
# parse yyyy-mm-dd hh:mm:ss
# parse yyyy-mm-dd
parseDateWithHMS = (dateString) ->
[dateStr, timeStr] = dateString.split ' '
date = new Date dateStr
if timeStr?
[hh, mm, ss] = timeStr.split ':'
date.setHours(parseInt hh)
date.setMinutes(parseInt mm)
date.setSeconds(parseInt ss)
date
# helper to format number to 2 digit
formatXX = (x) -> if x < 10 then '0' + x.toString() else x.toString()
# find first Error in Array
firstErrorInArray = (arr) ->
for x in arr
if x instanceof Error then return x
# remove an element from array and return it, return undefined if not in array
removeFromArray = (arr, x) ->
i = arr.indexOf x
if i != -1 then arr.splice i, 1
undefined
svgCounter = 0
svg = (svg) ->
m 'i', key: svgCounter++, svg
spinner = (color, size = '2em', interval = '1s') ->
svg m.trust(
"""
<svg version="1.1"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
x="0px"
y="0px"
width="#{size}"
viewBox="0 0 80 80"
xml:space="preserve">
<path
fill="#{color}"
d="M40,72C22.4,72,8,57.6,8,40C8,22.4,
22.4,8,40,8c17.6,0,32,14.4,32,32c0,1.1-0.9,2-2,2
s-2-0.9-2-2c0-15.4-12.6-28-28-28S12,24.6,12,40s12.6,
28,28,28c1.1,0,2,0.9,2,2S41.1,72,40,72z"
<!-- ANIMATION START -->
<animateTransform
attributeType="xml"
attributeName="transform"
type="rotate"
from="0 40 40"
to="360 40 40"
dur="#{interval}"
repeatCount="indefinite"
/>
</path>
</svg>
"""
)
debounce = (fn, delay, leading = false) ->
pending = false
if leading
->
unless pending
fn.apply this, arguments
pending = true
setTimeout (-> pending = false), delay
else
args = undefined
->
args = arguments
self = this
unless pending
pending = true
setTimeout(
->
pending = false
fn.apply self, args
, delay
)
module.exports = {
getTarget
, getTargetData
, getCurrentTargetData
, targetHasClass
, cancelBubble
, clearDateHMS
, formatXX
, formatDate
, formatHMS
, formatDateWithHMS
, parseDateWithHMS
, firstErrorInArray
, removeFromArray
, noOp: (->)
, svg
, spinner
, debounce
}