1- 'use strict' ;
2-
3- var directiveModule = angular . module ( 'angularjs-dropdown-multiselect' , [ ] ) ;
4-
5- directiveModule . run ( [ '$templateCache' , function ( $templateCache )
6- {
7- var template = '<div class="multiselect-parent btn-group dropdown-multiselect" data-ng-class="{open: open}">' ;
8- template += '<button type="button" class="btn btn-default dropdown-toggle" data-ng-click="open=!open;">{{getButtonText()}}<span class="caret"></span></button>' ;
9- template += '<ul class="dropdown-menu">' ;
10- template += '<li><a data-ng-click="selectAll()"><span class="glyphicon glyphicon-ok"></span> Check All</a>' ;
11- template += '<li><a data-ng-click="deselectAll();"><span class="glyphicon glyphicon-remove"></span> Uncheck All</a></li>' ;
12- template += '<li class="divider"></li>' ;
13- template += '<li data-ng-repeat="option in options"><a data-ng-click="setSelectedItem(getPropertyForObject(option,settings.idProp))"><span data-ng-class="isChecked(getPropertyForObject(option,settings.idProp))"></span>{{getPropertyForObject(option, settings.displayProp)}}</a></li>' ;
14- template += '</ul>' ;
15- template += '</div>' ;
16-
17- $templateCache . put ( 'dropdown-multiselect-template.html' , template ) ;
18- } ] ) ;
19-
20- directiveModule . directive ( 'ngDropdownMultiselect' , [ '$filter' , '$document' , function ( $filter , $document ) {
21-
22- return {
23- restrict : 'AE' ,
24- scope :{
25- selectedModel : '=' ,
26- options : '=' ,
27- extraSettings : '='
28- } ,
29- templateUrl : 'dropdown-multiselect-template.html' ,
30- link : function ( $scope , $element ) {
31- $scope . settings = {
32- dynamicTitle : true ,
33- defaultText : 'Select' ,
34- closeOnBlur : true ,
35- displayProp : 'label' ,
36- idProp : 'id' ,
37- externalIdProp : 'id' } ;
38-
39- angular . extend ( $scope . settings , $scope . extraSettings || [ ] ) ;
40-
41- if ( $scope . settings . closeOnBlur ) {
42- $document . on ( 'click' , function ( e ) {
43- var target = e . target . parentElement ;
44- var parentFound = false ;
45-
46- while ( angular . isDefined ( target ) && target != null && ! parentFound ) {
47- if ( _ . contains ( target . classList , 'multiselect-parent' ) && ! parentFound ) {
48- parentFound = true ;
49- }
50- target = target . parentElement ;
51- }
52-
53- if ( ! parentFound ) {
54- $scope . $apply ( function ( ) {
55- $scope . open = false ;
56- } ) ;
57- }
58- } ) ;
59- }
60-
61- $scope . getButtonText = function ( )
62- {
63- if ( $scope . settings . dynamicTitle )
64- {
65- var totalSelected = angular . isDefined ( $scope . selectedModel ) ? $scope . selectedModel . length : 0 ;
66-
67- if ( totalSelected === 0 )
68- {
69- return $scope . settings . defaultText ;
70- }
71- else
72- {
73- return totalSelected + ' selected' ;
74- }
75- }
76- else
77- {
78- return $scope . settings . defaultText ;
79- }
80- }
81-
82-
83- $scope . getPropertyForObject = function ( object , property )
84- {
85- if ( object . hasOwnProperty ( property ) ) {
86- return object [ property ] ;
87- }
88-
89- return '' ;
90- } ;
91-
92- $scope . selectAll = function ( ) {
93- $scope . deselectAll ( ) ;
94-
95- angular . forEach ( $scope . options , function ( value )
96- {
97- $scope . setSelectedItem ( value [ $scope . settings . idProp ] , true ) ;
98- } ) ;
99- } ;
100-
101- $scope . deselectAll = function ( ) {
102- $scope . selectedModel = [ ] ;
103- } ;
104-
105- $scope . setSelectedItem = function ( id , dontRemove ) {
106- dontRemove = dontRemove || false ;
107- var findObj = { } ;
108- findObj [ $scope . settings . externalIdProp ] = id ;
109-
110- var exists = _ . findIndex ( $scope . selectedModel , findObj ) !== - 1 ;
111-
112- if ( ! dontRemove && exists ) {
113- $scope . selectedModel . splice ( _ . findIndex ( $scope . selectedModel , findObj ) , 1 ) ;
114- } else if ( ! exists ) {
115- $scope . selectedModel . push ( findObj ) ;
116- }
117-
118- return false ;
119- } ;
120-
121- $scope . isChecked = function ( id ) {
122- var findObj = { } ;
123- findObj [ $scope . settings . externalIdProp ] = id ;
124-
125- if ( _ . findIndex ( $scope . selectedModel , findObj ) !== - 1 ) {
126- return 'glyphicon glyphicon-ok' ;
127- }
128- return '' ;
129- } ;
130- }
131- } ;
1+ 'use strict' ;
2+
3+ var directiveModule = angular . module ( 'angularjs-dropdown-multiselect' , [ ] ) ;
4+
5+ directiveModule . run ( [ '$templateCache' , function ( $templateCache )
6+ {
7+ var template = '<div class="multiselect-parent btn-group dropdown-multiselect" data-ng-class="{open: open}">' ;
8+ template += '<button type="button" class="btn btn-default dropdown-toggle" data-ng-click="open=!open;">{{getButtonText()}}<span class="caret"></span></button>' ;
9+ template += '<ul class="dropdown-menu">' ;
10+ template += '<li><a data-ng-click="selectAll()"><span class="glyphicon glyphicon-ok"></span> Check All</a>' ;
11+ template += '<li><a data-ng-click="deselectAll();"><span class="glyphicon glyphicon-remove"></span> Uncheck All</a></li>' ;
12+ template += '<li class="divider"></li>' ;
13+ template += '<li data-ng-repeat="option in options"><a data-ng-click="setSelectedItem(getPropertyForObject(option,settings.idProp))"><span data-ng-class="isChecked(getPropertyForObject(option,settings.idProp))"></span>{{getPropertyForObject(option, settings.displayProp)}}</a></li>' ;
14+ template += '</ul>' ;
15+ template += '</div>' ;
16+
17+ $templateCache . put ( 'dropdown-multiselect-template.html' , template ) ;
18+ } ] ) ;
19+
20+ directiveModule . directive ( 'ngDropdownMultiselect' , [ '$filter' , '$document' , function ( $filter , $document ) {
21+
22+ return {
23+ restrict : 'AE' ,
24+ scope :{
25+ selectedModel : '=' ,
26+ options : '=' ,
27+ extraSettings : '='
28+ } ,
29+ templateUrl : 'dropdown-multiselect-template.html' ,
30+ link : function ( $scope , $element ) {
31+ $scope . settings = {
32+ dynamicTitle : true ,
33+ defaultText : 'Select' ,
34+ closeOnBlur : true ,
35+ displayProp : 'label' ,
36+ idProp : 'id' ,
37+ externalIdProp : 'id' } ;
38+
39+ angular . extend ( $scope . settings , $scope . extraSettings || [ ] ) ;
40+
41+ if ( $scope . settings . closeOnBlur ) {
42+ $document . on ( 'click' , function ( e ) {
43+ var target = e . target . parentElement ;
44+ var parentFound = false ;
45+
46+ while ( angular . isDefined ( target ) && target != null && ! parentFound ) {
47+ if ( _ . contains ( target . classList , 'multiselect-parent' ) && ! parentFound ) {
48+ parentFound = true ;
49+ }
50+ target = target . parentElement ;
51+ }
52+
53+ if ( ! parentFound ) {
54+ $scope . $apply ( function ( ) {
55+ $scope . open = false ;
56+ } ) ;
57+ }
58+ } ) ;
59+ }
60+
61+ $scope . getButtonText = function ( )
62+ {
63+ if ( $scope . settings . dynamicTitle )
64+ {
65+ var totalSelected = angular . isDefined ( $scope . selectedModel ) ? $scope . selectedModel . length : 0 ;
66+
67+ if ( totalSelected === 0 )
68+ {
69+ return $scope . settings . defaultText ;
70+ }
71+ else
72+ {
73+ return totalSelected + ' selected' ;
74+ }
75+ }
76+ else
77+ {
78+ return $scope . settings . defaultText ;
79+ }
80+ }
81+
82+
83+ $scope . getPropertyForObject = function ( object , property )
84+ {
85+ if ( object . hasOwnProperty ( property ) ) {
86+ return object [ property ] ;
87+ }
88+
89+ return '' ;
90+ } ;
91+
92+ $scope . selectAll = function ( ) {
93+ $scope . deselectAll ( ) ;
94+
95+ angular . forEach ( $scope . options , function ( value )
96+ {
97+ $scope . setSelectedItem ( value [ $scope . settings . idProp ] , true ) ;
98+ } ) ;
99+ } ;
100+
101+ $scope . deselectAll = function ( ) {
102+ $scope . selectedModel = [ ] ;
103+ } ;
104+
105+ $scope . setSelectedItem = function ( id , dontRemove ) {
106+ dontRemove = dontRemove || false ;
107+ var findObj = { } ;
108+ findObj [ $scope . settings . externalIdProp ] = id ;
109+
110+ var exists = _ . findIndex ( $scope . selectedModel , findObj ) !== - 1 ;
111+
112+ if ( ! dontRemove && exists ) {
113+ $scope . selectedModel . splice ( _ . findIndex ( $scope . selectedModel , findObj ) , 1 ) ;
114+ } else if ( ! exists ) {
115+ $scope . selectedModel . push ( findObj ) ;
116+ }
117+
118+ return false ;
119+ } ;
120+
121+ $scope . isChecked = function ( id ) {
122+ var findObj = { } ;
123+ findObj [ $scope . settings . externalIdProp ] = id ;
124+
125+ if ( _ . findIndex ( $scope . selectedModel , findObj ) !== - 1 ) {
126+ return 'glyphicon glyphicon-ok' ;
127+ }
128+ return '' ;
129+ } ;
130+ }
131+ } ;
132132} ] ) ;
0 commit comments