forked from OneDrive/onedrive-explorer-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
236 lines (216 loc) · 9.27 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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>onedrive explorer</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"
clientId="0000000048144E4F" scopes="onedrive.readonly wl.signin"
redirectUri="https://onedrive.github.io/odx/callback.html"></script>
</head>
<body>
<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>
// 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 = 'root';
}
$('<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) {
path = window.location.hash.substr(1);
beforePath =":";
afterPath = ":";
}
var odurl = "https://api.onedrive.com/v1.0/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' and 'c200x150_Crop' sizes"
// we also attach the access token as a query parameter to every request.
// we could also pass it in through the 'Authorization: bearer' header,
// but that would result in an extra CORS preflight request for every
// unique path.
var odquery = "?expand=thumbnails,children(expand=thumbnails(select=large,c200x150_Crop))&access_token=" + token;
$.ajax({
url: odurl + odquery,
dataType: 'json',
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 = "1drv " + 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.thumbnails && item.thumbnails.length > 0) {
$("<img>").
attr("src", item.thumbnails[0].c200x150_Crop.url).
appendTo(tile);
}
$("<div>").
addClass("nameplate").
text(item.name).
appendTo(tile);
});
}
else {
// 1-up view
var tile = $("<div>").
addClass("item").
addClass("oneup").
appendTo("#od-items");
var downloadUrl = data['@content.downloadUrl'];
if (downloadUrl) {
tile.click(function(){window.location = downloadUrl;});
}
if (data.folder) {
tile.addClass("folder");
}
if (data.thumbnails && data.thumbnails.length > 0) {
$("<img>").
attr("src", data.thumbnails[0].large.url).
appendTo(tile);
}
}
} 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>