Skip to content

SidebarNavHelper.isActive should check child items for dropdowns #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
maxtacco opened this issue Jun 12, 2019 · 16 comments
Closed

SidebarNavHelper.isActive should check child items for dropdowns #67

maxtacco opened this issue Jun 12, 2019 · 16 comments

Comments

@maxtacco
Copy link

Hi,

I have a case when urls of child items do not start with the same path as parent's url. In this scenario item selection is not working as expected. So, is it possible to change SidebarNavHelper.isActive to something like this:

public isActive(router, item) {
    const url = item.url;
    if (this.itemType(item) === 'dropdown') {
      if (url === '' || url === '#') {
        return item.children.some((childItem: any) => this.isActive(router, childItem));
      } else {
        return router.isActive(url, false);
      }
    } else {
      return router.isActive(url, true);
    }
  }
@kafo85
Copy link

kafo85 commented Dec 13, 2019

Hello,
I had the same problem, but in my case i don't have url in the parent item.

So i use this code as modification :

export class AppSidebarNavHelper extends ɵq {

    constructor() {
        super();
    }
    public isActive(router, item) {
        const url = item.url;
        if (this.itemType(item) === 'dropdown') {
            return item.children.some((childItem: any) => this.isActive(router, childItem));
        } else {
            return router.isActive(url, true);
        }
    }
}

And in the "app.module.ts":

AppSidebarNavHelper,
    {
      provide: ɵq,//SidebarNavHelper
      useClass: AppSidebarNavHelper
    }

@WolfgangVonL
Copy link

WolfgangVonL commented Jan 21, 2020

Hi @kafo85

I'm trying to implement your solution but I'm facing some issues. Note that I installed coreui through npm.

import { eq } from '@coreui/angular';
export class AppSidebarNavHelper extends ɵq {  
    constructor() {
        super();
    }
    public isActive(router, item) {
        const url = item.url;
        if (this.itemType(item) === 'dropdown') {
            return item.children.some((childItem: any) => this.isActive(router, childItem));
        } else {
            return router.isActive(url, true);
        }
    }
}
@NgModule({
...
    providers: [
        AppSidebarNavHelper,
        {
            provide: ɵq,//SidebarNavHelper
            useClass: AppSidebarNavHelper
        } 
    ]
})

But I get the following error:

export 'eq' was not found in '@coreui/angular'

I have been using Angular recently so I guess I missed something...
Could you explain me how you managed to extend the coreui SidebarNavHelper ?

Thanks

@xidedix
Copy link
Member

xidedix commented Jan 21, 2020

@WolfgangVonL
it is ɵ Latin small letter barred o (U+0275) not e

Unfortunately, it is not safe since the name ɵq can change with every build of @coreui/angular

Any class members prefixed with barred latin o (ɵ), double-barred latin o (ɵɵ), underscore (_) or symbols marked as private are not considered to be a public API surface.

@WolfgangVonL
Copy link

OK...
I was taught not to foolishly copy and paste but to rewrite to understand what I was doing ! Haha

Anyway, many thanks @xidedix !

@xidedix xidedix mentioned this issue Jan 21, 2020
@xidedix
Copy link
Member

xidedix commented Jan 21, 2020

@WolfgangVonL @kafo85

v2.7.4 adds SidebarNavHelper to the public-api surface, you can safely:

  • import
import { SidebarNavHelper } from '@coreui/angular';
  • extend
export class AppSidebarNavHelper extends SidebarNavHelper {
  ...
}

@WolfgangVonL
Copy link

WolfgangVonL commented Jan 21, 2020

Thanks @xidedix !

Is it also possible to extend directive from source code ?

When opening a sidebar nav dropdown, I'd like to close the others but I can't do that from SidebarNavHelper.
I've found the directive (NavDropdrownDirective) which deals with the "open" class and I would to extend it to owerwrite the toggle function with:

let children = this.el.nativeElement.parentNode.children
for(let i=0; i < children.length; i++){
    children[i].classList.remove('open')
}
this.el.nativeElement.classList.toggle('open')

My code is working but I can't overwrite the Directive. I've tried that

@Directive({
    selector: '[appNavDropdown]
})
class AppNavDropdownDirective extend ɵx {
    private _el;
    constructor(_el: ElementRef){
        super(_el)
    }
    toggle(){
        ... my code...
    }
}

@NgModule({
    declarations: [
        AppNavDropdownDirective 
    ]
})

No errors but my directive « extension » doesn’t seem to be fired.

@kriefsacha
Copy link

Please i have an error "Property 'itemType' does not exist on type 'AppSidebarNavHelper'. What is this.itemType ??

@kriefsacha
Copy link

@xidedix also after your comment i have an error :

"Module not found: Error: Can't resolve '@coreui/angular/lib/sidebar/app-sidebar-nav.service' in '....'"

@xidedix
Copy link
Member

xidedix commented Jan 22, 2020

@kriefsacha what's your version of @coreui/angular?

@kriefsacha
Copy link

@xidedix 2.7.4

@xidedix
Copy link
Member

xidedix commented Jan 22, 2020

@WolfgangVonL

Is it also possible to extend directive from source code?

I am afraid it's not possible for directives (afaik).

@WolfgangVonL
Copy link

WolfgangVonL commented Jan 22, 2020

Thanks @xidedix

Do you think of another way to do that properly ?

Or is there any chance that in a future release this feature would be directly implemented in @coreui/angular ?

@xidedix
Copy link
Member

xidedix commented Jan 23, 2020

@WolfgangVonL

This feature will be available as an option in the upcoming CoreUI v3 for Angular.

@kriefsacha
Copy link

Do we know when the v3 will be out ? And how do we update from v2 to v3 ?

@kafo85
Copy link

kafo85 commented Mar 15, 2022

Since V4, it's working without extending "SidebarNavHelper"

For me, you can close this issue...

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants