This repository has been archived by the owner on Oct 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSBObj.js
139 lines (118 loc) · 4.04 KB
/
SBObj.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
/**
* JavaScript Object library for asynchronously interacting with a storagebin
* service using Cross-Origin Resource Sharing. NO jQuery needed.
*
* CORS browser support minimums: Gecko 1.9.1 (Firefox 3.5, SeaMonkey 2.0),
* Safari 4, Google Chrome 3, MSHTML/Trident 4.0 (Internet Explorer 8) via
* XDomainRequest, Opera 12.00, Opera Mobile 12
* http://en.wikipedia.org/wiki/Cross-origin_resource_sharing#Browser_support
*
* `getDefault` method body taken from http://stackoverflow.com/a/894877
*/
function SBObj(theOwnerKey, theDataId) {
var private_ApiBaseUrl = "https://storagebin.appspot.com";
var private_DATA_BASE_URL = "/data";
var private_METHOD_DELETE = "DELETE";
var private_METHOD_GET = "GET";
var private_METHOD_POST = "POST";
var private_OnLoad = function() {};
var private_OnError = function() {};
this.setOnLoad = function(theOnLoadFunc) {
private_OnLoad = theOnLoadFunc;
};
this.setOnError = function(theOnErrorFunc) {
private_OnError = theOnErrorFunc;
};
this.getDefault = function(arg, val) {
return typeof arg !== 'undefined' ? arg : val;
};
this.setApiBaseUrl = function(theNewUrl) {
private_ApiBaseUrl = theNewUrl;
};
var private_OwnerKey = this.getDefault(theOwnerKey, false);
var private_DataId = this.getDefault(theDataId, false);
/**
* create a CORS request object
*
* @param method {String}
* @param url {String}
* @returns {XMLHttpRequest}
*/
function private_CreateRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
xhr = new XDomainRequest();
xhr.open(method, url, true);
} else {
xhr = null;
}
return xhr;
}
/**
* create the String representation of the data end point URL
*
* @param isPut {Boolean}
* @returns {String}
*/
function private_CreateRequestUrl(isPut) {
var aRetEventUrl = private_ApiBaseUrl + private_DATA_BASE_URL;
if (private_OwnerKey) {
aRetEventUrl += "/" + private_OwnerKey;
}
if (!isPut && private_DataId) {
aRetEventUrl += "/" + private_DataId;
}
return aRetEventUrl;
}
/**
* kick-off the CORS data end point interaction
*
* @param method {String}
* @param theDataObj {Object} - must contain fields "type" and "content"
*/
function private_SendDataObj(method, theDataObj) {
if (method && theDataObj && theDataObj.content) {
var aRequest = private_CreateRequest(method,
private_CreateRequestUrl((method === private_METHOD_POST)));
if (aRequest) {
if (theDataObj.type) {
aRequest.setRequestHeader("Content-Type", theDataObj.type);
}
aRequest.onload = private_OnLoad;
aRequest.onerror = private_OnError;
aRequest.send(theDataObj.content);
}
}
}
this.DELETE = function() {
var aDataObj = {
"type": "application/json",
"content": "{}"
};
// no content needed
private_SendDataObj(private_METHOD_DELETE, aDataObj);
};
this.GET = function() {
var aDataObj = {
"type": "application/json",
"content": "{}"
};
// no content needed
private_SendDataObj(private_METHOD_GET, aDataObj);
};
this.PUT = function(theData, theDataContentType) {
theDataContentType = this.getDefault(theDataContentType, "text/plain");
var aBlob = new Blob([ theData ], {
type: theDataContentType
});
var aFormData = new FormData();
aFormData.append("file", aBlob);
var aDataObj = {
"type": false,
"content": aFormData
};
private_SendDataObj(private_METHOD_POST, aDataObj);
};
}