forked from jeroencoumans/andamio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
facebook.html
459 lines (368 loc) · 14.1 KB
/
facebook.html
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<!DOCTYPE html>
<html lang="nl" class="website">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<title>Andamio</title>
<link rel="stylesheet" type="text/css" href="dist/main.css">
<style>
.fb-cover {
position: relative;
overflow: hidden;
height: 200px;
background: #ccc;
}
.fb-cover-photo {
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
background-repeat: no-repeat;
background-position: top center;
background-size: cover;
}
.fb-cover-avatar {
border: 1px solid #fff;
position: absolute;
bottom: 10px;
left: 10px;
}
#swipeview .media-fluid {
box-shadow: 0 3px 6px -3px rgba(0,0,0, .5);
border: 3px solid #fff;
width: auto;
height: auto;
max-width: 100%;
max-height: 100%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
pointer-events: none;
}
#swipeview .caption {
margin: 5px 10px;
padding: 0 10px;
text-align: center;
background: #fff;
box-shadow: 0 1px 4px rgba(0,0,0, .5);
position: absolute;
bottom: 0;
}
</style>
</head>
<body class="viewport">
<div id="page-view" class="page-view">
<div id="parent-view" class="parent-view">
<div class="page-header">
<a href="/" class="page-logo">Facebook</a>
<a href="javascript:void(0);" class="button-header-wrapper action-show-nav"><span class="button-header">Friends</span></a>
<a href="javascript:void(0)" class="button-header-wrapper-alternate action-login"><span class="button-header button-primary">Login</span></a>
<a href="javascript:void(0)" class="button-header-wrapper-alternate action-logout" style="display: none"><span class="button-header">Logout</span></a>
</div>
<div class="page-content overthrow">
<div class="alert">Please login to continue</div>
</div>
</div>
<div id="child-view" class="child-view view-hidden">
<div class="page-header">
<a href="/" class="page-logo">My app</a>
<a href="javascript:void(0);" class="button-header-wrapper action-pop"><span class="button-header">Back</span></a>
</div>
<div class="page-content overthrow">
</div>
</div>
</div>
<div id="page-navigation" class="page-navigation overthrow">
<h2 class="navigation-header">Friends</h2>
</div>
<a href="javascript:void(0)" class="page-navigation-toggle action-hide-nav"></a>
<div id="modal-view" class="modal-view view-hidden">
<div class="page-header">
<a href="/" class="page-logo">Photos</a>
<a href="javascript:void(0);" class="button-header-wrapper-alternate action-hide-modal"><span class="button-header button-primary">Close</span></a>
</div>
<div class="page-content overthrow">
<div id="swipeview" style="width:100%; height: 100%">
</div>
<div class="pager-dots">
<a href="javascript:void(0)" class="pager-dot-item pager-dot-active"></a>
<a href="javascript:void(0)" class="pager-dot-item"></a>
<a href="javascript:void(0)" class="pager-dot-item"></a>
</div>
</div>
</div>
<div id="loader" class="page-loader" style="display:none">
<img src="img/spinner-dark.gif" class="spinner" width="32" height="32">
<div class="loader-text"></div>
</div>
<div id="fb-root"></div>
<!-- Initialize Facebook API -->
<script id="facebook-jssdk" async="" src="//connect.facebook.net/en_US/all.js"></script>
<!-- Andamio -->
<script src="js/lib/handlebars.js"></script>
<script src="js/lib/swipeview.js"></script>
<script src="dist/main.js"></script>
<!--
templates
-->
<!-- Profile page -->
<script id="fb-profile" type="text/x-handlebars-template">
{{#if cover}}
<div class="fb-cover">
<div class="fb-cover-photo" style="background-image: url({{cover.source}});"></div>
</div>
{{else}}
<h2 class="table-heder"></h2>
{{/if}}
<div class="table-view-grouped">
<div class="table-cell media-object">
<img class="media-img" width="50" height="50" src="https://graph.facebook.com/{{id}}/picture?type=normal">
<div class="media-bd">
<h4>{{name}}</h4>
{{hometown.name}}
</div>
</div>
{{#if statuses}}
<div class="table-cell">
{{statuses.data.0.message}}
</div>
{{/if}}
</div>
</script>
<!-- Navigation items -->
<script id="fb-friend-item" type="text/x-handlebars-template">
<a href="javascript:void(0)" class="navigation-item media-object action-user" data-user-id="{{id}}">
<img class="media-img" width="25" height="25" src="{{picture.data.url}}">
{{name}}
</a>
</script>
<!-- Album items -->
<script id="fb-album-item" type="text/x-handlebars-template">
<a href="javascript:void(0)" data-photo-id="{{id}}" class="table-cell media-object action-album-photos">
<i class="icon icon-arrow-right media-action"></i>
{{#if photos}}
<img class="media-img" width="25" height="25" src="{{photos.data.0.picture}}">
{{/if}}
<h4 class="media-bd">{{name}} <span class="detail">({{count}})</span></h4>
</a>
</script>
<!-- Photo's -->
<script id="fb-photo-item" type="text/x-handlebars-template">
<img src="{{source}}" class="media-fluid">
<div class="caption">{{name}}</div>
</script>
<!-- end templates -->
<script>
/**
* Module for loading Facebook profile & album data
*/
APP.facebook = (function () {
// Variables
var pageNav,
pageNavActive,
parentView,
swipeElem,
modalTitle;
/**
* Sort a list alphabetically
*/
var sorter = function(a, b) {
var x = a.name.toLowerCase();
var y = b.name.toLowerCase();
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
/**
* Generate a template with Handlebars
*/
function getTemplate(template, data) {
var temp = $(template).html(),
compile = Handlebars.compile(temp),
context = data,
result = compile(context);
return result;
}
/**
* Load user albums
* id: the facebook id of the user
*/
function loadAlbums(id) {
// get albums data, sort them and render them as HTML
FB.api('/' + id + '/albums', { fields: 'id, count, name, photos.limit(1).fields(picture)' }, function(response) {
var photos = response.data.sort(sorter),
source = $("#fb-album-item").html(),
template = Handlebars.compile(source),
context = photos,
html = "";
for (var i=0; i < photos.length; i++) {
if (photos[i].count > 0) {
html = html + template(photos[i]);
}
}
var albums = $('<div class="table-view-grouped">').prepend(html).prepend('<h2 class="table-header">Photos</h2>');
parentView.append(albums);
APP.loader.hide();
});
}
/**
* Load user profile
* id: the facebook id of the user
*/
function showProfile(id) {
APP.loader.show();
// make sure parentview is empty otherwise user might see stale data
parentView.empty();
setTimeout(function () {
// get profile information
FB.api('/' + id, { fields: 'id, name, picture, cover, albums.fields(count), hometown, statuses.limit(1).fields(place,message)' }, function(response) {
// manually put it inside the parent view
parentView.prepend(getTemplate("#fb-profile", response));
if (response.albums) {
loadAlbums(id);
} else {
APP.loader.hide();
}
});
}, 0);
}
/**
* Load user friends
*/
function loadFriends() {
// set first navigation item to loggedin user
FB.api("/me/", {fields: 'id, name, picture' }, function(response) {
// wrap this as Zepto object so we can set it as active item
var result = $(getTemplate("#fb-friend-item", response)).addClass("active");
// update active navigation
pageNavActive = result;
pageNav.prepend(result).prepend('<h2 class="navigation-header">My profile</h2>');
});
// the rest of the navigation items are the loggedin user's friends
FB.api("/me/friends", { fields: 'name, picture' }, function(response) {
var friends = response.data.sort(sorter),
template = $("#fb-friend-item").html(),
compile = Handlebars.compile(template),
data = friends,
result = "";
for (var i=0; i < friends.length; i++) {
result = result + compile(friends[i]);
}
pageNav.append(result);
});
}
// Get login and initial data
function handleStatusChange(response) {
$('body').addClass(response.authResponse ? 'connected' : 'not_connected');
if (response.authResponse) {
$(".action-login").hide();
$(".action-logout").show();
showProfile('me');
loadFriends();
}
}
/**
* Attach event listeners
*/
function attachListeners() {
// login
APP.events.attachClickHandler(".action-login", function () {
FB.login(null, {scope: 'email,read_friendlists,user_photos,friends_photos'});
});
// logout
APP.events.attachClickHandler(".action-logout", function () {
FB.logout(function() {
window.location.reload();
});
});
// open modal with all photo's
APP.events.attachClickHandler(".action-album-photos", function (event) {
var targetAlbum = $(event.target).closest(".action-album-photos"),
targetAlbumName = targetAlbum.text(),
targetAlbumId = targetAlbum.data("photo-id");
modalTitle.text(targetAlbumName);
swipeElem.empty();
APP.modal.show();
APP.loader.show();
FB.api(targetAlbumId + "/photos", function(response) {
var photos = response.data,
source = $("#fb-photo-item").html(),
template = Handlebars.compile(source),
context = photos,
slides = [],
i,
page,
carousel;
for (i=0; i < photos.length; i++) {
slides[i] = template(photos[i]);
}
carousel = new SwipeView('#swipeview', {
numberOfPages: photos.length,
hastyPageFlip: true
});
// Load initial data
for (i=0; i<3; i++) {
page = i === 0 ? slides.length-1 : i-1;
$(slides[page]).appendTo(carousel.masterPages[i]);
}
carousel.onFlip(function () {
var upcoming;
for (i=0; i<3; i++) {
upcoming = carousel.masterPages[i].dataset.upcomingPageIndex;
if (upcoming != carousel.masterPages[i].dataset.pageIndex) {
carousel.masterPages[i].innerHTML = slides[upcoming];
}
}
});
APP.loader.hide();
});
});
// load friend profile
APP.events.attachClickHandler(".action-user", function (event) {
var targetUser = $(event.target);
var targetUserId = targetUser.closest(".action-user").data("user-id");
// set active class
pageNavActive.removeClass("active");
pageNavActive = targetUser.addClass("active");
APP.nav.hide();
// open child page
APP.views.openParentPage();
showProfile(targetUserId);
});
}
/***
* Initialize capabilities and attach listeners
*/
function init() {
// Assign variables
pageNav = $("#page-navigation");
pageNavActive = pageNav.find(".active");
parentView = $("#parent-view .page-content");
swipeElem = $("#swipeview");
modalTitle = $("#modal-view .page-logo");
// initialise Facebook API
window.fbAsyncInit = function() {
FB.init({
appId : '497899636895210', // App ID
status : true, // check login status
cookie : true // enable cookies to allow the server to access the session
});
FB.Event.subscribe('auth.statusChange', handleStatusChange);
};
attachListeners();
}
return {
"init": init,
"loadAlbums": loadAlbums,
"showProfile": showProfile
};
})();
$(document).ready(function() {
APP.core.init();
APP.facebook.init();
});
</script>
</body>
</html>