Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Commit

Permalink
fix(dropdown): add directive for clicking outside of an element (#2615)
Browse files Browse the repository at this point in the history
* fix(directive): add directive for clicking outside an element

* fix(selectDropdown): add clickOut directive to Select-dropdown

* fix(datatable): close config when clicked outside

* fix(config): use clickOut directive to close the dropdown
  • Loading branch information
sahil143 authored and nimishamukherjee committed May 20, 2018
1 parent 1dea93a commit 9184140
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ <h4>Please wait, we are loading your data.</h4>
</alm-work-item-quick-add>
</div>
<div class="f8-wi-list__settings f8-wi__table-config">
<div class="dropdown-kebab-pf pull-right dropdown margin-left-10"
<div (clickOut)="clickOut()" class="dropdown-kebab-pf pull-right dropdown margin-left-10"
dropdown
[autoClose]="false"
[isOpen]="isTableConfigOpen"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ export class PlannerListComponent implements OnInit, OnDestroy, AfterViewChecked
this.isTableConfigOpen = false;
}

clickOut() {
if(this.isTableConfigOpen)
this.isTableConfigOpen = false;
}

// End: Setting(tableConfig) Dropdown

togglePanelState(event) {
Expand Down
2 changes: 2 additions & 0 deletions src/app/components_ngrx/planner-list/planner-list.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { AlmIconModule } from 'ngx-widgets';
import { EmptyStateModule } from 'patternfly-ng/empty-state';
import { UrlService } from '../../services/url.service';
import { InfotipService } from '../../services/infotip.service';
import { ClickOutModule } from '../../widgets/clickout/clickout.module';

let providers = [];

Expand Down Expand Up @@ -106,6 +107,7 @@ if (process.env.ENV == 'inmemory') {
AlmIconModule,
AssigneesModule,
CommonModule,
ClickOutModule,
PlannerListRoutingModule,
PlannerLayoutModule,
PlannerModalModule,
Expand Down
49 changes: 49 additions & 0 deletions src/app/widgets/clickout/clickout.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {
Directive,
ElementRef,
EventEmitter,
HostListener,
Inject,
Input,
Output
} from '@angular/core';

@Directive({
selector: '[clickOut]'
})
export class ClickOutDirective {

@Input('exclude') exclude: string;

@Output('clickOut') clickOutside: EventEmitter<any> = new EventEmitter();

@HostListener('document:click', ['$event', '$event.target'])
onClick(event: MouseEvent, target: HTMLElement): void {
if (!target) {
return;
}

const clickedInside = this.element.nativeElement.contains(target);

if (this.exclude) {
this.isExcluded = this.excludeCheck(target);
}

if (!clickedInside && !this.isExcluded) {
this.clickOutside.emit(event);
}
}

private isExcluded: boolean = false;
constructor(private element: ElementRef) {
}

excludeCheck(target: HTMLElement) {
const excludeElements = Array.from(document.querySelectorAll(this.exclude)) as Array<HTMLElement>;
for (let element of excludeElements) {
if(element.contains(target)){
return true;
}
}
}
}
9 changes: 9 additions & 0 deletions src/app/widgets/clickout/clickout.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { NgModule } from "@angular/core";
import { ClickOutDirective } from "./clickout.directive";

@NgModule({
declarations: [ClickOutDirective],
exports: [ClickOutDirective]
})

export class ClickOutModule {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="select-dropdown-container dropdown">
<div (clickOut)="clickOut()" class="select-dropdown-container dropdown">
<span (click)="openDropdown()">
<ng-container *ngTemplateOutlet="toggleButtonRef">
</ng-container>
Expand Down
24 changes: 7 additions & 17 deletions src/app/widgets/select-dropdown/select-dropdown.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,7 @@ export class SelectDropdownComponent implements OnInit {
@Output() onOpen: EventEmitter<any> = new EventEmitter();
@Output() onClose: EventEmitter<any> = new EventEmitter();

@HostListener('document:click', ['$event', '$event.target'])
onClick(event: MouseEvent, target: HTMLElement) :void {
if (this.displayDropdown) {
if (!target) {
return;
}

const clickedInside = this._el.nativeElement.contains(target);

if (!clickedInside) {
this.closeDropdown();
}
}
}

constructor(private _el: ElementRef) {

constructor() {
}


Expand All @@ -69,4 +53,10 @@ export class SelectDropdownComponent implements OnInit {
searchItem(text: string) {
this.onSearch.emit(text);
}

clickOut() {
if(this.displayDropdown) {
this.closeDropdown();
}
}
}
3 changes: 2 additions & 1 deletion src/app/widgets/select-dropdown/select-dropdown.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { SelectDropdownComponent } from './select-dropdown.component';
import { AlmEditableModule } from 'ngx-widgets';
import { ClickOutModule } from '../clickout/clickout.module';

@NgModule({
declarations: [ SelectDropdownComponent ],
imports: [ CommonModule ],
imports: [ CommonModule, ClickOutModule ],
exports: [ SelectDropdownComponent ]
})
export class SelectDropdownModule { }

0 comments on commit 9184140

Please sign in to comment.