Advertisement
Guest User

Untitled

a guest
Dec 17th, 2023
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. ```dataviewjs
  2. const rows = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
  3. const columns = [" ", "Habit 1 ๐Ÿ˜€๐Ÿ‘", "Habit 2 ๐Ÿฅฉ๐Ÿฅ•", "Habit 3 ๐Ÿข๐Ÿ‘จโ€๐Ÿ’ผ", "Habit 4 ๐Ÿ—ฃ๐ŸŽ‰", "Habit 5 โœ๐Ÿ–ฅ", "Habit 6 ๐Ÿ‹๏ธโ€โ™€๐Ÿšฒ", "Habit 7 ๐Ÿงฝ๐Ÿงน"];
  4.  
  5. // Create table element
  6. const table = document.createElement("table");
  7. table.className = 'habit-table';
  8. // Create and append header row
  9. const headerRow = document.createElement("tr");
  10. headerRow.className = "habit-header";
  11. columns.forEach(column => {
  12. const header = document.createElement("th");
  13. header.textContent = column;
  14. headerRow.appendChild(header);
  15. });
  16. table.appendChild(headerRow);
  17.  
  18. // Gets everything between the 2 frontmatter delimiters, but has to be in the format [Word]: [comma separated list of numbers]
  19. // This isnt general code at all, just for my specific use case.
  20. function processLines(input) {
  21. const result = [];
  22. let tempArray = [];
  23. let delimiterCount = 0;
  24.  
  25. for (const element of input) {
  26. if (element === '---') {
  27. delimiterCount++;
  28. if (delimiterCount === 2) {
  29. break;
  30. }
  31. } else if (delimiterCount === 1 && element.includes(':')) {
  32. const dataParts = element.split(': ')[1];
  33. if (dataParts) {
  34. const data = dataParts.split(',').map(Number);
  35. tempArray.push(data);
  36. }
  37. }
  38. }
  39.  
  40. if (tempArray.length > 0) {
  41. result.push(tempArray);
  42. }
  43.  
  44. return result.length > 0 ? result[0] : [];
  45. }
  46.  
  47.  
  48. // Gets the frontmatter value at position row, col
  49. async function getValue(row, col)
  50. {
  51. let fileContent = await dv.io.load(dv.current().file.path.toString());
  52. let lines = processLines(fileContent.split('\n'));
  53. return lines[row][col]
  54. }
  55.  
  56. // Flips the frontmatter value at position row, col. Basically the logic to edit the habit value
  57. async function flipValue(row, col)
  58. {
  59. let fileContent = await dv.io.load(dv.current().file.path.toString());
  60. let file = fileContent.split("\n");
  61. let lines = processLines(file);
  62. lines[row][col] = lines[row][col] === 0 ? 1 : 0;
  63. for(i=0; i<rows.length; i++)
  64. {
  65. file[i+1] = rows[i] + ": " + lines[i].join(",");
  66. }
  67.  
  68. let newString = file.join('\n');
  69. app.vault.adapter.write(dv.current().file.path.toString(), newString)
  70.  
  71. }
  72.  
  73. // Creates the button with HTML. Buttons have id ending -[row]-[col] so i can find which button was pressed easily in the onclick method
  74. async function createButton(rowIndex, colIndex) {
  75. const button = document.createElement("button");
  76. button.id = `button-habit-${rowIndex}-${colIndex}`;
  77.  
  78. // Await the getValue call here
  79. button.textContent = await getValue(rowIndex, colIndex) === 1 ? "๐ŸŸข" : "๐Ÿ”ด";
  80. button.style = '';
  81. button.onclick = async (event) => {
  82. const parts = event.target.id.split('-');
  83. const row = parseInt(parts[2], 10);
  84. const col = parseInt(parts[3], 10);
  85. await flipValue(row, col);
  86. button.textContent = button.textContent === "๐ŸŸข" ? "๐Ÿ”ด" : "๐ŸŸข";
  87. };
  88.  
  89. return button;
  90. }
  91.  
  92. // Create and append data rows
  93. rows.forEach((_, rowIndex) => {
  94. const rowElement = document.createElement("tr");
  95. const day = document.createElement("td");
  96. day.textContent = _;
  97. day.className = "habit-day"
  98. rowElement.appendChild(day);
  99. columns.forEach(async (_, colIndex) => {
  100. if( _ != " "){
  101. const cell = document.createElement("td");
  102. const button = await createButton(rowIndex, colIndex-1);
  103. cell.appendChild(button);
  104. rowElement.appendChild(cell);
  105. }
  106. });
  107. table.appendChild(rowElement);
  108. });
  109.  
  110.  
  111. let calloutTitle = dv.paragraph(`> [!habits|Habits]\n> \n`);
  112. let content = calloutTitle.querySelector("div:last-child");
  113.  
  114. const calloutContent = document.createElement("div");
  115. calloutContent.className = "callout-content";
  116. calloutContent.appendChild(table);
  117. content.appendChild(calloutContent);
  118. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement