@@ -10,6 +10,7 @@ import * as _ from 'lodash';
10
10
11
11
import { services } from 'typescript-angular-utilities' ;
12
12
import __validation = services . validation ;
13
+ import __object = services . object ;
13
14
14
15
import {
15
16
IComponentValidator ,
@@ -30,24 +31,34 @@ export class SelectController {
30
31
validator : __validation . IValidationHandler ;
31
32
label : string ;
32
33
ngDisabled : boolean ;
34
+ nullOption : string ;
33
35
34
36
ngModel : angular . INgModelController ;
35
37
selectValidator : IComponentValidator ;
36
38
loading : boolean ;
37
39
40
+ private _nullOption : any = {
41
+ __isNullOption : true ,
42
+ } ;
43
+
38
44
get selection ( ) : any {
39
45
return this . ngModel . $viewValue ;
40
46
}
41
47
42
48
set selection ( value : any ) {
43
- this . ngModel . $setViewValue ( value ) ;
49
+ if ( value . __isNullOption ) {
50
+ this . ngModel . $setViewValue ( null ) ;
51
+ } else {
52
+ this . ngModel . $setViewValue ( value ) ;
53
+ }
44
54
}
45
55
46
- static $inject : string [ ] = [ '$element' , '$scope' , '$q' , componentValidatorFactoryName ] ;
56
+ static $inject : string [ ] = [ '$element' , '$scope' , '$q' , componentValidatorFactoryName , __object . serviceName ] ;
47
57
constructor ( $element : angular . IAugmentedJQuery
48
58
, $scope : angular . IScope
49
59
, private $q : angular . IQService
50
- , componentValidatorFactory : IComponentValidatorFactory ) {
60
+ , componentValidatorFactory : IComponentValidatorFactory
61
+ , private object : __object . IObjectUtility ) {
51
62
this . ngModel = $element . controller ( 'ngModel' ) ;
52
63
53
64
if ( _ . isUndefined ( this . options ) ) {
@@ -56,6 +67,8 @@ export class SelectController {
56
67
this . options = options ;
57
68
this . loading = false ;
58
69
} ) ;
70
+ } else {
71
+ this . options = this . configureOptions ( this . options ) ;
59
72
}
60
73
61
74
if ( ! _ . isUndefined ( this . validator ) ) {
@@ -72,17 +85,31 @@ export class SelectController {
72
85
return null ;
73
86
}
74
87
88
+ if ( item . __isNullOption ) {
89
+ return this . nullOption ;
90
+ }
91
+
75
92
return _ . isFunction ( this . selector )
76
93
? ( < { ( item : any ) : string } > this . selector ) ( item )
77
94
: item [ < string > this . selector ] ;
78
95
}
79
96
80
97
loadItems ( ) : angular . IPromise < any [ ] > {
98
+ let promise : angular . IPromise < any [ ] > ;
81
99
if ( _ . isFunction ( this . getOptions ) ) {
82
- return this . getOptions ( ) ;
100
+ promise = this . getOptions ( ) ;
83
101
} else {
84
- return this . $q . when ( this . options ) ;
102
+ promise = this . $q . when ( this . options ) ;
85
103
}
104
+ return promise . then ( ( options : any [ ] ) : any [ ] => { return this . configureOptions ( options ) ; } ) ;
105
+ }
106
+
107
+ configureOptions ( options : any [ ] ) : any [ ] {
108
+ if ( ! this . object . isNullOrWhitespace ( this . nullOption ) ) {
109
+ options . unshift ( this . _nullOption ) ;
110
+ }
111
+
112
+ return options ;
86
113
}
87
114
}
88
115
@@ -101,6 +128,7 @@ export function select(): angular.IDirective {
101
128
validator : '=' ,
102
129
label : '@' ,
103
130
ngDisabled : '=' ,
131
+ nullOption : '@' ,
104
132
} ,
105
133
} ;
106
134
}
0 commit comments