/
io.js
executable file
·304 lines (266 loc) · 9.93 KB
/
io.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
var io = {
saveForm: function() {
var buttons = [];
var newFormName = $("#saveFormName").val();
if (!newFormName || newFormName == L.default_save_form_empty_str) {
gd.errors = [];
gd.errors.push({ els: null, error: L.no_form_name });
utils.displayErrors("#gdMessages");
return false;
}
// if the name already exists, check with the user that it's okay to overwrite it
var form_exists = false;
var form_list_dd = $("#formList")[0];
for (var i=0; i<form_list_dd.length; i++) {
if (form_list_dd[i].text == newFormName) {
form_exists = true;
}
}
if (form_exists) {
if (!confirm(L.form_exists_overwrite_confirmation)) {
return false;
}
}
// okay! now we serialize the form data and submit it for storage
var countries = [];
for (i=0; i<document.data["countryChoice[]"].length; i++) {
if (document.data["countryChoice[]"][i].checked) {
countries.push(document.data["countryChoice[]"][i].value);
}
}
var rowData = [];
var orderedRowIDs = gd._getRowOrder();
for (var i=0; i<orderedRowIDs.length; i++) {
var currRow = orderedRowIDs[i];
var func_ns = $("#type_" + currRow).val() + "_ns";
// not every data type NEEDS a save-load function beyond the data type dropdown value, e.g.
// "email" data type
var currData = {};
if (typeof window[func_ns] === "object" && typeof window[func_ns].saveRow === "function") {
currData = window[func_ns].saveRow(currRow);
}
currData.title = $("#title_" + currRow).val();
currData.dataType = $("#type_" + currRow).val();
rowData.push(currData);
}
var data = {
numResults: document.data.numResults.value,
resultType: $("input[name=resultType]:checked").val(),
countries: countries,
xmlSettings: {
rootNodeName: $("#xml_root_node_name").val(),
recordNodeName: $("#xml_record_node_name").val(),
xml_use_custom_format: $("#xml_use_custom_format").attr("checked"),
xml_custom_format: $("#xml_custom_format").val()
},
csvSettings: {
delimiter: $("#csv_delimiter").val(),
csv_line_endings: $("#csv_line_endings").val()
},
sqlSettings: {
dbTableName: $("#sql_table_name").val(),
dbType: $("#sql_database").val(),
includeCreateTableQuery: $("#sql_create_table").is(":checked"),
includeDropTableQuery: $("#sql_drop_table").is(":checked"),
encloseWithBackquotes: $("#enclose_with_backquotes").is(":checked"),
statementType: $("input[name=sql_statement_type]:checked").val(),
primaryKey: $("input[name=sql_primary_key]:checked").val()
},
rowData: rowData
};
var data_str = 'form_name=' + newFormName + '&form_content=' + $.toJSON(data);
utils.startProcessing();
$.ajax({
url: "code/ajax_save.php",
type: "POST",
data: data_str,
success: function(data) {
var json = $.evalJSON(data);
// if this form isn't listed in the form dropdown list, add it
var already_listed = false;
var form_list_dd = $("#formList")[0];
for (var f=0; f<form_list_dd.length; f++) {
if (form_list_dd[f].text == json.form_name) {
already_listed = true;
}
}
if (!already_listed) {
form_list_dd[form_list_dd.length] = new Option(json.form_name, json.form_id, false, false);
}
g.displayMessage(L.form_saved);
g.stopProcessing();
},
error: function() {
alert(L.fatal_error);
gd.stopProcessing();
}
});
},
loadForm: function() {
if (gd.queue.length) {
alert(L.script_thinking);
return false;
}
utils.startProcessing();
// get the form to load
$.ajax({
url: "code/ajax_load.php",
type: "POST",
data: "form_id=" + $("#formList").val(),
success: function(data) {
var json = $.evalJSON(data);
if (json.success == "true") {
var info = json.form_content;
var form_list = $("#formList")[0];
for (var i=0; i<form_list.length; i++) {
if (form_list[i].selected) {
$("#saveFormName").val(form_list[i].text);
}
}
// remove all the existing rows (except the heading row)
gd.emptyForm(false, 0);
$("#numResults").val(info.numResults);
for (var i=0; i<document.data.resultType.length; i++) {
if (document.data.resultType[i].value == info.resultType) {
document.data.resultType[i].checked = true;
gd.changeResultType(info.resultType);
break;
}
}
for (i=0; i<document.data["countryChoice[]"].length; i++) {
if ($.inArray(document.data["countryChoice[]"][i].value, info.countries) != -1) {
document.data["countryChoice[]"][i].checked = true;
} else {
document.data["countryChoice[]"][i].checked = false;
}
}
gd.updateCountryChoice();
// XML
$("#xml_root_node_name").val(info.xmlSettings.rootNodeName);
$("#xml_record_node_name").val(info.xmlSettings.recordNodeName);
$("#xml_use_custom_format").attr("checked", info.xmlSettings.xml_use_custom_format);
$("#xml_custom_format").val(info.xmlSettings.xml_custom_format);
gd.toggleCustomXMLFormat.call($("#xml_use_custom_format"));
// CSV
$("#csv_delimiter").val(info.csvSettings.delimiter);
$("#csv_line_endings").val(info.csvSettings.csv_line_endings);
// SQL
$("#sql_table_name").val(info.sqlSettings.dbTableName);
$("#sql_database").val(info.sqlSettings.dbType);
$("#sql_create_table").attr("checked", info.sqlSettings.includeCreateTableQuery);
$("#sql_drop_table").attr("checked", info.sqlSettings.includeDropTableQuery);
$("#enclose_with_backquotes").attr("checked", info.sqlSettings.encloseWithBackquotes);
$("input:radio[name=sql_statement_type]").filter("[value=" + info.sqlSettings.statementType + "]").attr("checked", true);
$("input:radio[name=sql_primary_key]").filter("[value=" + info.sqlSettings.primaryKey + "]").attr("checked", true);
// add the new blank rows
gd.addRows(info.rowData.length);
var rowOrder = gd._getRowOrder();
// now populate the rows. Do everything that we can: create the rows, populate the titles & select
// the data type. The remaining fields are custom to the data type, so we leave them to their
// [ns].loadRow function (if defined)
for (var i=0; i<rowOrder.length; i++) {
var currRow = rowOrder[i];
$("#title_" + currRow).val(info.rowData[i]["title"]); // decodeURIComponent
$("#type_" + currRow).val(info.rowData[i]["dataType"]);
gd.changeRowType("type_" + currRow, info.rowData[i]["dataType"]);
var func_ns = $("#type_" + currRow).val() + "_ns";
if (typeof window[func_ns] === "object" && typeof window[func_ns].loadRow === "function") {
gd.queue.push(window[func_ns].loadRow(currRow, info.rowData[i]));
}
}
gd.processQueue();
} else {
gd.errors = [];
gd.errors.push({ els: null, error: json.message });
utils.displayErrors("#gdMessages");
}
g.stopProcessing();
}
});
},
/*
loadRowContent: function()
{
for (currRow=1; currRow<=g_lastJsonResponse.num_rows; currRow++)
{
row_type = decodeURIComponent(g_lastJsonResponse["type" + currRow]);
switch (row_type)
{
case "Date":
var date_info = decodeURIComponent(g_lastJsonResponse["option" + currRow]).split(",");
$("#fromDate_" + currRow).value = date_info[0];
$("#toDate_" + currRow).value = date_info[1];
$("#option_" + currRow).value = date_info[2];
break;
case "Text-Fixed":
$("#numWords_" + currRow).value = decodeURIComponent(g_lastJsonResponse["option" + currRow]);
break;
case "Text-Random":
var rand_text_info = decodeURIComponent(g_lastJsonResponse["option" + currRow]).split(",");
$("#startsWithLipsum_" + currRow).checked = (rand_text_info[0] == "true") ? true : false;
$("#numWordsMin_" + currRow).value = rand_text_info[1];
$("#numWordsMax_" + currRow).value = rand_text_info[2];
break;
case "Auto-Increment":
$("#datatype_" + currRow).value = decodeURIComponent(g_lastJsonResponse["example" + currRow]);
var auto_increment_info = decodeURIComponent(g_lastJsonResponse["option" + currRow]).split(",");
$("#autoIncrementStart_" + currRow).value = auto_increment_info[0];
$("#autoIncrementValue_" + currRow).value = auto_increment_info[1];
break;
case "Number-Range":
var num_range = decodeURIComponent(g_lastJsonResponse["option" + currRow]).split(",");
$("#numRangeMin_" + currRow).value = num_range[0];
$("#numRangeMax_" + currRow).value = num_range[1];
break;
case "List":
$("#datatype_" + currRow).value = decodeURIComponent(g_lastJsonResponse["example" + currRow]);
var list = decodeURIComponent(g_lastJsonResponse["option" + currRow]).split(",");
$("#listType1_" + currRow).checked = (list[0] == "true") ? true : false;
$("#listType2_" + currRow).checked = (list[1] == "true") ? true : false;
$("#exactly_" + currRow).value = list[2];
$("#atMost_" + currRow).value = list[3];
$("#option_" + currRow).value = list[4];
break;
}
}
},
*/
deleteForm: function() {
var form_id = $("#formList").val();
if (!form_id) {
gd.errors = [];
gd.errors.push({ els: null, error: "Please select a form to delete." });
utils.displayErrors("#gdMessages");
return false;
}
// check it's okay with the user first
if (!confirm(L.confirm_delete_form)) {
return false;
}
g.startProcessing();
$.ajax({
url: "code/ajax_delete_form.php",
data: "form_id=" + $("#formList").val(),
success: function(data) {
var json = $.evalJSON(data);
if (json.success) {
// it was deleted. Remove it from the dropdown list and inform the user
var formList = $("#formList")[0];
for (var i=0; i<formList.options.length; i++) {
if (formList.options[i].value == json.form_id) {
formList.options[i] = null;
}
}
g.displayMessage(L.form_deleted);
} else {
g.displayMessage(L.form_not_deleted);
}
g.stopProcessing();
},
error: function(data) {
alert(L.fatal_error);
gd.stopProcessing();
}
});
}
}