Advertisement
Guest User

av_table

a guest
Nov 18th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.60 KB | None | 0 0
  1. const d = new Date(); // d is a date of today. example: 30-09-2019
  2. let month = d.getMonth(); // month is a month of today example: var month for september : 9
  3. let year = d.getFullYear(); // year is a year of today
  4. const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  5. const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  6.  
  7. export const nextAndPreviousTable = document.getElementById("nextPreviousMonth");
  8. export const firstRow = nextAndPreviousTable.querySelectorAll("tbody tr:first-child")[0];
  9. const currentMonthAndYear = firstRow.querySelectorAll("td:first-child span:first-of-type")[0];
  10. currentMonthAndYear.innerHTML = months[month] + " " + year;
  11. const tableArea = document.getElementById("tableArea");
  12. export function start() {
  13. createTable2(month, year);
  14. }
  15.  
  16. export function next() {
  17. deleteTable2(month, year);
  18. if (month >= months.length) {
  19. year = year + 1;
  20. month = month - 12;
  21. currentMonthAndYear.innerHTML = months[month] + " " + year;
  22. createTable2(month, year);
  23. }
  24. if (month < months.length) {
  25. month = month + 1;
  26. if (month >= months.length) {
  27. year = year + 1;
  28. month = month - 12;
  29. currentMonthAndYear.innerHTML = months[month] + " " + year;
  30. createTable2(month, year);
  31. } else {
  32. currentMonthAndYear.innerHTML = months[month] + " " + year;
  33. createTable2(month, year);
  34. }
  35. }
  36.  
  37. }
  38.  
  39. export function previous() {
  40. deleteTable2(month, year)
  41. if (month <= 0) {
  42. month = month + 12;
  43. year = year - 1;
  44. currentMonthAndYear.innerHTML = months[month] + " " + year;
  45. createTable2(month, year);
  46. }
  47. if (month > 0) {
  48. month = month - 1;
  49. if (month < 0) {
  50. month = month + 12;
  51. year = year - 1;
  52. currentMonthAndYear.innerHTML = months[month] + " " + year;
  53. createTable2(month, year);
  54. } else {
  55. currentMonthAndYear.innerHTML = months[month] + " " + year;
  56. createTable2(month, year);
  57. }
  58. }
  59. }
  60.  
  61. /*
  62. createTable2() create a table
  63. */
  64.  
  65. function createTable2(month, year) {
  66.  
  67. if (month == 0 || month == 2 || month == 4 || month == 6 || month == 7 || month == 9 || month == 11) {
  68. for (let day = 1; day <= 31; day++) {
  69. createTheRowForTable2(day, month, year);
  70. }
  71. // create a table for months with 31 days
  72. }
  73. if (month == 3 || month == 5 || month == 8 || month == 10) {
  74. for (let day = 1; day <= 30; day++) {
  75. createTheRowForTable2(day, month, year);
  76. }
  77. // create a table for months with 30 days
  78. }
  79. if (month == 1 && year % 4 != 0) {
  80. for (let day = 1; day <= 28; day++) {
  81. createTheRowForTable2(day, month, year);
  82. }
  83. // create a table for february with 28 days
  84. }
  85. if (month == 1 && year % 4 == 0) {
  86. for (let day = 1; day <= 29; day++) {
  87. createTheRowForTable2(day, month, year);
  88. }
  89. // create a table for february with 29 days
  90. }
  91. }
  92.  
  93. function deleteTable2(month, year) {
  94. if (month == 0 || month == 2 || month == 4 || month == 6 || month == 7 || month == 9 || month == 11) {
  95. for (let day = 1; day <= 31; day++) {
  96. tableArea.deleteRow(1);
  97. }
  98. }
  99. if (month == 3 || month == 5 || month == 8 || month == 10) {
  100. for (let day = 1; day <= 30; day++) {
  101. tableArea.deleteRow(1);
  102. }
  103. }
  104. if (month == 1 && year % 4 != 0) {
  105. for (let day = 1; day <= 28; day++) {
  106. tableArea.deleteRow(1);
  107. }
  108. }
  109. if (month == 1 && year % 4 == 0) {
  110. for (let day = 1; day <= 29; day++) {
  111. tableArea.deleteRow(1);
  112. }
  113. }
  114. }
  115. /*
  116. this function create a row for the different days of a month
  117. */
  118.  
  119. function createTheRowForTable2(day, month, year) {
  120.  
  121. let tr = tableArea.insertRow(day);
  122. tr.id = "tr";
  123. let td1 = tr.insertCell(0);
  124. td1.id = "td1";
  125. let td2 = tr.insertCell(1);
  126. td2.id = "td2";
  127.  
  128. let td3 = tr.insertCell(2);
  129. td3.id = "td3";
  130.  
  131. let fullDate = new Date(year, month, day);
  132. let weekDay = fullDate.getDay(); // example: weekDay=1=monday
  133.  
  134. const checkboxForAvailability = document.createElement("table");
  135. checkboxForAvailability.className = "checkboxArea";
  136. const rowForAvailability = checkboxForAvailability.insertRow(0);
  137. const availabilityOptions = ["available", "not available","preferred"];
  138. availabilityOptions.forEach(function(option){
  139. let cellForAvailability = rowForAvailability.insertCell(availabilityOptions.indexOf(option)); // to find the index of availabilityOptions
  140. const choice = document.createElement("input");
  141. choice.type ="radio";
  142. choice.name = "availability"+day;
  143. choice.value = option;
  144. let labelForChoice = document.createElement("label");
  145. labelForChoice.className="container";
  146. labelForChoice.appendChild(choice);
  147. labelForChoice.appendChild(document.createTextNode(option));
  148. cellForAvailability.appendChild(labelForChoice);
  149. });
  150.  
  151. const checkboxForShift = document.createElement("table");
  152. checkboxForShift.className = "checkboxArea";
  153. const rowForShift = checkboxForShift.insertRow(0);
  154. const shiftOptions = ["08:00-20:00", "20:00-08:00"];
  155. shiftOptions.forEach(function(option){
  156. let cellForShift = rowForShift.insertCell(shiftOptions.indexOf(option));
  157. const choice = document.createElement("input");
  158. choice.type ="checkbox";
  159. choice.name = option;
  160. choice.value = option;
  161. const labelForChoice = document.createElement("label");
  162. labelForChoice.className="container";
  163. labelForChoice.appendChild(choice);
  164. labelForChoice.appendChild(document.createTextNode(option));
  165. cellForShift.appendChild(labelForChoice);
  166. });
  167.  
  168. if (calcDoubleShifts(day, month, year) == true) {
  169. td1.innerHTML = days[weekDay] + " " + day + " " + months[month] + " " + year;
  170. td2.appendChild(checkboxForAvailability);
  171. td3.appendChild(checkboxForShift);
  172. } else {
  173. td1.innerHTML = days[weekDay] + " " + day + " " + months[month] + " " + year;
  174. td2.appendChild(checkboxForAvailability);
  175. const singleShift = document.createTextNode(shiftOptions[1]);
  176. td3.appendChild(singleShift);
  177. }
  178. day++;
  179. }
  180. /*
  181. this function give us the days with 2 shifts
  182. */
  183. export function calcDoubleShifts(day, month, year) {
  184. let doubleShift = false;
  185. let fullDate = new Date(year, month, day);
  186. let weekDay = fullDate.getDay();
  187. let monthAndDay = (month + 1) + "-" + day;
  188. if (weekDay == 0 || weekDay == 6) {
  189. doubleShift = true;
  190. }
  191.  
  192. if (monthAndDay == "1-1" || monthAndDay == "5-1" || monthAndDay == "10-3" ||
  193. monthAndDay == "12-25" || monthAndDay == "12-26" || monthAndDay == "11-1") {
  194. doubleShift = true;
  195. }
  196. let eastern = calcEastern(year);
  197. let easterMonth = 3;
  198. if (eastern > 31) {
  199. eastern = eastern - 31;
  200. easterMonth = 4;
  201. if (eastern <= 2) {
  202. if (monthAndDay == "3-" + (eastern + 29) || monthAndDay == "4-" + (eastern + 1)) {
  203. doubleShift = true;
  204. }
  205. } else {
  206. if (monthAndDay == "4-" + (eastern - 2) || monthAndDay == "4-" + (eastern + 1)) {
  207. doubleShift = true;
  208. }
  209. }
  210. } else {
  211. if (monthAndDay == "3-" + (eastern - 2) || monthAndDay == "3-" + (eastern + 1)) {
  212. doubleShift = true;
  213. }
  214. }
  215. /*
  216. let easterDate = new Date(year,easterMonth,eastern);
  217. easterDate.setDate(eastern+39);
  218. console.log(easterDate);
  219. */
  220. return doubleShift;
  221.  
  222. }
  223. /*
  224. calcEastern give us the day of eastern for different years
  225. */
  226. export function calcEastern(year) {
  227.  
  228. let k = year / 100;
  229. let m = 15 + (3 * k + 3) / 4 - (8 * k + 13) / 25;
  230. let s = 2 - (3 * k + 3) / 4;
  231. let a = year % 19;
  232. let d = (19 * a + m) % 30;
  233. let r = (d + a / 11) / 29;
  234. let og = 21 + d - r;
  235. let sz = 7 - (year + year / 4 + s) % 7;
  236. let oe = 7 - (og - sz) % 7;
  237. let dayOfEastern = og + oe;
  238.  
  239. return Math.trunc(dayOfEastern);
  240. }
  241.  
  242. export function sendTheChoice() {
  243.  
  244. if (month == 0 || month == 2 || month == 4 || month == 6 || month == 7 || month == 9 || month == 11) {
  245. for (let day = 1; day <= 31; day++) {
  246. let selector = document.getElementById("selected2" + day);
  247. let choice = selector.options[selector.selectedIndex].value;
  248. if (choice == "---") {
  249. console.log("error");
  250. alert("check your answers");
  251. }
  252.  
  253. }
  254. }
  255. if (month == 3 || month == 5 || month == 8 || month == 10) {
  256. for (let day = 1; day <= 30; day++) {
  257.  
  258. let selector = document.getElementById("selected2" + day);
  259. let choice = selector.options[selector.selectedIndex].value;
  260. if (choice == "---") {
  261. console.log("error");
  262. alert("check your answers");
  263. }
  264. }
  265. }
  266. if (month == 1 && year % 4 != 0) {
  267.  
  268. for (let day = 1; day <= 28; day++) {
  269. let selector = document.getElementById("selected2" + day);
  270. let choice = selector.options[selector.selectedIndex].value;
  271. if (choice == "---") {
  272. alert("check your answers");
  273. }
  274. }
  275.  
  276. }
  277.  
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement