Commit 8aa836f3 authored by Corentin Guillevic's avatar Corentin Guillevic

Refactoring of controller

parent 952640fe
......@@ -200,14 +200,8 @@ function getUserInformation(token){
function getPostInformation(userId, idPost){
var post = Object.assign({}, postsInformation[idPost]);
var like = likeTable[userId];
post.isLiked = false;
for(var i = 0 ; i < like.length ; ++i){
if(like[i] == idPost){
post.isLiked = true;
}
}
post.isLiked = like.includes(idPost);
post.user = usersInformation[post.userId];
/*post.isOwnPost = userId == post.userId ? true : false;*/
return post;
}
......
......@@ -4,7 +4,6 @@ Google authentification
function googleAuthChangeUser(){
controller.setGoogleAuth(gapi.auth2.getAuthInstance());
console.log("Change");
}
function googleAuthInit(){
......@@ -48,7 +47,7 @@ m.route(root, "/", {
"/profile/:id" : ProfilePage,
"/account": {
onmatch: function() {
if (!controller.isConnected) m.route.set("/")
if (!controller.user.isConnected) m.route.set("/")
else return AccountPage
}
}
......
......@@ -42,7 +42,7 @@ var Header = {
m("div", {class : "search-div"}, [
m("input", {id : "searchbar", placeholder : "Search"})
]),
m(controller.isConnected ? HeaderUser: HeaderLog)
m(controller.user.isConnected ? HeaderUser: HeaderLog)
])
])
}
......@@ -104,7 +104,7 @@ var PostRightBottom = {
}
return m("div", {class : "bottom"}, [
m("div", {class : "tools"}, [
controller.user.id == post.userId ?
controller.isUserId(post.userId) ?
m("span", {class : "post-modification", onclick : modifiateClick}, m("p", "Modifiate"))
: m("span", {class : post.isLiked ? "like liked" : "like", onclick : likeClick}, [m(SvgCoeur)])
]),
......@@ -195,7 +195,7 @@ var ProfileTop = {
m("div", {class : "right"}, [
m("div", {class : "top"}, [
m("p", {class : "profile-name"}, profile.name),
profile.id == controller.user.id ?
controller.isUserId(profile.id) ?
m("span", {class : "profile-modification"}, "Modifiate")
: m("span", {class : profile.isFollowed ? "followed" : "follow-button"}, profile.isFollowed ? "Followed" : "Follow")
]),
......@@ -224,7 +224,7 @@ var Profile = {
},
view : function(vnode){
var profile = controller.profile.content;
function closeExtendProfile(){
function closeExtendPost(){
controller.unextendPost();
}
return m("div", {class : "body-container"},[
......@@ -235,9 +235,9 @@ var Profile = {
return m(PictureGallery, {post : post})
})
]),
m("div", {id : "extend", class : controller.profile.isExtend ? "active" : ""}, [
m("div", {class : "overlay", onclick : closeExtendProfile}),
controller.profile.isExtend ? m(Post, {post : controller.profile.postExtend}) : ""
m("div", {id : "extend", class : controller.postExtend.extendPostActive ? "active" : ""}, [
m("div", {class : "overlay", onclick : closeExtendPost}),
controller.postExtend.extendPostActive ? m(Post, {post : controller.postExtend.post}) : ""
])
]);
}
......@@ -253,6 +253,12 @@ var PopMessage = {
}
}
var PopError = {
view : function(){
return m("div", {class : "pop-content pop-error"}, m("p", controller.pop.message))
}
}
var PopRegister = {
oncreate : function(){
gapiRender("g-signin2-register");
......@@ -294,11 +300,12 @@ var Pop = {
function closePop(){
controller.closePop();
}
return m("div", {class : controller.pop.isOpen ? "pop active" : "pop"}, [
return m("div", {class : controller.pop.popActive ? "pop active" : "pop"}, [
m("div", {class : "overlay", onclick : closePop}),
controller.pop.isLogin ? m(PopLogin) : "",
controller.pop.isRegister ? m(PopRegister) : "",
controller.pop.isMessage ? m(PopMessage) : ""
controller.pop.id == LOGIN_ID ? m(PopLogin) : "",
controller.pop.id == REGISTER_ID ? m(PopRegister) : "",
controller.pop.id == MESSAGE_ID ? m(PopMessage) : "",
controller.pop.id == ERROR_ID ? m(PopError) : ""
])
}
}
......
/*********************
Controller
*********************/
//Pop's id
const ERROR_ID = 0;
const MESSAGE_ID = 1;
const LOGIN_ID = 2;
const REGISTER_ID = 3;
var controller = {
//variables
isConnected : false,
googleAuth : null,
/*****************
Attributes
*****************/
auth : {
googleAuth : null,
token : null
},
user : {
token : null,
id : null,
name : null,
imageUrl : null
imageUrl : null,
posts : null,
stats : null,
isConnected : false
},
timeline : {
posts : getTimelineAnonyme()
},
profile : {
isOpen : false,
isExtend : false,
postExtend : null,
content : null
},
pop : {
isOpen : false,
isLogin : false,
isRegister : false
},
//functions
setConnected : function(b){
if(b != this.isConnected){
this.isConnected = b;
this.refresh();
}
id : -1,
popActive : false,
text : null
},
postExtend : {
post : null,
extendPostActive : false
},
/*****************
Methods
*****************/
refresh : function(){
m.route.set(m.route.get());
},
setUser : function(id){
var user = getUserInformation(id);
//Setter
setUser : function(user){
if(user != null){
this.user = user;
this.setConnected(true);
this.user.id = user.id;
this.user.name = user.name;
this.user.imageUrl = user.imageUrl;
this.user.posts = user.posts;
this.user.stats = user.stats;
return true;
}
else{
return false;
}
},
openProfile : function(id){
this.profile.isOpen = true;
this.setProfile(id);
},
setProfile : function(id){
this.profile.content = this.isConnected ? getProfileInformation(this.user.id, id) : getProfileInformationAnonyme(id);
/**Keep in memory the profiles load ? : content -> contents (map id -> content) */
this.profile.content = this.user.isConnected ? getProfileInformation(this.user.id, id) : getProfileInformationAnonyme(id);
},
setGoogleAuth : function(googleAuth){
this.auth.googleAuth = googleAuth;
if(this.auth.googleAuth.isSignedIn.get()){
var googleUser = googleAuth.currentUser.get();
var user = getUserInformation(googleUser.getBasicProfile().getEmail());
if(this.setUser(user)){
this.auth.token = googleUser.getAuthResponse().id_token;
this.user.isConnected = true;
this.closePop();
}
else{
this.register();
}
this.refresh();
}
else{
this.disconnectUser();
}
this.updateTimeline();
},
updateTimeline : function(){
this.timeline.posts = this.isConnected ? getTimeline(this.user.id) : getTimelineAnonyme();
this.timeline.posts = this.user.isConnected ? getTimeline(this.user.id) : getTimelineAnonyme();
},
openProfile : function(id){
this.profile.isOpen = true;
this.setProfile(id);
},
//Post
extendPost : function(id){
var post = this.isConnected ? getPostInformation(this.user.id, id) : getPostInformationAnonyme(id);
var post = this.user.isConnected ? getPostInformation(this.user.id, id) : getPostInformationAnonyme(id);
if(this.profile.isOpen){
this.profile.postExtend = post;
this.profile.isExtend = true;
this.postExtend.post = post;
this.postExtend.extendPostActive = true;
}
this.refresh();
},
unextendPost : function(){
if(this.profile.isOpen){
this.profile.isExtend = false;
this.postExtend.extendPostActive = false;
}
this.refresh();
},
toggleLike : function(idPost){
if(!this.isConnected){
if(!this.user.isConnected){
//TODO Make a pop message "To like a post, you need to be register"
this.login();
return;
}
toggleLikePost(this.user.id, idPost);
if(this.profile.isOpen && this.profile.postExtend.id == idPost){
this.profile.postExtend = getPostInformation(this.user.id, idPost);
if(this.postExtend.extendPostActive && this.postExtend.post.id == idPost){
this.postExtend.post = getPostInformation(this.user.id, idPost);
}
//TODO update only the post
this.updateTimeline();
},
//Tests
isUserId : function(id){
return id == this.user.id;
},
//Pops
closePop : function(){
this.pop.isLogin = false;
this.pop.isRegister = false;
this.pop.isMessage = false;
this.pop.isOpen = false;
this.pop.popActive = false;
this.refresh();
},
popMessage : function(message){
this.pop.popActive = true;
this.pop.id = MESSAGE_ID;
this.pop.message = message;
},
//Authentification
login : function(){
if(this.googleAuth.isSignedIn.get() && !this.isConnected){
this.popMessage("You must register you.")
if(this.auth.googleAuth.isSignedIn.get()){
if(!this.user.isConnected){
this.popMessage("You must register you.");
}
else{
updateTimeline();
}
}
else{
this.pop.isOpen = true;
this.pop.isLogin = true;
this.pop.popActive = true;
this.pop.id = LOGIN_ID;
this.refresh();
}
},
loginValidation(mail, token){
if(this.setUser(mail)){
this.user.token = token;
this.closePop();
}
else{
this.closePop();
this.register();
}
},
register : function(){
this.pop.isOpen = true;
this.pop.isRegister = true;
this.pop.popActive = true;
this.pop.id = REGISTER_ID;
this.refresh();
},
registerValidation : function(){
if(this.googleAuth.isSignedIn.get()){
var googleUser = this.googleAuth.currentUser.get().getBasicProfile();
if(this.auth.googleAuth.isSignedIn.get() && !this.user.isConnected){
var googleUser = this.auth.googleAuth.currentUser.get().getBasicProfile();
var user = new Object();
user.id = googleUser.getEmail();
user.name = googleUser.getName();
user.mail = googleUser.getEmail();
user.imageUrl = "ressources/img/favicon.png";
user.token = this.googleAuth.currentUser.get().getAuthResponse().id_token;
user.token = this.auth.googleAuth.currentUser.get().getAuthResponse().id_token;
if(existUser(user.mail)){
this.popMessage("Your email adress are already registered");
}
else{
addUser(user);
this.loginValidation(user.id, user.token);
if(this.setUser(getUserInformation(user.mail))){
this.auth.token = user.token;
this.user.isConnected = true;
this.refresh();
this.closePop();
}
}
}
else{
this.disconnectUser();
}
},
popMessage : function(message){
this.closePop();
this.pop.isOpen = true;
this.pop.isMessage = true;
this.pop.message = message;
},
disconnectUser : function(){
this.isConnected = false;
this.user.isConnected = false;
this.refresh();
},
setGoogleAuth : function(googleAuth){
this.googleAuth = googleAuth;
if(this.googleAuth.isSignedIn.get()){
var googleUser = googleAuth.currentUser.get();
if(this.setUser(googleUser.getBasicProfile().getEmail())){
this.user.token = googleUser.getAuthResponse().id_token;
this.refresh();
}
}
else{
this.disconnectUser();
}
},
refresh : function(){
m.route.set(m.route.get());
}
}
//Pop's id
const ERROR_ID = 0;
const MESSAGE_ID = 1;
const LOGIN_ID = 2;
const REGISTER_ID = 3;
var controllerRefactore = {
/*****************
Attributes
*****************/
auth : {
googleAuth : null,
token : null
},
user : {
id : null,
name : null,
imageUrl : null,
isConnected : false
},
timeline : {
posts : null
},
profile : {
content : null
},
pop : {
id : -1,
popActive : false,
text : null
},
extendPost : {
post : null,
extendPostActive : false
},
/*****************
Methods
*****************/
refresh : function(){
m.route.set(m.route.get());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment