forked from MrSwitch/hello.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
albums.html
239 lines (185 loc) · 4.74 KB
/
albums.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
<!DOCTYPE html>
<link rel="stylesheet" href="/adorn/adorn.css"/>
<script src="/adorn/adorn.js" async></script>
<script src="client_ids.js"></script>
<style>
img.picture{
width:80px;
border:1px solid #ccc;
padding:1%;
display: inline-block;
float:left;
vertical-align: top;
background-color: white;
}
img.picture:nth-child(2n){
-webkit-transform:rotate(10deg);
-moz-transform:rotate(10deg);
-ms-transform:rotate(10deg);
transform:rotate(10deg);
}
img.picture:nth-child(3n){
-webkit-transform:rotate(-10deg);
-moz-transform:rotate(-10deg);
-ms-transform:rotate(-10deg);
transform:rotate(-10deg);
}
.demo{
max-height:500px;
white-space: nowrap;
overflow: auto;
}
.column{
display:inline-block;
white-space: normal;
width:33%;
min-width: 200px;
padding:0.3%;
}
div.column{
height:300px;
overflow: auto;
background-color: #eee;
border: 2px solid white;
}
.column button{
width:100%;
}
</style>
<title>Photo Albums w/ hello.js</title>
<h1>Photo Albums w/ hello.js</h1>
<h2>Demo</h2>
<div class="demo">
<b class="column">Provider</b>
<b class="column">Albums</b>
<b class="column">Photos</b>
<br />
<div class="column">
<button id="windows" onclick="getAlbums('windows')">Get Albums from Windows</button>
<button id="facebook" onclick="getAlbums('facebook')">Get Albums from Facebook</button>
<button id="google" onclick="getAlbums('google')">Get Albums from Google</button>
</div><div class="column" id="albums">
</div><div class="column" id="photos">
</div>
</div>
<h2>Source</h2>
<p>Include hello.js + modules</p>
<script class="pre" src="../src/hello.js"></script>
<script class="pre" src="../src/modules/windows.js"></script>
<script class="pre" src="../src/modules/facebook.js"></script>
<script class="pre" src="../src/modules/google.js"></script>
<p>Setup a listener</p>
<script class="pre">
// Get User
hello.on('auth.login', function(auth){
// Get Profile
hello(auth.network).api('me').then(function(r) {
document.getElementById(auth.network).innerHTML = "Get Albums from " + r.name + " at "+auth.network+"";
});
});
</script>
<h3>Initiate HelloJS</h3>
<p>Plug the app keys (client_id')</p>
<script class="pre">
// Initiate hellojs
hello.init( CLIENT_IDS, {
scope: "files, photos",
redirect_uri: "../redirect.html"
});
</script>
<h3>Get Albums</h3>
<script class="pre">
function getAlbums(network){
// Target where to put the list of albums
var list = document.getElementById('albums');
list.innerHTML = ''; // flush its content
//
// Setting force:false means we'll only trigger auth flow if the user is not already signed in with the correct credentials
var hi = hello(network);
hi.login({
force: false
})
.then(function(auth) {
// Get albums
return hi.api('me/albums');
})
.then(function(r) {
if (!r.data || r.data.length === 0) {
message(list, "There are no albums in the users account");
return
}
// Build buttons with the albums
r.data.forEach(buildAlbumBtn.bind(null, network));
})
.then(null, function (e) {
message(list, "Could not open albums from "+network+", try resigning in");
});
}
</script>
<h3>Album button control</h3>
<script class="pre">
// Create a button selecting the album
function buildAlbumBtn(network, item){
// Target where to put the list of albums
var list = document.getElementById('albums');
// construct the button
var btn = document.createElement('button');
btn.innerHTML = item.name;
// Append the photo
appendPhoto(btn, item.thumbnail);
// Add the controls
btn.onclick = function(){
// Trigger get
getPhotos( network, item.id );
};
// Append to the list
list.appendChild(btn);
}
</script>
<h3>Get Photos</h3>
<script class="pre">
function getPhotos(network, id){
var list = document.getElementById('photos');
list.innerHTML = ''; // flush its content
hello(network)
.api('me/album', {
id: id,
limit:10
})
.then(function(r) {
if (!r.data || r.data.length === 0) {
message(list, "There are no photos in this album");
return
}
// Create a new image in the DOM, give it some randomness and insert it into the dom.
r.data.forEach(buildPhotoThumnail);
}, function(r) {
message(list, r && r.error ? r.error.message : 'something went wrong');
});
}
</script>
<h3>Create photo thumbnail</h3>
<script class="pre">
function buildPhotoThumnail(item){
var list = document.getElementById('photos');
// Append to the list
var o = appendPhoto(list, item.thumbnail);
o.title = item.name;
}
function appendPhoto(el, url) {
if (url) {
var o = document.createElement('img');
o.className = 'picture';
o.src = url;
// Append to the list
el.appendChild(o);
return o;
}
}
</script>
<script>
// Report any notifications to the containing element
function message(target, str){
target.appendChild(document.createTextNode(str));
}
</script>