-
Notifications
You must be signed in to change notification settings - Fork 0
/
oxg.js
148 lines (127 loc) · 4.44 KB
/
oxg.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
// Create Object
var AjaxHelper = {};
// Making a function
AjaxHelper.createXHR = (url, options) => {
// Setting xhr object to null because of browser support
var xhr = false;
/* Check if browser supports ajax */
// if browser is IE 8 or Older version
if ( window.ActiveXObject) {
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
xhr = false;
}
}
// Any other browser that doesn't support ajax
if ( !window.XMLHttpRequest ) return false;
// if browser supports Ajax, then create AjaxRequest
xhr = new XMLHttpRequest();
// As it is a function, we have constructor, and we should get values from users
// Check if user passes options
options = options || {};
// Check if user passess request method
options.method = options.method || "GET";
// Check if user passes any data for POST requests
options.data = options.data || null;
// if post request passed then convert it to the post query
if (options.data !== null) {
var qstring = [];
// filter through and set values
for (var key in options.data)
qstring.push(encodeURIComponent(key)+"="+encodeURIComponent(options.data[key]));
// as last part join data with & so that POST query can realize
options.data = qstring.join("&");
}
// Check if chacing is enabled or not
if (options.cache == false && options.method.toUpperCase() == "GET")
url = url+"?_="+ new Date().getTime()
// Parts of ajax request
xhr.onreadystatechange = () => {
// if request is loading
if (xhr.readyState == 1 && options.before)
options.before.call(xhr);
// if ajax request is successefull
if ( (xhr.readyState == 4) && xhr.status == 200 || xhr.status == 304 ) {
// Set Content Type That Server Can realize what is that about
var contentType = xhr.getResponseHeader('Content-Type');
// If Any Error
if (!options.complete) return false;
/* GET REQUEST */
// Check if GET request == json
if (contentType == "application/json")
return options.complete.call(xhr, JSON.parse(xhr.responseText));
// Check if GET request == xml
if (contentType == "text/xml" || contentType == "application/xml")
return options.complete.call(xhr, xhr.responseXML);
// As a default, set GET request file to anyFormat
return options.complete.call(xhr, xhr.responseText);
}
};
// Open the request
xhr.open(options.method, url);
// And return Object
return xhr;
};
// Now function for user
AjaxHelper.make = (url, options) => {
try {
// lets use function we made above
var xhr = AjaxHelper.createXHR(url, options);
// if Ajax request is not supported then return false
if (!xhr) return false;
// Set default Request Header That Server Can Realize
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
// Check if request type == POST
if (options.method.toUpperCase() == "POST")
// if request type == POST, then set content type
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
// Send the data to the server
xhr.send(options.data);
// Return request
return xhr;
} catch(e) {
return e;
}
};
class oxg {
constructor(url, data, method) {
this.url = url;
this.data = data;
this.method = method;
}
static get(url, data) {
this.url = url;
this.data = data;
this.method = "GET";
return new this(url, data, "GET");
}
cache(boolean) {
if (boolean) {
return;
}
this.cache = true;
return this;
}
loading(callback) {
this.loadingCallback = callback;
return this;
}
then(callback) {
this.ajax = AjaxHelper.make(this.url, {
method: this.method,
data: this.data,
cache: this.cache,
before: this.loadingCallback,
complete: callback
});
return this;
}
static post(url, data) {
this.url = url;
this.data = data;
this.method = "POST";
return new this(url, data, "POST");
}
}
module.exports = oxg;