1- import { Component , Input , Output , EventEmitter , ElementRef , OnInit , OnDestroy } from 'angular2/core' ;
1+ import { Component , Input , Output , EventEmitter , ElementRef , OnInit } from 'angular2/core' ;
22import { SelectItem } from './select-item' ;
33import { HighlightPipe , stripTags } from './select-pipes' ;
44import { OptionsBehavior } from './select-interfaces' ;
55import { escapeRegexp } from './common' ;
6+ import { OffClickDirective } from './off-click' ;
67
78let optionsTemplate = `
8- <ul *ngIf="optionsOpened && options && options.length > 0 && !itemObjects[0].hasChildren() "
9+ <ul *ngIf="optionsOpened && options && options.length > 0 && !firstItemHasChildren "
910 class="ui-select-choices ui-select-choices-content ui-select-dropdown dropdown-menu">
1011 <li class="ui-select-choices-group">
1112 <div *ngFor="#o of options"
@@ -20,7 +21,7 @@ let optionsTemplate = `
2021 </li>
2122 </ul>
2223
23- <ul *ngIf="optionsOpened && options && options.length > 0 && itemObjects[0].hasChildren() "
24+ <ul *ngIf="optionsOpened && options && options.length > 0 && firstItemHasChildren "
2425 class="ui-select-choices ui-select-choices-content ui-select-dropdown dropdown-menu">
2526 <li *ngFor="#c of options; #index=index" class="ui-select-choices-group">
2627 <div class="divider" *ngIf="index > 0"></div>
@@ -41,18 +42,20 @@ let optionsTemplate = `
4142` ;
4243@Component ( {
4344 selector : 'ng-select' ,
45+ directives : [ OffClickDirective ] ,
4446 pipes : [ HighlightPipe ] ,
4547 template : `
4648 <div tabindex="0"
4749 *ngIf="multiple === false"
4850 (keyup)="mainClick($event)"
51+ [offClick]="clickedOutside"
4952 class="ui-select-container ui-select-bootstrap dropdown open">
5053 <div [ngClass]="{'ui-disabled': disabled}"></div>
5154 <div class="ui-select-match"
5255 *ngIf="!inputMode">
5356 <span tabindex="-1"
5457 class="btn btn-default btn-secondary form-control ui-select-toggle"
55- (^ click)="matchClick()"
58+ (click)="matchClick($event )"
5659 style="outline: 0;">
5760 <span *ngIf="active.length <= 0" class="ui-select-placeholder text-muted">{{placeholder}}</span>
5861 <span *ngIf="active.length > 0" class="ui-select-match-text pull-left"
@@ -110,7 +113,7 @@ let optionsTemplate = `
110113 </div>
111114 `
112115} )
113- export class Select implements OnInit , OnDestroy {
116+ export class Select implements OnInit {
114117 @Input ( ) public allowClear :boolean = false ;
115118 @Input ( ) public placeholder :string = '' ;
116119 @Input ( ) public initData :Array < any > = [ ] ;
@@ -141,7 +144,6 @@ export class Select implements OnInit, OnDestroy {
141144 public activeOption :SelectItem ;
142145 public element :ElementRef ;
143146
144- private offSideClickHandler :any ;
145147 private inputMode :boolean = false ;
146148 private optionsOpened :boolean = false ;
147149 private behavior :OptionsBehavior ;
@@ -151,6 +153,7 @@ export class Select implements OnInit, OnDestroy {
151153
152154 public constructor ( element :ElementRef ) {
153155 this . element = element ;
156+ this . clickedOutside = this . clickedOutside . bind ( this ) ;
154157 }
155158
156159 public inputEvent ( e :any , isUpMode :boolean = false ) :void {
@@ -229,21 +232,14 @@ export class Select implements OnInit, OnDestroy {
229232 }
230233
231234 public ngOnInit ( ) :any {
232- this . behavior = this . itemObjects [ 0 ] . hasChildren ( ) ?
235+ this . behavior = ( this . firstItemHasChildren ) ?
233236 new ChildrenBehavior ( this ) : new GenericBehavior ( this ) ;
234- this . offSideClickHandler = this . getOffSideClickHandler ( this ) ;
235- document . addEventListener ( 'click' , this . offSideClickHandler ) ;
236237 if ( this . initData ) {
237238 this . active = this . initData . map ( ( data :any ) => new SelectItem ( data ) ) ;
238239 this . data . emit ( this . active ) ;
239240 }
240241 }
241242
242- public ngOnDestroy ( ) :any {
243- document . removeEventListener ( 'click' , this . offSideClickHandler ) ;
244- this . offSideClickHandler = void 0 ;
245- }
246-
247243 public remove ( item :SelectItem ) :void {
248244 if ( this . _disabled === true ) {
249245 return ;
@@ -267,6 +263,15 @@ export class Select implements OnInit, OnDestroy {
267263 }
268264 }
269265
266+ public clickedOutside ( ) :void {
267+ this . inputMode = false ;
268+ this . optionsOpened = false ;
269+ }
270+
271+ public get firstItemHasChildren ( ) :boolean {
272+ return this . itemObjects [ 0 ] && this . itemObjects [ 0 ] . hasChildren ( ) ;
273+ }
274+
270275 protected matchClick ( e :any ) :void {
271276 if ( this . _disabled === true ) {
272277 return ;
@@ -337,27 +342,6 @@ export class Select implements OnInit, OnDestroy {
337342 this . optionsOpened = true ;
338343 }
339344
340- private getOffSideClickHandler ( context :any ) :Function {
341- return function ( e :any ) :void {
342- if ( e . target && e . target . nodeName === 'INPUT'
343- && e . target . className && e . target . className . indexOf ( 'ui-select' ) >= 0 ) {
344- return ;
345- }
346-
347- if ( e . srcElement . contains ( context . element . nativeElement )
348- && e . srcElement && e . srcElement . className &&
349- e . srcElement . className . indexOf ( 'ui-select' ) >= 0 ) {
350- if ( e . target . nodeName !== 'INPUT' ) {
351- context . matchClick ( void 0 ) ;
352- }
353- return ;
354- }
355-
356- context . inputMode = false ;
357- context . optionsOpened = false ;
358- } ;
359- }
360-
361345 private hideOptions ( ) :void {
362346 this . inputMode = false ;
363347 this . optionsOpened = false ;
0 commit comments