From 7b942d621f98101e47fbf9ded1343e363a8398ba Mon Sep 17 00:00:00 2001 From: Rico Date: Tue, 11 Feb 2020 00:42:22 +0100 Subject: [PATCH] fix: remove unnecessary check for midnight The check if moment() > this.midnight is not necessary. getDom should always get the dom, no matter what time it is. This is also one reason why the issue #2 exists. Also this commits changes some strange usages of moment() and removes unused code. --- calendar_monthly.js | 265 +++++++++++++++++++++----------------------- 1 file changed, 126 insertions(+), 139 deletions(-) diff --git a/calendar_monthly.js b/calendar_monthly.js index 0b52556..84ed781 100644 --- a/calendar_monthly.js +++ b/calendar_monthly.js @@ -5,7 +5,7 @@ * Beer Licensed (meaning, if you like this module, feel free to have a beer on me, or send me one.) */ - Module.register("calendar_monthly", { +Module.register("calendar_monthly", { // Module defaults defaults: { @@ -41,159 +41,146 @@ Log.log("Starting module: " + this.name); // Set locale moment.locale(config.language); - - // Calculate next midnight and add updateDelay - var now = moment(); - this.midnight = moment([now.year(), now.month(), now.date() + 1]).add(this.config.updateDelay, "seconds"); - - this.loaded = false; this.scheduleUpdate(this.config.initialLoadDelay * 1000); }, // Override dom generator getDom: function() { + var year = moment().year(); + var monthName = moment().format("MMMM"); + var monthLength = moment().daysInMonth(); + + // Find first day of the month, LOCALE aware + var startingDay = moment().date(1).weekday(); + + var wrapper = document.createElement("table"); + wrapper.className = 'xsmall'; + wrapper.id = 'calendar-table'; + + // Create THEAD section with month name and 4-digit year + var header = document.createElement("tHead"); + var headerTR = document.createElement("tr"); + + // We only fill in the THEAD section if the .showHeader config is set to true + if (this.config.showHeader) { + var headerTH = document.createElement("th"); + headerTH.colSpan = "7"; + headerTH.scope = "col"; + headerTH.id = "calendar-th"; + var headerMonthSpan = document.createElement("span"); + headerMonthSpan.id = "monthName"; + headerMonthSpan.innerHTML = monthName; + var headerYearSpan = document.createElement("span"); + headerYearSpan.id = "yearDigits"; + headerYearSpan.innerHTML = year; + // Add space between the two elements + // This can be used later with the :before or :after options in the CSS + var headerSpace = document.createTextNode(" "); + + headerTH.appendChild(headerMonthSpan); + headerTH.appendChild(headerSpace); + headerTH.appendChild(headerYearSpan); + headerTR.appendChild(headerTH); + } + header.appendChild(headerTR); + wrapper.appendChild(header); - if ((moment() > this.midnight) || (!this.loaded)) { - - var month = moment().month(); - var year = moment().year(); - var monthName = moment().format("MMMM"); - var monthLength = moment().daysInMonth(); - - // Find first day of the month, LOCALE aware - var startingDay = moment().date(1).weekday(); - - var wrapper = document.createElement("table"); - wrapper.className = 'xsmall'; - wrapper.id = 'calendar-table'; - - // Create THEAD section with month name and 4-digit year - var header = document.createElement("tHead"); - var headerTR = document.createElement("tr"); - - // We only fill in the THEAD section if the .showHeader config is set to true - if (this.config.showHeader) { - var headerTH = document.createElement("th"); - headerTH.colSpan = "7"; - headerTH.scope = "col"; - headerTH.id = "calendar-th"; - var headerMonthSpan = document.createElement("span"); - headerMonthSpan.id = "monthName"; - headerMonthSpan.innerHTML = monthName; - var headerYearSpan = document.createElement("span"); - headerYearSpan.id = "yearDigits"; - headerYearSpan.innerHTML = year; - // Add space between the two elements - // This can be used later with the :before or :after options in the CSS - var headerSpace = document.createTextNode(" "); - - headerTH.appendChild(headerMonthSpan); - headerTH.appendChild(headerSpace); - headerTH.appendChild(headerYearSpan); - headerTR.appendChild(headerTH); - } - header.appendChild(headerTR); - wrapper.appendChild(header); - - // Create TFOOT section -- currently used for debugging only - var footer = document.createElement('tFoot'); - var footerTR = document.createElement("tr"); - footerTR.id = "calendar-tf"; - - var footerTD = document.createElement("td"); - footerTD.colSpan ="7"; - footerTD.className = "footer"; - if (this.config.debugging) { - footerTD.innerHTML = "Calendar currently in DEBUG mode!
Please see console log."; - } else { - footerTD.innerHTML = " "; - } + // Create TFOOT section -- currently used for debugging only + var footer = document.createElement('tFoot'); + var footerTR = document.createElement("tr"); + footerTR.id = "calendar-tf"; - footerTR.appendChild(footerTD); - footer.appendChild(footerTR); - wrapper.appendChild(footer); + var footerTD = document.createElement("td"); + footerTD.colSpan ="7"; + footerTD.className = "footer"; + if (this.config.debugging) { + footerTD.innerHTML = "Calendar currently in DEBUG mode!
Please see console log."; + } else { + footerTD.innerHTML = " "; + } + + footerTR.appendChild(footerTD); + footer.appendChild(footerTR); + wrapper.appendChild(footer); - // Create TBODY section with day names - var bodyContent = document.createElement("tBody"); - var bodyTR = document.createElement("tr"); - bodyTR.id = "calendar-header"; + // Create TBODY section with day names + var bodyContent = document.createElement("tBody"); + var bodyTR = document.createElement("tr"); + bodyTR.id = "calendar-header"; - for (var i = 0; i <= 6; i++ ){ + for (var i = 0; i <= 6; i++ ){ + var bodyTD = document.createElement("td"); + bodyTD.className = "calendar-header-day"; + bodyTD.innerHTML = moment().weekday(i).format("ddd"); + bodyTR.appendChild(bodyTD); + } + bodyContent.appendChild(bodyTR); + wrapper.appendChild(bodyContent); + + // Create TBODY section with the monthly calendar + var bodyContent = document.createElement("tBody"); + var bodyTR = document.createElement("tr"); + bodyTR.className = "weekRow"; + + // Fill in the days + var day = 1; + var nextMonth = 1; + // Loop for amount of weeks (as rows) + for (var i = 0; i < 9; i++) { + // Loop for each weekday (as individual cells) + for (var j = 0; j <= 6; j++) { var bodyTD = document.createElement("td"); - bodyTD.className = "calendar-header-day"; - bodyTD.innerHTML = moment().weekday(i).format("ddd"); - bodyTR.appendChild(bodyTD); - } - bodyContent.appendChild(bodyTR); - wrapper.appendChild(bodyContent); - - // Create TBODY section with the monthly calendar - var bodyContent = document.createElement("tBody"); - var bodyTR = document.createElement("tr"); - bodyTR.className = "weekRow"; - - // Fill in the days - var day = 1; - var nextMonth = 1; - // Loop for amount of weeks (as rows) - for (var i = 0; i < 9; i++) { - // Loop for each weekday (as individual cells) - for (var j = 0; j <= 6; j++) { - var bodyTD = document.createElement("td"); - bodyTD.className = "calendar-day"; - var squareDiv = document.createElement("div"); - squareDiv.className = "square-box"; - var squareContent = document.createElement("div"); - squareContent.className = "square-content"; - var squareContentInner = document.createElement("div"); - var innerSpan = document.createElement("span"); - - if (j < startingDay && i == 0) { - // First row, fill in empty slots - innerSpan.className = "monthPrev"; - innerSpan.innerHTML = moment().subtract(1, 'months').endOf('month').subtract((startingDay - 1) - j, 'days').date(); - } else if (day <= monthLength && (i > 0 || j >= startingDay)) { - if (day == moment().date()) { - innerSpan.id = "day" + day; - innerSpan.className = "today"; - } else { - innerSpan.id = "day" + day; - innerSpan.className = "daily"; - } - innerSpan.innerHTML = day; - day++; - } else if (day > monthLength && i > 0) { - // Last row, fill in empty space - innerSpan.className = "monthNext"; - innerSpan.innerHTML = moment([year, month, monthLength]).add(nextMonth, 'days').date(); - nextMonth++; + bodyTD.className = "calendar-day"; + var squareDiv = document.createElement("div"); + squareDiv.className = "square-box"; + var squareContent = document.createElement("div"); + squareContent.className = "square-content"; + var squareContentInner = document.createElement("div"); + var innerSpan = document.createElement("span"); + + if (j < startingDay && i == 0) { + // First row, fill in empty slots + innerSpan.className = "monthPrev"; + innerSpan.innerHTML = moment().subtract(1, 'months').endOf('month').subtract((startingDay - 1) - j, 'days').date(); + } else if (day <= monthLength && (i > 0 || j >= startingDay)) { + if (day == moment().date()) { + innerSpan.id = "day" + day; + innerSpan.className = "today"; + } else { + innerSpan.id = "day" + day; + innerSpan.className = "daily"; } - squareContentInner.appendChild(innerSpan); - squareContent.appendChild(squareContentInner); - squareDiv.appendChild(squareContent); - bodyTD.appendChild(squareDiv); - bodyTR.appendChild(bodyTD); - } - // Don't need any more rows if we've run out of days - if (day > monthLength) { - break; - } else { - bodyTR.appendChild(bodyTD); - bodyContent.appendChild(bodyTR); - var bodyTR = document.createElement("tr"); - bodyTR.className = "weekRow"; + innerSpan.innerHTML = day; + day++; + } else if (day > monthLength && i > 0) { + // Last row, fill in empty space + innerSpan.className = "monthNext"; + innerSpan.innerHTML = moment().endOf('month').add(nextMonth, 'days').date(); + nextMonth++; } - } - - bodyContent.appendChild(bodyTR); - wrapper.appendChild(bodyContent); - - this.loaded = true; - return wrapper; + squareContentInner.appendChild(innerSpan); + squareContent.appendChild(squareContentInner); + squareDiv.appendChild(squareContent); + bodyTD.appendChild(squareDiv); + bodyTR.appendChild(bodyTD); + } + // Don't need any more rows if we've run out of days + if (day > monthLength) { + break; + } else { + bodyTR.appendChild(bodyTD); + bodyContent.appendChild(bodyTR); + var bodyTR = document.createElement("tr"); + bodyTR.className = "weekRow"; + } + } - } + bodyContent.appendChild(bodyTR); + wrapper.appendChild(bodyContent); + this.loaded = true; + return wrapper; }, scheduleUpdate: function(initialDelay = 0) {