From 35a7d4170477b2f475d6257fd9fb09836fa397d7 Mon Sep 17 00:00:00 2001 From: Hadi Nategh Date: Mon, 30 Nov 2020 14:35:28 +0100 Subject: [PATCH] Add new option to countdown widget inorder to hide empty values --- api/js/etemplate/et2_widget_countdown.js | 33 ++++++++++++++++++--- api/js/etemplate/et2_widget_countdown.ts | 37 +++++++++++++++++++++--- 2 files changed, 62 insertions(+), 8 deletions(-) diff --git a/api/js/etemplate/et2_widget_countdown.js b/api/js/etemplate/et2_widget_countdown.js index b7e15ae25a..5a5a6fb1e4 100644 --- a/api/js/etemplate/et2_widget_countdown.js +++ b/api/js/etemplate/et2_widget_countdown.js @@ -84,10 +84,29 @@ var et2_countdown = /** @class */ (function (_super) { var distance = time.getTime() - now.getTime(); if (distance < 0) return 0; - this.days.text(Math.floor(distance / (1000 * 60 * 60 * 24)) + this._getIndicator("days")); - this.hours.text(Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)) + this._getIndicator("hours")); - this.minutes.text(Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)) + this._getIndicator("minutes")); - this.seconds.text(Math.floor((distance % (1000 * 60)) / 1000) + this._getIndicator("seconds")); + var values = { + days: Math.floor(distance / (1000 * 60 * 60 * 24)), + hours: Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)), + minutes: Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)), + secounds: Math.floor((distance % (1000 * 60)) / 1000) + }; + this.days.text(values.days + this._getIndicator("days")); + this.hours.text(values.hours + this._getIndicator("hours")); + this.minutes.text(values.minutes + this._getIndicator("minutes")); + this.seconds.text(values.secounds + this._getIndicator("seconds")); + if (this.options.hideEmpties) { + if (values.days == 0) { + this.days.hide(); + if (values.hours == 0) { + this.hours.hide(); + if (values.minutes == 0) { + this.minutes.hide(); + if (values.secounds == 0) + this.seconds.hide(); + } + } + } + } return distance; }; et2_countdown.prototype._getIndicator = function (_v) { @@ -111,6 +130,12 @@ var et2_countdown = /** @class */ (function (_super) { type: "js", default: et2_no_init, description: "Callback function to call when the countdown is finished." + }, + hideEmpties: { + name: "hide empties", + type: "string", + default: true, + description: "Only displays none empty values." } }; return et2_countdown; diff --git a/api/js/etemplate/et2_widget_countdown.ts b/api/js/etemplate/et2_widget_countdown.ts index d98779c70d..af46cf7ab2 100644 --- a/api/js/etemplate/et2_widget_countdown.ts +++ b/api/js/etemplate/et2_widget_countdown.ts @@ -41,6 +41,12 @@ export class et2_countdown extends et2_baseWidget { type: "js", default: et2_no_init, description: "Callback function to call when the countdown is finished." + }, + hideEmpties: { + name: "hide empties", + type: "string", + default: true, + description: "Only displays none empty values." } }; @@ -108,11 +114,34 @@ export class et2_countdown extends et2_baseWidget { if (distance < 0) return 0; - this.days.text(Math.floor(distance / (1000 * 60 * 60 * 24))+this._getIndicator("days")); - this.hours.text(Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))+this._getIndicator("hours")); - this.minutes.text(Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60))+this._getIndicator("minutes")); - this.seconds.text(Math.floor((distance % (1000 * 60)) / 1000)+this._getIndicator("seconds")); + let values = { + days: Math.floor(distance / (1000 * 60 * 60 * 24)), + hours: Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)), + minutes: Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)), + secounds: Math.floor((distance % (1000 * 60)) / 1000) + }; + this.days.text(values.days+this._getIndicator("days")); + this.hours.text(values.hours+this._getIndicator("hours")) + this.minutes.text(values.minutes+this._getIndicator("minutes")); + this.seconds.text(values.secounds+this._getIndicator("seconds")); + + if (this.options.hideEmpties) + { + if (values.days == 0) + { + this.days.hide(); + if(values.hours == 0) + { + this.hours.hide(); + if(values.minutes == 0) + { + this.minutes.hide(); + if(values.secounds == 0) this.seconds.hide(); + } + } + } + } return distance; }