@@ -4,17 +4,17 @@ function init(Survey, $) {
44 name : "tagbox" ,
55 title : "Tag box" ,
66 iconName : "icon-tagbox" ,
7- widgetIsLoaded : function ( ) {
7+ widgetIsLoaded : function ( ) {
88 return typeof $ == "function" && ! ! $ . fn . select2 ;
99 } ,
1010 defaultJSON : {
1111 choices : [ "Item 1" , "Item 2" , "Item 3" ]
1212 } ,
1313 htmlTemplate : "<select multiple='multiple' style='width: 100%;'></select>" ,
14- isFit : function ( question ) {
14+ isFit : function ( question ) {
1515 return question . getType ( ) === "tagbox" ;
1616 } ,
17- activatedByChanged : function ( activatedBy ) {
17+ activatedByChanged : function ( activatedBy ) {
1818 Survey . JsonObject . metaData . addClass (
1919 "tagbox" ,
2020 [
@@ -41,16 +41,18 @@ function init(Survey, $) {
4141 ]
4242 } ;
4343 } ,
44- fixStyles : function ( el ) {
44+ fixStyles : function ( el ) {
4545 el . parentElement . querySelector ( ".select2-search__field" ) . style . border =
4646 "none" ;
4747 } ,
48- afterRender : function ( question , el ) {
48+ afterRender : function ( question , el ) {
4949 var self = this ;
5050 var select2Config = question . select2Config ;
51- var settings = select2Config && typeof select2Config == 'string' ? JSON . parse ( select2Config ) : select2Config ;
51+ var settings =
52+ select2Config && typeof select2Config == "string"
53+ ? JSON . parse ( select2Config )
54+ : select2Config ;
5255 var $el = $ ( el ) . is ( "select" ) ? $ ( el ) : $ ( el ) . find ( "select" ) ;
53-
5456 self . willUnmount ( question , el ) ;
5557
5658 $el . select2 ( {
@@ -60,19 +62,25 @@ function init(Survey, $) {
6062 } ) ;
6163
6264 self . fixStyles ( el ) ;
63-
64- var updateValueHandler = function ( ) {
65- $el . val ( question . value ) . trigger ( "change" ) ;
65+ var question ;
66+ var updateValueHandler = function ( ) {
67+ if ( question . hasSelectAll && question . isAllSelected ) {
68+ $el
69+ . val ( [ question . selectAllItemValue . value ] . concat ( question . value ) )
70+ . trigger ( "change" ) ;
71+ } else {
72+ $el . val ( question . value ) . trigger ( "change" ) ;
73+ }
6674 self . fixStyles ( el ) ;
6775 } ;
68- var updateChoices = function ( ) {
76+ var updateChoices = function ( ) {
6977 $el . select2 ( ) . empty ( ) ;
7078
7179 if ( settings ) {
7280 if ( settings . ajax ) {
7381 $el . select2 ( settings ) ;
7482 } else {
75- settings . data = question . visibleChoices . map ( function ( choice ) {
83+ settings . data = question . visibleChoices . map ( function ( choice ) {
7684 return {
7785 id : choice . value ,
7886 text : choice . text
@@ -82,44 +90,53 @@ function init(Survey, $) {
8290 }
8391 } else {
8492 $el . select2 ( {
85- data : question . visibleChoices . map ( function ( choice ) {
93+ data : question . visibleChoices . map ( function ( choice ) {
8694 return {
8795 id : choice . value ,
8896 text : choice . text
8997 } ;
9098 } )
9199 } ) ;
92100 }
93-
94101 updateValueHandler ( ) ;
95102 } ;
96-
97- question . _propertyValueChangedFnSelect2 = function ( ) {
103+ var isAllItemSelected = function ( value ) {
104+ return (
105+ question . hasSelectAll && value === question . selectAllItemValue . value
106+ ) ;
107+ } ;
108+ question . _propertyValueChangedFnSelect2 = function ( ) {
98109 updateChoices ( ) ;
99110 } ;
100111
101- question . readOnlyChangedCallback = function ( ) {
112+ question . readOnlyChangedCallback = function ( ) {
102113 $el . prop ( "disabled" , question . isReadOnly ) ;
103114 } ;
104115 question . registerFunctionOnPropertyValueChanged (
105116 "visibleChoices" ,
106117 question . _propertyValueChangedFnSelect2
107118 ) ;
108119 question . valueChangedCallback = updateValueHandler ;
109- $el . on ( "select2:select" , function ( e ) {
110- question . value = ( question . value || [ ] ) . concat ( e . params . data . id ) ;
120+ $el . on ( "select2:select" , function ( e ) {
121+ if ( isAllItemSelected ( e . params . data . id ) ) {
122+ question . selectAll ( ) ;
123+ } else {
124+ question . value = ( question . value || [ ] ) . concat ( e . params . data . id ) ;
125+ }
111126 } ) ;
112- $el . on ( "select2:unselect" , function ( e ) {
127+ $el . on ( "select2:unselect" , function ( e ) {
113128 var index = ( question . value || [ ] ) . indexOf ( e . params . data . id ) ;
114- if ( index !== - 1 ) {
129+ if ( isAllItemSelected ( e . params . data . id ) ) {
130+ question . clearValue ( ) ;
131+ } else if ( index !== - 1 ) {
115132 var val = [ ] . concat ( question . value ) ;
116133 val . splice ( index , 1 ) ;
117134 question . value = val ;
118135 }
119136 } ) ;
120137 updateChoices ( ) ;
121138 } ,
122- willUnmount : function ( question , el ) {
139+ willUnmount : function ( question , el ) {
123140 if ( ! question . _propertyValueChangedFnSelect2 ) return ;
124141
125142 $ ( el )
0 commit comments