-
Notifications
You must be signed in to change notification settings - Fork 0
/
RESTDataProvider.js
187 lines (173 loc) · 6.82 KB
/
RESTDataProvider.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
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
(function (ns) {
"use strict";
/**
* @name wader.RESTDataProvider
* @class Wader REST Data Provider
* @augments wader.ADataProvider
* @author sc0rp10 <dev@weblab.pro>
* @version 0.3
*/
ADataProvider.extend("wader.RESTDataProvider",
/** @lends wader.RESTDataProvider */
{
},
/** @lends wader.RESTDataProvider# */
{
init: function (resource, baseUrl) {
this.resource = resource;
this.baseUrl = baseUrl;
},
/**
* @param {String|Hash} data
* @return {String}
*/
_buildQueryParams: function (data) {
var value,
key,
tmp = [],
that = this,
urlencode = function (str) {
str = (str + "").toString();
return encodeURIComponent(str).replace(/!/g, "%21").replace(/"/g, "%27").replace(/\(/g, "%28").replace(/\)/g, "%29").replace(/\*/g, "%2A").replace(/%20/g, "+");
},
arg_separator = "&",
buildQueryHelper = function (key, val) {
var k, tmp = [];
if (val === true) {
val = "1";
} else if (val === false) {
val = "0";
}
if (val !== null && typeof(val) === "object") {
for (k in val) {
if (val[k] !== null) {
tmp.push(buildQueryHelper(key + "[" + k + "]", val[k]));
}
}
return tmp.join(arg_separator);
} else if (typeof(val) !== "function") {
return urlencode(key) + "=" + urlencode(val);
} else if (typeof(val) == "function") {
return "";
} else {
throw new Error("Incorrect Parameters");
}
};
if (!data) {
return "";
}
if (typeof data == "string") {
return data + "/";
}
data["email"] = CURRENT_USER.email;
for (key in data) {
value = data[key];
tmp.push(buildQueryHelper(key, value));
}
return "?" + tmp.join(arg_separator);
},
/**
* @param {String} method
* @param {String} key
* @param {String} value
* @return {jqXHR}
*/
_makeRequest: function (method, key, value) {
var url = this.baseUrl + this.resource + "/",
data = {},
value = value || {};
switch (method) {
case "get":
if (key) {
url += key + "/"
};
url += "?email=" + escape(CURRENT_USER.email);
return this._handleResult(url, method);
case "post":
if (key) {
url += key + "/";
}
delete value["disabled"];
delete value["_created_at"];
delete value["model_id"];
return this._handleResult(url, method, JSON.stringify(value));
case "put":
url += key + "/";
delete value["disabled"];
delete value["_created_at"];
delete value["model_id"];
delete value["uid"];
return this._handleResult(url, method, JSON.stringify(value));
case "delete":
url += key + "/";
return this._handleResult(url, method, JSON.stringify(value));
case "getMulti":
if (key && Object.keys(key),length) {
key["email"] = CURRENT_USER.email;
};
url += this._buildQueryParams(key);
return this._handleResult(url, "get");
}
},
/**
* @param {jqXHR} xhr
* @param {String} status
* @param {String} error
* @return {undefined}
*/
_handleError: function (xhr, status, error) {
switch (xhr.status) {
case 12029:
case 0:
ADataProvider.handleError("error", "Произошла ошибка при соединении с сервером, проверьте соединение с сетью и ");
RUN_PING = true;
break;
case 401:
ADataProvider.handleError("login");
break;
case 400:
ADataProvider.handleError("error", "Произошла ошибка при соединении с сервером — ведутся временные технические работы. Пожалуйста, ");
break;
case 502:
ADataProvider.handleError("critical", "Произошла ошибка при соединении с сервером — ведутся временные технические работы.");
case 500:
ADataProvider.handleError("critical", "Произошла ошибка при соединении с сервером — ведутся временные технические работы.");
break;
}
},
/**
* @param {String} url
* @param {String} status
* @param {Hash} data
* @return {jqXHR}
*/
_handleResult: function (url, method, data) {
if (method != "get") {
url += "?api_key=" + SECRET + "&email=" + escape(CURRENT_USER.email);
}
var request = {
url: url,
timeout: 30000,
data: data,
type: method,
/** @inner */
success: function (response) {
if (response && "email" in response) {
var currentEmail = CURRENT_USER.email;
var newEmail = response.email;
if (currentEmail && newEmail != currentEmail) {
window.location.reload();
}
}
},
dataType: "json",
error: this.proxy("_handleError"),
contentType: "application/json; charset=utf-8"
};
return $.ajax(request);
}
});
if (ns !== wader) {
ns.RESTDataProvider = wader.RESTDataProvider;
}
})(window.WADER_NS || window);