Advertisement
314ma

sidebar-refactored

Jan 20th, 2023 (edited)
988
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {css, html, LitElement} from "https://unpkg.com/lit-element@2.0.1/lit-element.js?module";
  2.  
  3. class LeftSideBarCard extends LitElement {
  4.  
  5.     static get properties() {
  6.         return {
  7.             hass: {},
  8.             config: {},
  9.             _current_submenu: {}
  10.         };
  11.     }
  12.  
  13.     constructor() {
  14.         super();
  15.         this._current_submenu = -1;
  16.     }
  17.  
  18.     render() {
  19.         const url = window.location.href.split("/").pop();
  20.  
  21.         return html`
  22.             <div class="main">
  23.                 <aside style="width: ${this.isSubmenuOpened() ? '50px' : '90px'}">
  24.                     <div class="empty"></div>
  25.                     ${this.config.items.map(item => {
  26.                         return item.url ?
  27.                                 html`
  28.                                     <a href="${item.url}"
  29.                                        class="${this.isItemActive(item, url) ? 'active' : 'deactive'}">
  30.                                         <i aria-hidden="true">
  31.                                             <ha-icon icon="${item.icon}"/>
  32.                                         </i>
  33.                                     </a>
  34.                                 ` :
  35.                                 item.submenu ?
  36.                                         html`
  37.                                             <a class="${this.isSubmenuItemOpen(item) ? 'active' : 'deactive'}">
  38.                                                 <i @click=${() => {
  39.                                                     this._current_submenu = this.toggleSubmenu(item);
  40.                                                 }}>
  41.                                                     <ha-icon icon="${item.icon}"/>
  42.                                                 </i>
  43.                                             </a>
  44.                                         ` : null;
  45.                     })}
  46.                     <div class="empty"></div>
  47.                 </aside>
  48.                 <div class="sub_menu" style="display: ${this._current_submenu >= 0 ? 'flex' : 'none'}">
  49.                     ${this.isSubmenuOpened() ?
  50.                             this.config.items[this._current_submenu].submenu.map(sub => {
  51.                                 return html`
  52.                                     <a class="button_option_menu ${sub.url === url ? 'button_option_menu_active' : ' '}"
  53.                                        href="${sub.url}">
  54.                                         <ha-icon class="icon" icon="${sub.icon}"/>
  55.                                         </ha-icon>
  56.                                     </a
  57.                                 `;
  58.                             }) : null}
  59.                 </div>
  60.             </div>
  61.  
  62.         `;
  63.     }
  64.  
  65.     isItemActive(item, url) {
  66.         return item.url === url;
  67.     }
  68.  
  69.     isSubmenuOpened() {
  70.         return this._current_submenu !== -1;
  71.     }
  72.  
  73.     isSubmenuOpen(item) {
  74.         return this._current_submenu === this.config.items.indexOf(item);
  75.     }
  76.  
  77.     isSubmenuItemOpen(item) {
  78.         const url = window.location.href.split("/").pop();
  79.         return this.isSubmenuOpen(item) && item.submenu.filter(i => i.url === url).length > 0;
  80.     }
  81.  
  82.     toggleSubmenu(item) {
  83.         return this._current_submenu === this.config.items.indexOf(item) ? -1 : this.config.items.indexOf(item);
  84.     }
  85.  
  86.     connectedCallback() {
  87.         const url = window.location.href.split("/").pop();
  88.         this._current_submenu = this.findSubmenuWithUrl(url);
  89.         super.connectedCallback()
  90.     }
  91.  
  92.     findSubmenuWithUrl(url) {
  93.         return this.config.items.findIndex(i =>
  94.             !i.url &&
  95.             i.submenu &&
  96.             i.submenu.filter(mi => mi.url === url).length > 0);
  97.     }
  98.  
  99.     disconnectedCallback() {
  100.         super.disconnectedCallback()
  101.     }
  102.  
  103.     setConfig(config) {
  104.         if (!config.items) {
  105.             throw new Error("You need to define entities");
  106.         }
  107.  
  108.         this.config = config;
  109.     }
  110.  
  111.     getCardSize() {
  112.         return this.config.items.length + 1;
  113.     }
  114.  
  115.     static get styles() {
  116.         return css`
  117.  
  118.     :host {
  119.       --layout-margin: 4px 4px 0px 4px;
  120.     }
  121.  
  122.     .main {
  123.       // width: 80px;
  124.       height: 99.6vh;
  125.       display: flex;
  126.       flex-direction:rows;
  127.       justify-content: center;
  128.       text-align: center;
  129.       vertical-align: middle;
  130.       // margin: 2vh 0 2vh 10px;
  131.  
  132.     }
  133.  
  134.     .empty {
  135.       background-color: transparent;
  136.       height: 100%;
  137.       border-right: 1px solid #dad7d7;
  138.     }
  139.    
  140.     aside {
  141.       color: #fff;
  142.      
  143.       height: 100%;
  144.       background-image: linear-gradient(270deg, #f6f5fa, transparent);
  145.       // background-image: linear-gradient(30deg, #0048bd, #44a7fd);
  146.       // background-color: var(--deactive-background-button-color);
  147.       // border-radius: 15px;
  148.       display: flex;
  149.       flex-direction: column;
  150.       justify-content: center;
  151.       text-align: center;
  152.       vertical-align: middle;
  153.     }
  154.    
  155.     aside a {
  156.       // color: #fff;
  157.       color: var(--sidebar-icon-color);
  158.       display: block;
  159.       padding: 20px 0px 20px 5px;
  160.       margin-left: 10px;
  161.       text-decoration: none;
  162.       -webkit-tap-highlight-color: transparent;
  163.       // border-right: 1px solid rgba(128, 128, 128, 0.2);
  164.     }
  165.     .deactive {
  166.       border-right: 1px solid #dad7d7;
  167.     }
  168.     .active {
  169.       color: #3f5efb;
  170.       background: #fff;
  171.       outline: none;
  172.       position: relative;
  173.       background-color: #fff;
  174.       border-top-left-radius: 15px;
  175.       border-bottom-left-radius: 15px;
  176.       border-left: 1px solid #dad7d7;
  177.       border-top: 1px solid #dad7d7;
  178.       border-bottom: 1px solid #dad7d7;
  179.       border-right-color: #dad7d7;
  180.       border-right-width: 1px;
  181.       // box-shadow: 0 -4px 3px -4px, 0 4px 3px -4px, -4px 0px 3px -4px;
  182.     }
  183.    
  184.     .active.sub_menu_button {
  185.       background: #aaa;
  186.       background-color: #aaa;
  187.       z-index: 1;
  188.     }
  189.    
  190.     .active.sub_menu_button:after {
  191.       box-shadow: #aaa 0px 15px 0px 0px;
  192.       pointer-events: none;
  193.     }
  194.    
  195.     .active.sub_menu_button:before {
  196.       box-shadow: 0 -15px 0 0 #aaa;
  197.       pointer-events: none;
  198.     }
  199.    
  200.     aside a i {
  201.       margin-right: 15px;
  202.     }
  203.    
  204.     .active:after{
  205.       content: "";
  206.       position: absolute;
  207.       background-color: transparent;
  208.       bottom: 100%;
  209.       right: 0px;
  210.       height: 100%;
  211.       width: 25px;
  212.       border-bottom-right-radius: 15px;
  213.       border-right-color: #dad7d7;
  214.       border-bottom: 1px solid #dad7d7;
  215.       box-shadow: rgb(255 255 255) 0px 15px 0px 0px;
  216.       pointer-events: none;
  217.     }
  218.    
  219.     .active:before {
  220.       content: "";
  221.       position: absolute;
  222.       background-color: transparent;
  223.       top: 64px;
  224.       right: 0px;
  225.       height: 100%;
  226.       width: 25px;
  227.       border-top-right-radius: 15px;
  228.       border-right: 1px solid #dad7d7;
  229.       border-top: 1px solid #dad7d7;
  230.       box-shadow: 0 -15px 0 0 #fff;
  231.       pointer-events: none;
  232.     }
  233.    
  234.     aside p {
  235.       margin: 0;
  236.       padding: 40px 0;
  237.     }
  238.    
  239.     body {
  240.       font-family: "Roboto";
  241.       width: 100%;
  242.       height: 100vh;
  243.       margin: 0;
  244.     }
  245.    
  246.     i:before {
  247.       width: 14px;
  248.       height: 14px;
  249.       font-size: 14px;
  250.       position: fixed;
  251.       color: #fff;
  252.       background: #0077b5;
  253.       padding: 10px;
  254.       border-radius: 50%;
  255.       top: 5px;
  256.       right: 5px;
  257.     }
  258.     .sub_menu {
  259.      
  260.       flex-direction: column;
  261.       justify-content: center;
  262.       text-align: center;
  263.       vertical-align: middle;
  264.       cursor: pointer;
  265.       width: 45px;
  266.  
  267.     }
  268.     .button_option_menu {
  269.       padding: 15px;
  270.       border-right: 2px solid var(--sidebar-border-color);
  271.       background-color: transparent;
  272.     }
  273.     .button_option_menu_active {
  274.       border-right: 4px solid var(--state-icon-active-color);
  275.     }
  276.  
  277.     .icon {
  278.       color: var(--sidebar-icon-color)
  279.     }
  280.    
  281.     `;
  282.     }
  283.  
  284. }
  285.  
  286. customElements.define('leftside-card', LeftSideBarCard);
  287.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement