index.html 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <html>
  2. <head>
  3. <script>
  4. // Bed
  5. function importRssFeed (feedUrl) {
  6. var rssRequest = new XMLHttpRequest();
  7. rssRequest.onreadystatechange = function (e) {
  8. var r = rssRequest;
  9. if (r.readyState == XMLHttpRequest.DONE) {
  10. if (r.status >= 200 && r.status < 300) {
  11. console.log("Got RSS (" + r.status + "): ");
  12. var items = importRSSDoc(r.responseXML);
  13. populateItems( items );
  14. createPlot( items );
  15. importTvFeed( "https://harlanji.com/tv.xml" );
  16. }
  17. }
  18. };
  19. //rssRequest.open("GET", "bed-harlanji.xml");
  20. rssRequest.open("GET", feedUrl);
  21. rssRequest.send();
  22. }
  23. function importRSSDoc( rssDoc ) {
  24. console.log("importRSSDoc:");
  25. console.log( rssDoc );
  26. var feedLink = rssDoc.querySelector("link").textContent.trim();
  27. var items = Array.from( rssDoc.querySelectorAll("item") );
  28. items = items.map(function (i) {
  29. var item = {
  30. title: i.querySelector("title").textContent.trim(),
  31. date: new Date( i.querySelector("pubDate").textContent.trim() ),
  32. description: i.querySelector("description").textContent.trim(),
  33. link: i.querySelector("link").textContent.trim(),
  34. feedLink: feedLink
  35. };
  36. var occurenceParts = splitString( item.description, ":", 2);
  37. item.occurence = parseInt( occurenceParts[0] ) || 1;
  38. item.occurenceNote = occurenceParts[1].trim();
  39. if (item.occurenceNote == "") {
  40. item.occurenceNote = "Morning routine";
  41. }
  42. return item;
  43. });
  44. items = items.sort(function (a, b) {
  45. return b.date - a.date;
  46. });
  47. console.log("items:");
  48. console.log(items);
  49. return items;
  50. }
  51. function populateItems( items ) {
  52. console.log("populateItems");
  53. console.log( items );
  54. var rows = items.map(function (item) {
  55. var row = document.createElement( "tr" );
  56. var date = document.createElement( "td" );
  57. var occurence = document.createElement( "td" );
  58. var note = document.createElement( "td" );
  59. date.textContent = dateString( item.date );
  60. occurence.textContent = item.occurence;
  61. note.textContent = item.occurenceNote;
  62. row.appendChild( date );
  63. row.appendChild( occurence );
  64. row.appendChild( note );
  65. return row;
  66. });
  67. var tableBody = document.querySelector("#bed-makings tbody");
  68. console.log("populate: ");
  69. console.log(tableBody);
  70. console.log(rows);
  71. while (tableBody.lastChild) {
  72. tableBody.removeChild(tableBody.lastChild);
  73. }
  74. rows.forEach(function (row) {
  75. tableBody.appendChild(row);
  76. });
  77. }
  78. var dataset;
  79. function initDataset () {
  80. dataset = new vis.DataSet();
  81. }
  82. var groups = [];
  83. function groupFor (str) {
  84. var idx = groups.indexOf(str);
  85. if (idx == -1) {
  86. idx = groups.length;
  87. groups.push(str);
  88. }
  89. return idx;
  90. }
  91. function createPlot ( feedItems ) {
  92. console.log("createPlot");
  93. var container = document.getElementById('visualization');
  94. while( container.lastChild ) {
  95. container.removeChild( container.lastChild );
  96. }
  97. if (feedItems.length == 0) {
  98. return;
  99. }
  100. var items = feedItems.map(function (item) {
  101. var startDate = startOfDay(item.date);
  102. return {
  103. x: startDate,
  104. y: (item.date - startDate) / (1000 * 60),
  105. group: groupFor(item.feedLink),
  106. id: item.feedLink + item.link
  107. };
  108. });
  109. // feed items are in descending order
  110. var firstDate = startOfDay( feedItems[ feedItems.length - 1 ].date );
  111. var lastDate = startOfDay( feedItems[ 0 ].date );
  112. var currentFirstDate = dataset.get("firstDateSunrise");
  113. if (!currentFirstDate || currentFirstDate.x > firstDate) {
  114. var firstTimes = SunCalc.getTimes(firstDate, 44.986656, -93.258133)
  115. items.push({id: "firstDateSunrise",
  116. x: firstDate,
  117. y: (firstTimes.sunrise - firstDate) / (1000 * 60),
  118. group: groupFor("sunrise")});
  119. }
  120. var currentLastDate = dataset.get("lastDateSunrise");
  121. if (!currentLastDate || currentLastDate.x < lastDate) {
  122. var lastTimes = SunCalc.getTimes(lastDate, 44.986656, -93.258133)
  123. items.push({id: "lastDateSunrise",
  124. x: lastDate,
  125. y: (lastTimes.sunrise - lastDate) / (1000 * 60),
  126. group: groupFor("sunrise")});
  127. }
  128. dataset.update(items);
  129. var options = {
  130. sort: false,
  131. sampling:false,
  132. style:'points',
  133. dataAxis: {
  134. left: {
  135. range: {
  136. min: 0, max: (60 * 24)
  137. }
  138. }
  139. },
  140. drawPoints: {
  141. enabled: true,
  142. size: 6,
  143. style: 'circle' // square, circle
  144. },
  145. defaultGroup: 'Scatterplot',
  146. height: '400px',
  147. width: '90%'
  148. };
  149. var graph2d = new vis.Graph2d(container, dataset, options);
  150. // sunrise is the earliest and latest feed items
  151. var windowStartDate = new Date(dataset.get("firstDateSunrise").x);
  152. var windowEndDate = new Date(dataset.get("lastDateSunrise").x);
  153. // day before first and after the latest day
  154. windowStartDate.setDate( windowStartDate.getDate() - 1 );
  155. windowEndDate.setDate( windowEndDate.getDate() + 1 );
  156. graph2d.setWindow(windowStartDate, windowEndDate, {animation: false});
  157. }
  158. // TV
  159. function importTvFeed (feedUrl) {
  160. var rssRequest = new XMLHttpRequest();
  161. rssRequest.onreadystatechange = function (e) {
  162. var r = rssRequest;
  163. if (r.readyState == XMLHttpRequest.DONE) {
  164. if (r.status >= 200 && r.status < 300) {
  165. console.log("Got TV RSS (" + r.status + "): ");
  166. var items = importTvDoc(r.responseXML);
  167. addTvToPlot( items );
  168. }
  169. }
  170. };
  171. //rssRequest.open("GET", "bed-harlanji.xml");
  172. rssRequest.open("GET", feedUrl);
  173. rssRequest.send();
  174. }
  175. function importTvDoc( rssDoc ) {
  176. console.log("importTvDoc:");
  177. console.log( rssDoc );
  178. var items = Array.from( rssDoc.querySelectorAll("item") );
  179. items = items.map(function (i) {
  180. var title = i.querySelector("title").textContent.trim();
  181. var state = title.indexOf("on") > -1;
  182. var item = {
  183. title: title,
  184. state: state,
  185. date: new Date( i.querySelector("pubDate").textContent.trim() ),
  186. description: i.querySelector("description").textContent.trim()
  187. };
  188. return item;
  189. });
  190. items = items.sort(function (a, b) {
  191. return b.date - a.date;
  192. });
  193. console.log("tv items:");
  194. console.log(items);
  195. return items;
  196. }
  197. function dateString (date) {
  198. // return "d: " + date;
  199. var str = (date.getYear() + 1900) + "-"
  200. + new String(date.getMonth() + 1).padStart(2, "0") + "-"
  201. + new String(date.getDate()).padStart(2, "0") + " "
  202. + new String(date.getHours()).padStart(2, "0") + ":"
  203. + new String(date.getMinutes()).padStart(2, "0") + ":"
  204. + new String(date.getSeconds()).padStart(2, "0") + " ";
  205. var tzOffset = date.getTimezoneOffset() / 60;
  206. if (tzOffset >= 0) {
  207. str += "+";
  208. }
  209. str += new String(tzOffset * 100).padStart(4, "0");
  210. return str;
  211. }
  212. function addTvToPlot ( feedItems ) {
  213. console.log("addTvToPlot");
  214. var items = feedItems.map(function (item) {
  215. var startDate = startOfDay(item.date);
  216. var group = item.state ? 3 : 4;
  217. return {
  218. x: startDate,
  219. y: (item.date - startDate) / (1000 * 60),
  220. group: group
  221. };
  222. });
  223. dataset.add( items );
  224. }
  225. function splitString(string, delimiter, n) {
  226. var parts = string.split(delimiter);
  227. return parts.slice(0, n - 1).concat([parts.slice(n - 1).join(delimiter)]);
  228. }
  229. </script>
  230. <script defer data-domain="cityapper.com" src="https://pa.cityapper.com/js/plausible.js"></script>
  231. <script src="vis-timeline-graph2d.min.js"></script>
  232. <link href="vis-timeline-graph2d.min.css" rel="stylesheet" type="text/css" />
  233. <script src="suncalc.js"></script>
  234. </head>
  235. <body>
  236. <script>
  237. function importAuthorSubmitted( form ) {
  238. console.log("submitted:");
  239. console.log(form);
  240. }
  241. var authors = {
  242. "HarlanJI": "https://harlanji.com/bed.xml",
  243. "Marty": "https://harlanji.com/bed-marty.xml"
  244. }
  245. function populateAuthors () {
  246. var authorsElem = document.querySelector("#author");
  247. while(authorsElem.lastChild) {
  248. authorsElem.removeChild(authorsElem.lastChild);
  249. }
  250. Object.entries(authors).forEach(function (e, i) {
  251. var option = document.createElement("option");
  252. option.value = e[1];
  253. option.textContent = e[0];
  254. if ( i == 0 ) {
  255. option.selected = "selected";
  256. }
  257. authorsElem.appendChild(option);
  258. console.log(e);
  259. });
  260. authorsElem.dispatchEvent(new Event('change'));
  261. }
  262. function startOfDay (date) {
  263. var startDate = new Date(date);
  264. startDate.setHours(0);
  265. startDate.setMinutes(0);
  266. startDate.setSeconds(0);
  267. startDate.setMilliseconds(0);
  268. return startDate;
  269. }
  270. </script>
  271. <h1>Author Made Bed</h1>
  272. <form id="import-author" action="#" onsubmit="console.log('s')">
  273. <p>
  274. Author:
  275. <select id="author" onchange="importRssFeed(this.options[this.selectedIndex].value)">
  276. </select>
  277. </p>
  278. </form>
  279. <div id="visualization"></div>
  280. <table id="bed-makings">
  281. <thead>
  282. <tr>
  283. <th>Date Time</th>
  284. <th>#</th>
  285. <th>Note</th>
  286. </tr>
  287. </thead>
  288. <tbody>
  289. </tbody>
  290. </table>
  291. <script>
  292. //importRssFeed( document.querySelector("#author").value );
  293. populateAuthors();
  294. initDataset();
  295. </script>
  296. </body>
  297. </html>