-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathutils.js
104 lines (104 loc) · 2.3 KB
/
utils.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
// Generated by CoffeeScript 1.10.0
window.utils = {
getRandomInt: function(min, max) {
if (min == null) {
min = 1;
}
if (max == null) {
max = 10;
}
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
},
postJson: function(url, data) {
return $.ajax({
type: 'POST',
url: url,
data: JSON.stringify(data),
contentType: 'application/json'
});
},
getJson: function(url, data) {
return $.ajax({
type: 'GET',
url: url,
data: data,
contentType: 'application/json'
});
},
tryGet: function(url, obj, message, times, timeout) {
var _get, dfd, me, params, t;
me = this;
dfd = $.Deferred();
params = {
url: url
};
if (obj) {
params = $.extend(params, obj);
}
if (times == null) {
times = 3;
}
if (timeout == null) {
timeout = 3 * 1000;
}
t = 0;
_get = function() {
t += 1;
return $.ajax(params).then((function(res) {
return dfd.resolve(res);
}), function(jqXHR, textStatus) {
var msg;
msg = "ajax failed (" + t + "/" + times + ")";
if (t < times) {
console.warn(msg, params, jqXHR);
return setTimeout(_get, timeout);
} else {
console.error(msg, params, jqXHR);
return me.postJson(gConfig.logUrl, {
message: message || msg,
description: {
params: params,
jqXHR: jqXHR
}
}).always(function(res) {
return 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: function(event, sk1, sk2, key) {
if (sk1 && !event[sk1.toLowerCase() + 'Key']) {
return false;
}
if (sk2 && !event[sk2.toLowerCase() + 'Key']) {
return false;
}
if (this.extraKeyMap[key]) {
if (event.keyCode !== this.extraKeyMap[key]) {
return false;
}
} else if (key && event.keyCode !== key.charCodeAt(0)) {
return false;
}
return true;
}
};