index-3.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <html>
  2. <head>
  3. <script>
  4. function importRssFeed (feedUrl) {
  5. var rssRequest = new XMLHttpRequest();
  6. rssRequest.onreadystatechange = function (e) {
  7. var r = rssRequest;
  8. if (r.readyState == XMLHttpRequest.DONE) {
  9. if (r.status >= 200 && r.status < 300) {
  10. console.log("Got RSS (" + r.status + "): ");
  11. var items = importRSSDoc(r.responseXML);
  12. populateItems( items );
  13. createPlot( items );
  14. }
  15. }
  16. };
  17. //rssRequest.open("GET", "bed-harlanji.xml");
  18. rssRequest.open("GET", feedUrl);
  19. rssRequest.send();
  20. }
  21. function importRSSDoc( rssDoc ) {
  22. console.log("importRSSDoc:");
  23. console.log( rssDoc );
  24. //var author = rssDoc.querySelector("generator").textContent.trim();
  25. var items = Array.from( rssDoc.querySelectorAll("item") );
  26. items = items.map(function (i) {
  27. var item = {
  28. title: i.querySelector("title").textContent.trim(),
  29. date: new Date( i.querySelector("pubDate").textContent.trim() ),
  30. description: i.querySelector("description").textContent.trim()
  31. };
  32. var occurenceParts = splitString( item.description, ":", 2);
  33. item.occurence = parseInt( occurenceParts[0] ) || 1;
  34. item.occurenceNote = occurenceParts[1].trim();
  35. if (item.occurenceNote == "") {
  36. item.occurenceNote = "Morning routine";
  37. }
  38. return item;
  39. });
  40. items = items.sort(function (a, b) {
  41. return b.date - a.date;
  42. });
  43. console.log("items:");
  44. console.log(items);
  45. return items;
  46. }
  47. function populateItems( items ) {
  48. console.log("populateItems");
  49. console.log( items );
  50. var rows = items.map(function (item) {
  51. var row = document.createElement( "tr" );
  52. var date = document.createElement( "td" );
  53. var occurence = document.createElement( "td" );
  54. var note = document.createElement( "td" );
  55. date.textContent = dateString( item.date );
  56. occurence.textContent = item.occurence;
  57. note.textContent = item.occurenceNote;
  58. row.appendChild( date );
  59. row.appendChild( occurence );
  60. row.appendChild( note );
  61. return row;
  62. });
  63. var tableBody = document.querySelector("#bed-makings tbody");
  64. console.log("populate: ");
  65. console.log(tableBody);
  66. console.log(rows);
  67. while (tableBody.lastChild) {
  68. tableBody.removeChild(tableBody.lastChild);
  69. }
  70. rows.forEach(function (row) {
  71. tableBody.appendChild(row);
  72. });
  73. }
  74. function dateString (date) {
  75. // return "d: " + date;
  76. var str = (date.getYear() + 1900) + "-"
  77. + new String(date.getMonth() + 1).padStart(2, "0") + "-"
  78. + new String(date.getDate()).padStart(2, "0") + " "
  79. + new String(date.getHours()).padStart(2, "0") + ":"
  80. + new String(date.getMinutes()).padStart(2, "0") + ":"
  81. + new String(date.getSeconds()).padStart(2, "0") + " ";
  82. var tzOffset = date.getTimezoneOffset() / 60;
  83. if (tzOffset >= 0) {
  84. str += "+";
  85. }
  86. str += new String(tzOffset * 100).padStart(4, "0");
  87. return str;
  88. }
  89. function splitString(string, delimiter, n) {
  90. var parts = string.split(delimiter);
  91. return parts.slice(0, n - 1).concat([parts.slice(n - 1).join(delimiter)]);
  92. }
  93. </script>
  94. <script defer data-domain="cityapper.com" src="https://pa.cityapper.com/js/plausible.js"></script>
  95. <script src="vis-timeline-graph2d.min.js"></script>
  96. <link href="vis-timeline-graph2d.min.css" rel="stylesheet" type="text/css" />
  97. <script src="suncalc.js"></script>
  98. </head>
  99. <body>
  100. <script>
  101. function importAuthorSubmitted( form ) {
  102. console.log("submitted:");
  103. console.log(form);
  104. }
  105. var authors = {
  106. "HarlanJI": "https://harlanji.com/bed.xml",
  107. "Marty": "https://harlanji.com/bed-marty.xml"
  108. }
  109. function populateAuthors () {
  110. var authorsElem = document.querySelector("#author");
  111. while(authorsElem.lastChild) {
  112. authorsElem.removeChild(authorsElem.lastChild);
  113. }
  114. Object.entries(authors).forEach(function (e, i) {
  115. var option = document.createElement("option");
  116. option.value = e[1];
  117. option.textContent = e[0];
  118. if ( i == 0 ) {
  119. option.selected = "selected";
  120. }
  121. authorsElem.appendChild(option);
  122. console.log(e);
  123. });
  124. authorsElem.dispatchEvent(new Event('change'));
  125. }
  126. function startOfDay (date) {
  127. var startDate = new Date(date);
  128. startDate.setHours(0);
  129. startDate.setMinutes(0);
  130. startDate.setSeconds(0);
  131. startDate.setMilliseconds(0);
  132. return startDate;
  133. }
  134. function createPlot ( feedItems ) {
  135. console.log("createPlot");
  136. var container = document.getElementById('visualization');
  137. while( container.lastChild ) {
  138. container.removeChild( container.lastChild );
  139. }
  140. var items = feedItems.map(function (item) {
  141. var startDate = startOfDay(item.date);
  142. return {
  143. x: startDate,
  144. y: (item.date - startDate) / (1000 * 60),
  145. group: 0
  146. };
  147. });
  148. // feed items are in descending order
  149. var firstDate = startOfDay( feedItems[ feedItems.length - 1 ].date );
  150. var lastDate = startOfDay( feedItems[ 0 ].date );
  151. var firstTimes = SunCalc.getTimes(firstDate, 44.986656, -93.258133)
  152. var lastTimes = SunCalc.getTimes(lastDate, 44.986656, -93.258133)
  153. items.push({x: firstDate,
  154. y: (firstTimes.sunrise - firstDate) / (1000 * 60),
  155. group: 1});
  156. items.push({x: lastDate,
  157. y: (lastTimes.sunrise - lastDate) / (1000 * 60),
  158. group: 1});
  159. var dataset = new vis.DataSet(items);
  160. var options = {
  161. sort: false,
  162. sampling:false,
  163. style:'points',
  164. dataAxis: {
  165. left: {
  166. range: {
  167. min: 0, max: (60 * 24)
  168. }
  169. }
  170. },
  171. drawPoints: {
  172. enabled: true,
  173. size: 6,
  174. style: 'circle' // square, circle
  175. },
  176. defaultGroup: 'Scatterplot',
  177. height: '400px',
  178. width: '90%'
  179. };
  180. var graph2d = new vis.Graph2d(container, dataset, options);
  181. }
  182. </script>
  183. <h1>Author Made Bed</h1>
  184. <form id="import-author" action="#" onsubmit="console.log('s')">
  185. <p>
  186. Author:
  187. <select id="author" onchange="importRssFeed(this.options[this.selectedIndex].value)">
  188. </select>
  189. </p>
  190. </form>
  191. <div id="visualization"></div>
  192. <table id="bed-makings">
  193. <thead>
  194. <tr>
  195. <th>Date Time</th>
  196. <th>#</th>
  197. <th>Note</th>
  198. </tr>
  199. </thead>
  200. <tbody>
  201. </tbody>
  202. </table>
  203. <script>
  204. //importRssFeed( document.querySelector("#author").value );
  205. populateAuthors();
  206. </script>
  207. </body>
  208. </html>