Skip to content

Commit 877a9bd

Browse files
committed
Merge branch 'INT-424-connect-name'
2 parents e446f89 + ff71697 commit 877a9bd

File tree

8 files changed

+135
-40
lines changed

8 files changed

+135
-40
lines changed

src/connect/index.jade

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
.page
1+
.page.connect
22
.content.with-sidebar
33
.container-fluid
44
form.form-horizontal
55
.message.alert(style='margin-top: 10px;', data-hook='message')
6-
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')
116
.form-group
127
label.col-sm-2.control-label Hostname
138
.col-sm-10
14-
input.form-control(type='text', name='hostname', placeholder='localhost')
9+
input.form-control(type='text', name='hostname', placeholder='localhost', data-hook='hostname')
1510
.form-group
1611
label.col-sm-2.control-label Port
1712
.col-sm-10
18-
input.form-control(type='number', name='port', placeholder='27017')
13+
input.form-control(type='number', name='port', placeholder='27017', data-hook='port', min=0)
14+
hr
15+
.form-group
16+
label.col-sm-2.control-label Save As... (optional)
17+
i.fa.fa-info-circle(tabindex="0", role="button", data-toggle="popover", data-trigger="focus", data-content="You can choose a friendly connection name for this connection, for example \"Development Server\". If you enter a name here, the connection will be saved and appears on the left side.")
18+
.col-sm-10
19+
input.form-control(type='text', name='name', placeholder='Connection Name', data-hook='name')
1920
.form-group
2021
.col-sm-offset-2.col-sm-10
2122
button.btn.btn-primary(type='submit') Connect
22-
23+
2324
div(data-hook='sidebar-subview')

src/connect/index.js

Lines changed: 99 additions & 19 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,
@@ -14,7 +19,7 @@ var ConnectionView = View.extend({
1419
}
1520
},
1621
events: {
17-
click: 'onClick',
22+
'click a': 'onClick',
1823
dblclick: 'onDoubleClick',
1924
mouseover: 'onMouseOver',
2025
mouseout: 'onMouseOut',
@@ -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,38 +119,88 @@ var ConnectView = View.extend({
98119
]
99120
},
100121
events: {
122+
'input [data-hook=name]': 'onNameChanged',
123+
'input [data-hook=port]': 'onPortChanged',
124+
'input [data-hook=hostname]': 'onHostNameChanged',
101125
'submit form': 'onSubmit'
102126
},
127+
onNameChanged: function(evt) {
128+
this.displayedConnection.name = evt.target.value;
129+
},
130+
onPortChanged: function(evt) {
131+
this.displayedConnection.portString = evt.target.value;
132+
},
133+
onHostNameChanged: function(evt) {
134+
this.displayedConnection.hostname = evt.target.value;
135+
},
103136
initialize: function() {
104137
document.title = 'Connect to MongoDB';
138+
this.displayedConnection.set({
139+
name: '',
140+
portString: '',
141+
hostname: ''
142+
});
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...');
148+
149+
// choose default connection values if unset
150+
if (!this.displayedConnection.hostname) {
151+
this.displayedConnection.unset('hostname');
152+
}
153+
if (!this.displayedConnection.portString) {
154+
this.displayedConnection.unset('portString');
155+
}
156+
112157
this.has_error = false;
113158

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';
159+
var existingConnection = this.connections.get(this.displayedConnection.uri);
160+
if (this.displayedConnection.name !== ''
161+
&& existingConnection
162+
&& existingConnection.name !== this.displayedConnection.name) {
163+
// the connection uri (`host:port`) exists already, but under a different name
164+
app.statusbar.hide();
165+
this.has_error = true;
166+
this.message = format('This connection already exists '
167+
+ 'under the name "%s". Click "Connect" again to use this connection.',
168+
existingConnection.name);
169+
this.displayedConnection.name = existingConnection.name;
170+
return;
171+
}
117172

118-
new Connection({
119-
name: name,
120-
hostname: hostname,
121-
port: port
122-
}).test(this.onConnectionTested.bind(this));
173+
// now test if the connection name already exists with another uri
174+
existingConnection = this.connections.get(this.displayedConnection.name, 'name');
175+
if (this.displayedConnection.name !== ''
176+
&& existingConnection
177+
&& existingConnection.uri !== this.displayedConnection.uri) {
178+
// the connection name exists already, but with a different uri
179+
app.statusbar.hide();
180+
this.has_error = true;
181+
this.message = format('Another connection with the name "%s" already exists. Please '
182+
+ 'choose a different name.',
183+
existingConnection.name);
184+
return;
185+
}
186+
187+
// now test if the server is reachable
188+
app.statusbar.show();
189+
this.message = '';
190+
this.displayedConnection.test(this.onConnectionTested.bind(this));
123191
},
124192
onConnectionTested: function(err, model) {
125193
app.statusbar.hide();
126194
if (err) {
127195
this.has_error = true;
128-
this.message = format('Could not connect to %s', model.uri);
196+
this.message = format('Could not connect to %s, please check that a MongoDB instance '
197+
+ 'is running there.', model.uri);
129198
} else {
130-
model.save();
131-
this.connections.add(model);
132-
this.message = 'Connected!';
199+
if (model.name !== '') {
200+
// only store if the user chose a name to save the connection
201+
model.save();
202+
this.connections.add(model);
203+
}
133204
window.open(format('%s?uri=%s#schema', window.location.origin, model.uri));
134205

135206
setTimeout(this.set.bind(this, {
@@ -138,7 +209,16 @@ var ConnectView = View.extend({
138209
setTimeout(window.close, 1000);
139210
}
140211
},
141-
template: require('./index.jade'),
212+
render: function() {
213+
this.renderWithTemplate(this);
214+
// enable popovers
215+
$(this.query('[data-toggle="popover"]'))
216+
.popover({
217+
container: 'body',
218+
placement: 'top',
219+
trigger: 'hover'
220+
});
221+
},
142222
subviews: {
143223
sidebar: {
144224
waitFor: 'connections',

src/connect/index.less

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.connect form {
2+
margin-top: 30px;
3+
}

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 Saved Connections
55
ul.list-group(data-hook='connection-list', style='top: 32px;')
66

77
.sidebar-bg

src/electron/window-manager.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ var DEFAULT_HEIGHT = 700;
1515
var DEFAULT_HEIGHT_DIALOG;
1616

1717
if (process.platform === 'win32') {
18-
DEFAULT_HEIGHT_DIALOG = 460;
18+
DEFAULT_HEIGHT_DIALOG = 500;
1919
} else if (process.platform === 'linux') {
20-
DEFAULT_HEIGHT_DIALOG = 430;
20+
DEFAULT_HEIGHT_DIALOG = 470;
2121
} else {
22-
DEFAULT_HEIGHT_DIALOG = 400;
22+
DEFAULT_HEIGHT_DIALOG = 440;
2323
}
24-
var DEFAULT_WIDTH_DIALOG = 600;
24+
var DEFAULT_WIDTH_DIALOG = 640;
2525

2626
var connectWindow;
2727
var windowsOpenCount = 0;

src/index.less

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
// Components
55
@import "./src/collection-stats/index.less";
6+
@import "./src/connect/index.less";
67
@import "./src/document-list/index.less";
78
@import "./src/field-list/index.less";
89
@import "./src/home/index.less";

src/models/connection-collection.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ module.exports = Collection.extend(lodashMixin, restMixin, {
99
model: Connection,
1010
comparator: 'last_used',
1111
mainIndex: 'uri',
12+
indexes: ['name'],
1213
sync: connectionSync
1314
});

src/models/connection.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,45 @@ 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
/**
1515
* User specified name for this connection.
1616
*/
1717
name: {
1818
type: 'string',
19-
default: 'Local'
19+
default: '',
20+
required: true
2021
},
2122
/**
2223
* Hostname or IP address of the Instance to connect to in the Deployment.
2324
*/
2425
hostname: {
2526
type: 'string',
26-
default: 'localhost'
27+
default: 'localhost',
28+
required: true
2729
},
2830
/**
2931
* Port the Instance to connect to in the Deployment is listening on.
3032
*/
31-
port: {
32-
type: 'number',
33-
default: 27017
33+
portString: {
34+
type: 'string',
35+
default: '27017',
36+
required: true
3437
},
3538
/**
3639
* Updated on each successful connection to the Deployment.
3740
*/
3841
last_used: 'date'
3942
},
4043
derived: {
44+
port: {
45+
deps: ['portString'],
46+
fn: function() {
47+
return parseInt(this.portString, 10);
48+
}
49+
},
4150
uri: {
4251
deps: ['hostname', 'port'],
4352
fn: function() {
@@ -47,7 +56,7 @@ module.exports = Model.extend({
4756
},
4857
use: function(uri) {
4958
var data = types.url(uri).data;
50-
this.port = data.hosts[0].port;
59+
this.portString = '' + data.hosts[0].port;
5160
this.hostname = data.hosts[0].host.toLowerCase();
5261
this.fetch();
5362
},

0 commit comments

Comments
 (0)