From 5891b19968667992e881a6c0471058f279c31612 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Sun, 12 Jan 2014 01:24:16 +0100 Subject: [PATCH] Add login and sync support --- inc/myco.js | 123 ++++++++++++++++++++++++++++++++++++++++++++ inc/myco_objects.js | 84 ++++++++++++++++++++++++++++++ index.html | 85 ++++++++++++++++++++++++++++-- 3 files changed, 289 insertions(+), 3 deletions(-) diff --git a/inc/myco.js b/inc/myco.js index 54475c8..867d77d 100644 --- a/inc/myco.js +++ b/inc/myco.js @@ -366,6 +366,117 @@ on_confirm_remove_group=function(group) { } } +/******************** + * Login + ********************/ +on_close_login_modal=function(e) { + $('#login_modal form')[0].reset(); +} + +sync_server=false; +on_valid_login_modal=function(e) { + email=$('#login_modal #login_email')[0].value; + pass=$('#login_modal #login_pass')[0].value; + server=$('#login_modal #login_server')[0].value; + $('#login_modal').modal('hide'); + sync_server.login(server,email,pass, + function(data) { + localStorage.user=JSON.stringify({ + 'name': data.name, + 'email': email, + 'password': pass, + 'server': server, + }); + logged_menu(); + }, + function(data) { + if (jQuery.type(data) != 'object' && data.loginerror) { + alert(data.loginerror); + } + else { + console.log(data); + alert('Erreur durant la connexion au serveur'); + } + } + ); +} + + + +/******************** + * Sync + ********************/ +on_click_sync_btn=function(e) { + if (jQuery.type(localStorage.user)!="undefined") { + user=JSON.parse(localStorage.user); + localStorage.oldgroups=groups.export(); + sync_server.sync(user.server, user.email, user.password, groups.export(), + function(data) { + console.log(data); + if (data.groups) { + groups.import(data.groups); + groups.save(); + refresh_group_list(); + alert('Groupes synchronisés'); + } + else { + console.log(data); + alert('Erreur durant la synchronisation :('); + } + }, + function(data) { + console.log(data); + alert('Impossible de contacter le serveur :('); + } + ); + } + else { + alert("Vous devez vous connecter pour commencer"); + $('#login_modal').modal('show'); + } +} + +/* + * User menu + */ +user=false; +logged_menu=function() { + user=JSON.parse(localStorage.user); + $('#user-name').html(user.name); + $('#user-menu li').remove(); + menu=$('#user-menu'); + menu.html("
  • Mon compte
  • "+ + "
  • Synchroniser
  • " + + "
  • " + + "
  • Déconnexion
  • "); + $('#myaccount_btn').bind('click',on_click_myaccount_btn); + $('#sync_btn').bind('click',on_click_sync_btn); + $('#logoff_btn').bind('click',on_click_logoff_btn); +} + +logged_out_menu=function() { + $('#user-name').html('Connexion'); + $('#user-menu li').remove(); + menu=$('#user-menu'); + menu.html("
  • Connexion
  • "); + $('#login_btn').bind('click',on_click_login_btn); +} + +on_click_myaccount_btn=function() { + $('#myaccount_modal #myaccount_email').html(user.email); + $('#myaccount_modal #myaccount_name')[0].value=user.name; + $('#myaccount_modal').modal('show'); +} + +on_click_login_btn=function() { + $('#login_modal').modal('show'); +} + +on_click_logoff_btn=function() { + delete localStorage.user; + logged_out_menu(); +} + /********************* * Activate @@ -375,10 +486,18 @@ $( document ).ready( function() { groups=new GroupList(); groups.loadFromLocalStorage(); refresh_group_list(); + if (jQuery.type(localStorage.user)!='undefined') { + logged_menu(); + } + else { + logged_out_menu(); + } } else { alert('Local storage not supported !'); + return; } + sync_server=new SyncServer(); $('#add_group_btn').bind('click',on_click_add_group_btn); $('#add_group_submit').bind('click',on_valid_add_group_modal); @@ -388,6 +507,10 @@ $( document ).ready( function() { $('#clear_local_data').bind('click',clear_local_data); + $('#login_modal').on('hidden.bs.modal',on_close_login_modal); + $('#login_modal #login_submit').bind('click',on_valid_login_modal); + $('#login_modal form').bind('submit',on_valid_login_modal); + $('#view-group #contributor').bind('change',on_contributor_change); $('#add_contributor_btn').bind('click',on_click_add_contributor_btn); diff --git a/inc/myco_objects.js b/inc/myco_objects.js index 641c2af..626f5a1 100644 --- a/inc/myco_objects.js +++ b/inc/myco_objects.js @@ -22,6 +22,20 @@ function GroupList() { return ret; } + this.import=function(groups) { + ret={}; + for (el in this) { + if (this.isGroup(this[el])) { + delete ret[el]; + } + } + for (el in groups) { + this[el]=new Group(el,groups[el]); + } + return true; + } + + this.save=function() { localStorage.groups=JSON.stringify({ 'lastChange': new Date().getTime(), @@ -228,3 +242,73 @@ function Contribution(contributor,cost,title,date,id,lastChange) { return ''; } } + +function SyncServer() { + this.url=false; + this.email=false; + this.password=false; + this.logged=false; + + this.login=function(url,email,password,onsuccess,onerror) { + this.url=url; + this.email=email; + this.password=password; + + try { + jQuery.getJSON( + this.url+'/login', + {'email':email,'password':password}, + function(data, textStatus) { + console.log(data); + if (textStatus=='success') { + if(jQuery.type(data.email) && jQuery.type(data.name)) { + console.log('Login success return'); + console.log(onsuccess); + onsuccess(data); + return true; + } + } + onerror(data); + } + ).fail(onerror); + } + catch(e) { + if(jQuery.type(onerror)=='function') { + onerror(); + } + } + } + + this.sync=function(url,email,password,groups,onsuccess,onerror) { + this.url=url; + this.email=email; + this.password=password; + try { + jQuery.getJSON( + this.url+'/sync', + { + 'email':email, + 'password':password, + 'groups': JSON.stringify(groups) + }, + function(data, textStatus) { + console.log(data); + if (textStatus=='success') { + if(jQuery.type(data.groups)) { + console.log('Sync success return'); + onsuccess(data); + return true; + } + } + onerror(data); + } + ).fail(onerror); + } + catch(e) { + if(jQuery.type(onerror)=='function') { + onerror(); + } + } + } + +} diff --git a/index.html b/index.html index 43690da..95dcd79 100644 --- a/index.html +++ b/index.html @@ -58,14 +58,21 @@ body{ @@ -187,6 +194,42 @@ body{ + +