You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
3.0 KiB

7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
  1. /**********************************************************************************
  2. *
  3. * Copyright (C) 2017 MuK IT GmbH
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. **********************************************************************************/
  19. odoo.define('muk_web_client.channel', function (require) {
  20. "use strict";
  21. var WebClient = require('web.WebClient');
  22. var session = require('web.session');
  23. var bus = require('bus.bus');
  24. WebClient.include({
  25. init: function(parent, client_options){
  26. this._super.apply(this, arguments);
  27. this.bus_channels = [];
  28. this.bus_events = [];
  29. },
  30. show_application: function() {
  31. var res = this._super();
  32. bus.bus.on('notification', this, this.bus_notification);
  33. bus.bus.start_polling();
  34. return res;
  35. },
  36. destroy: function() {
  37. var self = this;
  38. bus.bus.off('notification', this, this.bus_notification);
  39. $.each(this.bus_channels, function(index, channel) {
  40. self.bus_delete_channel(channel);
  41. });
  42. $.each(this.bus_events, function(index, event) {
  43. self.bus_off(event[0], event[1]);
  44. });
  45. this._super.apply(this, arguments);
  46. },
  47. bus_declare_channel: function(channel, method) {
  48. if($.inArray(channel, this.bus_channels) === -1) {
  49. this.bus_on(channel, method);
  50. this.bus_channels.push(channel);
  51. bus.bus.add_channel(channel);
  52. }
  53. },
  54. bus_delete_channel: function(channel) {
  55. var index = $.inArray(channel, this.bus_channels);
  56. bus.bus.delete_channel(channel);
  57. this.bus_channels.splice(index, 1);
  58. },
  59. bus_notification: function(notifications) {
  60. var self = this;
  61. $.each(notifications, function(index, notification) {
  62. var channel = notification[0];
  63. var message = notification[1];
  64. if($.inArray(channel, self.bus_channels) !== -1) {
  65. bus.bus.trigger(channel, message);
  66. }
  67. });
  68. },
  69. bus_on: function(name, event) {
  70. bus.bus.on(name, this, event);
  71. this.bus_events.push([name, event]);
  72. },
  73. bus_off: function(name, event) {
  74. var index = $.map(this.bus_events, function(tuple, index) {
  75. if(tuple[0] === name && tuple[1] === event) {
  76. return index;
  77. }
  78. });
  79. bus.bus.off(name, this, event);
  80. this.bus_events.splice(index, 1);
  81. },
  82. });
  83. });