@@ -5,6 +5,11 @@ var format = require('util').format;
5
5
var $ = require ( 'jquery' ) ;
6
6
var app = require ( 'ampersand-app' ) ;
7
7
8
+ // var debug = require('debug')('scout:connect:index');
9
+
10
+ require ( 'bootstrap/js/popover' ) ;
11
+ require ( 'bootstrap/js/tooltip' ) ;
12
+
8
13
var ConnectionView = View . extend ( {
9
14
props : {
10
15
model : Connection ,
@@ -14,7 +19,7 @@ var ConnectionView = View.extend({
14
19
}
15
20
} ,
16
21
events : {
17
- click : 'onClick' ,
22
+ ' click a' : 'onClick' ,
18
23
dblclick : 'onDoubleClick' ,
19
24
mouseover : 'onMouseOver' ,
20
25
mouseout : 'onMouseOut' ,
@@ -34,9 +39,8 @@ var ConnectionView = View.extend({
34
39
event . stopPropagation ( ) ;
35
40
event . preventDefault ( ) ;
36
41
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 ( ) ) ;
40
44
} ,
41
45
onDoubleClick : function ( event ) {
42
46
this . onClick ( event ) ;
@@ -55,6 +59,7 @@ var ConnectionView = View.extend({
55
59
}
56
60
} ) ;
57
61
62
+
58
63
var SidebarView = View . extend ( {
59
64
template : require ( './sidebar.jade' ) ,
60
65
render : function ( ) {
@@ -67,6 +72,10 @@ var SidebarView = View.extend({
67
72
* @todo (imlucas) Use ampersand-form-view.
68
73
*/
69
74
var ConnectView = View . extend ( {
75
+ template : require ( './index.jade' ) ,
76
+ children : {
77
+ displayedConnection : Connection
78
+ } ,
70
79
collections : {
71
80
connections : ConnectionCollection
72
81
} ,
@@ -81,6 +90,18 @@ var ConnectView = View.extend({
81
90
}
82
91
} ,
83
92
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
+ } ,
84
105
has_error : {
85
106
hook : 'message' ,
86
107
type : 'booleanClass' ,
@@ -98,38 +119,88 @@ var ConnectView = View.extend({
98
119
]
99
120
} ,
100
121
events : {
122
+ 'input [data-hook=name]' : 'onNameChanged' ,
123
+ 'input [data-hook=port]' : 'onPortChanged' ,
124
+ 'input [data-hook=hostname]' : 'onHostNameChanged' ,
101
125
'submit form' : 'onSubmit'
102
126
} ,
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
+ } ,
103
136
initialize : function ( ) {
104
137
document . title = 'Connect to MongoDB' ;
138
+ this . displayedConnection . set ( {
139
+ name : '' ,
140
+ portString : '' ,
141
+ hostname : ''
142
+ } ) ;
105
143
this . connections . fetch ( ) ;
106
144
} ,
107
145
onSubmit : function ( event ) {
108
146
event . stopPropagation ( ) ;
109
147
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
+
112
157
this . has_error = false ;
113
158
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
+ }
117
172
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 ) ) ;
123
191
} ,
124
192
onConnectionTested : function ( err , model ) {
125
193
app . statusbar . hide ( ) ;
126
194
if ( err ) {
127
195
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 ) ;
129
198
} 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
+ }
133
204
window . open ( format ( '%s?uri=%s#schema' , window . location . origin , model . uri ) ) ;
134
205
135
206
setTimeout ( this . set . bind ( this , {
@@ -138,7 +209,16 @@ var ConnectView = View.extend({
138
209
setTimeout ( window . close , 1000 ) ;
139
210
}
140
211
} ,
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
+ } ,
142
222
subviews : {
143
223
sidebar : {
144
224
waitFor : 'connections' ,
0 commit comments