-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
134 lines (125 loc) · 3.61 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// model
;(function ($) {
var app = this.app || (this.app = {});
app.model = {};
}).call(this, jQuery);
// view, must be init after "app.model" is ready
;(function($){
var app = this.app || (this.app = {});
app.view = {};
app.view.showPersonal = function(info){
$("#userInfo").html(info);
}
// app.view.dropPost = function(postId){
// var post = $("#" + postId);
// if (post.length){
// console.log(post);
// console.log("reseting " + postId);
// post.remove();
// }
// }
app.view.addPost = function(time, message, postId){
// app.view.dropPost();
var localTime = new Date(time);
if (typeof(message) == 'undefined'){
message = "";
}
$("#userInfo").append("<div id='" + postId + "'><p class='timestamp'>" + localTime.toLocaleString() + "</p>" + "<p>" + message + "</p>" + "</div>");
}
app.view.addPhoto = function(id, pictureSrc){
console.log(id);
$("#" + id).append("<img class='postImage' src=" + pictureSrc + "></img>");
}
}).call(this, jQuery);
// controller
;(function($){
var app = this.app || (this.app = {});
app.controller = {};
app.controller.init = function(){
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
console.log('Logged in.');
app.controller.loadGroupInfo();
$("#login").addClass("hidden");
$("#loginDiv").addClass("hidden");
$("#refreshB").removeClass("hidden");
// $("#loginButtonM").html("refresh");
$("#refreshB").click(function(){
$("#userInfo").html("");
app.controller.loadGroupInfo();
});
} else {
// $("#refreshB").addClass("hidden");
$("#login").click(function(){
FB.login(function(response){
$("#login").addClass("hidden");
$("#loginDiv").addClass("hidden");
$("#refreshB").removeClass("hidden");
app.controller.loadGroupInfo();
// $("#loginButtonM").html("refresh");
$("#refreshB").click(function(){
$("#userInfo").html("");
app.controller.loadGroupInfo();
});
}, {scope: 'user_groups,email'});
});
}
});
}
app.controller.loadGroupInfo = function(){
FB.api(
"/198136763558550/feed",
function (response) {
var compare = function(a, b){
if (a.created_time > b.created_time){
return -1;
}else if (a.created_time < b.created_time){
return 1;
}else {
return 0;
}
}
if (response && !response.error) {
/* handle the result */
response.data.sort(compare);
$(response.data).each(function (index){
// app.view.resetPost(this.id);
app.view.addPost(this.created_time, this.message, this.id);
app.controller.loadPostInfo(this.id);
});
// app.view.showPersonal(response.id);
}
}
);
}
app.controller.loadPostInfo = function(postId){
FB.api(
"/" + postId + "?fields=attachments,message,type",
function (response) {
console.log(response);
if (response.type == "photo"){
if (response.attachments.data[0].subattachments){
$(response.attachments.data[0].subattachments.data).each(function(){
app.view.addPhoto(postId, this.media.image.src);
console.log("subattachments " + this.media.image.src);
});
}else {
app.view.addPhoto(postId, response.attachments.data[0].media.image.src);
}
// app.controller.loadPhotoInfo(response.object_id);
}
}
);
}
app.controller.loadPhotoInfo = function(objectId){
FB.api(
objectId,
function (response) {
console.log(response);
$(response.images).each(function(index){
app.view.addPhoto(objectId, this.source);
});
}
);
}
}).call(this, jQuery);