diff options
| author | Aaron Bohy <aab@odoo.com> | 2017-12-21 12:29:50 +0100 |
|---|---|---|
| committer | Alexandre Kühn <alex.e.kuhn@gmail.com> | 2018-02-01 14:51:58 +0100 |
| commit | 02ec09cb1c3e2d7bc7968f40c18f2208d7f3f498 (patch) | |
| tree | 10d91136786b13666e78cfb94b12f1609d6f883d /addons/bus/static/src | |
| parent | 0cd656b5af38051df3f7d6319e99705150bb1cd8 (diff) | |
[REF][IMP] *: mail js refactoring
This commit improves the JS code of the mail module,
which comes maily from the new coding guidelines
and the addition of "JS services".
JS services are important objects that do not fit
well in the component tree, such as chat_manager
or ajax.
The benefits of JS services are improved readability
of the code, reduced coupling, and more testable
modules. In particular, discuss was hard to
Summary of the changes:
- ClientAction has been renamed into Discuss
- Clear instantiation of chatManager
- New coding guidelines in most mail modules
- JS Services can interact with each other
- Chat Manager and Window Manager are services
- Window Manager renamed to Chat Window Manager
- bus.bus is now encapsulated in Bus Service (a service)
- The test infrastructure has been tweaked with JS Services
- Chat Mixin has been removed
A future improvement would be to translate some 'trigger' into 'trigger_up'.
Diffstat (limited to 'addons/bus/static/src')
| -rw-r--r-- | addons/bus/static/src/js/services/bus_service.js | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/addons/bus/static/src/js/services/bus_service.js b/addons/bus/static/src/js/services/bus_service.js new file mode 100644 index 00000000000..78be7a9740a --- /dev/null +++ b/addons/bus/static/src/js/services/bus_service.js @@ -0,0 +1,96 @@ +odoo.define('bus.BusService', function (require) { +"use strict"; + +var bus = require('bus.bus').bus; + +var AbstractService = require('web.AbstractService'); + +var BusService = AbstractService.extend({ + name: 'bus_service', + /** + * @override + */ + init: function () { + this._super.apply(this, arguments); + this.bus = bus; + this._audio = null; + }, + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + /** + * Get the bus + * + * @return {web.Bus} + */ + getBus: function () { + return this.bus; + }, + + /** + * Send a notification, and notify once per browser's tab + * + * @param {web.Widget} widget the widget that called sendNotification + * @param {string} title + * @param {string} content + */ + sendNotification: function (widget, title, content) { + if (window.Notification && Notification.permission === "granted") { + if (this.bus.is_master) { + this._sendNativeNotification(title, content); + } + } else { + widget.do_notify(title, content); + if (this.bus.is_master) { + this._beep(widget); + } + } + }, + + //-------------------------------------------------------------------------- + // Private + //-------------------------------------------------------------------------- + + /** + * Lazily play the 'beep' audio on sent notification + * + * @private + * @param {web.Widget} widget + */ + _beep: function (widget) { + if (typeof(Audio) !== "undefined") { + if (!this._audio) { + this._audio = new Audio(); + var ext = this._audio.canPlayType("audio/ogg; codecs=vorbis") ? ".ogg" : ".mp3"; + var session = widget.getSession(); + this._audio.src = session.url("/mail/static/src/audio/ting" + ext); + } + this._audio.play(); + } + }, + /** + * Show a browser notification + * + * @private + * @param {string} title + * @param {string} content + */ + _sendNativeNotification: function (title, content) { + var notification = new Notification(title, {body: content, icon: "/mail/static/src/img/odoo_o.png"}); + notification.onclick = function () { + window.focus(); + if (this.cancel) { + this.cancel(); + } else if (this.close) { + this.close(); + } + }; + }, + +}); + +return BusService; + +}); |
