-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
291 lines (253 loc) · 6.77 KB
/
index.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
const BOARD_NAME = 'FamilySearch Research Tasks';
const BOARD_DESCRIPTION = 'Track your research tasks in Trello. Used by the FamilySearch Custom Trello Tab.';
setup();
function setup() {
// When the login is clicked we initiate interactive login meaning we use the popup
$('#login').click(function(){
loading();
login(true);
});
// Noninteractive login to check if an auth token already exists in storage
login(false);
}
/**
* Initiate the auth sequence and handle the result
*
* @param {Boolean=} interactive Whether to use the popup or just try to authenticate with existing tokens stored locally.
*/
async function login(interactive = true) {
const loggedIn = await trelloAuth(interactive);
if(!loggedIn) {
$('#login').css('display', 'flex');
notLoading();
} else {
begin();
}
}
/**
* Initiate Authentication with Trello.
*
* @param {Boolean=} interactive Whether to use the popup or just try to authenticate with existing tokens stored locally.
* @return {Promise<boolean>} resolves to true (authenticated) or false (not authenticated)
*/
function trelloAuth(interactive = true){
return new Promise((resolve, reject) => {
Trello.authorize({
type: 'popup',
name: 'FamilySearch Trello Tab',
scope: {
read: 'true',
write: 'true'
},
interactive,
expiration: 'never',
success: () => resolve(true),
error: () => resolve(false)
});
});
}
/**
* Promisify the Trello.rest() method provided by the client.js Trello library
*
* @param {String} method
* @param {String} url
* @param {Object=} params
* @return {Promise} resolves to response object
*/
function trelloRequest(method, url, params = {}) {
return new Promise((resolve, reject) => {
Trello.rest(method, url, params, resolve, reject);
});
}
async function begin() {
loading();
$('#login').hide();
// Get/create Trello board and list
const boardId = await getBoardId();
const pid = getFSPersonId();
const personsName = await getFSPersonsName(pid);
const newCardDesc = `${personsName}'s profile: https://familysearch.org/tree/person/${pid}`;
const tempListName = personsName ? `${personsName} - ${pid}` : pid;
let list = await getList(boardId, pid);
$('#list-title').text(list ? list.name : tempListName);
if(list) {
await displayList(list.id);
}
// Setup event listeners
$('#new-card-link').click(function(e){
$(this).hide();
$('#new-card').show();
$('#new-card-title').focus();
e.preventDefault();
e.stopPropagation();
});
$(document.body).click(() => {
$('#new-card-link').show();
$('#new-card').hide();
});
$('#new-card-button').click(async function(e) {
let list = await ensureList();
addNewCard(list.id, newCardDesc);
e.stopPropagation();
});
$('#new-card-title').keypress(async function(e) {
if(e.which == 13) {
e.stopPropagation();
e.preventDefault();
let list = await ensureList();
addNewCard(list.id, newCardDesc);
}
});
$('#content').show();
// Now we're done loading
notLoading();
async function ensureList() {
if(!list) {
list = await createList(boardId, tempListName);
}
return list;
}
}
function loading() {
$('#loading').css('display', 'flex');
}
function notLoading() {
$('#loading').hide();
}
/**
* Add a new card to the list
*
* @param {String} listId Trello list ID
* @param {String} desc
*/
function addNewCard(listId, desc) {
const $title = $('#new-card-title');
const title = $title.val().trim();
if(!title) {
return;
}
trelloRequest('POST', `/cards`, {
name: title,
idList: listId,
desc
}).then(() => {
$title.val('');
$('#new-card-button').prop('disabled', false);
$('#new-card-title').focus();
loading();
displayList(listId);
notLoading();
});
}
/**
* Display the Trello list
*
* @param {String} listId
*/
async function displayList(listId) {
const cards = await trelloRequest('GET', `/lists/${listId}/cards`);
// Clear any existing cards
const $list = $('#list').html('');
// Render cards
if(cards.length) {
cards.forEach(c => {
$list.append(displayCard(c));
});
}
}
/**
* Generate the DOM for a card
*
* @param {Object} card card data from the Trello API
* @return {jQuery Element}
*/
function displayCard(card) {
const $card = $(`<div class="card"><div class="card-title">${card.name}</div></div>`).click(() => {
window.open(card.url, 'fstrello');
});
if(card.desc){
$card.append(`<div class="card-desc"> </div>`);
}
return $card;
}
/**
* Get the Trello list for this person, or create a new one
*
* @param {String} boardId Trello board ID
* @param {String} pid FamilySearch person ID
*/
async function getList(boardId, pid) {
const nameRegex = new RegExp(`${pid}$`);
return await trelloRequest('GET', `/board/${boardId}/lists`, {
filter: 'open'
}).then((lists) => {
return lists.find(l => nameRegex.test(l.name));
});
}
/**
* Create a Trello list
*
* @param {String} boardId Trello board ID
* @param {String} name Name of the new Trello board
* @return {Object} trello list
*/
async function createList(boardId, name) {
return await trelloRequest('POST', `/board/${boardId}/lists`, {
name,
pos: 'bottom'
});
}
/**
* Get the ID of the FamilySearch board or create a new board and return its ID
*/
async function getBoardId() {
// Get a list of open boards
const existingFamilySearchBoard = await trelloRequest('GET', '/members/me/boards', {
filter: 'open'
})
// Filter to just personal boards
.then((boards) => {
return boards.filter(b => b.idOrganization === null);
})
// Choose a FamilySearch board if one exists
.then((boards) => {
return boards.find(b => b.name === BOARD_NAME);
});
if(existingFamilySearchBoard) {
return existingFamilySearchBoard.id;
}
// Create a board if one doesn't exist
else {
const response = await trelloRequest('POST', '/boards', {
name: BOARD_NAME,
desc: BOARD_DESCRIPTION,
defaultLists: false
});
return response.id;
}
}
/**
* Get the name of the FS person
*
* @param {String} pid FamilySearch person ID
*/
async function getFSPersonsName(pid) {
const response = await fetch(`https://familysearch.org/platform/tree/persons/${pid}`, {
headers: new Headers({
Accept: 'application/json',
Authorization: `Bearer ${getFSToken()}`
})
});
if(response.ok) {
const json = await response.json();
return json.persons[0].display.name;
}
}
function getFSPersonId() {
const params = (new URL(document.location.href)).searchParams;
return params.get('pid');
}
function getFSToken() {
const params = (new URL(document.location.href)).searchParams;
return params.get('token');
}