forked from sendwithus/competencies
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathapp.js
More file actions
280 lines (258 loc) · 9.21 KB
/
app.js
File metadata and controls
280 lines (258 loc) · 9.21 KB
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
var CLIENT_ID = '220295937636-845541ei1vmqcedoa2lgtim8l215aqe5.apps.googleusercontent.com';
$.cookie.defaults.path = '/'
$.cookie.defaults.expires = 10000
$.cookie.json = true;
let personValues = {}
let people = {}
let currentSheetId = ''
let title = ''
$(document).ready(function (event) {
if (window.location.hash) {
$.cookie('selectedPerson', window.location.hash.substring(1))
}
title = $('h1').text()
let people = $.cookie('people')
let person = $.cookie('selectedPerson')
if (people && person) {
document.title = people[person] + ' Competencies'
}
$('h1').html($(`
<a href="../index.html"><img class="w-6 inline-block mr-3" src="seedling.png"></a>
<a class="mr-4 text-white bg-blue-800 px-2 p-1 hover:bg-blue-600 shadow-2xl rounded-full float-right text-xs" style="display:none" title="sign in and get access to the tracking system" class="sign-in-link" id="signIn" href="javascript:;">sign in</a>
<div style="display:none" id="buttonGroup">
<select class="person-chooser mr-4 bg-white" id="personChooser"></select>
<select id="titleChooser" class="person-chooser text-gray-600 bg-white"></select>
<a style="display:none" title="view associated google sheet" class="sheet-link text-sm pt-1 ml-4 hover:opacity-75" target="_blank" id="sheetLink" href=""><i class="icon fab fa-google-drive"></i> drive</a>
<span class="float-right whitespace-no-wrap">
<div id="error" style="display:none" class="mr-10 inline-block p-1 px-4 text-sm rounded-full bg-red-700 text-white"></div>
<a title="add a person to the dropdown" class="text-sm sheet-link pt-1 mr-4 hover:opacity-75" id="addButton" href="javascript:;"><i class="icon fas fa-user-plus"></i> add person</a>
<a title="delete this person from the dropdown" class="text-sm trash-link pt-1 mr-4 hover:opacity-75" id="deleteButton" href="javascript:;"><i class="icon fas fa-user-minus"></i> remove person</a>
<a style="display:none" title="sign out" class="mr-4 text-white bg-blue-800 p-1 px-2 hover:bg-blue-600 shadow-2xl rounded-full text-xs" id="signOut" href="javascript:;">sign out</a>
</span>
</div>
`
))
$('#addButton').on('click', () => {
let response = prompt('enter spreadsheet ID')
if (response && response !== '') {
checkForSheet(response)
}
})
$('#deleteButton').on('click', () => {
let doit = confirm('Delete this person from the drop down list?')
if (doit) {
removeSelected()
}
})
$('#personChooser').on('change', () => {
let selected = getSelected()
$.cookie('selectedPerson', selected)
window.location.hash = selected
checkForSheet(selected)
})
$('.drive-link').on('click', (e) => {
let el = $(e.target).parent().parent();
let name = el.text().trim()
let id = $(el).attr('id')
addInProgress(id, name)
})
$('#signOut').on('click', () => {
signOut();
})
$('#signIn').on('click', () => {
signIn();
})
setupTitleDropdown()
$('#titleChooser').on('change', () => {
switchPage()
})
loadCookieValues()
gapi.load('client:auth2', initClient);
});
function switchPage() {
window.location.href = $('#titleChooser').val() + (window.location.hash ? window.location.hash : '')
}
function setupTitleDropdown() {
let titles = ''
let pathname = window.location.pathname.substring(window.location.pathname.lastIndexOf('/') + 1)
options.split(',').forEach((option) => {
if (option === '') {
return true
}
let link = option + ".html"
$('#titleChooser').append($('<option value="' + link + '" ' + (pathname === link ? 'SELECTED' : '') + '>' + option.replaceAll('-', ' ') + '</option>'))
});
}
function removeSelected() {
delete people[currentSheetId]
$.cookie('people', people)
$.cookie('selectedPerson', '')
$("#personChooser option[value='" + currentSheetId + "']").remove();
checkForSheet(getSelected())
}
function addInProgress(id, name) {
$('.drive-link', $('#' + id)).fadeOut(() => {
$('.' + id).addClass('in-progress')
});
name = name.replace(/: level [0-9]+/, '')
var params = {
spreadsheetId: currentSheetId,
range: 'Sheet1!A2:B',
valueInputOption: 'USER_ENTERED'
};
var valueRangeBody = {
'range': 'Sheet1!A2:B',
'majorDimension': 'ROWS',
'values': [
[name, '0']
]
}
gapi.client.sheets.spreadsheets.values.append(params, valueRangeBody).then(function (response) {
// expected path.
}, function (reason) {
error(reason.result.error.message)
console.error('error: ' + reason);
});
}
function getSelected() {
if ($('#personChooser').children("option:selected").length == 0) { return ''; }
return $('#personChooser').children("option:selected").val();
}
function loadCookieValues() {
peopleLoaded = $.cookie('people')
if (!peopleLoaded) {
return
}
people = peopleLoaded
let selectedPerson = $.cookie('selectedPerson')
for (person in people) {
createOption(person, people[person], selectedPerson == person)
}
}
function createOption(sheetId, name, selected) {
$('#personChooser').append($('<option value="' + sheetId + '"' + (selected ? ' selected' : '') + '>' + name + '</option>'))
}
function cleanValues() {
$('.competency').removeClass('complete')
$('.competency').removeClass('in-progress')
$('.drive-link').hide()
$('.fa-check').remove()
}
function checkForSheet(sheetId) {
cleanValues()
if (!sheetId || sheetId === '') {
alert('must select a sheet ID or add one using the add button.')
$('#sheetLink').hide()
return
}
var params = {
spreadsheetId: sheetId,
range: 'Sheet1!A1:B'
};
gapi.client.sheets.spreadsheets.values.get(params).then(function (response) {
$('.drive-link').show()
currentSheetId = sheetId
$('#sheetLink').attr('href', 'https://docs.google.com/spreadsheets/d/' + sheetId);
$('#sheetLink').show()
let values = response.result.values
if (!values || !values[0] || values[0][0].trim() === '') {
error('must have name in cell A1 - add a name then try again')
return;
}
let name = values[0][0]
personValues = {}
for (var i = 1; i < values.length; i++) {
personValues[values[i][0]] = values[i][1]
}
if (!people[sheetId]) {
createOption(sheetId, name)
addName(name, sheetId)
}
checkOffCompetencies()
setSelected(sheetId)
errorHide()
}).catch((err) => {
$('#sheetLink').hide()
console.error('error', err)
error(err.result.error.message)
});
}
function errorHide() {
$('#error').hide()
}
function error(msg) {
$('#error').html(msg)
$('#error').show()
setTimeout(errorHide, 3000)
}
function setSelected(sheetId) {
$('#personChooser').val(sheetId);
$.cookie('selectedPerson', sheetId)
window.location.hash = sheetId
}
function checkOffCompetencies() {
for (skill in personValues) {
let id = name2Id(skill)
let level = parseInt(personValues[skill])
$('.' + id).each((i, el) => {
let targetLevel = parseInt($(el).attr('level'))
$('.drive-link', $(el)).hide();
if (level === 0 || level < targetLevel) {
$(el).addClass('in-progress')
}
if (targetLevel <= level) {
$(el).addClass('complete')
$(el).prepend('<i class="fas fa-check"></i> ')
}
})
}
}
function name2Id(name) {
return "c-" + name.toLowerCase().replaceAll(' ', '')
}
function initClient() {
var SCOPE = 'https://www.googleapis.com/auth/spreadsheets';
gapi.client.init({
'clientId': CLIENT_ID,
'scope': SCOPE,
'discoveryDocs': ['https://sheets.googleapis.com/$discovery/rest?version=v4'],
}).then(function () {
gapi.auth2.getAuthInstance().isSignedIn.listen(updateSignInStatus);
updateSignInStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
});
}
function addName(name, sheetId) {
let people = $.cookie('people')
if (!people) {
people = {}
}
people[sheetId] = name;
$.cookie('people', people)
}
function updateSignInStatus(isSignedIn) {
if (isSignedIn) {
$('#signOut').show()
$('#signIn').hide()
email = gapi.auth2.getAuthInstance().currentUser.get().getBasicProfile().getEmail();
document.getElementById('buttonGroup').style.display = 'inline'
if (getSelected() || getSelected() !== '') {
checkForSheet(getSelected())
}
} else {
$('#signOut').hide()
$('#buttonGroup').hide()
$('#signIn').show()
cleanValues()
}
}
function signIn() {
gapi.auth2.getAuthInstance().signIn();
}
function signOut() {
$('h1').html(title)
gapi.auth2.getAuthInstance().signOut();
}
String.prototype.replaceAll = function (search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};