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.

213 lines
7.6 KiB

  1. //-*- coding: utf-8 -*-
  2. //############################################################################
  3. //
  4. // OpenERP, Open Source Management Solution
  5. // This module copyright (C) 2015 Therp BV <http://therp.nl>.
  6. //
  7. // This program is free software: you can redistribute it and/or modify
  8. // it under the terms of the GNU Affero General Public License as
  9. // published by the Free Software Foundation, either version 3 of the
  10. // License, or (at your option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU Affero General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU Affero General Public License
  18. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. //
  20. //############################################################################
  21. openerp.web_menu_autohide = function(instance)
  22. {
  23. instance.web.WebClient.include({
  24. show_bar_threshold_navbar: 10,
  25. show_bar_threshold_leftbar: 10,
  26. hide_delay_navbar: 10000,
  27. hide_delay_leftbar: 10000,
  28. leftbar_hide_timeout_id: null,
  29. main_menu_hide_timeout_id: null,
  30. navbar_query: '#oe_main_menu_navbar',
  31. leftbar_query: '.oe_leftbar',
  32. start: function()
  33. {
  34. var self = this,
  35. addon_name = 'web_menu_autohide',
  36. parameters = _.map(
  37. ['show_bar_threshold_navbar', 'hide_delay_navbar',
  38. 'show_bar_threshold_leftbar', 'hide_delay_leftbar',
  39. ],
  40. function(a) { return addon_name + '.' + a });
  41. return (new openerp.web.Model('ir.config_parameter'))
  42. .query(['key', 'value'])
  43. .filter([['key', 'in', parameters]])
  44. .all()
  45. .then(function(params)
  46. {
  47. _.each(params, function(param)
  48. {
  49. self[param.key.replace(addon_name + '.', '')] =
  50. parseInt(param.value);
  51. });
  52. })
  53. .then(this.proxy(this._super))
  54. .then(function()
  55. {
  56. if(self.hide_delay_navbar)
  57. {
  58. jQuery('.openerp.openerp_webclient_container')
  59. .css('height', '100%');
  60. jQuery(self.navbar_query)
  61. .css({
  62. position: 'absolute',
  63. left: '0px',
  64. right: '0px',
  65. top: '0px',
  66. 'z-index': 1110
  67. });
  68. }
  69. if(self.hide_delay_leftbar)
  70. {
  71. jQuery(self.leftbar_query)
  72. .css({
  73. display: 'block',
  74. position: 'absolute',
  75. left: '0px',
  76. top: '0px',
  77. bottom: '0px',
  78. 'z-index': 1110,
  79. width: 'auto',
  80. });
  81. jQuery('.openerp .oe_leftbar > div')
  82. .css('width', 'auto');
  83. }
  84. if(self.hide_delay_navbar || self.hide_delay_leftbar)
  85. {
  86. self.$el
  87. .bind('mousemove', _.bind(self.on_mousemove, self))
  88. .bind('click', _.bind(self.on_click, self));
  89. }
  90. })
  91. },
  92. show_application: function()
  93. {
  94. this._super.apply(this, arguments);
  95. openerp.client.toggle_main_menu(false, this.hide_delay_navbar);
  96. },
  97. toggle_bars: function(hide)
  98. {
  99. this.toggle_main_menu(hide);
  100. this.toggle_left_bar(hide);
  101. },
  102. toggle_menu_element: function(selector, timeout_id, show, delay)
  103. {
  104. if(this[timeout_id])
  105. {
  106. clearTimeout(this[timeout_id]);
  107. this[timeout_id] = null;
  108. }
  109. if(delay)
  110. {
  111. this[timeout_id] = setTimeout(
  112. _.bind(
  113. this.toggle_menu_element, this,
  114. selector, timeout_id, show),
  115. delay);
  116. }
  117. else
  118. {
  119. this.$(selector).toggle(show);
  120. }
  121. },
  122. toggle_main_menu: function(show, delay)
  123. {
  124. if(!this.hide_delay_navbar)
  125. {
  126. return;
  127. }
  128. this.toggle_menu_element(
  129. this.navbar_query, 'main_menu_hide_timeout_id', show,
  130. delay);
  131. },
  132. toggle_left_bar: function(show, delay)
  133. {
  134. if(!this.hide_delay_leftbar)
  135. {
  136. return;
  137. }
  138. this.toggle_menu_element(
  139. this.leftbar_query, 'leftbar_hide_timeout_id', show, delay);
  140. },
  141. on_click: function(e)
  142. {
  143. var on_main_menu = jQuery(e.target)
  144. .parents(this.navbar_query).length > 0,
  145. on_left_bar = jQuery(e.target)
  146. .parents(this.leftbar_query).length > 0;
  147. if(!on_left_bar && !on_main_menu)
  148. {
  149. this.toggle_left_bar(false);
  150. }
  151. },
  152. on_mousemove: function(e)
  153. {
  154. var on_main_menu = jQuery(e.target)
  155. .parents(this.navbar_query).length > 0,
  156. on_left_bar = jQuery(e.target)
  157. .parents(this.leftbar_query).length > 0;
  158. if(on_left_bar && openerp.client.leftbar_hide_timeout_id)
  159. {
  160. clearTimeout(openerp.client.leftbar_hide_timeout_id);
  161. openerp.client.leftbar_hide_timeout_id = null;
  162. }
  163. if(on_main_menu && openerp.client.main_menu_hide_timeout_id)
  164. {
  165. clearTimeout(openerp.client.main_menu_hide_timeout_id);
  166. openerp.client.main_menu_hide_timeout_id = null;
  167. }
  168. if(!on_left_bar && !openerp.client.leftbar_hide_timeout_id)
  169. {
  170. this.toggle_left_bar(false, openerp.client.hide_delay_leftbar);
  171. }
  172. if(!on_main_menu && !openerp.client.main_menu_hide_timeout_id)
  173. {
  174. this.toggle_main_menu(false);
  175. }
  176. if(e.pageX < this.show_bar_threshold_leftbar)
  177. {
  178. this.toggle_left_bar(true);
  179. }
  180. if(e.pageY < this.show_bar_threshold_navbar)
  181. {
  182. this.toggle_main_menu(true);
  183. }
  184. },
  185. });
  186. instance.web.Menu.include({
  187. close_leftbar: false,
  188. start: function()
  189. {
  190. this.on('menu_click', this, this.on_menu_click_with_action);
  191. openerp.client.toggle_left_bar(false, openerp.client.hide_delay_leftbar);
  192. return this._super.apply(this, arguments);
  193. },
  194. on_menu_click_with_action: function(menu, $element)
  195. {
  196. //close if it's not a menu containing other menus
  197. this.close_leftbar = (
  198. $element.parents(openerp.client.navbar_query).length == 0 &&
  199. $element.parent().children('ul').length == 0
  200. );
  201. },
  202. open_menu: function()
  203. {
  204. this._super.apply(this, arguments);
  205. if(this.close_leftbar)
  206. {
  207. openerp.client.toggle_left_bar(false);
  208. }
  209. this.close_leftbar = false;
  210. },
  211. });
  212. }