forked from kurtgn/toggl-on-calendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
215 lines (172 loc) · 7.03 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/3.2.0/fullcalendar.min.js'></script>
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/3.2.0/fullcalendar.min.css' />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pnotify/3.2.1/pnotify.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/pnotify/3.2.1/pnotify.js"></script>
<style>
#calendar {
margin:auto;
width:80%;
}
body {
padding-top:80px
}
</style>
</head>
<body>
<form class="form-horizontal">
<div class="row">
<div class="col-sm-8 col-sm-offset-1">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="api_key" class="col-sm-4 control-label">API key</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="api_key">
</div>
</div>
<div class="form-group">
<label for="workspace_id" class="col-sm-4 control-label">Workspace ID</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="workspace_id">
<span class="help-block">Your workspace ID can be found <a href="http://prnt.sc/efy3px">here</a></span>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-4 col-sm-8">
<button type="button" id="submit_api_btn" class="btn btn-default">Submit</button>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="date_start" class="col-sm-4 control-label">Start date</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="date_start">
</div>
</div>
<div class="form-group">
<label for="date_end" class="col-sm-4 control-label">End date</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="date_end">
</div>
</div>
</div>
</div>
</div>
</div>
</form>
<div id="calendar"></div>
<script>
function make_base_auth(user, password) {
var tok = user + ':' + password;
var hash = Base64.encode(tok);
return "Basic " + hash;
}
function storageAvailable(type) {
try {
var storage = window[type],
x = '__storage_test__';
storage.setItem(x, x);
storage.removeItem(x);
return true;
}
catch(e) {
return false;
}
}
if (storageAvailable('localStorage')) {
var stored_api_key = localStorage.getItem('stored_api_key'),
stored_workspace_id = localStorage.getItem('stored_workspace_id');
if(stored_api_key){
$('#api_key').val(stored_api_key);
}
if(stored_workspace_id){
$('#workspace_id').val(stored_workspace_id);
}
}
$( "#date_start, #date_end" ).datepicker({
dateFormat: "dd.mm.yy"
});
$('#date_start').val(
moment().subtract(10, 'days').format('DD.MM.YYYY')
);
$('#calendar').fullCalendar({
defaultView: 'agendaWeek',
firstDay: 1,
scrollTime: '08:00:00',
nowIndicator: true,
businessHours: {
// days of week. an array of zero-based day of week integers (0=Sunday)
dow: [1, 2, 3, 4, 5],
start: '09:00',
end: '19:00'
}
});
function fetch_page_display(page_num, notice){
var api_key = $('#api_key').val(),
workspace_id = $('#workspace_id').val(),
date_start = $('#date_start').val(),
date_end = $('#date_end').val();
if (storageAvailable('localStorage')) {
localStorage.setItem('stored_api_key', api_key);
localStorage.setItem('stored_workspace_id', workspace_id);
}
if(!date_start){
new PNotify({ text: 'Set start date.', type: 'warning', styling: 'bootstrap3', delay: 10000});
return
}
if(typeof(notice)=='undefined'){
var notice = new PNotify({text: 'fetching page ' + page_num, type: 'info', styling: 'bootstrap3', hide: false });
} else {
notice.update({text: 'fetching page ' + page_num+ '...'});
}
console.log('fetching page ' + page_num);
var since = moment(date_start, 'DD.MM.YYYY').format(),
until = date_end ? moment(date_end, 'DD.MM.YYYY').format() : null;
$.get({
url: 'https://toggl.com/reports/api/v2/details',
data: {
user_agent: '123',
workspace_id: workspace_id,
since: since,
until: until,
page: page_num
},
beforeSend: function (xhr) {
xhr.setRequestHeader ("Authorization", "Basic " + btoa(api_key + ":" + 'api_token'));
},
success: function (result) {
console.log('got ' + result.data.length + ' entries.');
result.data.forEach(function(item, idx){
var title = item.description ? item.description : item.project;
$('#calendar').fullCalendar('addEventSource',[{
title: title,
start: item.start,
end: item.end
}]);
});
if(result.data.length){
fetch_page_display(page_num+1, notice);
} else {
notice.update({ text: 'Finished!', type: 'success', styling: 'bootstrap3', delay: 2000, hide: true});
}
}
})
}
$('#submit_api_btn').click(function(e){
e.preventDefault();
fetch_page_display(1);
});
</script>
</body>
</html>