Skip to content

Commit 3b177b0

Browse files
committed
🐛 Edit existing connection, fix flow control bugs
1 parent 49f850e commit 3b177b0

File tree

5 files changed

+69
-29
lines changed

5 files changed

+69
-29
lines changed

src/connect/connect-form-view.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
var app = require('ampersand-app');
21
var FormView = require('ampersand-form-view');
32
var InputView = require('./input-view');
43
var Connection = require('../models/connection');
@@ -9,6 +8,11 @@ require('bootstrap/js/popover');
98
require('bootstrap/js/tooltip');
109

1110
var ConnectFormView = FormView.extend({
11+
props: {
12+
connection_id: {
13+
type: 'string'
14+
}
15+
},
1216
namespace: 'ConnectFormView',
1317
/**
1418
* callback when user hits submit (or presses enter). Run some general checks here
@@ -17,6 +21,9 @@ var ConnectFormView = FormView.extend({
1721
* @param {Object} obj contains the clean()'ed up data from the form.
1822
*/
1923
submitCallback: function(obj) {
24+
if (this.connection_id !== '') {
25+
obj._id = this.connection_id;
26+
}
2027
debug('form submitted', obj);
2128
this.parent.onFormSubmitted(new Connection(obj));
2229
},

src/connect/index.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,14 @@ var ConnectView = View.extend({
189189
onAuthTabClicked: function(evt) {
190190
this.authMethod = $(evt.target).data('method');
191191
},
192+
createNewConnection: function() {
193+
debug('new connection requested');
194+
this.reset();
195+
this.form.connection_id = '';
196+
this.form.reset();
197+
this.authMethod = null;
198+
this.authOpen = false;
199+
},
192200

193201
/**
194202
* Triggers when the auth methods has changed (or set back to null)
@@ -221,21 +229,15 @@ var ConnectView = View.extend({
221229
* a list item like in `./sidebar`.
222230
*
223231
* @param {Connection} model
224-
* @param {Object} [options]
225-
* @option {Boolean} close - Close the connect dialog on success [Default: `false`].
226232
* @api public
227233
*/
228-
connect: function(model, options) {
229-
options = _.defaults(options || {}, {
230-
close: false
231-
});
232-
234+
connect: function(model) {
233235
app.statusbar.show();
234236

235237
debug('testing credentials are usable...');
236238
model.test(function(err) {
237239
if (!err) {
238-
this.onConnectionSuccessful(model, options);
240+
this.onConnectionSuccessful(model);
239241
return;
240242
}
241243

@@ -256,7 +258,7 @@ var ConnectView = View.extend({
256258
this.onError(new Error('Could not connect to MongoDB.'), model);
257259
return;
258260
}
259-
this.onConnectionSuccessful(model, options);
261+
this.onConnectionSuccessful(model);
260262
}.bind(this));
261263
}.bind(this));
262264
},
@@ -266,13 +268,12 @@ var ConnectView = View.extend({
266268
* view using it.
267269
*
268270
* @param {Connection} model
269-
* @param {Object} [options]
270271
* @api private
271272
*/
272-
onConnectionSuccessful: function(model, options) {
273-
options = _.defaults(options, {
274-
close: true
275-
});
273+
onConnectionSuccessful: function(model) {
274+
app.statusbar.hide();
275+
this.form.connection_id = '';
276+
276277
/**
277278
* The save method will handle calling the correct method
278279
* of the sync being used by the model, whether that's
@@ -301,9 +302,7 @@ var ConnectView = View.extend({
301302
message: ''
302303
}), 500);
303304

304-
if (options.close) {
305-
setTimeout(window.close, 1000);
306-
}
305+
setTimeout(window.close, 1000);
307306
},
308307
/**
309308
* If there is a validation or connection error show a nice message.
@@ -350,6 +349,7 @@ var ConnectView = View.extend({
350349

351350
if (!model.isValid()) {
352351
this.onError(model.validationError);
352+
return;
353353
}
354354

355355
this.connect(model);
@@ -363,7 +363,7 @@ var ConnectView = View.extend({
363363
* @api public
364364
*/
365365
onConnectionSelected: function(model) {
366-
// If the new model has auth, expand the auth options container
366+
// If the new model has auth, expand the auth settings container
367367
// and select the correct tab.
368368
this.authMethod = model.auth_mechanism;
369369

@@ -385,6 +385,8 @@ var ConnectView = View.extend({
385385
debug('Populating form fields with keys', keys);
386386
var values = _.pick(model, keys);
387387

388+
this.form.connection_id = model.getId();
389+
388390
// Populates the form from values in the model.
389391
this.form.setValues(values);
390392
},

src/connect/sidebar.jade

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
div
22
.sidebar.panel
33
.panel-heading(style='padding: 10px;')
4-
.panel-title Saved Connections
5-
ul.list-group(data-hook='connection-list', style='top: 32px;')
4+
.panel-title Recent Connections
5+
ul.list-group(data-hook='connection-list', style='top: 32px;position: initial;')
6+
ul.list-group(data-hook='connection-list-controls', style='top: initial;')
7+
li.list-group-item
8+
a(href="#new-connection", data-hook="new-connection")
9+
i.fa.fa-fw.fa-plus
10+
| New Connection
611
.sidebar-bg

src/connect/sidebar.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ var SidebarItemView = View.extend({
4646
},
4747
template: require('./connection.jade'),
4848
onClick: function(event) {
49-
this.parent.onItemClick(event, this.model);
49+
this.parent.onItemClick(event, this);
5050
},
5151
onDoubleClick: function(event) {
52-
this.parent.onItemDoubleClick(event, this.model);
52+
this.parent.onItemDoubleClick(event, this);
5353
},
5454
onCloseClick: function(event) {
5555
event.stopPropagation();
@@ -69,20 +69,44 @@ var SidebarItemView = View.extend({
6969
* Renders all existing connections as list in the sidebar.
7070
*/
7171
var SidebarView = View.extend({
72+
session: {
73+
active_item_view: {
74+
type: 'state'
75+
}
76+
},
77+
events: {
78+
'click a[data-hook=new-connection]': 'onNewConnectionClick'
79+
},
7280
namespace: 'SidebarView',
7381
template: require('./sidebar.jade'),
7482
render: function() {
7583
this.renderWithTemplate();
7684
this.renderCollection(this.collection, SidebarItemView, this.queryByHook('connection-list'));
7785
},
78-
onItemClick: function(event, model) {
86+
onNewConnectionClick: function(event) {
7987
event.stopPropagation();
8088
event.preventDefault();
81-
this.parent.onConnectionSelected(model);
89+
90+
if (this.active_item_view) {
91+
this.active_item_view.el.classList.remove('active');
92+
this.active_item_view = null;
93+
}
94+
this.parent.createNewConnection();
95+
},
96+
onItemClick: function(event, view) {
97+
event.stopPropagation();
98+
event.preventDefault();
99+
if (this.active_item_view) {
100+
this.active_item_view.el.classList.remove('active');
101+
}
102+
103+
this.active_item_view = view;
104+
this.active_item_view.el.classList.add('active');
105+
this.parent.onConnectionSelected(view.model);
82106
},
83-
onItemDoubleClick: function(event, model) {
84-
this.onItemClick(event, model);
85-
this.parent.connect(model);
107+
onItemDoubleClick: function(event, view) {
108+
this.onItemClick(event, view);
109+
this.parent.connect(view.model);
86110
}
87111
});
88112

src/models/connection-collection.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ var restMixin = require('ampersand-collection-rest-mixin');
77
module.exports = Collection.extend(lodashMixin, restMixin, {
88
namespace: 'ConnectionCollection',
99
model: Connection,
10-
comparator: 'last_used',
10+
comparator: function(model) {
11+
return -model.last_used;
12+
},
1113
mainIndex: '_id',
1214
sync: connectionSync
1315
});

0 commit comments

Comments
 (0)