From eb860a5c2383f8429ded0f26493ed6bb625077b0 Mon Sep 17 00:00:00 2001 From: Pierre Date: Wed, 3 Jan 2018 13:38:06 +0100 Subject: [PATCH] Added countdown timer --- assets/css/components/posts/ui.css | 5 +++ assets/js/common/functionsSchema.js | 8 +++++ assets/js/components/countdown.js | 49 +++++++++++++++++++++++++++++ assets/js/components/posts/ui.js | 8 +---- system/config/dev.config.php | 3 ++ 5 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 assets/js/components/countdown.js diff --git a/assets/css/components/posts/ui.css b/assets/css/components/posts/ui.css index 26a86963..d8fb7642 100644 --- a/assets/css/components/posts/ui.css +++ b/assets/css/components/posts/ui.css @@ -28,4 +28,9 @@ .post .post-youtube { width: 100%; height: 300px; +} + +.post .post-countdown { + text-align: center; + padding: 10px; } \ No newline at end of file diff --git a/assets/js/common/functionsSchema.js b/assets/js/common/functionsSchema.js index 1b922faa..a551062d 100644 --- a/assets/js/common/functionsSchema.js +++ b/assets/js/common/functionsSchema.js @@ -666,6 +666,14 @@ var ComunicWeb = { textarea: { }, + + /** + * Countdown timer + */ + countdown: { + //TODO : implement + }, + }, /** diff --git a/assets/js/components/countdown.js b/assets/js/components/countdown.js new file mode 100644 index 00000000..5e0bc6b2 --- /dev/null +++ b/assets/js/components/countdown.js @@ -0,0 +1,49 @@ +/** + * Countdown timer component + * + * @author Pierre HUBERT + */ + +ComunicWeb.components.countdown = { + + /** + * Initializate countdown timer + * + * @param {int} time_end The end time for the countdown timer + * @param {HTMLElement} target The target for the countdown timer + */ + init: function(time_end, target){ + + //Initialise variable + var days, hours, minutes, seconds; + + //Setup interval + var interval = setInterval(function(){ + + //Check if target still exists or not + if(!target.isConnected) + clearInterval(interval); + + // find the amount of "seconds" between now and target + var current_date = parseInt(new Date().getTime() / 1000); + var seconds_left = time_end - current_date; + + // do some time calculations + days = parseInt(seconds_left / 86400); + seconds_left = seconds_left % 86400; + + hours = parseInt(seconds_left / 3600); + seconds_left = seconds_left % 3600; + + minutes = parseInt(seconds_left / 60); + seconds = parseInt(seconds_left % 60); + + // format countdown string + set tag value + target.innerHTML = '' + days + ' Days ' + hours + ' Hours ' + + minutes + ' Minutes ' + seconds + ' Seconds'; + + }, 1000); + + }, + +} \ No newline at end of file diff --git a/assets/js/components/posts/ui.js b/assets/js/components/posts/ui.js index 55fd0d72..c7c3f013 100644 --- a/assets/js/components/posts/ui.js +++ b/assets/js/components/posts/ui.js @@ -236,14 +236,8 @@ ComunicWeb.components.posts.ui = { class: "post-countdown" }); - //Set the date of the countdown time - var date = new Date(); - date.setFullYear(infos.year_end); - date.setMonth(infos.month_end - 1); //Months starts from 0 (january) to 11 (december) - date.setDate(infos.day_end); - //Initialize countdown timer - ComunicWeb.components.countdown.init(date, target); + ComunicWeb.components.countdown.init(infos.time_end, target); } //If the kind of post was not implemented diff --git a/system/config/dev.config.php b/system/config/dev.config.php index 78eca0b6..f799fcaf 100644 --- a/system/config/dev.config.php +++ b/system/config/dev.config.php @@ -201,6 +201,9 @@ class Dev { //Modern textarea handler "js/components/textarea.js", + //Countdown timer + "js/components/countdown.js", + //User scripts "js/user/loginTokens.js", "js/user/userLogin.js",