From 1f37fda94fec8a9d33d76c0d76393c14529eaa0c Mon Sep 17 00:00:00 2001 From: Miguel Date: Tue, 10 Sep 2024 23:45:40 -0400 Subject: [PATCH] we now have a blank sliding side menu to hold the profile info --- frontend/ts/profile.js | 141 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 frontend/ts/profile.js diff --git a/frontend/ts/profile.js b/frontend/ts/profile.js new file mode 100644 index 0000000..1eae883 --- /dev/null +++ b/frontend/ts/profile.js @@ -0,0 +1,141 @@ +/* WEPA this is miggymofongo's contribution to underBBS! I'm practicing web components +in this bishhh + +this template contains the html code of the side bar +*/ +const template = document.createElement("template") + +template.innerHTML = ` + + +/* the div block has an id and class so that its targeted by the styles +and the component underneath*/ + +
+ + + + + + +
+ + + +` + +/* setting up a web component will extend the set of available HTML elements for you to use in your +project and keep your codebase organized and modular. Set up a web component in a separate file +and link it directly in your html document head. + +creating a custom web component is just like creating a class. after setting up +the constructor function, you need to create some methods that tell the browser more about it. +you can add whatever methods you want, but they at least need some of the following: + +1) connectedCallback() => call this method when the element is added to the document, +2) disconnectedCallback() => call this method when the element is removed from the document +3) static get observedAttributes() => this returns an array of attribute names to track for changes +4) attributeChangedCallback(name, oldValue, newValue) => this one is called when one of the attributes +5) adoptedCallback() => called when the element is moved to a new document +*/ + + + +class ProfileSideBar extends HTMLElement { +// + constructor() { // the constructor function initiates the new side bar + super() + this.appendChild(template.content.cloneNode(true)) + this.sidenav = this.getElementById("#mySidenav"); + this.openBtn = this.getElementById("#openBtn"); + this.closeBtn = this.querySelector(".closeBtn"); + this.mainContent = document.getElementById("main"); + + } +/* this method adds event listeners to the openBtn and closeBtn classes that +call the openNav closeNav function to open and close the sidebar*/ + connectedCallback() { + + this.openBtn.addEventListener("click", this.openNav.bind(this)); + this.closeBtn.addEventListener("click", this.closeNav.bind(this)); + + } + + + disconnectedCallback() { + this.openBtn.removeEventListener("click", this.openNav.bind(this)); + this.closeBtn.removeEventListener("click", this.closeNav.bind(this)); + + } + +/* these two methods will open and close the profile side menu*/ + openNav() { + this.sidenav.style.width = "450px"; + if (this.mainContent) { + this.mainContent.style.marginLeft = "250px"; + } + } + + closeNav() { + this.sidenav.style.width = "0"; + if (this.mainContent) { + this.mainContent.style.marginLeft = "0" + } + } +} +/*lastly, we need to define the element tag itself and attach it to the webcomponent */ +customElements.define("profile-side-bar", ProfileSideBar) \ No newline at end of file