Skip to content

Commit 81a966c

Browse files
rueckstiesskangas
authored andcommitted
WIP: error handling and optional connectio name
1 parent e446f89 commit 81a966c

File tree

4 files changed

+81
-26
lines changed

4 files changed

+81
-26
lines changed

src/connect/index.jade

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@
44
form.form-horizontal
55
.message.alert(style='margin-top: 10px;', data-hook='message')
66
hr
7-
.form-group
8-
label.col-sm-2.control-label Name
9-
.col-sm-10
10-
input.form-control(type='text', name='name', placeholder='Local')
117
.form-group
128
label.col-sm-2.control-label Hostname
139
.col-sm-10
14-
input.form-control(type='text', name='hostname', placeholder='localhost')
10+
input.form-control(type='text', name='hostname', placeholder='localhost', data-hook='hostname')
1511
.form-group
1612
label.col-sm-2.control-label Port
1713
.col-sm-10
18-
input.form-control(type='number', name='port', placeholder='27017')
14+
input.form-control(type='number', name='port', placeholder='27017', data-hook='port')
15+
.form-group
16+
label.col-sm-2.control-label Connection Name (optional)
17+
a.btn.btn-info.btn-circle.btn-circle-micro(tabindex="0", role="button", data-toggle="popover", data-trigger="focus", title="Connection Name", data-content="You can choose a friendly connection name for this connection, for example \"Development Server\". The connection will appear on the left side under this name.")
18+
i.fa.fa-question
19+
.col-sm-10
20+
input.form-control(type='text', name='name', placeholder='My Localhost', data-hook='name')
1921
.form-group
2022
.col-sm-offset-2.col-sm-10
2123
button.btn.btn-primary(type='submit') Connect
22-
24+
2325
div(data-hook='sidebar-subview')

src/connect/index.js

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ var format = require('util').format;
55
var $ = require('jquery');
66
var app = require('ampersand-app');
77

8+
// var debug = require('debug')('scout:connect:index');
9+
10+
require('bootstrap/js/popover');
11+
require('bootstrap/js/tooltip');
12+
813
var ConnectionView = View.extend({
914
props: {
1015
model: Connection,
@@ -34,9 +39,8 @@ var ConnectionView = View.extend({
3439
event.stopPropagation();
3540
event.preventDefault();
3641

37-
$('[name=hostname]').val(this.model.hostname);
38-
$('[name=port]').val(this.model.port);
39-
$('[name=name]').val(this.model.name);
42+
// fill in the form with the clicked connection details
43+
this.parent.parent.displayedConnection.set(this.model.serialize());
4044
},
4145
onDoubleClick: function(event) {
4246
this.onClick(event);
@@ -55,6 +59,7 @@ var ConnectionView = View.extend({
5559
}
5660
});
5761

62+
5863
var SidebarView = View.extend({
5964
template: require('./sidebar.jade'),
6065
render: function() {
@@ -67,6 +72,10 @@ var SidebarView = View.extend({
6772
* @todo (imlucas) Use ampersand-form-view.
6873
*/
6974
var ConnectView = View.extend({
75+
template: require('./index.jade'),
76+
children: {
77+
displayedConnection: Connection
78+
},
7079
collections: {
7180
connections: ConnectionCollection
7281
},
@@ -81,6 +90,18 @@ var ConnectView = View.extend({
8190
}
8291
},
8392
bindings: {
93+
'displayedConnection.name': {
94+
type: 'value',
95+
hook: 'name'
96+
},
97+
'displayedConnection.hostname': {
98+
type: 'value',
99+
hook: 'hostname'
100+
},
101+
'displayedConnection.port': {
102+
type: 'value',
103+
hook: 'port'
104+
},
84105
has_error: {
85106
hook: 'message',
86107
type: 'booleanClass',
@@ -98,34 +119,57 @@ var ConnectView = View.extend({
98119
]
99120
},
100121
events: {
122+
'click .btn-info': 'onInfoClicked',
123+
'input [data-hook=name]': 'onNameChanged',
124+
'input [data-hook=port]': 'onPortChanged',
125+
'input [data-hook=hostname]': 'onHostNameChanged',
101126
'submit form': 'onSubmit'
102127
},
128+
onInfoClicked: function(evt) {
129+
evt.stopPropagation();
130+
// debug('info clicked');
131+
},
132+
onNameChanged: function(evt) {
133+
this.displayedConnection.name = evt.target.value;
134+
},
135+
onPortChanged: function(evt) {
136+
this.displayedConnection.port = parseInt(evt.target.value, 10);
137+
},
138+
onHostNameChanged: function(evt) {
139+
this.displayedConnection.hostname = evt.target.value;
140+
},
103141
initialize: function() {
104142
document.title = 'Connect to MongoDB';
105143
this.connections.fetch();
106144
},
107145
onSubmit: function(event) {
108146
event.stopPropagation();
109147
event.preventDefault();
110-
app.statusbar.show();
111-
this.message = format('Testing connection...');
112-
this.has_error = false;
113148

114-
var hostname = $(this.el).find('[name=hostname]').val() || 'localhost';
115-
var port = parseInt($(this.el).find('[name=port]').val() || 27017, 10);
116-
var name = $(this.el).find('[name=name]').val() || 'Local';
149+
this.has_error = false;
117150

118-
new Connection({
119-
name: name,
120-
hostname: hostname,
121-
port: port
122-
}).test(this.onConnectionTested.bind(this));
151+
var existingConnection = this.connections.get(this.displayedConnection.uri);
152+
if (existingConnection && existingConnection.name !== this.displayedConnection.name) {
153+
// the connection uri (`host:port`) exists already, but under a different name
154+
app.statusbar.hide();
155+
this.has_error = true;
156+
this.message = format('A connection to this host and port already exists '
157+
+ 'under the name "%s". Click "Connect" again to connect to this host.',
158+
existingConnection.name);
159+
this.displayedConnection.name = existingConnection.name;
160+
} else {
161+
// now test if the server is reachable
162+
app.statusbar.show();
163+
this.message = format('Testing connection...');
164+
this.displayedConnection.test(this.onConnectionTested.bind(this));
165+
}
123166
},
124167
onConnectionTested: function(err, model) {
125168
app.statusbar.hide();
126169
if (err) {
127170
this.has_error = true;
128-
this.message = format('Could not connect to %s', model.uri);
171+
this.message = format('Could not connect to %s, please check that a MongoDB instance '
172+
+ 'is running there.', model.uri);
129173
} else {
130174
model.save();
131175
this.connections.add(model);
@@ -135,10 +179,19 @@ var ConnectView = View.extend({
135179
setTimeout(this.set.bind(this, {
136180
message: ''
137181
}), 500);
138-
setTimeout(window.close, 1000);
182+
// setTimeout(window.close, 1000);
139183
}
140184
},
141-
template: require('./index.jade'),
185+
render: function() {
186+
this.renderWithTemplate(this);
187+
// enable popovers
188+
$(this.query('[data-toggle="popover"]'))
189+
.popover({
190+
container: 'body',
191+
placement: 'top',
192+
trigger: 'hover'
193+
});
194+
},
142195
subviews: {
143196
sidebar: {
144197
waitFor: 'connections',

src/connect/sidebar.jade

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
div
22
.sidebar.panel
33
.panel-heading(style='padding: 10px;')
4-
.panel-title Connections
4+
.panel-title My Connections
55
ul.list-group(data-hook='connection-list', style='top: 32px;')
66

77
.sidebar-bg

src/models/connection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var debug = require('debug')('scout:models:connection');
88
* Configuration for connecting to a MongoDB Deployment.
99
*/
1010
module.exports = Model.extend({
11-
namespace: 'Connection',
11+
modelType: 'Connection',
1212
idAttribute: 'uri',
1313
props: {
1414
/**

0 commit comments

Comments
 (0)