Best JavaScript code snippet using best
sidebar.component.ts
Source:sidebar.component.ts
1import { Component, OnInit, OnDestroy, HostListener } from '@angular/core';2import { Router, NavigationEnd, ActivatedRoute } from '@angular/router';3import { filter, map } from 'rxjs/operators';4import { SidebarService, ISidebar } from './sidebar.service';5import menuItems, { IMenuItem } from 'src/app/constants/menu';6import { Subscription } from 'rxjs';7@Component({8 selector: 'app-sidebar',9 templateUrl: './sidebar.component.html',10 styles:[11 `12 a{13 text-align:center;14 }15 `16 ]17})18export class SidebarComponent implements OnInit, OnDestroy {19 menuItems: IMenuItem[] = menuItems;20 selectedParentMenu = '';21 viewingParentMenu = '';22 currentUrl: string;23 sidebar: ISidebar;24 subscription: Subscription;25 closedCollapseList = [];26 currentUser = null;27 constructor(28 private router: Router,29 private sidebarService: SidebarService,30 private activatedRoute: ActivatedRoute,31 32 ) {33 34 this.subscription = this.sidebarService.getSidebar().subscribe(35 (res) => {36 this.sidebar = res;37 },38 (err) => {39 console.error(`An error occurred: ${err.message}`);40 }41 );42 this.router.events43 .pipe(44 filter((event) => event instanceof NavigationEnd),45 map(() => this.activatedRoute),46 map((route) => {47 while (route.firstChild) {48 route = route.firstChild;49 }50 return route;51 })52 )53 .subscribe((event) => {54 const path = this.router.url.split('?')[0];55 const paramtersLen = Object.keys(event.snapshot.params).length;56 const pathArr = path57 .split('/')58 .slice(0, path.split('/').length - paramtersLen);59 this.currentUrl = pathArr.join('/');60 });61 router.events62 .pipe(filter((event) => event instanceof NavigationEnd))63 .subscribe((event: NavigationEnd) => {64 const { containerClassnames } = this.sidebar;65 this.selectMenu();66 this.toggle();67 this.sidebarService.setContainerClassnames(68 0,69 containerClassnames,70 this.sidebar.selectedMenuHasSubItems71 );72 window.scrollTo(0, 0);73 });74 }75 async ngOnInit(): Promise<void> {76 setTimeout(() => {77 this.selectMenu();78 const { containerClassnames } = this.sidebar;79 const nextClasses = this.getMenuClassesForResize(containerClassnames);80 this.sidebarService.setContainerClassnames(81 0,82 nextClasses.join(' '),83 this.sidebar.selectedMenuHasSubItems84 );85 this.isCurrentMenuHasSubItem();86 }, 100);87 }88 ngOnDestroy(): void {89 this.subscription.unsubscribe();90 }91 selectMenu(): void {92 this.selectedParentMenu = this.findParentInPath(this.currentUrl) || '';93 this.isCurrentMenuHasSubItem();94 }95 findParentInPath(path): any {96 const foundedMenuItem = this.menuItems.find((x) => x.to === path);97 if (!foundedMenuItem) {98 if (path.split('/').length > 1) {99 const pathArr = path.split('/');100 return this.findParentInPath(101 pathArr.slice(0, pathArr.length - 1).join('/')102 );103 } else {104 return undefined;105 }106 } else {107 return path;108 }109 }110 isCurrentMenuHasSubItem(): boolean {111 const { containerClassnames } = this.sidebar;112 const menuItem = this.menuItems.find(113 (x) => x.to === this.selectedParentMenu114 );115 const isCurrentMenuHasSubItem =116 menuItem && menuItem.subs && menuItem.subs.length > 0 ? true : false;117 if (isCurrentMenuHasSubItem !== this.sidebar.selectedMenuHasSubItems) {118 if (!isCurrentMenuHasSubItem) {119 this.sidebarService.setContainerClassnames(120 0,121 containerClassnames,122 false123 );124 } else {125 this.sidebarService.setContainerClassnames(126 0,127 containerClassnames,128 true129 );130 }131 }132 return isCurrentMenuHasSubItem;133 }134 changeSelectedParentHasNoSubmenu(parentMenu: string): void {135 const { containerClassnames } = this.sidebar;136 this.selectedParentMenu = parentMenu;137 this.viewingParentMenu = parentMenu;138 this.sidebarService.changeSelectedMenuHasSubItems(false);139 this.sidebarService.setContainerClassnames(0, containerClassnames, false);140 }141 openSubMenu(event: { stopPropagation: () => void }, menuItem: IMenuItem): void {142 if (event) {143 event.stopPropagation();144 }145 const { containerClassnames, menuClickCount } = this.sidebar;146 const selectedParent = menuItem.to;147 const hasSubMenu = menuItem.subs && menuItem.subs.length > 0;148 this.sidebarService.changeSelectedMenuHasSubItems(hasSubMenu);149 if (!hasSubMenu) {150 this.viewingParentMenu = selectedParent;151 this.selectedParentMenu = selectedParent;152 this.toggle();153 } else {154 const currentClasses = containerClassnames155 ? containerClassnames.split(' ').filter((x) => x !== '')156 : '';157 if (!currentClasses.includes('menu-mobile')) {158 if (159 currentClasses.includes('menu-sub-hidden') &&160 (menuClickCount === 2 || menuClickCount === 0)161 ) {162 this.sidebarService.setContainerClassnames(163 3,164 containerClassnames,165 hasSubMenu166 );167 } else if (168 currentClasses.includes('menu-hidden') &&169 (menuClickCount === 1 || menuClickCount === 3)170 ) {171 this.sidebarService.setContainerClassnames(172 2,173 containerClassnames,174 hasSubMenu175 );176 } else if (177 currentClasses.includes('menu-default') &&178 !currentClasses.includes('menu-sub-hidden') &&179 (menuClickCount === 1 || menuClickCount === 3)180 ) {181 this.sidebarService.setContainerClassnames(182 0,183 containerClassnames,184 hasSubMenu185 );186 }187 } else {188 this.sidebarService.addContainerClassname(189 'sub-show-temporary',190 containerClassnames191 );192 }193 this.viewingParentMenu = selectedParent;194 }195 }196 toggle(): void {197 const { containerClassnames, menuClickCount } = this.sidebar;198 const currentClasses = containerClassnames199 .split(' ')200 .filter((x) => x !== '');201 if (currentClasses.includes('menu-sub-hidden') && menuClickCount === 3) {202 this.sidebarService.setContainerClassnames(203 2,204 containerClassnames,205 this.sidebar.selectedMenuHasSubItems206 );207 } else if (208 currentClasses.includes('menu-hidden') ||209 currentClasses.includes('menu-mobile')210 ) {211 if (!(menuClickCount === 1 && !this.sidebar.selectedMenuHasSubItems)) {212 this.sidebarService.setContainerClassnames(213 0,214 containerClassnames,215 this.sidebar.selectedMenuHasSubItems216 );217 }218 }219 }220 toggleCollapse(id: string): void {221 if (this.closedCollapseList.includes(id)) {222 this.closedCollapseList = this.closedCollapseList.filter((x) => x !== id);223 } else {224 this.closedCollapseList.push(id);225 }226 }227 getMenuClassesForResize(classes: string): string[] {228 let nextClasses = classes.split(' ').filter((x: string) => x !== '');229 const windowWidth = window.innerWidth;230 if (windowWidth < this.sidebarService.menuHiddenBreakpoint) {231 nextClasses.push('menu-mobile');232 } else if (windowWidth < this.sidebarService.subHiddenBreakpoint) {233 nextClasses = nextClasses.filter((x: string) => x !== 'menu-mobile');234 if (235 nextClasses.includes('menu-default') &&236 !nextClasses.includes('menu-sub-hidden')237 ) {238 nextClasses.push('menu-sub-hidden');239 }240 } else {241 nextClasses = nextClasses.filter((x: string) => x !== 'menu-mobile');242 if (243 nextClasses.includes('menu-default') &&244 nextClasses.includes('menu-sub-hidden')245 ) {246 nextClasses = nextClasses.filter(247 (x: string) => x !== 'menu-sub-hidden'248 );249 }250 }251 return nextClasses;252 }253 @HostListener('document:click', ['$event'])254 handleDocumentClick(event): void {255 this.viewingParentMenu = '';256 this.selectMenu();257 this.toggle();258 }259 @HostListener('window:resize', ['$event'])260 handleWindowResize(event): void {261 if (event && !event.isTrusted) {262 return;263 }264 const { containerClassnames } = this.sidebar;265 const nextClasses = this.getMenuClassesForResize(containerClassnames);266 this.sidebarService.setContainerClassnames(267 0,268 nextClasses.join(' '),269 this.sidebar.selectedMenuHasSubItems270 );271 this.isCurrentMenuHasSubItem();272 }273 menuClicked(e: MouseEvent): void {274 e.stopPropagation();275 }276 // tslint:disable-next-line:no-shadowed-variable277 filteredMenuItems(menuItems: IMenuItem[]): IMenuItem[] {278 return menuItems279 ? menuItems.filter(280 (x) =>281 !x.roles || (x.roles && x.roles.includes(this.currentUser.role))282 )283 : [];284 }...
reducer.js
Source:reducer.js
1import {2 MENU_SET_CLASSNAMES,3 MENU_CONTAINER_ADD_CLASSNAME,4 MENU_CLICK_MOBILE_MENU,5 MENU_CHANGE_DEFAULT_CLASSES6} from '../../constants/actionTypes';7import {defaultMenuType, subHiddenBreakpoint, menuHiddenBreakpoint } from '../../constants/defaultValues'8const INIT_STATE = {9 containerClassnames: defaultMenuType,10 subHiddenBreakpoint,11 menuHiddenBreakpoint,12 menuClickCount: 013};14export default (state = INIT_STATE, action) => {15 switch (action.type) {16 case MENU_SET_CLASSNAMES:17 return Object.assign({}, state, {18 containerClassnames: action.payload.containerClassnames,19 menuClickCount:action.payload.menuClickCount20 })21 case MENU_CLICK_MOBILE_MENU:22 return Object.assign({}, state, {23 containerClassnames: action.payload.containerClassnames,24 menuClickCount:action.payload.menuClickCount25 })26 case MENU_CONTAINER_ADD_CLASSNAME:27 return Object.assign({}, state, {28 containerClassnames: action.payload29 })30 case MENU_CHANGE_DEFAULT_CLASSES:31 return Object.assign({}, state, {32 containerClassnames: action.payload33 })34 35 default: return { ...state };36 }...
Using AI Code Generation
1var BestFitContainer = require('best-fit-container');2var bestFitContainer = new BestFitContainer();3var containerClassNames = bestFitContainer.containerClassNames(768, 1024);4console.log(containerClassNames);5var BestFitContainer = require('best-fit-container');6var bestFitContainer = new BestFitContainer();7var containerClassNames = bestFitContainer.containerClassNames(768, 1024, 'container');8console.log(containerClassNames);9var BestFitContainer = require('best-fit-container');10var bestFitContainer = new BestFitContainer();11var containerClassNames = bestFitContainer.containerClassNames(768, 1024, 'container', 'container');12console.log(containerClassNames);13var BestFitContainer = require('best-fit-container');14var bestFitContainer = new BestFitContainer();15var containerClassNames = bestFitContainer.containerClassNames(768, 1024, 'container', 'container', 'container');16console.log(containerClassNames);17var BestFitContainer = require('best-fit-container');18var bestFitContainer = new BestFitContainer();19var containerClassNames = bestFitContainer.containerClassNames(768, 1024, 'container', 'container', 'container', 'container');20console.log(containerClassNames);21var BestFitContainer = require('best-fit-container');22var bestFitContainer = new BestFitContainer();23var containerClassNames = bestFitContainer.containerClassNames(768, 1024, 'container', 'container', 'container', 'container',
Using AI Code Generation
1import {BestReactComponent} from 'best-react-component';2const containerClassNames = BestReactComponent.containerClassNames;3import {BestReactComponent} from 'best-react-component';4const {containerClassNames} = BestReactComponent;5import {BestReactComponent} from 'best-react-component';6const {containerClassNames} = BestReactComponent;7import {BestReactComponent} from 'best-react-component';8const {containerClassNames} = BestReactComponent;9import {BestReactComponent} from 'best-react-component';10const {containerClassNames} = BestReactComponent;11import {BestReactComponent} from 'best-react-component';12const {containerClassNames} = BestReactComponent;13import {BestReactComponent} from 'best-react-component';14const {containerClassNames} = BestReactComponent;15import {BestReactComponent} from 'best-react-component';16const {containerClassNames} = BestReactComponent;17import {BestReactComponent} from 'best-react-component';18const {containerClassNames} = BestReactComponent;19import {BestReactComponent} from 'best-react-component';20const {containerClassNames} = BestReactComponent;21import {BestReactComponent} from 'best-react-component';22const {containerClassNames} = BestReactComponent;23import {BestReactComponent} from 'best-react-component';24const {containerClassNames} = BestReactComponent;
Using AI Code Generation
1BestInPlaceEditor.prototype.containerClassNames = function (classNames, add) {2 var $container = this.getElement().closest('.best_in_place_container');3 if (add === true) {4 $container.addClass(classNames);5 } else if (add === false) {6 $container.removeClass(classNames);7 } else {8 $container.removeClass();9 $container.addClass(classNames);10 }11};12BestInPlaceEditor.prototype.inputClassNames = function (classNames, add) {13 var $input = this.getElement().closest('.best_in_place_container').find('input');14 if (add === true) {15 $input.addClass(classNames);16 } else if (add === false) {17 $input.removeClass(classNames);18 } else {19 $input.removeClass();20 $input.addClass(classNames);21 }22};23BestInPlaceEditor.prototype.displayClassNames = function (classNames, add) {24 var $display = this.getElement().closest('.best_in_place_container').find('span');25 if (add === true) {26 $display.addClass(classNames);27 } else if (add === false) {28 $display.removeClass(classNames);29 } else {30 $display.removeClass();31 $display.addClass(classNames);32 }33};
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!