forked from D-Marc1/facebook-javascript-all-photos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
206 lines (182 loc) · 5.21 KB
/
script.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
let currentAlbum;
let pageLoaded = {fbAlbums: false, fbPhotos: false};
let fb = new FbAllPhotos();
let app = new Framework7({
root: '#app',
name: 'Get all Facebook Album Photos',
routes: [
{
path: '/',
url: 'index.html'
},
{
path: '/fb-albums/',
async: (routeTo, routeFrom, resolve, reject) => {
function getDataTemplate(response) {
resolve(
{
templateUrl: 'fb-albums.html'
},
{
context: {
albums: response.data
}
}
);
}
if(pageLoaded.fbAlbums) { //If page already loaded
getDataTemplate(fb.fullObj);
} else {
fb.getAlbums(5, response => {
getDataTemplate(response);
pageLoaded.fbAlbums = true;
}, response => {
var userMsg;
if(response === 'error') {
userMsg = 'Error getting albums';
} else if(response === 'noResponse') {
userMsg = 'No albums';
}
app.dialog.alert('', userMsg);
});
}
}
},
{
path: '/fb-photos/',
async: (routeTo, routeFrom, resolve, reject) => {
const index = fb.fullObj.data.findIndex(album => album.id == currentAlbum); //get index of album
if(index === -1) {
app.dialog.alert('', 'Album does not exist');
return;
}
function getDataTemplate(response) {
resolve(
{
templateUrl: 'fb-photos.html'
},
{
context: {
photos: response.data,
album_id: fb.fullObj.data[index].id,
album_name: fb.fullObj.data[index].name
}
}
);
}
if(pageLoaded.fbPhotos && fb.fullObj.data[index].hasOwnProperty('photos')) { //If page already loaded and fb.getPhotosInAlbum() called for album
getDataTemplate(fb.fullObj.data[index].photos);
} else {
fb.getPhotosInAlbum(currentAlbum, 5, response => {
getDataTemplate(response);
pageLoaded.fbPhotos = true;
}, response => {
var userMsg;
if(response === 'error') {
userMsg = 'Error getting photos';
} else if(response === 'noResponse') {
userMsg = 'No photos';
} else if(response === 'noAlbum') {
userMsg = 'Album does not exist';
}
app.dialog.alert('', userMsg);
reject();
});
}
}
}
],
on: {
pageInit: page => {
if(page.name === 'index') {
$$('#log-in-out-fb-link').click(function() {
const $this = $$(this);
const currStatus = $this.data('status');
let setStatus, setText, setAnchorText, setStatusColor, setBtnColor; //status and btn color are opposites
function setNewVals() {
$$('#log-in-out-text').text(setText)
.removeClass('text-color-' + setBtnColor)
.addClass('text-color-' + setStatusColor);
$this.text(setAnchorText)
.attr('data-status', setStatus)
.removeClass('color-' + setStatusColor)
.addClass('color-' + setBtnColor);
}
//set to opposite
if(currStatus === 'in') { //logging in
setText = 'logged out';
setAnchorText = 'Login';
setStatus = 'out';
setStatusColor = 'red';
setBtnColor = 'green';
FB.logout(response => {
setNewVals();
});
} else { //logging out
setText = 'logged in';
setAnchorText = 'Logout';
setStatus = 'in';
setStatusColor = 'green';
setBtnColor = 'red';
FB.login(response => {
if(response.authResponse) setNewVals();
else app.dialog.alert('', 'User cancelled authorization');
}, {scope: 'user_photos'});
}
});
} else if(page.name === 'fb-albums') {
$$('.album-thumb-link').click(function() {
currentAlbum = $$(this).attr('data-album-id');
mainView.router.navigate('/fb-photos/');
});
$$('#load-more-albums-link').click(function() {
fb.getMoreAlbums(response => {
mainView.router.refreshPage();
}, response => {
var userMsg;
if(response === 'error') {
userMsg = 'Error getting more albums';
} else if(response === 'noMore') {
userMsg = 'No more albums';
}
app.dialog.alert('', userMsg);
});
});
} else if(page.name === 'fb-photos') {
$$('#load-more-photos-link').click(function() {
currentAlbum = $$(this).data('album-id');
const index = fb.fullObj.data.findIndex(album => album.id == currentAlbum); //get index of album
if(index === -1) {
app.dialog.alert('', 'Album does not exist');
return;
}
if(fb.fullObj.data[index].photos.paging.hasOwnProperty('next')) {
fb.getMorePhotosInAlbum(currentAlbum, response => {
mainView.router.refreshPage();
}, response => {
//the only error needed to handle, due to if check above to check object's next prop
app.dialog.alert('', 'Error getting more photos');
});
} else {
app.dialog.alert('', 'No more photos in album');
}
});
$$('.photo-thumb-link').click(function() {
const photoUrl = $$(this).data('full-photo-url');
app.photoBrowser.create({
photos : [photoUrl],
theme: 'dark',
toolbar: false
}).open();
});
}
}
}
});
let $$ = Dom7;
let mainView = app.views.create('.view-main', {
url: '/',
stackPages: true
});
$$('.page-current').hide();
app.dialog.preloader('Loading Facebook JavaScript SDK');