|
| 1 | +(function() { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + // Constructor function for the Drawer plugin |
| 5 | + function Drawer(drawerId, openButtonId) { |
| 6 | + // Store the drawer and open button elements |
| 7 | + this.drawer = document.getElementById(drawerId); |
| 8 | + this.openButton = document.getElementById(openButtonId); |
| 9 | + |
| 10 | + // Initialize the drawer |
| 11 | + this.initDrawer(); |
| 12 | + } |
| 13 | + |
| 14 | + Drawer.prototype.initDrawer = function() { |
| 15 | + // Add click event listener to the open button |
| 16 | + this.openButton.addEventListener('click', this.toggleDrawer.bind(this)); |
| 17 | + |
| 18 | + // Add a click event listener to the document to close the drawer when clicking outside |
| 19 | + document.addEventListener('click', this.closeDrawerOutside.bind(this)); |
| 20 | + }; |
| 21 | + |
| 22 | + Drawer.prototype.toggleDrawer = function(event) { |
| 23 | + // Prevent the default behavior of the button click |
| 24 | + event.preventDefault(); |
| 25 | + // Toggle the "open" class to show/hide the drawer |
| 26 | + this.drawer.classList.toggle('open'); |
| 27 | + }; |
| 28 | + |
| 29 | + Drawer.prototype.closeDrawerOutside = function(event) { |
| 30 | + // Check if the clicked element is not inside the drawer or the open button |
| 31 | + if (!this.drawer.contains(event.target) && event.target !== this.openButton) { |
| 32 | + // Close the drawer |
| 33 | + this.drawer.classList.remove('open'); |
| 34 | + } |
| 35 | + }; |
| 36 | + |
| 37 | + // Export the Drawer as a global function or as an ES module |
| 38 | + if (typeof window === 'object') { |
| 39 | + window.Drawer = Drawer; |
| 40 | + } |
| 41 | + if (typeof exports === 'object') { |
| 42 | + module.exports = Drawer; |
| 43 | + } |
| 44 | +})(); |
0 commit comments