generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
functionsWeek.ts
449 lines (328 loc) · 16.1 KB
/
functionsWeek.ts
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
449
import ReleaseTimeline from "main";
import { getAPI, isPluginEnabled, DataviewAPI, DateTime } from "obsidian-dataview";
import { moment } from "obsidian";
import { create } from "domain";
import { createErrorMsg, createRowSeparator, createRowSeparatorYearMonth, createRowSeparatorWeek, createRowYear, createRowItem, createNewRow, parseQuerySortOrder, replaceThisInQuery } from "helperFunctions";
import { weekdays } from "moment";
export default class WeekTimeline {
plugin: ReleaseTimeline;
constructor(plugin: ReleaseTimeline) {
this.plugin = plugin;
}
async renderTimelineWeek(content) {
//get data from dataview
const dv = getAPI();
if ( typeof dv == 'undefined' ) { return createErrorMsg('Dataview is not installed. The Release Timeline plugin requires Dataview to properly function.'); }
let dvResults;
let dvResultsFiltered;
try {
content = replaceThisInQuery(content, this.plugin.app);
dvResults = await dv.query(content);
let dvResultsValues = dvResults.value.values;
//filter out null years
let a = dvResultsValues.filter( x => typeof x[1] !== 'undefined' && x[1] !== null );
//filter out years without a date
let b = a.filter( x => !(typeof(x[1]) == 'number') );
//filter out incorrect dates
dvResultsFiltered = b.filter( x => moment( x[1].toString() ).format('YYYY-MM') != "Invalid date" );
//convert all to moment
dvResultsFiltered.forEach( x => x[1] = moment(x[1].toString()) );
}
catch(error) {
return createErrorMsg("Error from dataview: " + error.message);
}
//works
const dvResultsTransformed = this.transformDvResults(dvResultsFiltered);
if (dvResultsTransformed.length == 0) { return createErrorMsg("No results"); }
//transformation
const fullWeekTimelineData = this.fillInMissingWeeks(dvResultsTransformed);
const collapsedEmptyWeeksTimelineData = this.collapseEmptyWeeks(fullWeekTimelineData);
const sortOrder = parseQuerySortOrder(content, this.plugin);
const sortedTimelineData = this.sortTimelineData(collapsedEmptyWeeksTimelineData, sortOrder);
const renderedTimeline = this.renderTimeline(sortedTimelineData);
return renderedTimeline;
}
transformDvResults(dvResults) {
let transformedResults = [];
dvResults.forEach(item => {
//const datePart = item[1].c;
// const yearPart = datePart.year;
// const monthPart = datePart.month - 1;
// const dayPart = datePart.day;
// const momentDate = moment( { year: yearPart, month: monthPart, day: dayPart } );
const momentDate = item[1];
const momentDateThursday = moment(momentDate).isoWeekday(4);
const newYear = momentDateThursday.format('Y');
const newMonth = momentDateThursday.format('Y-MM');
const newWeek = momentDateThursday.format('GGGG-[W]WW');
const newWeekStart = momentDateThursday.isoWeekday(1).format('GGGG-MM-DD');
const newWeekEnd = momentDateThursday.isoWeekday(7).format('GGGG-MM-DD');
const fileName = item[0].path.match(/([^\/]+(?=\.)).md/)[1];
const aliasName = item[2] === null || item[2] === undefined ? fileName : item[2];
const pageObject = {
fileName: fileName,
aliasName: aliasName,
date: momentDate.format('YYYY-MM-DD')
};
let element = transformedResults.find(e => e.week === newWeek);
if (element) {
element.contents.push(pageObject);
}
else {
let newWeekObject = {
year: newYear,
month: newMonth,
week: newWeek,
weekDisplay: '',
weekStart: newWeekStart,
weekEnd: newWeekEnd,
contents: [ pageObject ],
collapsed: false
}
newWeekObject.weekDisplay = this.setWeekFormatting(newWeekObject);
transformedResults.push(newWeekObject);
}
})
return transformedResults;
}
fillInMissingWeeks(contentData) {
/*
let contentData =
[
{ year: '2022', month: '2022-01', week: '2022-W01', collapsed: false, weekDisplay: 'W01: [03 - 09]', weekStart: '2023-01-03', weekEnd: '2023-01-09', contents: [
{fileName: 'Event1', aliasName: 'Event1', date: '2023-01-03'},
{fileName: 'Event2', aliasName: 'Event2', date: '2023-01-04'}
]
},
{
year:'2023', month: '2023-02', week: '2023-W06', collapsed: false, weekDisplay: 'W06: [06-12]', weekStart: '2023-02-06', weekEnd: '2022-02-12', contents: [
{fileName: 'Event3', aliasName: 'Event3', date: '2023-02-07'}
]
}
];
*/
//insert empty weeks
let filledInData = this.insertEmptyWeeksCollapsedNo(contentData);
return filledInData;
}
insertEmptyWeeksCollapsedNo(contentData) {
let existingWeeks = contentData.map(item => item.week).sort();
const minWeek = moment( existingWeeks[0] );
const maxWeek = moment( existingWeeks[ existingWeeks.length - 1 ] );
for (let week = minWeek; week.isSameOrBefore(maxWeek); week.add(1, 'weeks')) {
const weekFormatted = week.format('GGGG-[W]WW');
if ( ! existingWeeks.includes(weekFormatted) ) {
const newYear = moment(week).add(3, 'days').format('Y');
const newMonth = moment(week).add(3, 'days').format('Y-MM');
const newWeek = weekFormatted;
const newWeekStart = week.format('GGGG-MM-DD');
const newWeekEnd = moment(week).add(6, 'days').format('GGGG-MM-DD');
const newWeekObject = {
year: newYear,
month: newMonth,
week: newWeek,
weekDisplay: '',
weekStart: newWeekStart,
weekEnd: newWeekEnd,
contents: [],
collapsed: false
}
newWeekObject.weekDisplay = this.setWeekFormatting(newWeekObject);
contentData.push(newWeekObject);
}
}
return contentData;
}
collapseEmptyWeeks(fullWeekTimelineData) {
//don't do anything if option is turned off
if (this.plugin.settings.collapseEmptyMonthsWeeklyTimeline == false) {
return fullWeekTimelineData;
}
let minMonth = fullWeekTimelineData.reduce((min, item) => item.month < min ? item.month : min, fullWeekTimelineData[0].month);
let maxMonth = fullWeekTimelineData.reduce((max, item) => item.month > max ? item.month : max, fullWeekTimelineData[0].month);
for (let month = moment(minMonth); month.isSameOrBefore(moment(maxMonth)); month.add(1, 'months')) {
const monthFormatted = month.format('Y-MM');
const monthData = fullWeekTimelineData.filter(elem => elem.month == monthFormatted);
const itemsInMonth = monthData.reduce((acc, item) => acc + item.contents.length, 0);
if (itemsInMonth == 0) {
fullWeekTimelineData = fullWeekTimelineData.filter(elem => elem.month != monthFormatted);
const newYear = month.format('Y');
const newMonth = monthFormatted;
const newWeek = monthData.reduce((min, item) => item.week < min ? item.week : min, monthData[0].week);
const newWeekStart = monthData.reduce((min, item) => item.weekStart < min ? item.weekStart : min, monthData[0].weekStart);
const newWeekEnd = monthData.reduce((max, item) => item.weekEnd > max ? item.weekEnd : max, monthData[0].weekEnd);
const newWeekObject = {
year: newYear,
month: newMonth,
week: newWeek,
weekDisplay: '',
weekStart: newWeekStart,
weekEnd: newWeekEnd,
contents: [],
collapsed: true
}
newWeekObject.weekDisplay = this.setWeekFormatting(newWeekObject);
fullWeekTimelineData.push(newWeekObject);
}
}
return fullWeekTimelineData;
}
sortTimelineData(fullWeekTimelineData, sortOrder) {
if (sortOrder == 'asc') {
//sort weeks
fullWeekTimelineData.sort( (a,b) => a.week.localeCompare(b.week) );
//sort data within the weeks
fullWeekTimelineData.forEach(item => {
item.contents.sort( (a,b) => a.date.localeCompare(b.date) );
})
}
if (sortOrder == 'desc') {
//sort weeks
fullWeekTimelineData.sort( (a,b) => b.week.localeCompare(a.week) );
//sort data within the weeks
fullWeekTimelineData.forEach(item => {
item.contents.sort( (a,b) => b.date.localeCompare(a.date) );
})
}
return fullWeekTimelineData;
}
renderTimeline(sortedTimelineData) {
let rlsTbody = document.createElement("tbody");
//loop to render years
while(sortedTimelineData.length != 0) {
const currYear = sortedTimelineData[0].year;
const timelineDataFilteredByYear = sortedTimelineData.filter(elem => elem.year == currYear);
sortedTimelineData = sortedTimelineData.filter(elem => elem.year != currYear);
const htmlYearData = this.renderMonthsInYear(timelineDataFilteredByYear);
const yearRowSpanNb = this.calculateRowSpanYear(htmlYearData);
let htmlYearTr = createEl("tr");
let htmlYearTh = createEl("th", {cls: "year-header", text: currYear});
htmlYearTh.setAttribute("scope", "row");
htmlYearTh.setAttribute("rowspan", yearRowSpanNb);
htmlYearTr.appendChild(htmlYearTh);
rlsTbody.appendChild(htmlYearTr);
rlsTbody.appendChild(htmlYearData);
//add separator
if (sortedTimelineData.length != 0) {
const yearBorder = createRowSeparatorWeek('border');
const yearBorder2 = createRowSeparatorWeek('no-border');
rlsTbody.appendChild(yearBorder);
rlsTbody.appendChild(yearBorder2);
}
}
const rlsTbl = document.createElement("table");
rlsTbl.classList.add("release-timeline");
rlsTbl.appendChild(rlsTbody);
return(rlsTbl);
}
renderMonthsInYear(timelineDataFilteredByYear){
let yearContainer = document.createDocumentFragment();
let lastMonthIsCollpased = false;
//loop to render months
while(timelineDataFilteredByYear.length != 0) {
const currMonth = timelineDataFilteredByYear[0].month;
const currentMonthIsCollapsed = timelineDataFilteredByYear[0].collapsed;
const currMonthText = moment(timelineDataFilteredByYear[0].month).format('MMM');
//separators for collapsed months
if (lastMonthIsCollpased == true && currentMonthIsCollapsed == true) {
}
else {
yearContainer.appendChild(createRowSeparatorWeek('no-border'));
}
lastMonthIsCollpased = currentMonthIsCollapsed;
const timelineDataFilteredByMonth = timelineDataFilteredByYear.filter(elem => elem.month == currMonth);
timelineDataFilteredByYear = timelineDataFilteredByYear.filter(elem => elem.month != currMonth);
//make row with month --need to add rowspan depending on the nb of weeks
let htmlMonthTr = createEl("tr");
let htmlMonthTh;
if (currentMonthIsCollapsed == true) {
htmlMonthTh = createEl("td", {text: ''});
}
else {
htmlMonthTh = createEl("td", {cls: "weekly-month", text: currMonthText});
}
const monthRowSpanNb = this.calculateRowSpanMonth(timelineDataFilteredByMonth);
//don't render additional space if row is a collapsed month
if ( timelineDataFilteredByMonth[0].collapsed == false ) {
htmlMonthTh.setAttribute("scope", "row");
htmlMonthTh.setAttribute("rowspan", monthRowSpanNb);
htmlMonthTr.appendChild(htmlMonthTh);
yearContainer.appendChild(htmlMonthTr);
}
const htmlMonthData = this.renderWeeksInMonth(timelineDataFilteredByMonth);
yearContainer.appendChild(htmlMonthData);
}
return yearContainer;
}
renderWeeksInMonth(timelineDataFilteredByMonth){
let monthContainer = document.createDocumentFragment();
//loop to render weeks
timelineDataFilteredByMonth.forEach(weekData => {
const currWeekText = this.setWeekFormatting(weekData);
const currWeekHasData = weekData.contents.length;
let htmlWeekTr = createEl("tr");
let htmlWeekTh;
if (currWeekHasData == 0) {
htmlWeekTh = createEl("th", {cls: "year-nonexisting", text: currWeekText});
}
else {
htmlWeekTh = createEl("td", {cls: "year-existing", text: currWeekText});
}
const weekRowSpanNb = this.calculateRowSpanWeek(weekData);
htmlWeekTh.setAttribute("scope", "row");
htmlWeekTh.setAttribute("rowspan", weekRowSpanNb);
//for collapsed months add a blank data element
if (weekData.collapsed == true) {
let htmlWeekTd = createEl("td");
htmlWeekTr.appendChild(htmlWeekTd);
}
htmlWeekTr.appendChild(htmlWeekTh);
monthContainer.appendChild(htmlWeekTr);
const createBulletPoints = weekData.contents.length;
weekData.contents.forEach(weekEvent => {
//create event row
const rowItem = createRowItem( { fileName: weekEvent.fileName, fileAlias: weekEvent.aliasName } );
if (createBulletPoints > 1) {
rowItem.addClass('td-next');
}
const newRow = createNewRow(rowItem);
//insert event row
monthContainer.appendChild(newRow);
})
})
return monthContainer;
}
/******************/
calculateRowSpanYear(htmlYear) {
var trCount = htmlYear.querySelectorAll('tr').length + 1;
return trCount;
}
calculateRowSpanMonth(dataMonth) {
let distinctWeeks = dataMonth.length;
let distinctItems = dataMonth.reduce((acc, item) => acc + item.contents.length, 0);
return distinctWeeks + distinctItems + 1;
}
calculateRowSpanWeek(dataWeek) {
let distinctItems = dataWeek.contents.length;
return distinctItems + 1;
}
setWeekFormatting(weekData) {
const formatType = this.plugin.settings.weekDisplayFormat;
const weekNb = moment(weekData.weekStart).format('WW');
const weekStart = moment(weekData.weekStart).format('DD');
const weekEnd = moment(weekData.weekEnd).format('DD');
const monthNbStart = moment(weekData.weekStart).format('MM');
const monthNbEnd = moment(weekData.weekEnd).format('MM');
let weekText;
if (weekData.collapsed == true) {
weekText = moment(weekData.month).format('MMM');
}
else if (formatType == 'weekNames') {
weekText = 'W' + weekNb;
}
else if (formatType == 'dateNames') {
weekText = weekStart + '-' + weekEnd;
}
return weekText;
}
}