forked from d4software/QueryTree
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.js
More file actions
255 lines (224 loc) · 7.21 KB
/
connection.js
File metadata and controls
255 lines (224 loc) · 7.21 KB
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
function setVisibility() {
var type = $('#database-type option:selected').text()
if (type == 'MySQL' || type == 'PostgreSQL') {
$(".ssh-available").show();
} else {
$(".ssh-available").hide();
}
if ($("#use-ssh").is(':checked')) {
$(".ssh").show();
if ($("#use-ssh-key").is(':checked')) {
$(".ssh-key").show();
$(".ssh-password").hide();
if ($('#SshKeyFileID').val() == '') {
$('#not-uploaded').show();
$('#uploaded').hide();
} else {
$('#not-uploaded').hide();
$('#uploaded').show();
}
} else {
$(".ssh-key").hide();
$(".ssh-password").show();
}
} else {
$(".ssh").hide();
}
}
function setDefaultPort() {
var type = $('#database-type option:selected').text()
if (type == 'MySQL') {
$("#database-port").val("3306");
} else if (type == 'PostgreSQL') {
$("#database-port").val("5432");
} else {
$("#database-port").val("1433");
}
}
function setSshKeyFileLink(url, filename) {
}
function clearInputFile() {
var f = document.getElementById("key-file");
if (f.value) {
try {
f.value = ''; //for IE11, latest Chrome/Firefox/Opera...
} catch (err) { }
if (f.value) { //for IE5 ~ IE10
var form = document.createElement('form'),
parentNode = f.parentNode, ref = f.nextSibling;
form.appendChild(f);
form.reset();
parentNode.insertBefore(f, ref);
}
}
}
// these two methods prevent validation on keyup or focus out for the file upload input
jQuery.validator.defaults.onfocusout = function (element, event) {
if ($(element).attr('id') == "key-file") {
return;
}
this.element(element);
}
jQuery.validator.defaults.onkeyup = function (element, event) {
if ($(element).attr('id') == "key-file") {
return;
}
this.element(element);
}
// this adds validation to make sure the user has uploaded an ssh key if one is necessary
jQuery.validator.addMethod('sshkeyfile', function (value, element) {
return !$('#use-ssh-key').is(':checked') || $('#SshKeyFileID').val() != '';
}, '');
jQuery.validator.unobtrusive.adapters.add('sshkeyfile', {}, function (options) {
options.rules['sshkeyfile'] = true;
options.messages['sshkeyfile'] = options.message;
});
$(document).ready(function () {
$(document).on('invalid-form.validate', 'form', function () {
var button = $(this).find('#btnCreate');
setTimeout(function () {
button.removeAttr('disabled');
}, 1);
});
$(document).on('submit', 'form', function () {
var button = $(this).find('#btnCreate');
setTimeout(function () {
button.attr('disabled', 'disabled');
}, 0);
});
$('.qt-description-container').each(function () {
var parent = $(this);
var resizeFn = function () {
parent.children().each(function () {
var elem = $(this);
var sideBarNavWidth = parent.width() - parseInt(elem.css('paddingLeft')) - parseInt(elem.css('paddingRight')) - parseInt(elem.css('marginLeft')) - parseInt(elem.css('marginRight')) - parseInt(elem.css('borderLeftWidth')) - parseInt(elem.css('borderRightWidth'));
elem.css('width', sideBarNavWidth);
})
};
resizeFn();
$(window).resize(resizeFn);
});
var showDescription = function () {
var desc = $('#' + $(this).attr('aria-describedby'));
var other = $('.qt-description-content:not(#' + desc.attr('id') + ')');
$('.qt-description-container').show();
desc.show();
other.hide();
};
$('[aria-describedby]')
.focus(showDescription)
.hover(showDescription);
setVisibility();
setDefaultPort();
$("#database-type").change(setDefaultPort);
$("#use-ssh").click(setVisibility);
$("#use-ssh-key").change(setVisibility);
$("#database-type").change(setVisibility);
$("#change-key-file").click(function () {
$('#SshKeyFileID').val('');
clearInputFile();
setVisibility();
});
$("#key-file").change(function () {
var files = document.getElementById("key-file").files;
if (files.length > 0) {
var formData = new FormData();
formData.append("key-file", files[0]);
$.ajax({
type: "POST",
url: sshUploadUrl,
data: formData,
dataType: 'json',
contentType: false,
processData: false,
success: function (response) {
if (response && response.status && response.status == 'ok' && response.sshKeyFileID) {
$('#SshKeyFileID').val(response.sshKeyFileID);
$('#filename').text(response.filename);
raiseSuccessAlert('upload-msg', 'Successfully uploaded key file');
} else {
$('#SshKeyFileID').val('');
$('#filename').text('');
raiseErrorAlert('upload-msg', response.message ? response.message : 'An error occurred while uploading the file, please try again');
clearInputFile();
}
setVisibility();
},
error: function (error) {
$('#SshKeyFileID').val('');
$('#filename').text('');
raiseErrorAlert('upload-msg', 'An error occurred while uploading the file, please try again');
clearInputFile();
setVisibility();
}
});
}
});
$('#use-ssh').change(function () {
if ($(this).is(':checked')) {
$("[name='SshPort']").rules('add', {
required: true
});
$("[name='SshUsername']").rules('add', {
required: true
});
} else {
$("[name='SshPort']").rules('remove', 'required');
$("[name='SshUsername']").rules('remove', 'required');
}
});
$("#testconnection").click(function () {
var inputs = $('#mainForm').find(':enabled');
$("#testconnection").val('Connecting...');
var server = $("[name='Server']").val();
var port = $("[name='Port']").val();
var username = $("[name='Username']").val();
var password = $("[name='DbPssword']").val();
var type = $("[name='Type']").val();
var useSsh = $("[name='UseSsh']").is(':checked');
var sshServer = $("[name='SshServer']").val();
var sshPort = $("[name='SshPort']").val();
var sshUsername = $("[name='SshUsername']").val();
var sshPassword = $("[name='SshPassword']").val();
var UseSshKey = $("[name='UseSshKey']").is(':checked');
var SshKeyFileID = $("[name='SshKeyFileID']").val();
var databaseName = $("[name='DatabaseName']").val();
var databaseConnectionId = $("[name='DatabaseConnectionID']").val();
inputs.attr("disabled", true);
$.ajax({
url: testConnectionUrl,
data: {
'server': server,
'port': port,
'username': username,
'password': password,
'type': type,
'useSsh': useSsh,
'sshServer': sshServer,
'sshPort': sshPort,
'sshUsername': sshUsername,
'sshPassword': sshPassword,
'SshKeyFileID': SshKeyFileID,
'UseSshKey': UseSshKey,
'databaseName': databaseName,
'databaseConnectionId': databaseConnectionId
},
dataType: 'json',
type: "POST",
success: function (data) {
inputs.removeAttr("disabled")
$("#testconnection").val('Test this Connection');
if (data.message == 'Success') {
raiseSuccessAlert('testconnection-msg', 'Successfully connected to database');
} else {
raiseErrorAlert('testconnection-msg', data.message, 0);
}
},
error: function (error) {
inputs.removeAttr("disabled")
$("#testconnection").val('Test this Connection');
raiseErrorAlert('testconnection-msg', 'Failed to connect');
}
});
})
});