-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathindex.html
370 lines (330 loc) · 13.1 KB
/
index.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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>OneDrive Explorer Sample</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script id="odauth" src="odauth.js"></script>
<link rel="icon" href="https://d.sfx-df.ms/images/favicon.ico" type="image/x-icon">
<link rel="mask-icon" href="https://d.sfx-df.ms/images/mask_icon.svg" color="#094AB2">
<script>
function signInToOneDrive()
{
// Register your own application at https://apps.dev.microsoft.com
// and set the "clientId" and "redirectUri" variables accordingly.
var appInfo = {
"clientId": "e9a57c07-69e4-41f5-b868-0f123d2fda17",
"redirectUri": "http://localhost:9999/callback.html",
"scopes": "user.read files.read files.read.all sites.read.all",
"authServiceUri": "https://login.microsoftonline.com/common/oauth2/v2.0/authorize"
};
provideAppInfo(appInfo);
// use Microsoft Graph v1.0
var baseUrl = getQueryVariable("baseUrl")
msGraphApiRoot = (baseUrl) ? baseUrl : "https://graph.microsoft.com/v1.0/me";
challengeForAuth();
saveToCookie( { "apiRoot": msGraphApiRoot, "signedin": true } );
return false;
}
function showCustomLoginButton(show)
{
var loginButton = document.getElementById("od-login");
loginButton.style.display = show ? "block" : "none";
var logoutButton = document.getElementById("od-logoff");
logoutButton.style.display = show ? "none" : "block";
}
function getUrlParts(url)
{
var a = document.createElement("a");
a.href = url;
return { "hostname": a.hostname,
"path": a.pathname }
}
function setOneDriveTitle(title)
{
var element = document.getElementById("od-site");
element.innerText = title;
}
function saveToCookie(obj)
{
var expiration = new Date();
expiration.setTime(expiration.getTime() + 3600 * 1000);
var data = JSON.stringify(obj);
var cookie = "odexplorer=" + data +"; path=/; expires=" + expiration.toUTCString();
if (document.location.protocol.toLowerCase() == "https") {
cookie = cookie + ";secure";
}
document.cookie = cookie;
}
function loadFromCookie()
{
var cookies = document.cookie;
var name = "odexplorer=";
var start = cookies.indexOf(name);
if (start >= 0) {
start += name.length;
var end = cookies.indexOf(';', start);
if (end < 0) {
end = cookies.length;
}
else {
postCookie = cookies.substring(end);
}
var value = cookies.substring(start, end);
return JSON.parse(value);
}
return "";
}
function signOut()
{
logoutOfAuth();
saveToCookie( { "apiRoot": msGraphApiRoot, "signedin": false } );
$('#od-breadcrumb').empty();
$('#od-items').empty();
$('#od-json').empty();
location.reload();
}
function getQueryVariable(variable)
{
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if(pair[0] == variable){return pair[1];}
}
return(false);
}
</script>
</head>
<body>
<div id="od-site">
<div id="od-commands">
<div id="od-login" style="display: hidden">
<a href="#" onclick="signInToOneDrive()">Sign in to OneDrive</a>
</div>
<div id="od-logoff" style="display: hidden">
<a href="#" onclick="signOut()">Sign Out</a>
</div>
</div>
<div id="od-title">OneDrive Explorer Sample - JS</div>
</div>
<div id="od-loading"></div>
<div id="od-breadcrumb"></div>
<div id="od-content">
<div id="od-items" class="od-pagecol"></div>
<div id="od-json" class="od-pagecol"></div>
</div>
<script>
var baseUrl = getQueryVariable("baseUrl")
msGraphApiRoot = (baseUrl) ? baseUrl : "https://graph.microsoft.com/v1.0/me";
var data = loadFromCookie();
if (data)
{
if (!baseUrl)
msGraphApiRoot = data.apiRoot;
showCustomLoginButton(!data.signedin)
}
// we use the url fragment to specify the file path within onedrive
// (eg. #/pictures/foo.jpg), so we bind to the hashchange event to
// know when it changes. in cases where we change the hash from
// within this app due to a user click, we call odauth ourselves and
// pre-set loadedFromHash to the new value so that this handler doesn't
// call odauth() again. this is so that we handle the auth popup properly.
// odauth can either be called in response to a user click (in which)
// case it's allowed to pop up an auth dialog, or with no user click,
// in which case it's only allowed to show a 'sign-in' button (if it
// tried to pop up a window, most browsers' popup blockers would block it).
// this handler needs to call odauth in non-click mode since it doesn't
// know why the hash changed.
var loadedForHash = "";
$(window).bind('hashchange', function() {
if (window.location.hash != loadedForHash) {
loadedForHash = window.location.hash;
odauth();
}
return false;
});
// we bind to jquery's ajax start/stop events so that we can style the
// page differently when a network call is being made
$(document).on({
ajaxStart: function() {$('body').addClass('loading');},
ajaxStop: function() {$('body').removeClass('loading');}
});
// based on http://jsfiddle.net/KJQ9K/554/
function syntaxHighlight(obj) {
var json = JSON.stringify(obj, undefined, 2)
json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g,
function (match) {
var cls = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key';
} else {
cls = 'string';
}
} else if (/true|false/.test(match)) {
cls = 'boolean';
} else if (/null/.test(match)) {
cls = 'null';
}
return '<span class="' + cls + '">' + match + '</span>';
});
}
// called to update the breadcrumb bar at the top of the page
function updateBreadcrumb(decodedPath) {
var path = decodedPath || '';
$('#od-breadcrumb').empty();
var runningPath = '';
var segments = path.split('/');
for (var i = 0 ; i < segments.length; i++) {
if (i > 0) {
$('<span>').text(' > ').appendTo("#od-breadcrumb");
}
var segment = segments[i];
if (segment) {
runningPath = runningPath + '/' + encodeURIComponent(segment);
} else {
segment = 'Files';
}
$('<a>').
attr("href", "#" + runningPath).
click(function() {
// when the page changes in response to a user click,
// we set loadedForHash to the new value and call
// odauth ourselves in user-click mode. this causes
// the catch-all hashchange event handler not to
// process the page again. see comment at the top.
loadedForHash = $(this).attr('href');
window.location = loadedForHash;
odauth(true);
}).
text(segment).
appendTo("#od-breadcrumb");
}
}
// odauth calls our onAuthenticated method to give us the user's auth token.
// in this demo app we just use this as the method to drive the page logic
function onAuthenticated(token, authWindow) {
if (token) {
if (authWindow) {
removeLoginButton();
authWindow.close();
}
(function($){
// we extract the onedrive path from the url fragment and we
// flank it with colons to use the api's path-based addressing scheme
var path = "";
var beforePath = "";
var afterPath = "";
if (window.location.hash.length > 1) {
path = window.location.hash.substr(1);
beforePath =":";
afterPath = ":";
}
var odurl = msGraphApiRoot + "/drive/root" + beforePath + path + afterPath;
// the expand and select parameters mean:
// "for the item i'm addressing, include its thumbnails and children,
// and for each of the children, include its thumbnails. for those
// thumbnails, return the 'large' size"
var thumbnailSize = "large"
var odquery = "?expand=thumbnails,children(expand=thumbnails(select=" + thumbnailSize + "))";
$.ajax({
url: odurl + odquery,
dataType: 'json',
headers: { "Authorization": "Bearer " + token },
accept: "application/json;odata.metadata=none",
success: function(data) {
if (data) {
// clear out the old content
$('#od-items').empty();
$('#od-json').empty();
// add the syntax-highlighted json response
$("<pre>").html(syntaxHighlight(data)).appendTo("#od-json");
// process the response data. if we get back children (data.children)
// then render the tile view. otherwise, render the "one-up" view
// for the item's individual data. we also look for children in
// 'data.value' because if this app is ever configured to reqeust
// '/children' directly instead of '/parent?expand=children', then
// they'll be in an array called 'data'
var decodedPath = decodeURIComponent(path);
document.title = "OneDrive Explorer" + ((decodedPath.length > 0) ? " - " + decodedPath : "");
updateBreadcrumb(decodedPath);
var children = data.children || data.value;
if (children && children.length > 0) {
$.each(children, function(i,item) {
var tile = $("<div>").
attr("href", "#" + path + "/" + encodeURIComponent(item.name)).
addClass("item").
click(function() {
// when the page changes in response to a user click,
// we set loadedForHash to the new value and call
// odauth ourselves in user-click mode. this causes
// the catch-all hashchange event handler not to
// process the page again. see comment at the top.
loadedForHash = $(this).attr('href');
window.location = loadedForHash;
odauth(true);
}).
appendTo("#od-items");
// look for various facets on the items and style them accordingly
if (item.folder) {
tile.addClass("folder");
}
if (item.file) {
tile.addClass("file");
}
if (item.thumbnails && item.thumbnails.length > 0) {
var container = $("<div>").attr("class", "img-container").appendTo(tile)
$("<img>").
attr("src", item.thumbnails[0][thumbnailSize].url).
appendTo(container);
}
$("<div>").
addClass("nameplate").
text(item.name).
appendTo(tile);
});
}
else if (data.file) {
// 1-up view
var tile = $("<div>").
addClass("item").
addClass("oneup").
appendTo("#od-items");
var downloadUrl = data['@microsoft.graph.downloadUrl'];
if (downloadUrl) {
tile.click(function(){window.open(downloadUrl, "Download");});
}
if (data.folder) {
tile.addClass("folder");
}
if (data.thumbnails && data.thumbnails.length > 0) {
$("<img>").
attr("src", data.thumbnails[0].large.url).
appendTo(tile);
}
}
else {
$('<p>No items in this folder.</p>').appendTo('#od-items');
}
} else {
$('#od-items').empty();
$('<p>error.</p>').appendTo('#od-items');
$('#od-json').empty();
}
}
});
})(jQuery);
}
else {
alert("Error signing in");
}
}
// start the whole thing off by calling odauth() in non-click mode.
// if the user isn't logged in already, a sign-in link will appear
// for them to click.
odauth();
</script>
</body>
</html>