Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const d = new Date(); // d is a date of today. example: 30-09-2019
- let month = d.getMonth(); // month is a month of today example: var month for september : 9
- let year = d.getFullYear(); // year is a year of today
- const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
- const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
- export const nextAndPreviousTable = document.getElementById("nextPreviousMonth");
- export const firstRow = nextAndPreviousTable.querySelectorAll("tbody tr:first-child")[0];
- const currentMonthAndYear = firstRow.querySelectorAll("td:first-child span:first-of-type")[0];
- currentMonthAndYear.innerHTML = months[month] + " " + year;
- const tableArea = document.getElementById("tableArea");
- export function start() {
- createTable2(month, year);
- }
- export function next() {
- deleteTable2(month, year);
- if (month >= months.length) {
- year = year + 1;
- month = month - 12;
- currentMonthAndYear.innerHTML = months[month] + " " + year;
- createTable2(month, year);
- }
- if (month < months.length) {
- month = month + 1;
- if (month >= months.length) {
- year = year + 1;
- month = month - 12;
- currentMonthAndYear.innerHTML = months[month] + " " + year;
- createTable2(month, year);
- } else {
- currentMonthAndYear.innerHTML = months[month] + " " + year;
- createTable2(month, year);
- }
- }
- }
- export function previous() {
- deleteTable2(month, year)
- if (month <= 0) {
- month = month + 12;
- year = year - 1;
- currentMonthAndYear.innerHTML = months[month] + " " + year;
- createTable2(month, year);
- }
- if (month > 0) {
- month = month - 1;
- if (month < 0) {
- month = month + 12;
- year = year - 1;
- currentMonthAndYear.innerHTML = months[month] + " " + year;
- createTable2(month, year);
- } else {
- currentMonthAndYear.innerHTML = months[month] + " " + year;
- createTable2(month, year);
- }
- }
- }
- /*
- createTable2() create a table
- */
- function createTable2(month, year) {
- if (month == 0 || month == 2 || month == 4 || month == 6 || month == 7 || month == 9 || month == 11) {
- for (let day = 1; day <= 31; day++) {
- createTheRowForTable2(day, month, year);
- }
- // create a table for months with 31 days
- }
- if (month == 3 || month == 5 || month == 8 || month == 10) {
- for (let day = 1; day <= 30; day++) {
- createTheRowForTable2(day, month, year);
- }
- // create a table for months with 30 days
- }
- if (month == 1 && year % 4 != 0) {
- for (let day = 1; day <= 28; day++) {
- createTheRowForTable2(day, month, year);
- }
- // create a table for february with 28 days
- }
- if (month == 1 && year % 4 == 0) {
- for (let day = 1; day <= 29; day++) {
- createTheRowForTable2(day, month, year);
- }
- // create a table for february with 29 days
- }
- }
- function deleteTable2(month, year) {
- if (month == 0 || month == 2 || month == 4 || month == 6 || month == 7 || month == 9 || month == 11) {
- for (let day = 1; day <= 31; day++) {
- tableArea.deleteRow(1);
- }
- }
- if (month == 3 || month == 5 || month == 8 || month == 10) {
- for (let day = 1; day <= 30; day++) {
- tableArea.deleteRow(1);
- }
- }
- if (month == 1 && year % 4 != 0) {
- for (let day = 1; day <= 28; day++) {
- tableArea.deleteRow(1);
- }
- }
- if (month == 1 && year % 4 == 0) {
- for (let day = 1; day <= 29; day++) {
- tableArea.deleteRow(1);
- }
- }
- }
- /*
- this function create a row for the different days of a month
- */
- function createTheRowForTable2(day, month, year) {
- let tr = tableArea.insertRow(day);
- tr.id = "tr";
- let td1 = tr.insertCell(0);
- td1.id = "td1";
- let td2 = tr.insertCell(1);
- td2.id = "td2";
- let td3 = tr.insertCell(2);
- td3.id = "td3";
- let fullDate = new Date(year, month, day);
- let weekDay = fullDate.getDay(); // example: weekDay=1=monday
- const checkboxForAvailability = document.createElement("table");
- checkboxForAvailability.className = "checkboxArea";
- const rowForAvailability = checkboxForAvailability.insertRow(0);
- const availabilityOptions = ["available", "not available","preferred"];
- availabilityOptions.forEach(function(option){
- let cellForAvailability = rowForAvailability.insertCell(availabilityOptions.indexOf(option)); // to find the index of availabilityOptions
- const choice = document.createElement("input");
- choice.type ="radio";
- choice.name = "availability"+day;
- choice.value = option;
- let labelForChoice = document.createElement("label");
- labelForChoice.className="container";
- labelForChoice.appendChild(choice);
- labelForChoice.appendChild(document.createTextNode(option));
- cellForAvailability.appendChild(labelForChoice);
- });
- const checkboxForShift = document.createElement("table");
- checkboxForShift.className = "checkboxArea";
- const rowForShift = checkboxForShift.insertRow(0);
- const shiftOptions = ["08:00-20:00", "20:00-08:00"];
- shiftOptions.forEach(function(option){
- let cellForShift = rowForShift.insertCell(shiftOptions.indexOf(option));
- const choice = document.createElement("input");
- choice.type ="checkbox";
- choice.name = option;
- choice.value = option;
- const labelForChoice = document.createElement("label");
- labelForChoice.className="container";
- labelForChoice.appendChild(choice);
- labelForChoice.appendChild(document.createTextNode(option));
- cellForShift.appendChild(labelForChoice);
- });
- if (calcDoubleShifts(day, month, year) == true) {
- td1.innerHTML = days[weekDay] + " " + day + " " + months[month] + " " + year;
- td2.appendChild(checkboxForAvailability);
- td3.appendChild(checkboxForShift);
- } else {
- td1.innerHTML = days[weekDay] + " " + day + " " + months[month] + " " + year;
- td2.appendChild(checkboxForAvailability);
- const singleShift = document.createTextNode(shiftOptions[1]);
- td3.appendChild(singleShift);
- }
- day++;
- }
- /*
- this function give us the days with 2 shifts
- */
- export function calcDoubleShifts(day, month, year) {
- let doubleShift = false;
- let fullDate = new Date(year, month, day);
- let weekDay = fullDate.getDay();
- let monthAndDay = (month + 1) + "-" + day;
- if (weekDay == 0 || weekDay == 6) {
- doubleShift = true;
- }
- if (monthAndDay == "1-1" || monthAndDay == "5-1" || monthAndDay == "10-3" ||
- monthAndDay == "12-25" || monthAndDay == "12-26" || monthAndDay == "11-1") {
- doubleShift = true;
- }
- let eastern = calcEastern(year);
- let easterMonth = 3;
- if (eastern > 31) {
- eastern = eastern - 31;
- easterMonth = 4;
- if (eastern <= 2) {
- if (monthAndDay == "3-" + (eastern + 29) || monthAndDay == "4-" + (eastern + 1)) {
- doubleShift = true;
- }
- } else {
- if (monthAndDay == "4-" + (eastern - 2) || monthAndDay == "4-" + (eastern + 1)) {
- doubleShift = true;
- }
- }
- } else {
- if (monthAndDay == "3-" + (eastern - 2) || monthAndDay == "3-" + (eastern + 1)) {
- doubleShift = true;
- }
- }
- /*
- let easterDate = new Date(year,easterMonth,eastern);
- easterDate.setDate(eastern+39);
- console.log(easterDate);
- */
- return doubleShift;
- }
- /*
- calcEastern give us the day of eastern for different years
- */
- export function calcEastern(year) {
- let k = year / 100;
- let m = 15 + (3 * k + 3) / 4 - (8 * k + 13) / 25;
- let s = 2 - (3 * k + 3) / 4;
- let a = year % 19;
- let d = (19 * a + m) % 30;
- let r = (d + a / 11) / 29;
- let og = 21 + d - r;
- let sz = 7 - (year + year / 4 + s) % 7;
- let oe = 7 - (og - sz) % 7;
- let dayOfEastern = og + oe;
- return Math.trunc(dayOfEastern);
- }
- export function sendTheChoice() {
- if (month == 0 || month == 2 || month == 4 || month == 6 || month == 7 || month == 9 || month == 11) {
- for (let day = 1; day <= 31; day++) {
- let selector = document.getElementById("selected2" + day);
- let choice = selector.options[selector.selectedIndex].value;
- if (choice == "---") {
- console.log("error");
- alert("check your answers");
- }
- }
- }
- if (month == 3 || month == 5 || month == 8 || month == 10) {
- for (let day = 1; day <= 30; day++) {
- let selector = document.getElementById("selected2" + day);
- let choice = selector.options[selector.selectedIndex].value;
- if (choice == "---") {
- console.log("error");
- alert("check your answers");
- }
- }
- }
- if (month == 1 && year % 4 != 0) {
- for (let day = 1; day <= 28; day++) {
- let selector = document.getElementById("selected2" + day);
- let choice = selector.options[selector.selectedIndex].value;
- if (choice == "---") {
- alert("check your answers");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement