-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquery-remove-future.js
305 lines (281 loc) · 14.8 KB
/
query-remove-future.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
//v0.2
//To apply TODO filter, use Shift + Click on the query syntax when a query is loaded with results
//Use this link to see GIF of DEMO: https://user-images.githubusercontent.com/64155612/95497383-168bd680-0957-11eb-8949-e8e428ab44f0.gif
//The goal of this script is to filter out TODOs that only show up because they were added on daily notes page
//Mainly it is if you want to find overdue tasks, like from the last week, by default a TODO due next week will still
//show up if you added that TODO on a daily notes page from last week (within the between range)
//Resources about the script:
//For detailed discussion, see this Slack thread: https://roamresearch.slack.com/archives/C012WK8E9DK/p1601992542022100
//Here is my github repo: https://github.com/GitMurf/roam-javascript
function todoQueryFilter(evt){
if(evt.shiftKey || evt.altKey)
{
var clickedElem = evt.target
var clickedElemText = clickedElem.innerText
clickedElemText = clickedElemText.toLowerCase()
if(clickedElemText.includes("query:") || clickedElemText.includes("[[query]]:"))
{
var querySyntax = clickedElemText
var startOfBetween = querySyntax.indexOf('between:')
if(startOfBetween < 0){return;} //Exit function as no between query parameter
var hiddenCtr = 0;
var phToday = new Date(new Date().getFullYear(),new Date().getMonth() , new Date().getDate())
var phTomorrow = new Date(new Date().getFullYear(),new Date().getMonth() , new Date().getDate()+1)
var phNextWeek = new Date(new Date().getFullYear(),new Date().getMonth() , new Date().getDate()+7)
var phNextMonth = new Date(new Date().getFullYear(),new Date().getMonth() , new Date().getDate()+31)
var phYesterday = new Date(new Date().getFullYear(),new Date().getMonth() , new Date().getDate()-1)
var phLastWeek = new Date(new Date().getFullYear(),new Date().getMonth() , new Date().getDate()-7)
var phLastMonth = new Date(new Date().getFullYear(),new Date().getMonth() , new Date().getDate()-31)
//Find "between"
var afterBetween = querySyntax.substring(startOfBetween + 8)
var endOfBetween = afterBetween.indexOf('}')
var dateRangeStr = afterBetween.substring(0,endOfBetween).trim()
var startDateStr = dateRangeStr.substring(0,dateRangeStr.indexOf(' [[')).trim().split("[").join("").split("]").join("")
var endDateStr = dateRangeStr.substring(dateRangeStr.indexOf(' [[')).trim().split("[").join("").split("]").join("")
var startDate
var endDate
//console.log(startDateStr)
//console.log(endDateStr)
switch(startDateStr)
{
case 'today':
startDate = Date.parse(phToday)
break;
case 'tomorrow':
startDate = Date.parse(phTomorrow)
break;
case 'next week':
startDate = Date.parse(phNextWeek)
break;
case 'next month':
startDate = Date.parse(phNextMonth)
break;
case 'yesterday':
startDate = Date.parse(phYesterday)
break;
case 'last week':
startDate = Date.parse(phLastWeek)
break;
case 'last month':
startDate = Date.parse(phLastMonth)
break;
default:
startDateStr = startDateStr.replace("st,",",").replace("rd,",",").replace("th,",",").replace("nd,",",")
startDate = Date.parse(startDateStr)
}
switch(endDateStr)
{
case 'today':
endDate = Date.parse(phToday)
break;
case 'tomorrow':
endDate = Date.parse(phTomorrow)
break;
case 'next week':
endDate = Date.parse(phNextWeek)
break;
case 'next month':
endDate = Date.parse(phNextMonth)
break;
case 'yesterday':
endDate = Date.parse(phYesterday)
break;
case 'last week':
endDate = Date.parse(phLastWeek)
break;
case 'last month':
endDate = Date.parse(phLastMonth)
break;
default:
endDateStr = endDateStr.replace("st,",",").replace("rd,",",").replace("th,",",").replace("nd,",",")
endDate = Date.parse(endDateStr)
}
if(endDate < startDate)
{
var tmpEndDate = endDate
endDate = startDate
startDate = tmpEndDate
}
var queryParent = clickedElem.parentElement
var queryResults = queryParent.getElementsByClassName("rm-mentions refs-by-page-view");
queryResults = queryResults[0]
var childrenResult = queryResults.children;
//Loop through each page that has query results
for (var i = 0; i < childrenResult.length; i++)
{
//LOOPING THROUGH EACH "PAGE" that has query results under it
var eachChild = childrenResult[i];
var pageTitle = eachChild.getElementsByClassName("rm-ref-page-view-title");
pageTitle = pageTitle[0]
var pageTitleStr = pageTitle.innerText
//Check if page title is a date
//Don't actually use this part for anything but leaving in for future
pageTitleStr = pageTitleStr.replace("st,",",").replace("rd,",",").replace("th,",",").replace("nd,",",")
var pgDateTimeValue = Date.parse(pageTitleStr)
if(isNaN(pgDateTimeValue))
{
//console.log("PAGE is Not a date")
}
else
{
//console.log("PAGE is a date")
}
var childSections = eachChild.getElementsByClassName("rm-reference-item");
for (var j = 0; j < childSections.length; j++)
{
//LOOPING THROUGH EACH "SECTION" (nested location of the blocks) THAT HAS BLOCK RESULTS FOR QUERY
eachSection = childSections[j]
//console.log('*******************************')
//console.log(eachSection.innerText)
//console.log('*******************************')
//Look for any dates in the "parent" blocks which are called "zoom mentions" in Roam html
var zoomMentions = eachSection.getElementsByClassName("rm-zoom zoom-mentions-view");
zoomMentions = zoomMentions[0]
//Find all page links / tags to see if are dates
var parentDateLinks = zoomMentions.querySelectorAll('[data-link-title], [data-tag]')
//Counting how many dates found in parent blocks inside the between range and also in future outside between range
var parDatesFound = 0
var parDatesIn = 0
var parDatesFuture = 0
for (var y = 0; y < parentDateLinks.length; y++)
{
//LOOPING THROUGH EACH PAGE/TAG TO SEE IF IT IS A DATE
eachTag2 = parentDateLinks[y]
if(eachTag2 !== null && eachTag2 !== 'undefined' && typeof eachTag2 !== 'undefined')
{
var startingDate2 = eachTag2.innerText
//console.log(startingDate2)
startingDate2 = startingDate2.replace("st,",",").replace("rd,",",").replace("th,",",").replace("nd,",",").replace("#","")
var dateTimeValue2 = Date.parse(startingDate2)
if(isNaN(dateTimeValue2))
{
//console.log("Not a date...")
}
else
{
parDatesFound = parDatesFound + 1
if(dateTimeValue2 >= startDate && dateTimeValue2 <= endDate)
{
parDatesIn = parDatesIn + 1
//console.log("Date FOUND in range!")
}
else
{
//console.log("Date FOUND, but OUTSIDE range...")
//Check if future date (as opposed to before the between range)
if(dateTimeValue2 > endDate)
{
//If future date then we want to hide because it really isn't "past due"
parDatesFuture = parDatesFuture + 1
}
}
}
}
else
{
//No pages/tags in block
//console.log("No pages/tags found in parents")
}
}
var foundCtr = 0
var childBlocks = eachSection.getElementsByClassName("roam-block dont-unfocus-block hoverparent rm-block-text");
for (var k = 0; k < childBlocks.length; k++)
{
//LOOPING THROUGH EACH BLOCK THAT IS RESULT OF QUERY
eachBlock = childBlocks[k]
//console.log(eachBlock.innerText)
var dateLinks = eachBlock.querySelectorAll('[data-link-title], [data-tag]')
var foundDates = 0
var foundDateInRange = 0
var foundDateInFuture = 0
for (var x = 0; x < dateLinks.length; x++)
{
//LOOPING THROUGH EACH PAGE/TAG TO SEE IF IT IS A DATE
eachTag = dateLinks[x]
if(eachTag !== null && eachTag !== 'undefined' && typeof eachTag !== 'undefined')
{
//console.log(eachTag.innerText)
var startingDate = eachTag.innerText
startingDate = startingDate.replace("st,",",").replace("rd,",",").replace("th,",",").replace("nd,",",").replace("#","")
var dateTimeValue = Date.parse(startingDate)
if(isNaN(dateTimeValue))
{
//console.log("Not a date...")
}
else
{
foundDates = foundDates + 1
if(dateTimeValue >= startDate && dateTimeValue <= endDate)
{
foundDateInRange = foundDateInRange + 1
//console.log("Date FOUND!")
//console.log(dateTimeValue)
}
else
{
//console.log("Date FOUND, but OUTSIDE between range...")
//Check if future date (as opposed to before the between range)
if(dateTimeValue > endDate)
{
//If future date then we want to hide because it really isn't "past due"
foundDateInFuture = foundDateInFuture + 1
}
}
}
}
else
{
//No pages/tags in block
}
}
if(foundDates > 0)
{
//Date(s) in child/TODO block
if(foundDateInFuture > 0)
{
//Hide block as there is a future date in TODO block (not past due)
eachBlock.style.display = "none"
hiddenCtr = hiddenCtr + 1
}
else
{
//Keep showing block
foundCtr = foundCtr + 1
}
}
else if(parDatesFound > 0)
{
//Date(s) in parent block(s)
if(parDatesFuture > 0)
{
//Hide block as no dates in child/TODO and there is a future date in parent block (not past due)
eachBlock.style.display = "none"
hiddenCtr = hiddenCtr + 1
}
else
{
//Keep showing block
foundCtr = foundCtr + 1
}
}
else
{
//No dates found in child/TODO or Parent blocks
//Since it is showing up in query results, then the page its on must be a daily notes page within between range
foundCtr = foundCtr + 1
}
}
//Checks to see if any child/TODO block(s) were left shown, if so, do NOT hide the SECTION
if(foundCtr == 0)
{
//console.log("Hiding this section...")
//console.log(eachSection)
eachSection.style.display = "none"
}
}
}
console.log(hiddenCtr + " items hidden")
}
}
}
document.addEventListener('click', todoQueryFilter)