Skip to content

Commit 885638b

Browse files
WaleyChenrueckstiess
authored andcommitted
INT-677 move autofill connect code from window-manager to connectview
1 parent 7e5e0bb commit 885638b

File tree

3 files changed

+60
-55
lines changed

3 files changed

+60
-55
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
"keytar": "^3.0.0",
7676
"localforage": "^1.3.0",
7777
"mongodb-collection-model": "^0.1.1",
78-
"mongodb-connection-model": "^3.0.6",
78+
"mongodb-connection-model": "^3.0.7",
7979
"mongodb-instance-model": "^1.0.2",
8080
"mongodb-ns": "^1.0.1",
8181
"mongodb-js-metrics": "^0.2.2",

src/connect/index.js

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
var View = require('ampersand-view');
2-
var SidebarView = require('./sidebar');
31
var BehaviorStateMachine = require('./behavior');
4-
var ConnectionCollection = require('../models/connection-collection');
52
var ConnectFormView = require('./connect-form-view');
63
var Connection = require('../models/connection');
7-
var debug = require('debug')('scout:connect:index');
4+
var ConnectionCollection = require('../models/connection-collection');
5+
var MongoDBConnection = require('mongodb-connection-model');
6+
var SidebarView = require('./sidebar');
7+
var View = require('ampersand-view');
8+
89
var _ = require('lodash');
910
var app = require('ampersand-app');
11+
var debug = require('debug')('scout:connect:index');
1012
var format = require('util').format;
1113
var metrics = require('mongodb-js-metrics');
1214

15+
var remote = window.require('remote');
16+
var Clipboard = remote.require('clipboard');
17+
1318
/**
1419
* AuthenticationOptionCollection
1520
*/
@@ -227,13 +232,40 @@ var ConnectView = View.extend({
227232
this.replaceAuthMethodFields.bind(this));
228233
this.listenToAndRun(this, 'change:sslMethod',
229234
this.replaceSslMethodFields.bind(this));
230-
this.listenTo(app, 'update-connection',
231-
this.updateConnectionFromMsg.bind(this));
235+
236+
this.listenTo(app, 'autofill-connection-from-clipboard',
237+
this.autofillFromClipboard.bind(this));
238+
this.listenTo(app, 'connect-window-focused',
239+
this.checkIfCanAutofillFromClipboard.bind(this));
232240

233241
// always start in NEW_EMPTY state
234242
this.dispatch('new connection clicked');
235243
},
236244

245+
autofillFromClipboard: function() {
246+
var connectionFromCB = MongoDBConnection.from(this.lastClipboardTxt).toJSON();
247+
this.connection = new Connection();
248+
/* eslint guard-for-in: 0 */
249+
for (var attr in connectionFromCB) {
250+
this.connection[attr] = connectionFromCB[attr];
251+
}
252+
/* eslint guard-for-in: 1 */
253+
this.updateForm();
254+
},
255+
256+
checkIfCanAutofillFromClipboard: function() {
257+
var cbTxt = Clipboard.readText();
258+
if (cbTxt === this.lastClipboardTxt) {
259+
return;
260+
}
261+
this.lastClipboardTxt = cbTxt;
262+
263+
if (MongoDBConnection.isURI(cbTxt)) {
264+
debug('mongoURI detected.');
265+
app.sendMessage('show autofill connection notification');
266+
}
267+
},
268+
237269
connectionNameEmptyChanged: function() {
238270
if (this.connectionNameEmpty) {
239271
this.dispatch('name removed');
@@ -242,7 +274,6 @@ var ConnectView = View.extend({
242274
}
243275
},
244276

245-
246277
// === External hooks
247278

248279
/**
@@ -310,16 +341,6 @@ var ConnectView = View.extend({
310341
});
311342
},
312343

313-
updateConnectionFromMsg: function(connection) {
314-
this.connection = new Connection();
315-
/* eslint guard-for-in: 0 */
316-
for (var attr in connection) {
317-
this.connection[attr] = connection[attr];
318-
}
319-
/* eslint guard-for-in: 1 */
320-
this.updateForm();
321-
},
322-
323344
/**
324345
*
325346
* remove favorite, then saves the connection (if it has been used before)

src/electron/window-manager.js

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,56 +7,43 @@
77
var AppMenu = require('./menu');
88
var BrowserWindow = require('browser-window');
99
var Clipboard = require('clipboard');
10-
var Connection = require('mongodb-connection-model');
1110
var Notifier = require('node-notifier');
11+
var Path = require('path');
1212

1313
var _ = require('lodash');
1414
var app = require('app');
1515
var config = require('./config');
1616
var debug = require('debug')('scout-electron:window-manager');
1717
var dialog = require('dialog');
18-
var path = require('path');
1918

2019
/**
2120
* When running in electron, we're in `RESOURCES/src/electron`.
2221
*/
23-
var RESOURCES = path.resolve(__dirname, '../../');
22+
var RESOURCES = Path.resolve(__dirname, '../../');
2423
var SCOUT_ICON_PATH = RESOURCES + '/images/scout.png';
2524

2625
/**
2726
* The app's HTML shell which is the output of `./src/index.jade`
2827
* created by the `build:pages` gulp task.
2928
*/
30-
var DEFAULT_URL = 'file://' + path.join(RESOURCES, 'index.html#connect');
29+
var DEFAULT_URL = 'file://' + Path.join(RESOURCES, 'index.html#connect');
3130

3231
/**
3332
* We want the Connect dialog window to be special
3433
* and for there to ever only be one instance of it
3534
* so we'll use scope to essentially make it a Singleton.
3635
*/
3736
var connectWindow;
38-
var curNotifierOnClickFn;
39-
var lastClipboardTxt;
4037

4138
// @todo (imlucas): Removed in setup branch as we dont need to do this anymore
4239
// as a `all-windows-closed` event has been added to the `app` event api
4340
// since this code was laid down.
4441
var windowsOpenCount = 0;
4542

46-
var autofillConnect = function() {
47-
debug('autofillConnect()');
48-
var connection = Connection.from(Clipboard.readText()).toJSON();
49-
connectWindow.webContents.send('message', 'update-connection', connection);
50-
};
51-
5243
function isConnectDialog(url) {
5344
return url === DEFAULT_URL;
5445
}
5546

56-
function isMongoURI(str) {
57-
return str.indexOf('mongodb://') > -1;
58-
}
59-
6047
// returns true if the application is a single instance application otherwise
6148
// focus the second window (which we'll quit from) and return false
6249
// see "app.makeSingleInstance" in https://github.com/atom/electron/blob/master/docs/api/app.md
@@ -137,22 +124,7 @@ module.exports.create = function(opts) {
137124
connectWindow = _window;
138125
connectWindow.on('focus', function() {
139126
debug('connect window focused.');
140-
var cbTxt = Clipboard.readText();
141-
if (cbTxt === lastClipboardTxt) {
142-
return;
143-
}
144-
lastClipboardTxt = cbTxt;
145-
146-
if (isMongoURI(cbTxt)) {
147-
debug('mongoURI detected.');
148-
curNotifierOnClickFn = autofillConnect;
149-
Notifier.notify({
150-
'icon': SCOUT_ICON_PATH,
151-
'message': 'Click this notification to autofill the connection fields from your clipboard.',
152-
'title': 'Autofill Connection',
153-
'wait': true
154-
});
155-
}
127+
connectWindow.webContents.send('message', 'connect-window-focused');
156128
});
157129
connectWindow.on('closed', function() {
158130
debug('connect window closed.');
@@ -196,6 +168,19 @@ app.on('show about dialog', function() {
196168
});
197169
});
198170

171+
app.on('show autofill connection notification', function() {
172+
Notifier.notify({
173+
'icon': SCOUT_ICON_PATH,
174+
'message': 'Click this notification to autofill the connection fields from your clipboard.',
175+
'title': 'Autofill Connection',
176+
'wait': true
177+
}, function(err, resp) {
178+
if (resp === 'Activate\n') {
179+
connectWindow.webContents.send('message', 'autofill-connection-from-clipboard');
180+
}
181+
});
182+
});
183+
199184
app.on('hide connect submenu', function() {
200185
AppMenu.hideConnect();
201186
});
@@ -218,12 +203,15 @@ app.on('show share submenu', function() {
218203

219204
app.on('show bugsnag OS notification', function(errorMsg) {
220205
if (_.contains(['development', 'testing'], process.env.NODE_ENV)) {
221-
curNotifierOnClickFn = openDevTools;
222206
Notifier.notify({
223207
'icon': SCOUT_ICON_PATH,
224208
'message': errorMsg,
225209
'title': 'MongoDB Compass Exception',
226210
'wait': true
211+
}, function(err, resp) {
212+
if (resp === 'Activate\n') {
213+
openDevTools();
214+
}
227215
});
228216
}
229217
});
@@ -236,10 +224,6 @@ app.on('show bugsnag OS notification', function(errorMsg) {
236224
*/
237225
app.on('ready', function() {
238226
app.emit('show connect dialog');
239-
240-
Notifier.on('click', function() {
241-
curNotifierOnClickFn();
242-
});
243227
});
244228

245229
var ipc = require('ipc');

0 commit comments

Comments
 (0)