forked from Saquib764/dna-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dna-utility-behavior.html
161 lines (147 loc) · 4 KB
/
dna-utility-behavior.html
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
<script type="text/javascript">
var Defaults = {
method: 'GET',
headers: {
'Accept': "application/json, text/plain, */*",
},
}
$http = function(options){
var APPLICATION_JSON = 'application/json';
var CONTENT_TYPE_APPLICATION_JSON = {'Content-Type': APPLICATION_JSON + ';charset=utf-8'};
var isArray = function (obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
};
var makeParams = function (obj) {
var s =[]
for(attr in obj){
var v = obj[attr];
if(isArray(v)){
for(var i=0; i<v.length; i++){
var vv = v[i];
if(typeof(vv) != 'string')
vv = JSON.stringify(vv)
s.push(attr+'='+encodeURIComponent(vv))
}
} else if(typeof(v) == 'object'){
s.push(attr+'='+encodeURIComponent(JSON.stringify(v)))
} else{
s.push(attr+'='+encodeURIComponent(v))
}
}
return s.join('&').replace(/%20/g, '+');
};
toJson = function(str,k){
var obj;
if(typeof(str) == 'object'){
obj = str;
} else{
try{
obj = eval('('+str+')');
}
catch (e){
return str;
}
}
if(typeof(obj) == 'function')
return obj;
if(obj == null)
return null;
if(typeof(obj) == 'boolean')
return obj;
if(typeof(obj) == 'number')
return obj;
if(isArray(obj)){
for(var i = 0; i< obj.length; i++)
obj[i] = toJson(obj[i])
return obj;
}
for(attr in obj){
if(typeof(obj[attr]) != 'function')
obj[attr] = toJson(obj[attr], attr);
}
return obj;
}
function makeUrl(mUrl){
return mUrl
}
function ajaxRequest(){
var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
for (var i=0; i<activexmodes.length; i++){
try{
return new ActiveXObject(activexmodes[i])
}
catch(e){
//suppress error
}
}
}
else if (window.XMLHttpRequest) // if Mozilla, Safari etc
return new XMLHttpRequest()
else
return false
}
var req = new ajaxRequest();
var method = options.method || Defaults.method;
method = method.toUpperCase();
var url = makeUrl(options.url)
var data = options.data;
// data = makeParams(data);
data = JSON.stringify(data)
if(method == 'GET'){
url+= ((url.indexOf('?') == -1) ? '?' : '&') + data;
data = null;
}
req.open(method, url, true);
for(attr in Defaults.headers){
if(Defaults.headers[attr])
req.setRequestHeader(attr, Defaults.headers[attr])
}
req.send(data)
return new Promise(function(resolve, reject){
req.onreadystatechange=function(){
if (req.readyState==4){
if (req.status==200 || window.location.href.indexOf("http")==-1){
var res;
try{
res = eval('('+req.response+')');
}
catch(e){
res = toJson(req.response);
}
if(resolve){
resolve(res)
}
}
else{
console.log("An error has occured making the request")
}
}
}
});
}
$http.Defaults = Defaults;
$cookies = {
set: function(key, value, exp){
var d = new Date();
if(!exp)
exp = 365;
d.setTime(d.getTime() + (exp*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = key + "=" + value + "; " + expires;
},
delete: function(key){
this.set(key,'');
},
get: function(key){
var name = key + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return null;
},
};
</script>