generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
functionsMonth.ts
377 lines (267 loc) · 12.9 KB
/
functionsMonth.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
import ReleaseTimeline from "main";
import { getAPI, isPluginEnabled, DataviewAPI } from "obsidian-dataview";
import { moment } from "obsidian";
import { create } from "domain";
import { createErrorMsg, createRowSeparator, createRowSeparatorYearMonth, createRowSeparatorWeek, createRowYear, createRowItem, createNewRow, parseQuerySortOrder, replaceThisInQuery } from "helperFunctions";
export default class MonthTimeline {
plugin: ReleaseTimeline;
constructor(plugin: ReleaseTimeline) {
this.plugin = plugin;
}
async renderTimelineMonth(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.'); }
//filter data to remove non-dates
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 month
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);
}
//transform data to the new structure
const dvResultsTransformed = this.transformDvResults(dvResultsFiltered);
if (dvResultsTransformed.length == 0) { return createErrorMsg("No results"); }
//fill in empty months
const fullMonthTimelineData = this.fillInMissingMonths(dvResultsTransformed);
//collapse empty years
const collapsedEmptyYearsTimelineData = this.collapseEmptyYears(fullMonthTimelineData);
//sort data
const sortOrder = parseQuerySortOrder(content, this.plugin);
const sortedTimelineData = this.sortTimelineData(collapsedEmptyYearsTimelineData, sortOrder);
//mark rows with multiple items which will need separators
const markedSeparatorsTimelineData = this.markSeparators(sortedTimelineData);
//render
const renderedTimeline = this.renderTimeline(markedSeparatorsTimelineData);
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 newYear = moment(momentDate).format('Y');
const newMonth = moment(momentDate).format('Y-MM');
const newMonthDisplay = moment(momentDate).format('MMM');
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.month === newMonth);
if (element) {
element.contents.push(pageObject);
}
else {
let newMonthObject = {
year: newYear,
month: newMonth,
monthDisplay: newMonthDisplay,
contents: [ pageObject ],
collapsed: false,
separator: false
}
//newMonthObject.monthDisplay = this.setMonthFormatting(newMonthObject);
transformedResults.push(newMonthObject);
}
})
return transformedResults;
}
fillInMissingMonths(contentData) {
//insert empty weeks
let filledInData = this.insertEmptyMonthsCollapsedNo(contentData);
return filledInData;
}
insertEmptyMonthsCollapsedNo(contentData) {
let existingMonths = contentData.map(item => item.month).sort();
const minMonth = moment( existingMonths[0] );
const maxMonth = moment( existingMonths[ existingMonths.length - 1 ] );
for (let month = minMonth; month.isSameOrBefore(maxMonth); month.add(1, 'months')) {
const monthFormatted = month.format('Y-MM');
if ( ! existingMonths.includes(monthFormatted) ) {
const newYear = moment(month).format('Y');
const newMonth = monthFormatted;
const newMonthDisplay = moment(month).format('MMM')
const newMonthObject = {
year: newYear,
month: newMonth,
monthDisplay: newMonthDisplay,
contents: [],
collapsed: false,
separator: false
}
contentData.push(newMonthObject);
}
}
return contentData;
}
collapseEmptyYears(fullMonthTimelineData) {
let minYear = fullMonthTimelineData.reduce((min, item) => item.year < min ? item.year : min, fullMonthTimelineData[0].year);
let maxYear = fullMonthTimelineData.reduce((max, item) => item.year > max ? item.year : max, fullMonthTimelineData[0].year);
for (let year = moment(minYear); year.isSameOrBefore(moment(maxYear)); year.add(1, 'years')) {
const yearFormatted = year.format('Y');
const yearData = fullMonthTimelineData.filter(elem => elem.year == yearFormatted);
const itemsInYear = yearData.reduce((acc, item) => acc + item.contents.length, 0);
if (itemsInYear == 0) {
fullMonthTimelineData = fullMonthTimelineData.filter(elem => elem.year != yearFormatted);
const newYear = year.format('Y');
const newMonth = year.format('Y-MM')
const newObject = {
year: newYear,
month: newMonth,
monthDisplay: '',
contents: [],
collapsed: true,
separator: false
}
fullMonthTimelineData.push(newObject);
}
}
return fullMonthTimelineData;
}
sortTimelineData(fullMonthTimelineData, sortOrder) {
if (sortOrder == 'asc') {
//sort months
fullMonthTimelineData.sort( (a,b) => a.month.localeCompare(b.month) );
//sort data within the months
fullMonthTimelineData.forEach(item => {
item.contents.sort( (a,b) => a.date.localeCompare(b.date) );
})
}
if (sortOrder == 'desc') {
//sort weeks
fullMonthTimelineData.sort( (a,b) => b.month.localeCompare(a.month) );
//sort data within the months
fullMonthTimelineData.forEach(item => {
item.contents.sort( (a,b) => b.date.localeCompare(a.date) );
})
}
return fullMonthTimelineData;
}
markSeparators(timelineData) {
//get scope of year
//go through the month items in a year
//if prev item or next item has data - mark as separator
for (let i = 0; i<timelineData.length; i++) {
let minusTwoMonthNbItems = timelineData[i-2]?.contents.length ?? 0;
let minusOneMonthNbItems = timelineData[i-1]?.contents.length ?? 0;;
let currMonthNbItems = timelineData[i]?.contents.length ?? 0;;
let plusOneMonthNbItems = timelineData[i+1]?.contents.length ?? 0;;
let condition =
( minusOneMonthNbItems > 1 && (currMonthNbItems > 0 || minusTwoMonthNbItems > 0) )
|| ( currMonthNbItems > 1 && (minusOneMonthNbItems > 0 || plusOneMonthNbItems > 0) );
if ( condition ) {
timelineData[i].separator = true;
}
}
return timelineData;
}
renderTimeline(sortedTimelineData) {
let rlsTbody = document.createElement("tbody");
let prevYearCollapsed = undefined;
//loop to render years
while(sortedTimelineData.length != 0) {
const currYear = sortedTimelineData[0].year;
let currYearCollapsed = sortedTimelineData[0].collapsed;
//add separator
if (! ((currYearCollapsed == true && prevYearCollapsed == true) || prevYearCollapsed == undefined) ) {
const yearBorder = createRowSeparatorYearMonth('border');
const yearBorder2 = createRowSeparatorYearMonth('no-border');
rlsTbody.appendChild(yearBorder);
rlsTbody.appendChild(yearBorder2);
}
prevYearCollapsed = currYearCollapsed;
const timelineDataFilteredByYear = sortedTimelineData.filter(elem => elem.year == currYear);
sortedTimelineData = sortedTimelineData.filter(elem => elem.year != currYear);
if (currYearCollapsed == false) {
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);
}
else {
let htmlYearTr = createEl("tr");
let htmlYearTd = createEl("td");
let htmlYearTh = createEl("th", {cls: "year-nonexisting", text: currYear});
htmlYearTr.appendChild(htmlYearTd);
htmlYearTr.appendChild(htmlYearTh);
rlsTbody.appendChild(htmlYearTr);
}
}
const rlsTbl = document.createElement("table");
rlsTbl.classList.add("release-timeline");
rlsTbl.appendChild(rlsTbody);
return(rlsTbl);
}
renderMonthsInYear(timelineDataFilteredByYear){
let yearContainer = document.createDocumentFragment();
//loop to render weeks
timelineDataFilteredByYear.forEach(monthData => {
const currMonthText = monthData.monthDisplay;
const currMonthHasData = monthData.contents.length;
const renderSeparator = monthData.separator;
let htmlMonthTr = createEl("tr");
//render separator for months with multiple items
if (renderSeparator) {
let newSeparator = createRowSeparatorYearMonth('no-border');
yearContainer.appendChild(newSeparator);
}
let htmlMonthTh;
if (currMonthHasData == 0) {
htmlMonthTh = createEl("th", {cls: "year-nonexisting", text: currMonthText});
}
else {
htmlMonthTh = createEl("th", {cls: "year-existing", text: currMonthText});
}
const monthRowSpanNb = this.calculateRowSpanMonth(monthData);
htmlMonthTh.setAttribute("scope", "row");
htmlMonthTh.setAttribute("rowspan", monthRowSpanNb);
htmlMonthTr.appendChild(htmlMonthTh);
yearContainer.appendChild(htmlMonthTr);
const createBulletPoints = monthData.contents.length;
monthData.contents.forEach(monthEvent => {
//create event row
const rowItem = createRowItem( { fileName: monthEvent.fileName, fileAlias: monthEvent.aliasName } );
if (createBulletPoints > 1) {
rowItem.addClass('td-next');
}
const newRow = createNewRow(rowItem);
//insert event row
yearContainer.appendChild(newRow);
})
})
return yearContainer;
}
/****************/
calculateRowSpanYear(htmlYear) {
var trCount = htmlYear.querySelectorAll('tr').length + 1;
return trCount;
}
calculateRowSpanMonth(dataMonth) {
let distinctItems = dataMonth.contents.length;
return distinctItems + 1;
}
}