Add subscribe feature
Benjamin Renard

Benjamin Renard commited on 2014-01-18 21:41:59
Showing 3 changed files, with 132 additions and 1 deletions.

... ...
@@ -470,7 +470,51 @@ on_valid_login_modal=function(e) {
470 470
   );
471 471
 }
472 472
 
473
+on_click_go_to_subscribe_btn=function() {
474
+  $('#login_modal').modal('hide');
475
+  $('#subscribe_modal').modal('show');
476
+}
477
+
478
+/********************
479
+ * Subscribe
480
+ ********************/
481
+on_close_subscribe_modal=function(e) {
482
+  $('#subscribe_modal form')[0].reset();
483
+}
473 484
 
485
+sync_server=false;
486
+on_valid_subscribe_modal=function(e) {
487
+  e.preventDefault();
488
+  email=$('#subscribe_modal #subscribe_email')[0].value;
489
+  name=$('#subscribe_modal #subscribe_name')[0].value;
490
+  pass=$('#subscribe_modal #subscribe_pass')[0].value;
491
+  server=$('#subscribe_modal #subscribe_server')[0].value;
492
+  $('#subscribe_modal').modal('hide');
493
+  pleaseWaitShow();
494
+  sync_server.subscribe(server,email,name,pass,
495
+    function(data) {
496
+      localStorage.user=JSON.stringify({
497
+        'name': data.name,
498
+        'email': data.email,
499
+        'password': data.password,
500
+        'server': server,
501
+      });
502
+      pleaseWaitHide();
503
+      logged_menu();
504
+      alert('Bienvenu '+data.name+' !');
505
+    },
506
+    function(data) {
507
+      pleaseWaitHide();
508
+      if (jQuery.type(data) == 'object' && jQuery.type(data.subscribeerror)!='undefined') {
509
+        alert(data.subscribeerror);
510
+      }
511
+      else {
512
+        console.log(data);
513
+        alert('Erreur durant la connexion au serveur');
514
+      }
515
+    }
516
+  );
517
+}
474 518
 
475 519
 /********************
476 520
  * Sync
... ...
@@ -531,8 +575,10 @@ logged_out_menu=function() {
531 575
   $('#user-name').html('Connexion');
532 576
   $('#user-menu li').remove();
533 577
   menu=$('#user-menu');
534
-  menu.html("<li><a id='login_btn'>Connexion</a></li>");
578
+  menu.html("<li><a id='login_btn'>Connexion</a></li>"+
579
+	"<li><a id='subscribe_btn'>Inscription</a></li>");
535 580
   $('#login_btn').bind('click',on_click_login_btn);
581
+  $('#subscribe_btn').bind('click',on_click_subscribe_btn);
536 582
 }
537 583
 
538 584
 on_click_myaccount_btn=function() {
... ...
@@ -546,6 +592,10 @@ on_click_login_btn=function() {
546 592
   $('#login_modal').modal('show');
547 593
 }
548 594
 
595
+on_click_subscribe_btn=function() {
596
+  $('#subscribe_modal').modal('show');
597
+}
598
+
549 599
 on_click_logoff_btn=function() {
550 600
   delete localStorage.user;
551 601
   logged_out_menu();
... ...
@@ -598,8 +648,13 @@ $( document ).ready( function() {
598 648
 
599 649
   $('#login_modal').on('hidden.bs.modal',on_close_login_modal);
600 650
   $('#login_modal #login_submit').bind('click',on_valid_login_modal);
651
+  $('#login_modal #go_to_subscribe_btn').bind('click',on_click_go_to_subscribe_btn);
601 652
   $('#login_modal form').bind('submit',on_valid_login_modal);
602 653
 
654
+  $('#subscribe_modal').on('hidden.bs.modal',on_close_subscribe_modal);
655
+  $('#subscribe_modal #subscribe_submit').bind('click',on_valid_subscribe_modal);
656
+  $('#subscribe_modal form').bind('submit',on_valid_subscribe_modal);
657
+
603 658
   $('#view-group #contributor').bind('change',on_contributor_change);
604 659
 
605 660
   $('#add_contributor_btn').bind('click',on_click_add_contributor_btn);
... ...
@@ -372,6 +372,37 @@ function SyncServer() {
372 372
     }
373 373
   }
374 374
 
375
+  this.subscribe=function(url,email,name,password,onsuccess,onerror) {
376
+    this.url=url;
377
+    this.email=email;
378
+    this.name=name;
379
+    this.password=password;
380
+
381
+    try {
382
+      jQuery.getJSON(
383
+        this.url+'/subscribe',
384
+        {'email':email,'name': name,'password':password},
385
+        function(data, textStatus) {
386
+          console.log(data);
387
+          if (textStatus=='success') {
388
+            if(jQuery.type(data.email) != 'undefined' && jQuery.type(data.name) != 'undefined') {
389
+              onsuccess(data);
390
+              return true;
391
+            }
392
+          }
393
+          onerror(data);
394
+          return false;
395
+        }
396
+      ).fail(onerror);
397
+    }
398
+    catch(e) {
399
+      if(jQuery.type(onerror)=='function') {
400
+        onerror();
401
+      }
402
+    }
403
+  }
404
+
405
+
375 406
   this.sync=function(url,email,password,groups,onsuccess,onerror) {
376 407
     this.url=url;
377 408
     this.email=email;
... ...
@@ -253,11 +253,56 @@ body{
253 253
       <div class="modal-footer">
254 254
         <button type="button" class="btn btn-default" data-dismiss="modal">Annuler</button>
255 255
         <button type="button" class="btn btn-primary" id='login_submit'>Connexion</button>
256
+        <button type="button" class="btn btn-primary" id='go_to_subscribe_btn'>Inscription</button>
256 257
       </div>
257 258
     </div><!-- /.modal-content -->
258 259
   </div><!-- /.modal-dialog -->
259 260
 </div>
260 261
 
262
+<div class="modal fade" id="subscribe_modal" tabindex="-1" role="dialog" aria-labelledby="subscribeModal" aria-hidden="true">
263
+  <div class="modal-dialog">
264
+    <div class="modal-content">
265
+      <div class="modal-header">
266
+        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
267
+        <h4 class="modal-title">Inscription</h4>
268
+      </div>
269
+      <div class="modal-body">
270
+        <form class="form-horizontal" role="form">
271
+          <div class="form-group">
272
+            <div class="input-group">
273
+              <span class="input-group-addon">Email *</span>
274
+              <input type='text' id='subscribe_email' class="form-control" placeholder='Email'/>
275
+            </div>
276
+          </div>
277
+          <div class="form-group">
278
+            <div class="input-group">
279
+              <span class="input-group-addon">Nom *</span>
280
+              <input type='text' id='subscribe_name' class="form-control" placeholder='Nom'/>
281
+            </div>
282
+          </div>
283
+          <div class="form-group">
284
+            <div class="input-group">
285
+              <span class="input-group-addon">Mot de passe *</span>
286
+              <input type='password' id='subscribe_pass' class="form-control" placeholder='Mot de passe'/>
287
+            </div>
288
+          </div>
289
+          <div class="form-group">
290
+            <div class="input-group">
291
+              <span class="input-group-addon">Serveur *</span>
292
+              <input type='text' id='subscribe_server' class="form-control" value='http://mycoserver.zionetrix.net'/>
293
+            </div>
294
+          </div>
295
+        </form>
296
+      </div>
297
+      <div class="modal-footer">
298
+        <button type="button" class="btn btn-default" data-dismiss="modal">Annuler</button>
299
+        <button type="button" class="btn btn-primary" id='subscribe_submit'>Valider</button>
300
+      </div>
301
+    </div><!-- /.modal-content -->
302
+  </div><!-- /.modal-dialog -->
303
+</div>
304
+
305
+
261 306
 <div class="modal fade" id="add_contribution_modal" tabindex="-1" role="dialog" aria-labelledby="addContributorModal" aria-hidden="true">
262 307
   <div class="modal-dialog">
263 308
     <div class="modal-content">
264 309