m22_sidebar.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /** @odoo-module **/
  2. import publicWidget from "@web/legacy/js/public/public_widget";
  3. /**
  4. * M22 Sidebar Widget
  5. * Handles the collapsible sidebar functionality with smooth transitions
  6. * and proper state management.
  7. */
  8. publicWidget.registry.M22Sidebar = publicWidget.Widget.extend({
  9. selector: '#m22_sidebar',
  10. events: {
  11. 'click .sidebar-collapse-btn': '_onToggleSidebar',
  12. 'click .nav-link': '_onLinkClick',
  13. },
  14. // Transition duration in ms (should match CSS)
  15. TRANSITION_DURATION: 300,
  16. /**
  17. * @override
  18. */
  19. start: function () {
  20. this.backdrop = document.getElementById('m22_sidebar_backdrop');
  21. this.collapseBtn = this.el.querySelector('.sidebar-collapse-btn');
  22. this.collapseBtnIcon = this.collapseBtn?.querySelector('i');
  23. // Cache frequently accessed elements
  24. this.sidebarTexts = this.el.querySelectorAll('.sidebar-text, .sidebar-title, .sidebar-logo-text');
  25. this.navLinks = this.el.querySelectorAll('.nav-link');
  26. // Setup backdrop listener
  27. if (this.backdrop) {
  28. this._boundCloseMobile = this._onCloseMobileSidebar.bind(this);
  29. this.backdrop.addEventListener('click', this._boundCloseMobile);
  30. }
  31. // Restore state from localStorage (without animation on page load)
  32. this._restoreState(true);
  33. // Handle window resize
  34. this._boundResize = this._onWindowResize.bind(this);
  35. window.addEventListener('resize', this._boundResize);
  36. return this._super.apply(this, arguments);
  37. },
  38. /**
  39. * @override
  40. */
  41. destroy: function () {
  42. if (this.backdrop && this._boundCloseMobile) {
  43. this.backdrop.removeEventListener('click', this._boundCloseMobile);
  44. }
  45. if (this._boundResize) {
  46. window.removeEventListener('resize', this._boundResize);
  47. }
  48. this._super.apply(this, arguments);
  49. },
  50. //--------------------------------------------------------------------------
  51. // Private
  52. //--------------------------------------------------------------------------
  53. /**
  54. * Restores the sidebar state from localStorage
  55. * @param {Boolean} skipAnimation - Whether to skip the collapse animation
  56. * @private
  57. */
  58. _restoreState: function (skipAnimation = false) {
  59. const savedState = localStorage.getItem('m22_sidebar_collapsed');
  60. const isCollapsed = savedState === 'true';
  61. const htmlEl = document.documentElement;
  62. const hasEarlyInit = htmlEl.classList.contains('m22-sidebar-collapsed-init');
  63. // If early init already applied collapsed state (sidebar is hidden)
  64. if (hasEarlyInit && isCollapsed) {
  65. // Sync JS state while sidebar is still hidden
  66. this.el.setAttribute('data-collapsed', 'true');
  67. document.body.classList.add('sidebar-collapsed');
  68. // Update icon BEFORE showing sidebar
  69. this._updateCollapseIcon(true);
  70. // Hide texts
  71. this._hideTexts();
  72. // Now show sidebar by adding initialized class
  73. // Use double RAF to ensure styles are applied before visibility
  74. requestAnimationFrame(() => {
  75. requestAnimationFrame(() => {
  76. htmlEl.classList.add('m22-sidebar-initialized');
  77. });
  78. });
  79. return;
  80. }
  81. // Remove early init class if state is expanded (show sidebar immediately)
  82. if (hasEarlyInit && !isCollapsed) {
  83. // First set expanded state
  84. this.el.setAttribute('data-collapsed', 'false');
  85. document.body.classList.remove('sidebar-collapsed');
  86. this._updateCollapseIcon(false);
  87. this._showTexts();
  88. // Then show sidebar
  89. htmlEl.classList.remove('m22-sidebar-collapsed-init');
  90. }
  91. if (skipAnimation && !hasEarlyInit) {
  92. // Temporarily disable transitions
  93. this.el.style.transition = 'none';
  94. document.body.style.transition = 'none';
  95. }
  96. if (!hasEarlyInit) {
  97. this._setCollapsedState(isCollapsed, skipAnimation);
  98. }
  99. if (skipAnimation && !hasEarlyInit) {
  100. // Re-enable transitions after a frame
  101. requestAnimationFrame(() => {
  102. requestAnimationFrame(() => {
  103. this.el.style.transition = '';
  104. document.body.style.transition = '';
  105. });
  106. });
  107. }
  108. },
  109. /**
  110. * Sets the collapsed state of the sidebar
  111. * @param {Boolean} isCollapsed - Whether the sidebar should be collapsed
  112. * @param {Boolean} skipAnimation - Whether to skip animation
  113. * @private
  114. */
  115. _setCollapsedState: function (isCollapsed, skipAnimation = false) {
  116. const htmlEl = document.documentElement;
  117. this.el.setAttribute('data-collapsed', isCollapsed);
  118. if (isCollapsed) {
  119. document.body.classList.add('sidebar-collapsed');
  120. // Ensure init class is present for collapsed state
  121. htmlEl.classList.add('m22-sidebar-collapsed-init');
  122. this._updateCollapseIcon(true);
  123. // Hide text immediately when collapsing
  124. this._hideTexts();
  125. } else {
  126. document.body.classList.remove('sidebar-collapsed');
  127. // Remove init class when expanding to allow normal CSS behavior
  128. htmlEl.classList.remove('m22-sidebar-collapsed-init');
  129. this._updateCollapseIcon(false);
  130. // Show text with delay to match sidebar expansion
  131. if (!skipAnimation) {
  132. setTimeout(() => {
  133. this._showTexts();
  134. }, this.TRANSITION_DURATION / 2);
  135. } else {
  136. this._showTexts();
  137. }
  138. }
  139. },
  140. /**
  141. * Updates the collapse button icon based on state
  142. * @param {Boolean} isCollapsed
  143. * @private
  144. */
  145. _updateCollapseIcon: function (isCollapsed) {
  146. if (this.collapseBtnIcon) {
  147. this.collapseBtnIcon.classList.remove('fa-chevron-left', 'fa-chevron-right', 'fa-bars');
  148. this.collapseBtnIcon.classList.add(isCollapsed ? 'fa-chevron-right' : 'fa-chevron-left');
  149. }
  150. },
  151. /**
  152. * Hides sidebar text elements
  153. * @private
  154. */
  155. _hideTexts: function () {
  156. this.sidebarTexts.forEach(el => {
  157. el.style.opacity = '0';
  158. el.style.visibility = 'hidden';
  159. });
  160. },
  161. /**
  162. * Shows sidebar text elements
  163. * @private
  164. */
  165. _showTexts: function () {
  166. this.sidebarTexts.forEach(el => {
  167. el.style.opacity = '1';
  168. el.style.visibility = 'visible';
  169. });
  170. },
  171. /**
  172. * Checks if we're in mobile view
  173. * @returns {Boolean}
  174. * @private
  175. */
  176. _isMobile: function () {
  177. return window.innerWidth < 992;
  178. },
  179. //--------------------------------------------------------------------------
  180. // Handlers
  181. //--------------------------------------------------------------------------
  182. /**
  183. * Toggles the sidebar collapsed state
  184. * @param {Event} ev
  185. * @private
  186. */
  187. _onToggleSidebar: function (ev) {
  188. ev.preventDefault();
  189. ev.stopPropagation();
  190. if (this._isMobile()) {
  191. // On mobile, toggle the overlay sidebar
  192. const isOpen = this.el.classList.contains('m22-sidebar-open');
  193. if (isOpen) {
  194. this._onCloseMobileSidebar();
  195. } else {
  196. this._onOpenMobileSidebar();
  197. }
  198. } else {
  199. // On desktop, toggle collapse state
  200. const isCollapsed = this.el.getAttribute('data-collapsed') === 'true';
  201. const newState = !isCollapsed;
  202. this._setCollapsedState(newState);
  203. localStorage.setItem('m22_sidebar_collapsed', newState);
  204. }
  205. },
  206. /**
  207. * Closes sidebar on mobile when a link is clicked
  208. * @private
  209. */
  210. _onLinkClick: function () {
  211. if (this._isMobile() && this.el.classList.contains('m22-sidebar-open')) {
  212. this._onCloseMobileSidebar();
  213. }
  214. },
  215. /**
  216. * Opens the mobile sidebar
  217. * @private
  218. */
  219. _onOpenMobileSidebar: function () {
  220. this.el.classList.add('m22-sidebar-open');
  221. if (this.backdrop) {
  222. this.backdrop.classList.add('visible');
  223. }
  224. document.body.style.overflow = 'hidden';
  225. },
  226. /**
  227. * Closes the mobile sidebar
  228. * @private
  229. */
  230. _onCloseMobileSidebar: function () {
  231. this.el.classList.remove('m22-sidebar-open');
  232. if (this.backdrop) {
  233. this.backdrop.classList.remove('visible');
  234. }
  235. document.body.style.overflow = '';
  236. },
  237. /**
  238. * Handle window resize
  239. * @private
  240. */
  241. _onWindowResize: function () {
  242. // Close mobile sidebar if resizing to desktop
  243. if (!this._isMobile() && this.el.classList.contains('m22-sidebar-open')) {
  244. this._onCloseMobileSidebar();
  245. }
  246. }
  247. });
  248. /**
  249. * External trigger widget for opening the sidebar on mobile
  250. * Use data-action="open-m22-sidebar" on any element
  251. */
  252. publicWidget.registry.M22SidebarTrigger = publicWidget.Widget.extend({
  253. selector: '[data-action="open-m22-sidebar"]',
  254. events: {
  255. 'click': '_onTriggerClick',
  256. },
  257. _onTriggerClick: function (ev) {
  258. ev.preventDefault();
  259. const sidebar = document.getElementById('m22_sidebar');
  260. const backdrop = document.getElementById('m22_sidebar_backdrop');
  261. if (sidebar) {
  262. sidebar.classList.add('m22-sidebar-open');
  263. if (backdrop) {
  264. backdrop.classList.add('visible');
  265. }
  266. document.body.style.overflow = 'hidden';
  267. }
  268. }
  269. });