2020-11-23 21:11:09 +01:00
|
|
|
/**
|
|
|
|
* EGroupware eTemplate2 - Countdown timer widget
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
2021-06-07 17:33:53 +02:00
|
|
|
* @link https://www.egroupware.org
|
2020-11-23 21:11:09 +01:00
|
|
|
* @author Hadi Nategh
|
|
|
|
*/
|
|
|
|
/*egw:uses
|
|
|
|
/vendor/bower-asset/jquery/dist/jquery.js;
|
|
|
|
et2_core_baseWidget;
|
|
|
|
*/
|
2021-06-07 17:33:53 +02:00
|
|
|
import { et2_no_init } from "./et2_core_common";
|
|
|
|
import { ClassWithAttributes } from "./et2_core_inheritance";
|
|
|
|
import { et2_register_widget } from "./et2_core_widget";
|
|
|
|
import { et2_valueWidget } from "./et2_core_valueWidget";
|
|
|
|
import { egw } from "../jsapi/egw_global";
|
2020-11-23 21:11:09 +01:00
|
|
|
/**
|
|
|
|
* Class which implements the "countdown" XET-Tag
|
2021-01-28 11:07:52 +01:00
|
|
|
*
|
2021-01-28 18:52:40 +01:00
|
|
|
* Value for countdown is an integer duration in seconds or a server-side to a duration converted expiry datetime.
|
2021-01-28 15:02:38 +01:00
|
|
|
*
|
2021-01-28 18:52:40 +01:00
|
|
|
* The duration has the benefit, that it does not depend on the correct set time and timezone of the browser / computer of the user.
|
2020-11-23 21:11:09 +01:00
|
|
|
*/
|
2021-06-07 17:33:53 +02:00
|
|
|
export class et2_countdown extends et2_valueWidget {
|
2020-11-23 21:11:09 +01:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
2021-06-07 17:33:53 +02:00
|
|
|
constructor(_parent, _attrs, _child) {
|
2020-11-23 21:11:09 +01:00
|
|
|
// Call the inherited constructor
|
2021-06-07 17:33:53 +02:00
|
|
|
super(_parent, _attrs, ClassWithAttributes.extendAttributes(et2_countdown._attributes, _child || {}));
|
|
|
|
this.timer = null;
|
|
|
|
this.container = null;
|
|
|
|
this.days = null;
|
|
|
|
this.hours = null;
|
|
|
|
this.minutes = null;
|
|
|
|
this.seconds = null;
|
2020-11-23 21:11:09 +01:00
|
|
|
// Build countdown dom container
|
2021-06-07 17:33:53 +02:00
|
|
|
this.container = jQuery(document.createElement("div"))
|
2020-11-23 21:11:09 +01:00
|
|
|
.addClass("et2_countdown");
|
2021-06-07 17:33:53 +02:00
|
|
|
this.days = jQuery(document.createElement("span"))
|
|
|
|
.addClass("et2_countdown_days").appendTo(this.container);
|
|
|
|
this.hours = jQuery(document.createElement("span"))
|
|
|
|
.addClass("et2_countdown_hours").appendTo(this.container);
|
|
|
|
this.minutes = jQuery(document.createElement("span"))
|
|
|
|
.addClass("et2_countdown_minutes").appendTo(this.container);
|
|
|
|
this.seconds = jQuery(document.createElement("span"))
|
|
|
|
.addClass("et2_countdown_seconds").appendTo(this.container);
|
|
|
|
this.setDOMNode(this.container[0]);
|
2020-11-23 21:11:09 +01:00
|
|
|
}
|
2021-06-07 17:33:53 +02:00
|
|
|
set_value(_time) {
|
2021-01-28 18:52:40 +01:00
|
|
|
if (isNaN(_time))
|
2020-11-23 21:11:09 +01:00
|
|
|
return;
|
2021-06-07 17:33:53 +02:00
|
|
|
super.set_value(_time);
|
2021-01-28 18:52:40 +01:00
|
|
|
this.time = new Date();
|
|
|
|
this.time.setSeconds(this.time.getSeconds() + parseInt(_time));
|
2021-06-07 17:33:53 +02:00
|
|
|
let self = this;
|
2020-11-23 21:11:09 +01:00
|
|
|
this.timer = setInterval(function () {
|
2020-11-24 13:46:05 +01:00
|
|
|
if (self._updateTimer() <= 0) {
|
|
|
|
clearInterval(self.timer);
|
|
|
|
if (typeof self.onFinish == "function")
|
|
|
|
self.onFinish();
|
|
|
|
}
|
2020-11-23 21:11:09 +01:00
|
|
|
}, 1000);
|
2021-06-07 17:33:53 +02:00
|
|
|
}
|
|
|
|
_updateTimer() {
|
|
|
|
let now = new Date();
|
|
|
|
let distance = this.time.getTime() - now.getTime();
|
2020-11-23 21:11:09 +01:00
|
|
|
if (distance < 0)
|
|
|
|
return 0;
|
2020-12-02 16:54:50 +01:00
|
|
|
if (this.options.alarm > 0 && this.options.alarm == distance / 1000 && typeof this.onAlarm == 'function') {
|
|
|
|
this.onAlarm();
|
|
|
|
}
|
2021-06-07 17:33:53 +02:00
|
|
|
let values = {
|
2020-11-30 14:35:28 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-29 19:34:28 +01:00
|
|
|
if (this.options.precision) {
|
2021-06-07 17:33:53 +02:00
|
|
|
const units = ['days', 'hours', 'minutes', 'seconds'];
|
|
|
|
for (let u = 0; u < 4; ++u) {
|
2021-01-29 19:34:28 +01:00
|
|
|
if (values[units[u]]) {
|
2021-06-07 17:33:53 +02:00
|
|
|
for (let n = u + this.options.precision; n < 4; n++) {
|
2021-01-29 19:34:28 +01:00
|
|
|
this[units[n]].hide();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this[units[u]].hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-23 21:11:09 +01:00
|
|
|
return distance;
|
2021-06-07 17:33:53 +02:00
|
|
|
}
|
|
|
|
_getIndicator(_v) {
|
2020-11-23 21:11:09 +01:00
|
|
|
return this.options.format == 's' ? egw.lang(_v).substr(0, 1) : egw.lang(_v);
|
2021-06-07 17:33:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
et2_countdown._attributes = {
|
|
|
|
format: {
|
|
|
|
name: "display format",
|
|
|
|
type: "string",
|
|
|
|
default: "s",
|
|
|
|
description: "Defines display format; s (Initial letter) or l (Complete word) display, default is s."
|
|
|
|
},
|
|
|
|
onFinish: {
|
|
|
|
name: "on finish countdown",
|
|
|
|
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."
|
|
|
|
},
|
|
|
|
precision: {
|
|
|
|
name: "how many counters to show",
|
|
|
|
type: "integer",
|
|
|
|
default: 0,
|
|
|
|
description: "Limit number of counters, eg. 2 does not show minutes and seconds, if days are displayed"
|
|
|
|
},
|
|
|
|
alarm: {
|
|
|
|
name: "alarm",
|
|
|
|
type: "any",
|
|
|
|
default: "",
|
|
|
|
description: "Defines an alarm set before the countdown is finished, it should be in seconds"
|
|
|
|
},
|
|
|
|
onAlarm: {
|
|
|
|
name: "alarm callback",
|
|
|
|
type: "js",
|
|
|
|
default: "",
|
|
|
|
description: "Defines a callback to gets called at alarm - timer. This only will work if there's an alarm set."
|
|
|
|
}
|
|
|
|
};
|
|
|
|
et2_register_widget(et2_countdown, ["countdown"]);
|
2020-11-23 21:11:09 +01:00
|
|
|
//# sourceMappingURL=et2_widget_countdown.js.map
|