-
Notifications
You must be signed in to change notification settings - Fork 0
/
cartong-arcgis-crud.js
209 lines (190 loc) · 6.11 KB
/
cartong-arcgis-crud.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//TODO: this plugin is not totally finalized. To embed in Arcgis.Service class.
function CRUD(url){
//url should look like: http://MyServer/ArcGIS/rest/services/PaulFeaturewgs84/FeatureServer/1
this.url=url;
}//end function CRUD
CRUD.prototype={
add: function(addFeature,callback){//,action){
var promise = $.Deferred();
//needs to look like [{"geometry":{"x":-106,"y":35}, "attributes":{"name":"paul","number":123}}]
var addparams = "features="+addFeature.feature+"&f=json";
if (addFeature.token) { addparams += "&token=" + addFeature.token; }
var addurl=this.url+"/addFeatures";
console.log(addFeature.feature);
var http;
if (window.XMLHttpRequest){
http=new XMLHttpRequest();
}
else {
http=new ActiveXObject("Msxml2.XMLHTTP");
}
http.open("POST", addurl, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
//console.log(http.responseText);
//tryReload(http.responseText,'add');
if (callback) { callback(http.responseText); }//,'add');
promise.resolve(readCrudResponse(http.responseText))
}
else if (http.readyState == 4) {
promise.reject([99, 'CRUD: Unexpected error'])
}
}
http.send(addparams);
return promise
}, //end ADD
update: function(updateFeature,callback){
var promise = $.Deferred();
var updateparams = "features="+updateFeature.feature+"&f=json";
if (updateFeature.token) { updateparams += "&token=" + updateFeature.token; }
var updateurl=this.url+"/updateFeatures";
var http;
if (window.XMLHttpRequest){
http=new XMLHttpRequest();
}
else {
http=new ActiveXObject("Msxml2.XMLHTTP");
}
http.open("POST", updateurl, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
//console.log(http.responseText);
if (callback) { callback(http.responseText); }
promise.resolve(readCrudResponse(http.responseText))
}
else if (http.readyState == 4) {
promise.reject([99, 'CRUD: Unexpected error'])
}
}
http.send(updateparams);
return promise
}, //end UPDATE
delete: function(deleteFeature,callback){ //takes objectid
var promise = $.Deferred();
var deleteparams = "objectIds="+deleteFeature.feature+"&f=json";
if (deleteFeature.token) { deleteparams += "&token=" + deleteFeature.token; }
var deleteurl=this.url+"/deleteFeatures";
var http;
if (window.XMLHttpRequest){
http=new XMLHttpRequest();
}
else {
http=new ActiveXObject("Msxml2.XMLHTTP");
}
http.open("POST", deleteurl, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
//console.log(http.responseText);
if (callback) { callback(http.responseText); }
promise.resolve(readCrudResponse(http.responseText))
}
else if (http.readyState == 4) {
promise.reject([99, 'CRUD: Unexpected error'])
}
}
http.send(deleteparams);
return promise
}, //end DELETE
query: function(){
} // end QUERY
};//end CRUD.prototype
function readCrudResponse(httpResponseText) {
//var alerted = {"alert":""};
//if (alerted != 'yes') {
var httpResponse = JSON.parse(httpResponseText);
var httpResponseKeys = Object.keys(httpResponse);
var httpResponseFirstKey = httpResponseKeys[0];
var response = [];
if (httpResponseFirstKey == 'addResults' || httpResponseFirstKey == 'updateResults' || httpResponseFirstKey == 'deleteResults') {
var responseSuccess = true;
var successList = [];
var errorList = []
for (var i=0;i<httpResponse[httpResponseFirstKey].length;i++) {
const itemResult = httpResponse[httpResponseFirstKey][i]
if (!itemResult.success) {
errorList.push([itemResult.objectId, itemResult.error.description])
responseSuccess = false;
}
else { successList.push(itemResult.objectId) }
}
if (responseSuccess == true){
switch (httpResponseFirstKey) {
case 'addResults':
response = [1, "Item(s) " + successList.join(', ') + " added", successList];
break;
case 'updateResults':
response = [2, "Item " + successList.join(', ') + " updated", successList];
break;
case 'deleteResults':
response = [3, "Item " + successList.join(', ') + " deleted", successList];
break;
}
//alerted.alert ='yes';
//location.reload();
}
else {
var errorDescription = '';
for (var i=0;i<errorList.length;i++) {
errorDescription += errorList[i][0] + ': '+ errorList[i][1] + '\n';
}
response = [4, "Error:\n" + errorDescription];
//alerted.alert ='yes';
}
} else if (httpResponseFirstKey == 'error') {
function listDetails(details) {
detailsText = '';
for (var i=0; i<details.length; i++) {
detailsText += details[i];
}
return detailsText;
}
response = [5, "Error: " + httpResponse[httpResponseFirstKey].message + " " + listDetails(httpResponse[httpResponseFirstKey].details)];
//alerted.alert ='yes';
}
return response;
//}
}
function tryReload(httpResponseText){
var response = readCrudResponse(httpResponseText);
switch (response[0]) {
case 1:
case 2:
case 3:
//var update_string = '[{"attributes":{"OBJECTID":'+response[2]+',"gen_id":"assmnt_'+response[2]+'"}}]';
//var crud = new CRUD(featureServiceUrl);
//crud.update({feature: update_string});
if (swal) {
swal('info', response[1], 'info');
}
else {
alert(response[1]);
}
location.reload();
break;
case 4:
case 5:
if (swal) {
swal('info', response[1], 'info');
}
else {
alert(response[1]);
}
break;
default:
if (swal) {
swal('error', "CRUD: Unexpected error", 'error');
}
else {
alert("CRUD: Unexpected error");
}
break;
}
}
function setPattern(str, param) {
return (str || '').replace(/\{(\w+)\}/g, function(tag, key) {
return param[key] !== undefined ? param[key] : tag;
});
};