-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
150 lines (139 loc) · 4.14 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
<!doctype html>
<html>
<head>
<style>
#logout_content, #login_content {
display: none;
}
</style>
<script src="https://www.dropbox.com/static/api/dropbox-datastores-1.2-latest.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mustache.js/0.8.1/mustache.min.js"></script>
<!--<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.1/Chart.min.js"></script>-->
<script>
function _r(id, data) {
return Mustache.render($('#' + id).text(), data);
}
</script>
<script>
var client = new Dropbox.Client({key: 'ht7i4td3m1ge6t1'});
var _setsTable = null;
client.authenticate({interactive: false}, function(error) {
if (error) {
console.log('Authentication error', error);
}
});
function onAccountInfo(err, info, json) {
$('#login_menu .name').text(info.name);
}
function onSignOut(err) {
if (!err) {
$('#login_content').hide();
$('#logout_content').show();
}
}
function fields(row) {
var data = row.getFields();
data.id = row.getId();
return data;
}
function $appendRow(rowData) {
$('#login_content').append(
$(_r('_t_row', fields(rowData)))
);
}
function $updateRow(rowData, $oldRow) {
$oldRow.replaceWith($(_r('_t_row', fields(rowData))));
}
function $createRows(rr) {
$(rr).each(function() {
$appendRow(this);
});
}
function $updateRows(rr) {
$(rr).each(function() {
var $row = $('#login_content').find('p[data-id='+this.getId()+']');
if ($row.length == 1) {
$updateRow(this, $row);
}
else {
$appendRow(this);
}
});
}
$(document).ready(function() {
$('#loading_content').hide();
if (client.isAuthenticated()) {
console.log('Authenticated');
$('#login_content').show();
client.getAccountInfo({httpCache: true}, onAccountInfo);
// setup and load store contents
var datastoreManager = client.getDatastoreManager();
datastoreManager.openDefaultDatastore(function (error, datastore) {
if (!error) {
_setsTable = datastore.getTable('sets');
var results = _setsTable.query();
$createRows(results);
datastore.recordsChanged.addListener(function (event) {
$updateRows(event.affectedRecordsForTable('sets'));
});
}
else {
console.log('Error opening default datastore', error);
}
});
}
else {
console.log('Unauthenticated');
$('#logout_content').show();
}
});
$(document).on('click', '[data-do=logout]', function() {
client.signOut({}, onSignOut);
});
$(document).on('click', '[data-do=login]', function() {
client.authenticate();
});
$(document).on('click', '[data-do=push]', function() {
_setsTable.insert({
exid: 1,
dynamic: 1,
reps: 5,
weight: 100,
created: new Date()
});
});
$(document).on('click', '[data-do=change]', function() {
var id = $(this).closest('[data-id]').data('id');
var row = _setsTable.get(id);
if (row) {
row.set('weight', row.get('weight') + 5);
}
else {
console.log('Row not found', id);
}
});
</script>
</head>
<body>
<script type="text/mustache" id="_t_row">
<p data-id={{id}}>
this is an <b>{{exid}}</b> of weight <b>{{weight}}</b>
<button data-do="change">Change row</button>
</p>
</script>
<div id="loading_content">
Connecting...
</div>
<div id="logout_content">
<button data-do="login">Login</button>
</div>
<div id="login_content">
<div id="login_menu"><a href="#" data-do="logout">Log out <span class="name">User</span></a></div>
<div id="login_body">
Welcome
<button data-do="push">Add row</button>
</div>
</div>
</body>
</html>