summaryrefslogtreecommitdiff
path: root/addons/survey/static/src/js/survey_timer.js
diff options
context:
space:
mode:
authorDavid Beguin <dbe@odoo.com>2019-04-09 08:45:25 +0000
committerDavid Beguin <dbe@odoo.com>2019-11-19 12:52:22 +0000
commitf462bf0d9374a8360ed95f4c70f0e9fc6b3dcb68 (patch)
tree1404b6d56f15ee6ccf141a78e9e97cbfb75460e3 /addons/survey/static/src/js/survey_timer.js
parent3bb4ac8dc6ec1271c789bb36253cb880e596806a (diff)
[REF] survey : make survey timer a widget
This commit makes the survey timer js controller become a widget. This widget is called by the survey form widget. When the time is over, the widget triggers a time_up event instead of calling the submit. the time_up event is catched by the survey form widget that calls himself the submit, as the submit is on the form and not on the timer itself. Task ID : 1930132 PR #32419
Diffstat (limited to 'addons/survey/static/src/js/survey_timer.js')
-rw-r--r--addons/survey/static/src/js/survey_timer.js94
1 files changed, 61 insertions, 33 deletions
diff --git a/addons/survey/static/src/js/survey_timer.js b/addons/survey/static/src/js/survey_timer.js
index c9cc7b547fa..79f8b07c8dd 100644
--- a/addons/survey/static/src/js/survey_timer.js
+++ b/addons/survey/static/src/js/survey_timer.js
@@ -1,45 +1,73 @@
odoo.define('survey.timer', function (require) {
'use strict';
-require('web.dom_ready');
+var publicWidget = require('web.public.widget');
-if (!$('.js_survey_timer').length) {
- return Promise.reject("DOM doesn't contain '.js_survey_timer'");
-}
+var interval = null;
+var $timer = null;
+var countDownDate = null;
-var $parent = $('.js_survey_timer');
-var timeLimitMinutes = parseInt($parent.data('time_limit_minutes'));
+publicWidget.registry.SurveyTimerWidget = publicWidget.Widget.extend({
+ //--------------------------------------------------------------------------
+ // Widget
+ //--------------------------------------------------------------------------
-if (timeLimitMinutes <= 0) {
- return Promise.reject("Timer is not positive");
-}
+ /**
+ * @override
+ */
+ init: function (parent, params) {
+ this._super.apply(this, arguments);
+ this.$timer = params.timer;
+ this.$timeLimitMinutes = params.timeLimitMinutes;
+ },
-var countDownDate = moment.utc($parent.data('timer')).add(timeLimitMinutes, 'minutes');
-if (countDownDate.diff(moment.utc(), 'seconds') < 0) {
- return Promise.reject("Timer is already finished");
-}
+ /**
+ * Two responsabilities : Validate that time limit is not exceeded and Run timer otherwise.
+ *
+ * @override
+ */
+ start: function () {
+ var superDef = this._super.apply(this, arguments);
-var formatTime = function (time) {
- return time > 9 ? time : '0' + time;
-};
+ countDownDate = moment.utc(this.$timer).add(this.$timeLimitMinutes, 'minutes');
+ if (this.$timeLimitMinutes <= 0 || countDownDate.diff(moment.utc(), 'seconds') < 0) {
+ this.trigger_up('time_up');
+ } else {
+ $timer = this.$el.find('.timer');
+ this._updateTimer(this);
+ interval = setInterval(this._updateTimer.bind(this), 1000);
+ }
-var $timer = $parent.find('.timer');
-var interval = null;
-var updateTimer = function () {
- var timeLeft = countDownDate.diff(moment.utc(), 'seconds');
-
- if (timeLeft >= 0) {
- var timeLeftMinutes = parseInt(timeLeft / 60);
- var timeLeftSeconds = timeLeft - (timeLeftMinutes * 60);
- $timer.html(formatTime(timeLeftMinutes) + ':' + formatTime(timeLeftSeconds));
- } else {
- clearInterval(interval);
- $('.o_survey_form').submit();
- }
-};
-
-updateTimer();
-interval = setInterval(updateTimer, 1000);
+ return superDef;
+ },
+
+ // -------------------------------------------------------------------------
+ // Private
+ // -------------------------------------------------------------------------
+
+ _formatTime: function (time) {
+ return time > 9 ? time : '0' + time;
+ },
+
+ /**
+ * This function is responsible for the visual update of the timer DOM every second.
+ * When the time runs out, it triggers a 'time_up' event to notify the parent widget.
+ */
+ _updateTimer: function () {
+ var timeLeft = countDownDate.diff(moment.utc(), 'seconds');
+
+ if (timeLeft >= 0) {
+ var timeLeftMinutes = parseInt(timeLeft / 60);
+ var timeLeftSeconds = timeLeft - (timeLeftMinutes * 60);
+ $timer.html(this._formatTime(timeLeftMinutes) + ':' + this._formatTime(timeLeftSeconds));
+ } else {
+ clearInterval(interval);
+ this.trigger_up('time_up');
+ }
+ },
+});
+
+return publicWidget.registry.SurveyTimerWidget;
});