@@ -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 ,
@@ -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,34 +119,57 @@ var ConnectView = View.extend({
98
119
]
99
120
} ,
100
121
events : {
122
+ 'click .btn-info' : 'onInfoClicked' ,
123
+ 'input [data-hook=name]' : 'onNameChanged' ,
124
+ 'input [data-hook=port]' : 'onPortChanged' ,
125
+ 'input [data-hook=hostname]' : 'onHostNameChanged' ,
101
126
'submit form' : 'onSubmit'
102
127
} ,
128
+ onInfoClicked : function ( evt ) {
129
+ evt . stopPropagation ( ) ;
130
+ // debug('info clicked');
131
+ } ,
132
+ onNameChanged : function ( evt ) {
133
+ this . displayedConnection . name = evt . target . value ;
134
+ } ,
135
+ onPortChanged : function ( evt ) {
136
+ this . displayedConnection . port = parseInt ( evt . target . value , 10 ) ;
137
+ } ,
138
+ onHostNameChanged : function ( evt ) {
139
+ this . displayedConnection . hostname = evt . target . value ;
140
+ } ,
103
141
initialize : function ( ) {
104
142
document . title = 'Connect to MongoDB' ;
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...' ) ;
112
- this . has_error = false ;
113
148
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' ;
149
+ this . has_error = false ;
117
150
118
- new Connection ( {
119
- name : name ,
120
- hostname : hostname ,
121
- port : port
122
- } ) . test ( this . onConnectionTested . bind ( this ) ) ;
151
+ var existingConnection = this . connections . get ( this . displayedConnection . uri ) ;
152
+ if ( existingConnection && existingConnection . name !== this . displayedConnection . name ) {
153
+ // the connection uri (`host:port`) exists already, but under a different name
154
+ app . statusbar . hide ( ) ;
155
+ this . has_error = true ;
156
+ this . message = format ( 'A connection to this host and port already exists '
157
+ + 'under the name "%s". Click "Connect" again to connect to this host.' ,
158
+ existingConnection . name ) ;
159
+ this . displayedConnection . name = existingConnection . name ;
160
+ } else {
161
+ // now test if the server is reachable
162
+ app . statusbar . show ( ) ;
163
+ this . message = format ( 'Testing connection...' ) ;
164
+ this . displayedConnection . test ( this . onConnectionTested . bind ( this ) ) ;
165
+ }
123
166
} ,
124
167
onConnectionTested : function ( err , model ) {
125
168
app . statusbar . hide ( ) ;
126
169
if ( err ) {
127
170
this . has_error = true ;
128
- this . message = format ( 'Could not connect to %s' , model . uri ) ;
171
+ this . message = format ( 'Could not connect to %s, please check that a MongoDB instance '
172
+ + 'is running there.' , model . uri ) ;
129
173
} else {
130
174
model . save ( ) ;
131
175
this . connections . add ( model ) ;
@@ -135,10 +179,19 @@ var ConnectView = View.extend({
135
179
setTimeout ( this . set . bind ( this , {
136
180
message : ''
137
181
} ) , 500 ) ;
138
- setTimeout ( window . close , 1000 ) ;
182
+ // setTimeout(window.close, 1000);
139
183
}
140
184
} ,
141
- template : require ( './index.jade' ) ,
185
+ render : function ( ) {
186
+ this . renderWithTemplate ( this ) ;
187
+ // enable popovers
188
+ $ ( this . query ( '[data-toggle="popover"]' ) )
189
+ . popover ( {
190
+ container : 'body' ,
191
+ placement : 'top' ,
192
+ trigger : 'hover'
193
+ } ) ;
194
+ } ,
142
195
subviews : {
143
196
sidebar : {
144
197
waitFor : 'connections' ,
0 commit comments