-
Notifications
You must be signed in to change notification settings - Fork 25
/
utils.coffee
76 lines (70 loc) · 2.1 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
window.utils = {
getRandomInt: (min, max)->
min ?= 1
max ?= 10
min = Math.ceil(min)
max = Math.floor(max)
return Math.floor(Math.random() * (max - min)) + min
postJson: (url, data) ->
return $.ajax {
type: 'POST',
url: url,
data: JSON.stringify(data),
contentType: 'application/json'
}
getJson: (url, data) ->
return $.ajax {
type: 'GET',
url: url,
data: data,
contentType: 'application/json'
}
tryGet: (url, obj, message, times, timeout)->
me = this
dfd = $.Deferred()
params = {url}
params = $.extend params, obj if obj
times = 3 unless times?
timeout = 3*1000 unless timeout?
t = 0
_get = ()->
t += 1
$.ajax(params).then ((res)->
dfd.resolve(res)
), (jqXHR, textStatus)->
msg = "ajax failed (#{t}/#{times})"
if t < times
console.warn msg, params, jqXHR
setTimeout _get, timeout
else
console.error msg, params, jqXHR
me.postJson(gConfig.logUrl, {message: message or msg, description: {params, jqXHR}}).always (res)->
dfd.reject(res)
_get()
return dfd
extraKeyMap: {
Enter: 13,
Space: 32,
Tab: 9,
End: 35,
Home: 36,
PageDown: 34,
PageUp: 33,
ArrowDown: 40,
ArrowLeft: 37,
ArrowRight: 39,
ArrowUp: 38,
Escape: 27,
}
checkEventKey: (event, sk1, sk2, key)->
if sk1 and not event[sk1.toLowerCase() + 'Key']
return false
if sk2 and not event[sk2.toLowerCase() + 'Key']
return false
if @extraKeyMap[key]
if event.keyCode != @extraKeyMap[key]
return false
else if key and event.keyCode != key.charCodeAt(0)
return false
return true
}