@@ -8,12 +8,15 @@ var View = require('ampersand-view');
88
99var _ = require ( 'lodash' ) ;
1010var app = require ( 'ampersand-app' ) ;
11- var debug = require ( 'debug' ) ( 'scout:connect:index' ) ;
1211var format = require ( 'util' ) . format ;
1312var metrics = require ( 'mongodb-js-metrics' ) ;
1413
1514var remote = window . require ( 'remote' ) ;
15+ var dialog = remote . require ( 'dialog' ) ;
1616var Clipboard = remote . require ( 'clipboard' ) ;
17+ var BrowserWindow = remote . require ( 'browser-window' ) ;
18+
19+ var debug = require ( 'debug' ) ( 'scout:connect:index' ) ;
1720
1821/**
1922 * AuthenticationOptionCollection
@@ -66,6 +69,10 @@ var ConnectView = View.extend({
6669 previousSslMethod : {
6770 type : 'string' ,
6871 default : null
72+ } ,
73+ clipboardText : {
74+ type : 'string' ,
75+ default : ''
6976 }
7077 } ,
7178 derived : {
@@ -233,36 +240,53 @@ var ConnectView = View.extend({
233240 this . listenToAndRun ( this , 'change:sslMethod' ,
234241 this . replaceSslMethodFields . bind ( this ) ) ;
235242
236- this . listenTo ( app , 'autofill-connection-from-clipboard' ,
237- this . autofillFromClipboard . bind ( this ) ) ;
243+ // this.listenTo(app, 'autofill-connection-from-clipboard',
244+ // this.autofillFromClipboard.bind(this));
245+
238246 this . listenTo ( app , 'connect-window-focused' ,
239- this . checkIfCanAutofillFromClipboard . bind ( this ) ) ;
247+ this . onConnectWindowFocused . bind ( this ) ) ;
240248
241249 // always start in NEW_EMPTY state
242250 this . dispatch ( 'new connection clicked' ) ;
243251 } ,
244252
253+ // === MongoDB URI clipboard Handling
254+
255+ /**
256+ * Called when the user clicked "YES" in the message dialog after
257+ * a MongoDB URI was detected.
258+ */
245259 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 */
260+ this . connection = MongoDBConnection . from ( this . clipboardText ) ;
253261 this . updateForm ( ) ;
254262 } ,
255263
256- checkIfCanAutofillFromClipboard : function ( ) {
257- var cbTxt = Clipboard . readText ( ) ;
258- if ( cbTxt === this . lastClipboardTxt ) {
264+ /**
265+ * Called when the Connect Window receives focus.
266+ */
267+ onConnectWindowFocused : function ( ) {
268+ var clipboardText = Clipboard . readText ( ) ;
269+ if ( clipboardText === this . clipboardText ) {
270+ // we have seen this value already, don't ask user again
259271 return ;
260272 }
261- this . lastClipboardTxt = cbTxt ;
262-
263- if ( MongoDBConnection . isURI ( cbTxt ) ) {
264- debug ( 'mongoURI detected.' ) ;
265- app . sendMessage ( 'show autofill connection notification' ) ;
273+ this . clipboardText = clipboardText ;
274+
275+ if ( MongoDBConnection . isURI ( clipboardText ) ) {
276+ debug ( 'MongoDB URI detected.' , clipboardText ) ;
277+ // ask user if Compass should use it to fill out form
278+ dialog . showMessageBox ( BrowserWindow . getFocusedWindow ( ) , {
279+ type : 'info' ,
280+ message : 'MongoDB connection string detected' ,
281+ detail : 'Compass detected a MongoDB connection string in your '
282+ + 'clipboard. Do you want to use the connection string to '
283+ + 'fill out this form?' ,
284+ buttons : [ 'Yes' , 'No' ]
285+ } , function ( response ) {
286+ if ( response === 0 ) {
287+ this . autofillFromClipboard ( ) ;
288+ }
289+ } . bind ( this ) ) ;
266290 }
267291 } ,
268292
0 commit comments