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.

155 lines
6.5 KiB

  1. /******************************************************************************
  2. Web Easy Switch Company module for OpenERP
  3. Copyright (C) 2014 GRAP (http://www.grap.coop)
  4. @author Sylvain LE GAL (https://twitter.com/legalsylvain)
  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. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Affero General Public License for more details.
  13. You should have received a copy of the GNU Affero General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ******************************************************************************/
  16. openerp.web_easy_switch_company = function (instance) {
  17. /***************************************************************************
  18. Create an new 'SwitchCompanyWidget' widget that allow users to switch
  19. from a company to another more easily.
  20. ***************************************************************************/
  21. instance.web.SwitchCompanyWidget = instance.web.Widget.extend({
  22. template:'web_easy_switch_company.SwitchCompanyWidget',
  23. /***********************************************************************
  24. Overload section
  25. ***********************************************************************/
  26. /**
  27. * Overload 'init' function to initialize the values of the widget.
  28. */
  29. init: function(parent){
  30. this._super(parent);
  31. this.companies = [];
  32. this.current_company_id = 0;
  33. this.current_company_name = '';
  34. },
  35. /**
  36. * Overload 'start' function to load datas from DB.
  37. */
  38. start: function () {
  39. this._super();
  40. this._load_data();
  41. },
  42. /**
  43. * Overload 'renderElement' function to set events on company items.
  44. */
  45. renderElement: function() {
  46. var self = this;
  47. this._super();
  48. if (this.companies.length === 1) {
  49. this.$el.hide();
  50. }
  51. else{
  52. this.$el.show();
  53. this.$el.find('.easy_switch_company_company_item').on('click', function(ev) {
  54. var company_id = $(ev.target).data("company-id");
  55. if (company_id != self.current_company_id){
  56. var func = '/web_easy_switch_company/switch/change_current_company';
  57. var param = {'company_id': company_id}
  58. self.rpc(func, param).done(function(res) {
  59. window.location.reload()
  60. });
  61. }
  62. });
  63. }
  64. },
  65. /***********************************************************************
  66. Custom section
  67. ***********************************************************************/
  68. /**
  69. * helper function to load data from the server
  70. */
  71. _fetch: function(model, fields, domain, ctx){
  72. return new instance.web.Model(model).query(fields).filter(domain).context(ctx).all();
  73. },
  74. /**
  75. * - Load data of the companies allowed to the current users;
  76. * - Launch the rendering of the current widget;
  77. */
  78. _load_data: function(){
  79. var self = this;
  80. // Request for current users information
  81. this._fetch('res.users',['company_id'],[['id','=',this.session.uid]]).then(function(res_users){
  82. self.current_company_id = res_users[0].company_id[0];
  83. self.current_company_name = res_users[0].company_id[1];
  84. // Request for other companies
  85. // We have to go through fields_view_get to emulate the
  86. // exact (exotic) behavior of the user preferences form in
  87. // fetching the allowed companies wrt record rules.
  88. // Note: calling res.company.name_search with
  89. // user_preference=True in the context does
  90. // not work either.
  91. new instance.web.Model('res.company').call('name_search',{context:{'user_preference':'True'}}).then(function(res){
  92. var res_company = res;
  93. for ( var i=0 ; i < res_company.length; i++) {
  94. var logo_topbar, logo_state;
  95. // TODO: fetching the logo of other companies fails with the
  96. // default res.company record rule, so we should
  97. // probably remove the logos from the menu :(
  98. logo_topbar = self.session.url(
  99. '/web/binary/image', {
  100. model:'res.company',
  101. field: 'logo_topbar',
  102. id: res_company[i][0]
  103. });
  104. if (res_company[i][0] == self.current_company_id){
  105. logo_state = '/web_easy_switch_company/static/description/selection-on.png';
  106. }
  107. else{
  108. logo_state = '/web_easy_switch_company/static/description/selection-off.png';
  109. }
  110. self.companies.push({
  111. id: res_company[i][0],
  112. name: res_company[i][1],
  113. logo_topbar: logo_topbar,
  114. logo_state: logo_state
  115. });
  116. }
  117. // Update rendering
  118. self.renderElement();
  119. });
  120. });
  121. },
  122. });
  123. /***************************************************************************
  124. Extend 'UserMenu' Widget to insert a 'SwitchCompanyWidget' widget.
  125. ***************************************************************************/
  126. instance.web.UserMenu = instance.web.UserMenu.extend({
  127. init: function(parent) {
  128. this._super(parent);
  129. var switch_button = new instance.web.SwitchCompanyWidget();
  130. switch_button.appendTo(instance.webclient.$el.find('.oe_systray'));
  131. }
  132. });
  133. };