jqgrid_utils 1.1.2 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -97,6 +97,125 @@ module.exports = class Vanilla_website_utils
97
97
 
98
98
 
99
99
 
100
+
101
+ async upsert_row(row, url, req = {})
102
+ {
103
+ if (row.rowid.startsWith('jqg'))
104
+ {
105
+ const r0 = await this.insert_row(row, url);
106
+ return r0;
107
+ }
108
+ else
109
+ {
110
+ const r1 = await this.update_row(row, url);
111
+ return r1;
112
+ }
113
+ }
114
+
115
+
116
+ async insert_row(row, url)
117
+ {
118
+ let req = {};
119
+ let ret = '';
120
+ if (row.inputData.hasOwnProperty('id'))
121
+ {
122
+ req['_id'] = 'id';
123
+ req['_id_val'] = row.inputData['id'];
124
+ for (let i in row.inputData)
125
+ {
126
+ req[i] = row.inputData[i];
127
+ }
128
+ delete req['id'];
129
+ delete req['oper'];
130
+ //console.log(req);
131
+ ret = await this.put_json(url, JSON.stringify(req));
132
+ }
133
+ return ret;
134
+ }
135
+
136
+
137
+
138
+ async update_row(row, url, req = {})
139
+ {
140
+ let ret = '';
141
+ {
142
+ if(! req['_id'])
143
+ {
144
+ req['_id'] = 'id';
145
+ }
146
+ req['_id_val'] = row.inputData['id'];
147
+ for (let i in row.inputData)
148
+ {
149
+ req[i] = row.inputData[i];
150
+ }
151
+ delete req['id'];
152
+ delete req['oper'];
153
+ ret = await this.post_json(url, JSON.stringify(req));
154
+ }
155
+ return ret;
156
+ }
157
+
158
+
159
+ async delete_row(_id, url)
160
+ {
161
+ let ret = '';
162
+ if (url.indexOf('?') > -1)
163
+ {
164
+ url += "&_id=" + encodeURIComponent(unescape(_id));
165
+ }
166
+ else
167
+ {
168
+ url += "?_id=" + encodeURIComponent(unescape(_id));
169
+ }
170
+
171
+ ret = JSON.parse(await this.adelete_api(url));
172
+ return ret['message'];
173
+ }
174
+
175
+
176
+
177
+ async adelete_api(url, json = false)
178
+ {
179
+ const ctype = json ? "application/json;charset=UTF-8" : "application/x-www-form-urlencoded";
180
+ return new Promise((resolve, reject) =>
181
+ {
182
+ let xhr = new XMLHttpRequest();
183
+ xhr.open("DELETE", url);
184
+ xhr.setRequestHeader("Content-type", ctype);
185
+ xhr.onload = () => resolve(xhr.responseText);
186
+ xhr.onerror = () => reject(xhr.statusText);
187
+ xhr.send(null);
188
+ });
189
+ }
190
+
191
+
192
+ async post_json(url, data)
193
+ {
194
+ return new Promise((resolve, reject) =>
195
+ {
196
+ let xhr = new XMLHttpRequest();
197
+ xhr.open("POST", url);
198
+ xhr.setRequestHeader("Content-type", "application/json");
199
+ xhr.onload = () => resolve(xhr.responseText);
200
+ xhr.onerror = () => reject(xhr.statusText);
201
+ xhr.send(data);
202
+ });
203
+ }
204
+
205
+ async put_json(url, data)
206
+ {
207
+ return new Promise((resolve, reject) =>
208
+ {
209
+ let xhr = new XMLHttpRequest();
210
+ xhr.open("PUT", url);
211
+ xhr.setRequestHeader("Content-type", "application/json");
212
+ xhr.onload = () => resolve(xhr.responseText);
213
+ xhr.onerror = () => reject(xhr.statusText);
214
+ xhr.send(data);
215
+ });
216
+ }
217
+
218
+
100
219
  };
101
220
 
102
221
 
package/jqgrid_utils.js CHANGED
@@ -96,6 +96,125 @@ module.exports = class Vanilla_website_utils
96
96
 
97
97
 
98
98
 
99
+
100
+ async upsert_row(row, url, req = {})
101
+ {
102
+ if (row.rowid.startsWith('jqg'))
103
+ {
104
+ const r0 = await this.insert_row(row, url);
105
+ return r0;
106
+ }
107
+ else
108
+ {
109
+ const r1 = await this.update_row(row, url);
110
+ return r1;
111
+ }
112
+ }
113
+
114
+
115
+ async insert_row(row, url)
116
+ {
117
+ let req = {};
118
+ let ret = '';
119
+ if (row.inputData.hasOwnProperty('id'))
120
+ {
121
+ req['_id'] = 'id';
122
+ req['_id_val'] = row.inputData['id'];
123
+ for (let i in row.inputData)
124
+ {
125
+ req[i] = row.inputData[i];
126
+ }
127
+ delete req['id'];
128
+ delete req['oper'];
129
+ //console.log(req);
130
+ ret = await this.put_json(url, JSON.stringify(req));
131
+ }
132
+ return ret;
133
+ }
134
+
135
+
136
+
137
+ async update_row(row, url, req = {})
138
+ {
139
+ let ret = '';
140
+ {
141
+ if(! req['_id'])
142
+ {
143
+ req['_id'] = 'id';
144
+ }
145
+ req['_id_val'] = row.inputData['id'];
146
+ for (let i in row.inputData)
147
+ {
148
+ req[i] = row.inputData[i];
149
+ }
150
+ delete req['id'];
151
+ delete req['oper'];
152
+ ret = await this.post_json(url, JSON.stringify(req));
153
+ }
154
+ return ret;
155
+ }
156
+
157
+
158
+ async delete_row(_id, url)
159
+ {
160
+ let ret = '';
161
+ if (url.indexOf('?') > -1)
162
+ {
163
+ url += "&_id=" + encodeURIComponent(unescape(_id));
164
+ }
165
+ else
166
+ {
167
+ url += "?_id=" + encodeURIComponent(unescape(_id));
168
+ }
169
+
170
+ ret = JSON.parse(await this.adelete_api(url));
171
+ return ret['message'];
172
+ }
173
+
174
+
175
+
176
+ async adelete_api(url, json = false)
177
+ {
178
+ const ctype = json ? "application/json;charset=UTF-8" : "application/x-www-form-urlencoded";
179
+ return new Promise((resolve, reject) =>
180
+ {
181
+ let xhr = new XMLHttpRequest();
182
+ xhr.open("DELETE", url);
183
+ xhr.setRequestHeader("Content-type", ctype);
184
+ xhr.onload = () => resolve(xhr.responseText);
185
+ xhr.onerror = () => reject(xhr.statusText);
186
+ xhr.send(null);
187
+ });
188
+ }
189
+
190
+
191
+ async post_json(url, data)
192
+ {
193
+ return new Promise((resolve, reject) =>
194
+ {
195
+ let xhr = new XMLHttpRequest();
196
+ xhr.open("POST", url);
197
+ xhr.setRequestHeader("Content-type", "application/json");
198
+ xhr.onload = () => resolve(xhr.responseText);
199
+ xhr.onerror = () => reject(xhr.statusText);
200
+ xhr.send(data);
201
+ });
202
+ }
203
+
204
+ async put_json(url, data)
205
+ {
206
+ return new Promise((resolve, reject) =>
207
+ {
208
+ let xhr = new XMLHttpRequest();
209
+ xhr.open("PUT", url);
210
+ xhr.setRequestHeader("Content-type", "application/json");
211
+ xhr.onload = () => resolve(xhr.responseText);
212
+ xhr.onerror = () => reject(xhr.statusText);
213
+ xhr.send(data);
214
+ });
215
+ }
216
+
217
+
99
218
  };
100
219
 
101
220
 
package/package.json CHANGED
@@ -28,5 +28,5 @@
28
28
  {
29
29
  "test": "echo \"Error: no test specified\" && exit 1"
30
30
  },
31
- "version": "1.1.2"
31
+ "version": "1.1.3"
32
32
  }