Skip to content
This repository
Browse code

Candy.Core.connect(JID) will prompt user for password. fixes #2

  • Loading branch information...
commit b6680d48f3268462d72eff7a915f132a829b32ca 1 parent cfb03c8
Patrick Stadler authored August 06, 2011
3  .gitignore
... ...
@@ -1,5 +1,6 @@
  1
+docs
  2
+example/.htaccess
1 3
 .DS_Store
2 4
 ._*
3 5
 .ndproj
4  
-docs
5 6
 .idea
65  candy.bundle.js
@@ -29,7 +29,7 @@ var Candy = (function(self, $) {
29 29
 	 */
30 30
 	self.about = {
31 31
 		name: 'Candy',
32  
-		version: '1.0'
  32
+		version: '1.1-dev'
33 33
 	};
34 34
 
35 35
 	/** Function: init
@@ -180,37 +180,40 @@ Candy.Core = (function(self, Strophe, $) {
180 180
 	};
181 181
 
182 182
 	/** Function: connect
183  
-	 * Connect by javascript to the jabber server.
  183
+	 * Connect to the jabber host.
184 184
 	 *
185  
-	 * There are three different procedures to login:
  185
+	 * There are four different procedures to login:
186 186
 	 *   connect('JID', 'password') - Connect a registered user
187  
-	 *   connect('domain') - Connect anonymously
188  
-	 *   connect() - Show login form to let the user enter his JID / password manually
  187
+	 *   connect('domain') - Connect anonymously to the domain. The user should receive a random JID.
  188
+	 *   connect('JID') - Show login form and prompt for password. JID input is hidden.
  189
+	 *   connect() - Show login form and prompt for JID and password.
189 190
 	 * 
190 191
 	 * See:
191 192
 	 *   <Candy.Core.attach()> for attaching an already established session.
192 193
 	 *
193 194
 	 * Parameters:
194  
-	 *   (String) jidOrHost - Jabber ID or Host [supply host when anonymous login should be attempted]
195  
-	 *   (String) password - Password of the user [optional, when anonymous login should be attempted]
  195
+	 *   (String) jidOrHost - JID or Host
  196
+	 *   (String) password - Password of the user
196 197
 	 */
197 198
 	self.connect = function(jidOrHost, password) {
198 199
 		// Reset before every connection attempt to make sure reconnections work after authfail, alltabsclosed, ...
199 200
 		_connection.reset();
200 201
 		_registerEventHandlers();
201  
-		// Register event handlers
202  
-		if(!jidOrHost && !password) {
203  
-			// display login window
204  
-			Candy.Core.Event.Login();
205  
-		} else {
206  
-			if(jidOrHost && password) {
207  
-				// authentication
208  
-				_connection.connect(jidOrHost + '/' + Candy.about.name, password, Candy.Core.Event.Strophe.Connect);
209  
-				_user = new self.ChatUser(jidOrHost, Strophe.getNodeFromJid(jidOrHost));
210  
-			} else {
211  
-				// anonymous login
  202
+		if(jidOrHost && password) {
  203
+			// authentication
  204
+			_connection.connect(jidOrHost + '/' + Candy.about.name, password, Candy.Core.Event.Strophe.Connect);
  205
+			_user = new self.ChatUser(jidOrHost, Strophe.getNodeFromJid(jidOrHost));
  206
+		} else if(jidOrHost) {
  207
+			if(jidOrHost.indexOf("@") < 0) {
  208
+				// Not a JID, anonymous login
212 209
 				_connection.connect(jidOrHost, null, Candy.Core.Event.Strophe.Connect);
  210
+			} else {
  211
+				// Most likely a JID, display login modal
  212
+				Candy.Core.Event.Login(jidOrHost);
213 213
 			}
  214
+		} else {
  215
+			// display login modal
  216
+			Candy.Core.Event.Login();
214 217
 		}
215 218
 	};
216 219
 
@@ -1682,9 +1685,12 @@ Candy.Core.Event = (function(self, Strophe, $, observable) {
1682 1685
 
1683 1686
 	/** Function: Login
1684 1687
 	 * Notify view that the login window should be displayed
  1688
+	 *
  1689
+	 * Parameters:
  1690
+	 *   (String) presetJid - Preset user JID
1685 1691
 	 */
1686  
-	self.Login = function() {
1687  
-		self.notifyObservers(self.KEYS.LOGIN);
  1692
+	self.Login = function(presetJid) {
  1693
+		self.notifyObservers(self.KEYS.LOGIN, { presetJid: presetJid } );
1688 1694
 	};
1689 1695
 
1690 1696
 	/** Class: Candy.Core.Event.Jabber
@@ -2348,10 +2354,10 @@ Candy.View.Observer = (function(self, $) {
2348 2354
 		 *
2349 2355
 		 * Parameters:
2350 2356
 		 *   (Candy.Core.Event) obj - Candy core event object
2351  
-		 *   (Object) args - undefined
  2357
+		 *   (Object) args - {presetJid}
2352 2358
 		 */
2353 2359
 		update: function(obj, args) {
2354  
-			Candy.View.Pane.Chat.Modal.showLoginForm();
  2360
+			Candy.View.Pane.Chat.Modal.showLoginForm(null, args.presetJid);
2355 2361
 		}
2356 2362
 	};
2357 2363
 
@@ -2926,12 +2932,14 @@ Candy.View.Pane = (function(self, $) {
2926 2932
 			 *
2927 2933
 			 * Parameters:
2928 2934
 			 *  (String) message - optional message to display above the form
  2935
+			 *	(String) presetJid - optional user jid. if set, the user will only be prompted for password.
2929 2936
 			 */
2930  
-			showLoginForm: function(message) {
  2937
+			showLoginForm: function(message, presetJid) {
2931 2938
 				Candy.View.Pane.Chat.Modal.show((message ? message : '') + Mustache.to_html(Candy.View.Template.Login.form, {
2932 2939
 					_labelUsername: $.i18n._('labelUsername'),
2933 2940
 					_labelPassword: $.i18n._('labelPassword'),
2934  
-					_loginSubmit  : $.i18n._('loginSubmit')
  2941
+					_loginSubmit  : $.i18n._('loginSubmit'),
  2942
+					presetJid : (presetJid ? presetJid : false)
2935 2943
 				}));
2936 2944
 
2937 2945
 				// register submit handler
@@ -2939,7 +2947,8 @@ Candy.View.Pane = (function(self, $) {
2939 2947
 					var username = $('#username').val(),
2940 2948
 						password = $('#password').val(),
2941 2949
 						// guess the input and create a jid out of it
2942  
-						jid = Candy.Core.getUser() && username.indexOf("@") < 0 ? username + '@' + Strophe.getDomainFromJid(Candy.Core.getUser().getJid()) : username;
  2950
+						jid = Candy.Core.getUser() && username.indexOf("@") < 0 ?
  2951
+							username + '@' + Strophe.getDomainFromJid(Candy.Core.getUser().getJid()) : username;
2943 2952
 						
2944 2953
 					if(jid.indexOf("@") < 0 && !Candy.Core.getUser()) {
2945 2954
 						Candy.View.Pane.Chat.Modal.showLoginForm($.i18n._('loginInvalid'));
@@ -3945,7 +3954,11 @@ Candy.View.Template = (function(self){
3945 3954
 	};
3946 3955
 
3947 3956
 	self.Login = {
3948  
-		form: '<form method="post" id="login-form" class="login-form"><label for="username">{{_labelUsername}}</label><input type="text" id="username" name="username"/><label for="password">{{_labelPassword}}</label><input type="password" id="password" name="password" /><input type="submit" class="button" value="{{_loginSubmit}}" /></form>'
  3957
+		form: '<form method="post" id="login-form" class="login-form">'
  3958
+			+ '{{^presetJid}}<label for="username">{{_labelUsername}}</label><input type="text" id="username" name="username"/>{{/presetJid}}'
  3959
+			+ '{{#presetJid}}<input type="hidden" id="username" name="username" value="{{presetJid}}"/>{{/presetJid}}'
  3960
+			+ '<label for="password">{{_labelPassword}}</label><input type="password" id="password" name="password" />'
  3961
+			+ '<input type="submit" class="button" value="{{_loginSubmit}}" /></form>'
3949 3962
 	};
3950 3963
 
3951 3964
 	return self;
2  candy.min.js
1 addition, 1 deletion not shown
2  src/candy.js
@@ -29,7 +29,7 @@ var Candy = (function(self, $) {
29 29
 	 */
30 30
 	self.about = {
31 31
 		name: 'Candy',
32  
-		version: '1.0'
  32
+		version: '1.1-dev'
33 33
 	};
34 34
 
35 35
 	/** Function: init
37  src/core.js
@@ -128,37 +128,40 @@ Candy.Core = (function(self, Strophe, $) {
128 128
 	};
129 129
 
130 130
 	/** Function: connect
131  
-	 * Connect by javascript to the jabber server.
  131
+	 * Connect to the jabber host.
132 132
 	 *
133  
-	 * There are three different procedures to login:
  133
+	 * There are four different procedures to login:
134 134
 	 *   connect('JID', 'password') - Connect a registered user
135  
-	 *   connect('domain') - Connect anonymously
136  
-	 *   connect() - Show login form to let the user enter his JID / password manually
  135
+	 *   connect('domain') - Connect anonymously to the domain. The user should receive a random JID.
  136
+	 *   connect('JID') - Show login form and prompt for password. JID input is hidden.
  137
+	 *   connect() - Show login form and prompt for JID and password.
137 138
 	 * 
138 139
 	 * See:
139 140
 	 *   <Candy.Core.attach()> for attaching an already established session.
140 141
 	 *
141 142
 	 * Parameters:
142  
-	 *   (String) jidOrHost - Jabber ID or Host [supply host when anonymous login should be attempted]
143  
-	 *   (String) password - Password of the user [optional, when anonymous login should be attempted]
  143
+	 *   (String) jidOrHost - JID or Host
  144
+	 *   (String) password - Password of the user
144 145
 	 */
145 146
 	self.connect = function(jidOrHost, password) {
146 147
 		// Reset before every connection attempt to make sure reconnections work after authfail, alltabsclosed, ...
147 148
 		_connection.reset();
148 149
 		_registerEventHandlers();
149  
-		// Register event handlers
150  
-		if(!jidOrHost && !password) {
151  
-			// display login window
152  
-			Candy.Core.Event.Login();
153  
-		} else {
154  
-			if(jidOrHost && password) {
155  
-				// authentication
156  
-				_connection.connect(jidOrHost + '/' + Candy.about.name, password, Candy.Core.Event.Strophe.Connect);
157  
-				_user = new self.ChatUser(jidOrHost, Strophe.getNodeFromJid(jidOrHost));
158  
-			} else {
159  
-				// anonymous login
  150
+		if(jidOrHost && password) {
  151
+			// authentication
  152
+			_connection.connect(jidOrHost + '/' + Candy.about.name, password, Candy.Core.Event.Strophe.Connect);
  153
+			_user = new self.ChatUser(jidOrHost, Strophe.getNodeFromJid(jidOrHost));
  154
+		} else if(jidOrHost) {
  155
+			if(jidOrHost.indexOf("@") < 0) {
  156
+				// Not a JID, anonymous login
160 157
 				_connection.connect(jidOrHost, null, Candy.Core.Event.Strophe.Connect);
  158
+			} else {
  159
+				// Most likely a JID, display login modal
  160
+				Candy.Core.Event.Login(jidOrHost);
161 161
 			}
  162
+		} else {
  163
+			// display login modal
  164
+			Candy.Core.Event.Login();
162 165
 		}
163 166
 	};
164 167
 
7  src/core/event.js
@@ -103,9 +103,12 @@ Candy.Core.Event = (function(self, Strophe, $, observable) {
103 103
 
104 104
 	/** Function: Login
105 105
 	 * Notify view that the login window should be displayed
  106
+	 *
  107
+	 * Parameters:
  108
+	 *   (String) presetJid - Preset user JID
106 109
 	 */
107  
-	self.Login = function() {
108  
-		self.notifyObservers(self.KEYS.LOGIN);
  110
+	self.Login = function(presetJid) {
  111
+		self.notifyObservers(self.KEYS.LOGIN, { presetJid: presetJid } );
109 112
 	};
110 113
 
111 114
 	/** Class: Candy.Core.Event.Jabber
4  src/view/observer.js
@@ -182,10 +182,10 @@ Candy.View.Observer = (function(self, $) {
182 182
 		 *
183 183
 		 * Parameters:
184 184
 		 *   (Candy.Core.Event) obj - Candy core event object
185  
-		 *   (Object) args - undefined
  185
+		 *   (Object) args - {presetJid}
186 186
 		 */
187 187
 		update: function(obj, args) {
188  
-			Candy.View.Pane.Chat.Modal.showLoginForm();
  188
+			Candy.View.Pane.Chat.Modal.showLoginForm(null, args.presetJid);
189 189
 		}
190 190
 	};
191 191
 
9  src/view/pane.js
@@ -568,12 +568,14 @@ Candy.View.Pane = (function(self, $) {
568 568
 			 *
569 569
 			 * Parameters:
570 570
 			 *  (String) message - optional message to display above the form
  571
+			 *	(String) presetJid - optional user jid. if set, the user will only be prompted for password.
571 572
 			 */
572  
-			showLoginForm: function(message) {
  573
+			showLoginForm: function(message, presetJid) {
573 574
 				Candy.View.Pane.Chat.Modal.show((message ? message : '') + Mustache.to_html(Candy.View.Template.Login.form, {
574 575
 					_labelUsername: $.i18n._('labelUsername'),
575 576
 					_labelPassword: $.i18n._('labelPassword'),
576  
-					_loginSubmit  : $.i18n._('loginSubmit')
  577
+					_loginSubmit  : $.i18n._('loginSubmit'),
  578
+					presetJid : (presetJid ? presetJid : false)
577 579
 				}));
578 580
 
579 581
 				// register submit handler
@@ -581,7 +583,8 @@ Candy.View.Pane = (function(self, $) {
581 583
 					var username = $('#username').val(),
582 584
 						password = $('#password').val(),
583 585
 						// guess the input and create a jid out of it
584  
-						jid = Candy.Core.getUser() && username.indexOf("@") < 0 ? username + '@' + Strophe.getDomainFromJid(Candy.Core.getUser().getJid()) : username;
  586
+						jid = Candy.Core.getUser() && username.indexOf("@") < 0 ?
  587
+							username + '@' + Strophe.getDomainFromJid(Candy.Core.getUser().getJid()) : username;
585 588
 						
586 589
 					if(jid.indexOf("@") < 0 && !Candy.Core.getUser()) {
587 590
 						Candy.View.Pane.Chat.Modal.showLoginForm($.i18n._('loginInvalid'));
6  src/view/template.js
@@ -60,7 +60,11 @@ Candy.View.Template = (function(self){
60 60
 	};
61 61
 
62 62
 	self.Login = {
63  
-		form: '<form method="post" id="login-form" class="login-form"><label for="username">{{_labelUsername}}</label><input type="text" id="username" name="username"/><label for="password">{{_labelPassword}}</label><input type="password" id="password" name="password" /><input type="submit" class="button" value="{{_loginSubmit}}" /></form>'
  63
+		form: '<form method="post" id="login-form" class="login-form">'
  64
+			+ '{{^presetJid}}<label for="username">{{_labelUsername}}</label><input type="text" id="username" name="username"/>{{/presetJid}}'
  65
+			+ '{{#presetJid}}<input type="hidden" id="username" name="username" value="{{presetJid}}"/>{{/presetJid}}'
  66
+			+ '<label for="password">{{_labelPassword}}</label><input type="password" id="password" name="password" />'
  67
+			+ '<input type="submit" class="button" value="{{_loginSubmit}}" /></form>'
64 68
 	};
65 69
 
66 70
 	return self;

0 notes on commit b6680d4

Please sign in to comment.
Something went wrong with that request. Please try again.