Skip to content

Commit 9f18b19

Browse files
authored
Merge pull request #117 from RDjarbeng/vitePWA
Vite pwa
2 parents 116daac + a98c64a commit 9f18b19

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+10963
-2772
lines changed

app.js

Lines changed: 36 additions & 269 deletions
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,36 @@
1-
class Clock {
2-
/**
3-
* Create a countdown clock with a Date object
4-
* @constructor
5-
* @param {Date} endDate
6-
*/
7-
constructor(endDate) {
8-
// expecting a date object
9-
this.setEndDate(endDate)
10-
this.countDown();
11-
}
12-
/**
13-
* change the clock's end date, call this.countdown() after
14-
* @param {Date} endDate
15-
*/
16-
setEndDate(endDate) {
17-
//set endDate to end of year
18-
// todo: check endDate for validity as date
19-
this.endDate = endDate ||new Date(`Jan 1, ${new Date().getFullYear() + 1} 00:00:00`)
20-
21-
22-
}
23-
/**
24-
* Returns the time in seconds between end date and current time
25-
* @returns {number} n
26-
*/
27-
getDistance(){
28-
return this.endDate.getTime() - new Date().getTime();
29-
}
30-
/**
31-
* Calls the function to populate/refresh the time values in the clock
32-
*/
33-
countDown=()=> {
34-
var distance = this.getDistance()
35-
// account for case of the countdown being reached, reset
36-
if (this.getDistance() >= 0) {
37-
// console.log('Running the count');
38-
// Time calculations for days, hours, minutes and seconds
39-
this.calculateTimeValues(this.getDistance())
40-
} else {
41-
// clear date values
42-
this.resetMethod();
43-
}
44-
}
45-
/**
46-
* Defines what should happen when the countdown is reached
47-
*/
48-
resetMethod(){
49-
this.clearCounter();
50-
}
51-
52-
calculateTimeValues(distance){
53-
this.days = Math.floor(distance / (1000 * 60 * 60 * 24));
54-
this.hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
55-
this.minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
56-
this.seconds = Math.floor((distance % (1000 * 60)) / 1000);
57-
}
58-
/**
59-
* Return the number of days, account for leap year
60-
* @returns {Number} days Number of days in the year
61-
*/
62-
countDays() {
63-
//account for leap year
64-
this.dayLength = ((this.endDate.getFullYear() % 4 != 0) ? 365 : 366)
65-
return this.dayLength - this.days
66-
}
67-
/**
68-
* Sets the clock values, day, hour, year, second to 0, !not a replacement for stop clock
69-
*/
70-
clearCounter(){
71-
this.days=this.hours=this.minutes=this.seconds=0;
72-
}
73-
}
74-
/**
75-
* Clock which resets to New year for the next year
76-
*/
77-
class NewYearClock extends Clock{
78-
resetMethod(){
79-
//reset to New Year's for default
80-
this.setEndDate()
81-
}
82-
}
83-
/**
84-
* Clock that resets every year for given date
85-
*/
86-
class Anniversary extends Clock{
87-
// @ override
88-
constructor(endDate){
89-
super(endDate);
90-
// account for case of the countdown being reached, reset
91-
if (this.getDistance < 0) {
92-
// Time calculations for days, hours, minutes and seconds
93-
this.resetMethod()
94-
}
95-
}
96-
97-
resetMethod(){
98-
// console.log('calling reset', this.endDate.getFullYear()<= new Date().getFullYear(), 'first cond', this.getDistance<0);
99-
while(this.endDate.getFullYear()<= new Date().getFullYear() && this.getDistance()<0){
100-
// this.endDate.
101-
console.log(this.endDate, 'End date triggering' );
102-
this.endDate.setFullYear(this.endDate.getFullYear()+1)
103-
// console.log('Anniversary done', this);
104-
}
105-
}
106-
107-
}
1+
import { Clock, NewYearClock } from "./js/clock.js";
2+
import { waitForAnimation } from "./js/appfunctions.js";
3+
import { errorHandler } from "./js/error.js";
4+
import { setInnerHtmlForNotNull } from "./js/functions.js";
5+
// import registerUpdateServiceWorker from "./js/serviceWorkerUpdate"
1086

1097
// DOM nodes
110-
let dayCount = document.getElementById("countDay");
1118
const animatedCountDuration = 800;
9+
const HOMEPAGE_DOM_IDS ={
10+
clockDayElement:'day-num',
11+
clockHourElement: 'hour-num',
12+
clockMinuteElement: 'min-num',
13+
clockSecondElement: 'sec-num',
14+
countdownTextDisplay: 'countdown-text',
15+
dueDate: 'dueDate',
16+
}
11217

113-
const body = document.body;
114-
var dayNumber = document.getElementById('day-num');
115-
var hourNumber = document.getElementById("hour-num");
116-
var minNumber = document.getElementById("min-num");
117-
var secNumber = document.getElementById("sec-num");
118-
var dueDate = document.getElementById('dueDate');
18+
var dayNumber = document.getElementById(HOMEPAGE_DOM_IDS.clockDayElement);
19+
var hourNumber = document.getElementById(HOMEPAGE_DOM_IDS.clockHourElement);
20+
var minNumber = document.getElementById(HOMEPAGE_DOM_IDS.clockMinuteElement);
21+
var secNumber = document.getElementById(HOMEPAGE_DOM_IDS.clockSecondElement);
22+
var dueDate = document.getElementById(HOMEPAGE_DOM_IDS.dueDate);
11923

120-
//to stop the clock
121-
let intervalID;
122-
let customClockMovement = false;
123-
let dayClock = new NewYearClock();
12424
// Initialize default Clock class
12525
// var myclock = new Anniversary(new Date('5-5-2022'));
126-
var myclock = setMainClock();
26+
var myclock = setMainClock();
12727
setInnerHtmlForNotNull(dueDate, `${myclock.endDate.getDate() + ' ' + myclock.endDate.toLocaleString('default', { month: 'long' }) + ', ' + myclock.endDate.getFullYear()}`)
128-
var customClock;
12928

13029
function setMainClock() {
13130
let myclock = new NewYearClock();
132-
let mainclock = localStorage.getItem('mainClock');
133-
if (mainclock !== null && mainclock != undefined) { //countdown set to main
31+
let mainclock = localStorage.getItem('mainClock');
32+
console.log(mainclock);
33+
if (mainclock && mainclock != 'undefined') { //countdown set to main
13434
mainclock = JSON.parse(mainclock)
13535
myclock = new Clock(new Date(mainclock.date));
13636
setMainText(mainclock.text)
@@ -140,164 +40,32 @@ function setMainClock() {
14040
}
14141

14242
function setMainText(countdownText) {
143-
const textDisplay = document.getElementById('countdown-text');
43+
const textDisplay = document.getElementById(HOMEPAGE_DOM_IDS.countdownTextDisplay);
14444
setInnerHtmlForNotNull(textDisplay, countdownText)
14545
}
146-
/**
147-
*
148-
* @param {Clock} clock
149-
* @param {{dayNumber: HTMLElement, hourNumber: HTMLElement, minNumber: HTMLElement, secNumber: HTMLElement}}domElements should contain elements for day, hour, minutes, second
150-
* @param {Number} [duration=800] specifies how long the animation lasts in milliseconds
151-
*/
152-
async function waitForAnimation(clock, domElements, duration) {
153-
await stepIncreaseAndStart(clock || myclock, domElements, duration || animatedCountDuration)
154-
startClock(clock || myclock, domElements);
155-
}
15646

157-
function startClock(clock, { dayNumber, hourNumber, minNumber, secNumber }) {
158-
intervalID = setInterval(() => { startTime(clock, { dayNumber, hourNumber, minNumber, secNumber }); }, 500);
159-
}
16047

161-
function startTime(clock, { dayNumber, hourNumber, minNumber, secNumber }) {
162-
// console.log(clock);
163-
updateDisplay(clock, dayNumber, hourNumber, minNumber, secNumber);
164-
setInnerHtmlForNotNull(dayCount, dayClock.countDays());
165-
if (customClockMovement) {
166-
updateDisplay(customClock, customDayNumber, customHourNumber, customMinNumber, customSecNumber);
167-
}
168-
}
169-
/**
170-
* add zero in front of numbers < 10
171-
* @param {Number} num
172-
* @returns num number with 0 at the front
173-
*/
174-
function addZeros(num) {
175-
if (num < 10) {
176-
num = "0" + num;
177-
}
178-
return num;
179-
}
180-
/**
181-
* Updates the html dom nodes with the clock values, days, hours, minutes, seconds
182-
* @param {Clock} counter
183-
* @param {HTMLElement} dayDisplay
184-
* @param {HTMLElement} hourDisplay
185-
* @param {HTMLElement} minDisplay
186-
* @param {HTMLElement} secDisplay
187-
*/
188-
function updateDisplay(counter, dayDisplay, hourDisplay, minDisplay, secDisplay) {
189-
counter.countDown();
190-
let d = counter.days
191-
let h = counter.hours
192-
let m = counter.minutes
193-
let s = counter.seconds
194-
d = addZeros(d);
195-
h = addZeros(h);
196-
m = addZeros(m);
197-
s = addZeros(s);
198-
setInnerHtmlForNotNull(dayDisplay, `${d}`);
199-
setInnerHtmlForNotNull(hourDisplay, `${h}`);
200-
setInnerHtmlForNotNull(minDisplay, `${m}`);
201-
setInnerHtmlForNotNull(secDisplay, `${s}`);
202-
}
203-
204-
/**
205-
* Listens for a user input for date element
206-
*/
207-
// function listenForDate() {
208-
// const input = this.value;
209-
// // console.log(input, 'run');
210-
// if (input != '') {
211-
// customClock = new Clock(new Date(input));
212-
// displayClockRow();
213-
// // do the fast countdown
214-
// // set speed faster when day of the year is greater
215-
// // todo: change to animateValue
216-
// stepIncreaseAndStart(customClock, { customDayNumber, customHourNumber, customMinNumber, customSecNumber }, (365 - customClock.days < 100) ? 365 - customClock.days : 70);
217-
// }
218-
// }
219-
220-
function displayClockRow() {
221-
let customRow = document.getElementById("customDisplay");
222-
// show row
223-
customRow.style.display = 'block';
224-
}
225-
/* //restart the clock
226-
function restartTime() {
227-
if (customClockMovement) {
228-
return;
229-
} else {
230-
startClock();
231-
}
232-
}
233-
*/
234-
/**
235-
* Stop the clock with global var intervalID
236-
*/
237-
function stopClock() {
238-
clearTimeout(intervalID);
239-
customClockMovement = false;
240-
}
241-
242-
//for the animated Countdown
243-
function animateValue(domElement, start, end, duration) {
244-
let startTimestamp = null;
245-
const step = (timestamp) => {
246-
if (!startTimestamp) startTimestamp = timestamp;
247-
const progress = Math.min((timestamp - startTimestamp) / duration, 1);
248-
setInnerHtmlForNotNull(domElement, addZeros(Math.floor(progress * (end - start) + start)))
249-
if (progress < 1) {
250-
window.requestAnimationFrame(step);
251-
// animationComplete = false;
252-
}
253-
};
254-
window.requestAnimationFrame(step);
255-
}
256-
257-
async function stepIncreaseAndStart(clockElement, domElements, speed = 50, start_num = 0) {
258-
animateValue(domElements.dayNumber, start_num, clockElement.days, speed);
259-
animateValue(domElements.hourNumber, start_num, clockElement.hours, speed);
260-
animateValue(domElements.minNumber, start_num, clockElement.minutes, speed);
261-
animateValue(domElements.secNumber, start_num, clockElement.seconds, speed);
26248

263-
}
264-
265-
function addWhatappEventHandler() {
266-
let whatsappIcon = document.getElementById('sendWhatsappButton');
267-
if (whatsappIcon) {
268-
whatsappIcon.addEventListener('click', exportToWhatsapp);
269-
}
270-
271-
}
272-
273-
function exportToWhatsapp() {
274-
let dayNum = dayCount.innerText;
275-
window.open(`whatsapp://send?text= Day ${dayNum || 'rcountdown'}/365`);
276-
}
277-
/**
278-
* Checks if a DOM element variable is null before setting innerHTML
279-
* @param {HTMLElement} element
280-
* @param {String} value
281-
*/
282-
function setInnerHtmlForNotNull(element, value){
283-
if(element)//check for null
284-
element.innerHTML = value;
285-
}
28649
try {
28750
//show day value before animation runs
288-
setInnerHtmlForNotNull(dayCount, dayClock.countDays());
51+
waitForAnimation(myclock, { dayNumber, hourNumber, minNumber, secNumber }, animatedCountDuration);
28952

290-
// startTime();
291-
waitForAnimation(myclock, { dayNumber, hourNumber, minNumber, secNumber }, animatedCountDuration);
292-
addWhatappEventHandler();
293-
// as;
53+
// addWhatappEventHandler();
54+
// as;
29455
} catch (error) {
29556
errorHandler("Error in clock");
29657
console.log(error);
58+
console.log("Error on main page clock inside wait for animation")
29759
}
29860

299-
// service worker
300-
61+
/*
62+
if ("serviceWorker" in navigator) {
63+
// && !/localhost/.test(window.location)) {
64+
registerSW();
65+
}
66+
*/
67+
// old service worker
68+
/*
30169
if('serviceWorker' in navigator){
30270
window.addEventListener('load', () => {
30371
navigator.serviceWorker.register('/sw.js')
@@ -307,5 +75,4 @@ if('serviceWorker' in navigator){
30775
.catch((err)=> console.log('Service worker not registered', err));
30876
});
30977
310-
}
311-
78+
}*/

0 commit comments

Comments
 (0)