diff options
Diffstat (limited to 'addons/timer/static/src/js/timer.js')
| -rw-r--r-- | addons/timer/static/src/js/timer.js | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/addons/timer/static/src/js/timer.js b/addons/timer/static/src/js/timer.js new file mode 100644 index 00000000000..0042ebfaead --- /dev/null +++ b/addons/timer/static/src/js/timer.js @@ -0,0 +1,63 @@ +odoo.define('timer.timer', function (require) { +"use strict"; + +var fieldRegistry = require('web.field_registry'); +var AbstractField = require('web.AbstractField'); +var Timer = require('timer.Timer'); + +var TimerFieldWidget = AbstractField.extend({ + + /** + * @override + * @private + */ + isSet: function () { + return true; + }, + /** + * @override + * @private + */ + _render: function () { + this._super.apply(this, arguments); + this._startTimeCounter(); + }, + /** + * @override + */ + destroy: function () { + this._super.apply(this, arguments); + clearInterval(this.timer); + }, + /** + * @private + */ + _startTimeCounter: async function () { + if (this.record.data.timer_start) { + const serverTime = this.record.data.timer_pause || await this._getServerTime(); + this.time = Timer.createTimer(0, this.record.data.timer_start, serverTime); + this.$el.text(this.time.toString()); + this.timer = setInterval(() => { + if (this.record.data.timer_pause) { + clearInterval(this.timer); + } else { + this.time.addSecond(); + this.$el.text(this.time.toString()); + } + }, 1000); + } else if (!this.record.data.timer_pause){ + clearInterval(this.timer); + } + }, + _getServerTime: function () { + return this._rpc({ + model: 'timer.timer', + method: 'get_server_time', + args: [] + }); + } +}); + +fieldRegistry.add('timer_timer', TimerFieldWidget); + +}); |
