-
Notifications
You must be signed in to change notification settings - Fork 10
/
app.js
365 lines (297 loc) · 11.3 KB
/
app.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
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
$(function () {
var client = new Dropbox.Client({ key: 'gmd9bz0ihf8t30o' });
client.authDriver(new Dropbox.AuthDriver.Popup({
receiverUrl: window.location.origin + '/oauth_receiver.html'
}));
// Check to see if we're authenticated already.
client.authenticate({ interactive: false }, updateAuthenticationStatus);
// Authenticate when the user clicks the connect button.
$('#connect').click(function (e) {
e.preventDefault();
client.authenticate(updateAuthenticationStatus);
});
function escapeID(myid) {
return "#" + myid.replace( /(:|\.|\[|\])/g, "\\$1" );
}
// Called when the authentication status changes.
function updateAuthenticationStatus(err, client) {
// If the user is not authenticated, show the authentication modal
if (!client.isAuthenticated()) {
$('#login-modal').addClass('md-show');
return;
} else {
$('#login-modal').removeClass('md-show');
}
// Once authenticated, find whether the user is on a team and
// update UI accordingly.
client.getAccountInfo(function (err, info) {
if (info._json.team) {
$('#teamName').text(info._json.team.name);
$('#teamRole').show();
}
});
var datastoreManager = client.getDatastoreManager();
var datastore = null;
var selectedDsid = null;
var previousList = [];
datastoreManager.datastoreListChanged.addListener(function (e) {
var infos = e.getDatastoreInfos();
// Update the list of lists on the left-hand side of the page
$('#lists ul').empty().append(
_.chain(infos)
// Sort by modified time
.sortBy(function (info) {
return info.getModifiedTime();
})
// Generate list items like this:
// <li id="{datastore ID}">{title of datastore} <button class="enabled">X</button></li>
.map(function (info) {
var html = _.template('<li id="${dsid}">${title}<button class="enabled">×</button></li>', {
dsid: info.getId(),
title: info.getTitle()
});
return $(html);
})
.value());
// Highlighted the selected list
if (selectedDsid) {
$(escapeID(selectedDsid)).addClass('selected');
}
// Navigate to #{dsid} on click
$('#lists li').click(function (e) {
e.preventDefault();
window.location.hash = $(this).attr('id');
return false;
});
// Handle delete button click
$('#lists li button').click(function (e) {
e.preventDefault();
var dsid = $(this).parent().attr('id');
// If this is the list we're currently viewing
if (datastore !== null && datastore.getId() === dsid) {
// Close the datastore and null it
datastore.close();
datastore = null;
// Select the first remaining list
var first = _.find($('#lists li'), function (li) {
return $(li).attr('id') !== dsid;
});
window.location.hash = $(first).attr('id') || '';
}
// Delete the datastore
datastoreManager.deleteDatastore(dsid, function () { });
return false;
});
// Notify the user if the datastore they're viewing is removed
var dsidList = _.map(e.getDatastoreInfos(), function (info) { return info.getId(); });
var deleted = _.difference(previousList, dsidList);
if (deleted.indexOf(selectedDsid) >= 0) {
alert('The list you were viewing has been deleted, or your permissions have been revoked.');
datastore.close();
datastore = null;
window.location.hash = $('#lists li:first').attr('id') || '';
}
previousList = dsidList;
// If we're viewing a list, update its UI based on the current
// effective role.
if (datastore) {
updateUIBasedOnRole();
}
});
function updateUIBasedOnRole() {
if (datastore) {
var editing = datastore.getEffectiveRole() !== 'viewer';
// Delete buttons are only enabled if we're editing
$('#items li button').toggleClass('enabled', editing);
// New items can only be added if we're editing
$('#newItem').toggle(editing);
// Roles can only be changed if we're editing
$('.role').prop('disabled', !editing);
}
}
// Populate the right-hand side of the screen (list items)
function populateItems() {
$('#items h1').text(datastore.getTitle());
// Update the list, once on inital open and subsequently on
// changes to the datastore.
function updateList() {
var items = datastore.getTable('items').query();
// Rebuild the list of items
$('#items ul').empty().append(
_.chain(items)
// Sort by created date
.sortBy(function (record) {
return record.get('date');
})
// Convert to list items like this:
// <li id="{record ID}"><button>X</button>{text}</li>
.map(function (record) {
var html = _.template('<li id="${id}"><button>×</button>${text}</li>', {
id: record.getId(),
text: record.get('text')
});
return $(html);
}).value()
);
updateUIBasedOnRole();
// Reflect the latest ACLs in the sharing dialog
$('#public').val(datastore.getRole('public'));
$('#team').val(datastore.getRole('team'));
$('.role').prop('disabled', datastore.getEffectiveRole() === 'viewer');
// Handle deleting a record
$('#items li button').click(function (e) {
e.preventDefault();
var recordId = $(this).parent().attr('id');
datastore.getTable('items').get(recordId).deleteRecord();
});
}
// Update on changes.
datastore.recordsChanged.addListener(updateList);
// Update UI with initial data.
updateList();
}
// Handle the user selecting a list.
function select(dsid) {
// Ignore if this isn't a different list.
if (selectedDsid === dsid) { return; }
// Close sharing modal.
$('#sharing-modal').removeClass('md-show');
// Remember the selected DSID.
selectedDsid = dsid;
// If there's no selection, clear UI.
if (!dsid) {
$('#items ul').empty();
$('#items h1').text('');
$('#items').hide();
$('#lists li').removeClass('selected');
if (datastore !== null) {
datastore.close();
datastore = null;
}
return;
}
// Update selection class.
$('#lists li').removeClass('selected');
$(escapeID(dsid)).addClass('selected');
// If this represents a change from the current datastore
if (datastore === null || datastore.getId() !== dsid) {
// Clear the right-hand side of the page.
$('#items ul').empty();
$('#items h1').text('');
// Open the datastore
datastoreManager.openDatastore(dsid, function (err, ds) {
if (err) {
alert('Error opening list. Make sure you have the permission.');
return;
}
$('#items').show();
// If the selection has already changed, ignore.
if (ds.getId() !== selectedDsid) {
ds.close();
return;
}
// If there's an existing open datastore, close it.
if (datastore !== null && datastore.getId() !== ds.getId()) {
datastore.close();
datastore = null;
}
// Store a pointer to the new open datastore.
datastore = ds;
// Update the UI with items from this datastore.
populateItems();
});
}
}
// Add a new list (datastore)
$('#newList').submit(function (e) {
e.preventDefault();
var title = $('#listName').val();
// Refuse empty titles
if (title.length === 0) { return false; }
// Clear the input
$('#listName').val('');
// Create the datastore
datastoreManager.createDatastore(function (err, ds) {
// Select it
window.location.hash = ds.getId();
// If there's an existing datastore, close it.
if (datastore !== null && datastore.getId() !== ds.getId()) {
datastore.close();
datastore = null;
}
// Remember the new datastore we're looking at.
datastore = ds;
// Set the title
datastore.setTitle(title);
// Populate the right-hand side of the UI.
populateItems();
});
return false;
});
// Add a new item (record) to a list (datastore)
$('#newItem').submit(function (e) {
e.preventDefault();
// Refuse empty strings
if ($('#itemName').val().length === 0) { return false; }
// Insert the record.
datastore.getTable('items').insert({
date: new Date(),
text: $('#itemName').val()
});
// Clear the input
$('#itemName').val('');
return false;
});
// On hash changes, select the new list (datastore)
$(window).hashchange(function (e) {
e.preventDefault();
select(window.location.hash.substring(1));
});
// Update the role when a new value is chosen
$('select.role').change(function () {
var principal = $(this).attr('id');
var role = $(this).val();
datastore.setRole(principal, role);
});
// Trigger the hash change once to pick up the DSID (if any) that
// we started with.
$(window).hashchange();
}
if (window.location.hash.substring(1) && /(iPad|iPhone|iPod)/g.test(navigator.userAgent)) {
$('.md-modal').removeClass('md-show');
$('#ios-modal').addClass('md-show');
$('#ios').click(function (e) {
e.preventDefault();
window.location = 'lists://' + window.location.hash.substring(1);
});
}
$('#logout').click(function () {
client.signOut();
window.location.reload();
});
$('.md-modal input').click(function () {
$(this).select();
});
$('.sharing').click(function () {
$('#url').val(window.location);
$('#sharing-modal').addClass('md-show');
})
function closeModals() {
$('#ios-modal, #sharing-modal').removeClass('md-show');
// Reshow the auth modal if the user is not authenticated
if (!client.isAuthenticated()) {
$('#login-modal').addClass('md-show');
}
}
$('.md-overlay').click(function (e) {
closeModals();
e.preventDefault();
});
$(document).keydown(function (e) {
if (e.which === 27) {
closeModals();
e.preventDefault();
return false;
}
});
});