-
Notifications
You must be signed in to change notification settings - Fork 0
/
ist.fn.dyn.js
448 lines (386 loc) · 16.1 KB
/
ist.fn.dyn.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/* global Cookies, $, _, localStorage, showdown */
import { spinOut } from './ist.fn.event.js';
export { getDynalistContent, dynalistSetAuthEvents };
function getDynalistContent(commentContent, taskID) {
if (Cookies.get('dynalistToken') === undefined) {
const dynalistAuthHTML = $("<small>")
.append(
$("<em>").html(
"To authorize Ist to display your Dynalist documents, enter your " +
"<a href='https://dynalist.io/developer' target='_blank'>secret token</a>:" +
"<form id='dynalistAuthSubmit'>" +
"<input type='text' name='dynalistSecret' />" +
"<button name='submit' type='submit'>↵</button>" +
"</form>"
)
);
return dynalistAuthHTML;
} else {
const [dynalistFileID, dynalistSubItem] = commentContent
.slice(commentContent.lastIndexOf('/') + 1)
.split('#z='),
readCommands = { file_id: dynalistFileID };
postDynalistAPI('read', readCommands, function (output) {
const dynalistMenuButtonsArray = [
{
name: 'read',
symbol: '📜',
tooltip:
'View this Dynalist document as a comment. Useful for Todoist comments that require a more robust editing interface.'
},
{
name: 'checklist',
symbol: '🏁',
tooltip:
'View this Dynalist document as a checklist; when each top-level item is checked, the next is shown. Useful for tasks with many steps.'
},
{
name: 'rotating',
symbol: '⏬',
tooltip:
'WARNING! THIS EDITS THIS DOCUMENT. View the first top-level item in this Dynalist document; when marked done, it will be sent to the bottom of the document. Useful for lists of chores that only need to be done once in a while.'
},
{
name: 'project',
symbol: '📎',
tooltip:
'WARNING! THIS EDITS THIS DOCUMENT. View the first deepest item in this Dynalist document; when marked done, it will be marked done in Dynalist. Useful for projects with a set list of steps.'
}
],
dynalistMenu = $("<div class='dynalistmenu'></div>");
$.each(dynalistMenuButtonsArray, (i, button) => {
const buttonHTML = $(
`<button class='dynalistMenuButton' dynalistview='${button.name}' title='${button.tooltip}'>${button.symbol}<span>${button.name}</span></button>`
);
dynalistMenu.append(buttonHTML);
});
const dynalistLinkSymbol = $(
' <span class="dynalistLink" dynalistview="view"><svg height="28" viewBox="0 -4 23 23" width="28" xmlns="http://www.w3.org/2000/svg"><g fill="none" fill-rule="evenodd" stroke="silver" stroke-linecap="round" stroke-linejoin="round" transform="translate(4 4)"><path d="m5.5 7.5c.96940983 1.36718798 3.01111566 1.12727011 4.01111565 0l1.98888435-2c1.1243486-1.22807966 1.1641276-2.81388365 0-4-1.135619-1.15706921-2.86438099-1.15706947-4 0l-2 2"/><path d="m.64175661 12.3971156c.96940983 1.367188 3 1.1970433 4 .0697732l2-1.9748738c1.12434863-1.22807961 1.16412758-2.83900987 0-4.02512622-1.13561902-1.15706922-2.86438099-1.15706948-4 0l-2 2" transform="matrix(-1 0 0 -1 8.14 18.966)"/></g></svg></span>'
);
dynalistMenu.append(dynalistLinkSymbol);
$('#spinnerDynalist').remove();
const dynalistNodesOpen = _.filter(output.nodes, function (node) {
if (node.checked !== true) {
return true;
}
}),
parentID = dynalistSubItem || 'root',
parentNode = dynalistNodesOpen.find(
(task) => task.id === parentID
),
{ childNodes: dynalistNodesOrdered, parentNode: dynalistNodeParent } = treeDynalist(dynalistNodesOpen, parentID),
dynalistHTML = getDynalistHTML(
dynalistNodesOrdered,
taskID,
dynalistFileID,
parentNode,
dynalistNodeParent
);
const dynalistView = $(dynalistHTML).attr('dynalistView');
if (!dynalistView.startsWith('count')) {
$(dynalistMenu).find(`button[dynalistview='${dynalistView}']`).addClass('important');
dynalistHTML.prepend(dynalistMenu)
}
$('.taskComments').append(dynalistHTML);
if (
parentNode.note &&
!['checklist', 'rotating', 'project', 'count'].includes(parentNode.note.split(' ')[0])
) {
const converter = new showdown.Converter(),
parentNote = converter.makeHtml(parentNode.note),
parentNoteBox = $(
`<div id='parentnote'>${parentNote}</div>`
);
$(`.taskComment#${parentNode.id}`).append(parentNoteBox);
}
$('.taskComments').append(dynalistHTML);
$('li:has(table)').css('list-style-type', 'none');
dynalistSetEvents(commentContent, taskID);
});
}
}
function postDynalistAPI(endpoint, commands, callback) {
$.ajax({
type: 'POST',
url: `https://dynalist.io/api/v1/doc/${endpoint}`,
data: JSON.stringify({
token: Cookies.get('dynalistToken'),
...commands
}),
success: function (data) {
callback(data);
},
error: function (jqXHR, textStatus, errorThrown) {
console.error('API call failed:', textStatus, errorThrown);
Cookies.remove('dynalistToken');
}
});
}
function treeDynalist(nodesOpen, parentID) {
const parentNode = _.find(nodesOpen, function (value) {
return value.id === parentID;
});
const childNodes = parentNode.children;
const nodesTree = treeGetChildren(childNodes, nodesOpen, parentID);
delete parentNode.children;
return { childNodes: nodesTree, parentNode: parentNode };
}
function treeGetChildren(ids, nodesOpen) {
const nodesNew = [];
$.each(ids, (i, id) => {
const nodeNew = _.find(nodesOpen, function (value) {
return value.id === id;
});
if (nodeNew) {
if (nodeNew.children) {
nodeNew.childrenNodes = treeGetChildren(
nodeNew.children,
nodesOpen
);
}
nodesNew.push(nodeNew);
}
});
return nodesNew;
}
function getDynalistHTML(tree, taskID, dynalistFileID, parentNode, dynalistNodeParent) {
const treeHTML = $('<div class="taskComment"></div>');
let dynalistView = localStorage.getItem(`dynalistview.${taskID}`);
if (['count'].includes(parentNode.note.split(' ')[0])) {
dynalistView = parentNode.note
} else if (!dynalistView) {
if (
parentNode.note &&
['checklist', 'rotating', 'project'].includes(parentNode.note.split(' ')[0])
) {
dynalistView = parentNode.note;
} else {
dynalistView = 'read'
}
}
$(`.taskComment#${parentNode.id} .dynalistmenu button[dynalistview='${dynalistView || 'read'}']`).addClass('important');
let count;
if (dynalistView.indexOf('count') === 0) {
const parts = dynalistView.split(' ');
count = {
total: parseInt(parts[1].split('/')[0]),
current: parts[1].split('/')[1] ? parseInt(parts[1].split('/')[1]) : 0,
date: parts[2] ? new Date(parts[2]) : null
}
if ((!count.date) || (count.date && count.date < new Date().setHours(0, 0, 0, 0))) {
count.current = 0
}
}
let treeHTMLRender;
switch (parentNode.note.split(' ')[0]) {
default:
case 'read':
treeHTMLRender = treeHTMLGetChildren(tree, 'root', dynalistFileID);
treeHTMLRender
.addClass('bulletleft')
.find('ul')
.addClass('bulletleft');
treeHTML.append(treeHTMLRender);
break;
case 'checklist':
case 'rotating':
treeHTML.append(
treeHTMLGetChecklist(
tree,
dynalistView,
dynalistFileID,
parentNode.id
)
);
break;
case 'project':
treeHTML.append(
treeHTMLGetProject(tree, dynalistFileID, parentNode.id)
);
break;
case 'count':
treeHTML.append(
treeHTMLGetCount(dynalistNodeParent, count)
);
break;
}
treeHTML.attr('dynalistView', dynalistView).attr('id', parentNode.id).attr('dynalistFileID', dynalistFileID).attr('viewType', parentNode.note.split(' ')[0])
return treeHTML;
}
function treeHTMLGetChildren(children, parentID, dynalistFileID) {
const treeHTMLInner = $('<ul></ul>');
$.each(children, (i, node) => {
const converter = new showdown.Converter(),
nodeContentHTML = converter
.makeHtml(node.content)
.replace(/(<p[^>]+?>|<p>|<\/p>)/gim, ''),
nodeHTML = $('<li></li>')
.attr('dynalistid', node.id)
.attr('dynalistparentid', parentID)
.attr('dynalistfileid', dynalistFileID)
.html(nodeContentHTML);
if (node.childrenNodes) {
const nodeChildrenHTML = treeHTMLGetChildren(
node.childrenNodes,
node.id,
dynalistFileID
);
nodeHTML.append(nodeChildrenHTML);
}
treeHTMLInner.append(nodeHTML);
});
return treeHTMLInner;
}
function treeHTMLGetCount(dynalistNodeParent, count) {
const dynalistNodeParentHTML = $(`<ul>${dynalistNodeParent.content}</ul>`);
dynalistNodeParentHTML.addClass('nobullets');
const currentCountClass = count.current >= count.total ? ' completed' : '',
countsContainer = $(`<span class="counts${currentCountClass}"></span>`),
currentCountPercent = Math.round((count.current / count.total) * 100) + '%',
countsElements = $(`<small>${currentCountPercent}</small><span class="currentCount">${count.current}</span>/<span class="goalCount">${count.total}</span><br />`),
countsAmounts = [1, 5, 10];
let countsButtons = $(`<span class="countButtons"></span>`);
$.each(countsAmounts, (i, number) => {
countsButtons.append(`<button class="count-add-${number}">+${number}</button>`);
});
countsContainer.append(countsElements, countsButtons);
dynalistNodeParentHTML.append(countsContainer);
return dynalistNodeParentHTML;
}
function treeHTMLGetChecklist(tree, view, dynalistFileID, parentID) {
const treeHTMLChildren = treeHTMLGetChildren(
tree,
parentID,
dynalistFileID
),
treeHTMLChildrenCount = treeHTMLChildren.children().length;
treeHTMLChildren.addClass('nobullets');
treeHTMLChildren.children().prepend(function (index) {
return `<button class='done${view}'>done<br /><small>${index + 1}/${treeHTMLChildrenCount}</small></button>`;
});
treeHTMLChildren.children().not(':first').hide();
return treeHTMLChildren;
}
function treeHTMLGetProject(tree, dynalistFileID, parentID) {
const treeHTMLChildren = treeHTMLGetChildren(
tree,
parentID,
dynalistFileID
);
let taskFound = 0;
$.each($(treeHTMLChildren[0]).find('li'), (i, node) => {
node = $(node);
if (taskFound === 1) {
node.remove();
return true;
}
const firstChild = node.find('li');
if (firstChild.length === 0) {
node.addClass('projectTask nobullets').prepend(
$("<button class='doneproject'>done</button>")
);
taskFound = 1;
} else {
node.addClass('projectParent nobullets').prepend('... ');
}
});
treeHTMLChildren.addClass('bulletleft');
return treeHTMLChildren;
}
function dynalistSetEvents(link, taskID) {
$('.dynalistMenuButton, .dynalistLink').on('click auxclick', function () {
if ($(this).attr('dynalistview') === 'view') {
window.open(link, '_blank');
} else {
localStorage.setItem(
`dynalistview.${taskID}`,
$(this).attr('dynalistview')
);
spinOut();
}
});
$('.donechecklist').on('click auxclick', function () {
const dynalistNext = $(this).parent().next('li');
if (dynalistNext.length > 0) {
dynalistNext.show();
} else {
$('.taskComments').append(
'Checklist finished! Mark this task done or reload to start over.'
);
}
$(this).parent().hide();
});
$('.donerotating').on('click auxclick', function () {
const writeCommands = {
file_id: $(this).parent().attr('dynalistfileid'),
changes: [
{
action: 'move',
node_id: $(this).parent().attr('dynalistid'),
parent_id: $(this).parent().attr('dynalistparentid'),
index: -1
}
]
};
postDynalistAPI('edit', writeCommands, function (output) {
spinOut();
});
});
$('.doneproject').on('click auxclick', function () {
const writeCommands = {
file_id: $(this).parent().attr('dynalistfileid'),
changes: [
{
action: 'edit',
node_id: $(this).parent().attr('dynalistid'),
checked: true
}
]
};
postDynalistAPI('edit', writeCommands, function (output) {
spinOut();
});
});
$('.count-add-1, .count-add-5, .count-add-10').on('click auxclick', function () {
const buttonClicked = this,
added = Number($(buttonClicked).attr('class').split('-').pop()),
currentCountElement = $(buttonClicked).parent().siblings('.currentCount'),
oldCount = Number(currentCountElement.text()),
currentCount = oldCount + added,
goalCount = $(buttonClicked).parent().siblings('.goalCount').text(),
countPercent = Math.round((currentCount / goalCount) * 100) + '%',
today = new Date();
today.setHours(0, 0, 0, 0);
const formattedDate = (today.getMonth() + 1) + '-' + today.getDate() + '-' + today.getFullYear(),
newCountString = `count ${goalCount}/${currentCount} ${formattedDate}`,
dynalistFileID = $(buttonClicked).closest('.taskComment').attr('dynalistFileID');
const nodeID = $(buttonClicked).closest('.taskComment').attr('id');
const writeCommands = {
file_id: dynalistFileID,
changes: [
{
action: 'edit',
node_id: nodeID,
note: newCountString
}
]
};
postDynalistAPI('edit', writeCommands, function (output) {
$(currentCountElement).text(currentCount)
$(currentCountElement).siblings('small').text(countPercent)
if (currentCount >= goalCount) {
$(buttonClicked).closest('.counts').addClass('completed');
}
});
});
}
function dynalistSetAuthEvents() {
$('#dynalistAuthSubmit').on('submit', function (event) {
event.preventDefault();
const authURL = `?state=dynalist&code=${$('input[name=dynalistSecret]')
.val()
.replace(/[^a-z0-9áéíóúñü .,_-]/gim)
}`;
window.location.replace(authURL);
});
}