@@ -24,7 +24,7 @@ import select2 from 'select2'; //eslint-disable-line no-unused-vars
2424@customElement ( 'select-picker' )
2525export class Select2Picker {
2626
27- @bindable ( { defaultBindingMode : bindingMode . twoWay } ) value ;
27+ @bindable ( { defaultBindingMode : bindingMode . twoWay } ) value ;
2828 @bindable valueType = 'Number' ;
2929 @bindable valueProperty = 'id' ;
3030 @bindable labelProperty = 'name' ;
@@ -51,37 +51,37 @@ export class Select2Picker {
5151
5252 let options = {
5353 placeholder : caption ,
54- allowClear : true
55- } ;
54+ allowClear : true
55+ } ;
5656
57- if ( typeof this . config . filterSearch !== 'undefined' && this . config . filterSearch === false ) {
57+ if ( typeof this . config . filterSearch !== 'undefined' && this . config . filterSearch === false ) {
5858 options . minimumResultsForSearch = Infinity ;
5959 }
6060
61- if ( this . config . valueProperty ) {
61+ if ( this . config . valueProperty ) {
6262 this . valueProperty = this . config . valueProperty ;
6363 }
64- if ( this . config . labelProperty ) {
64+ if ( this . config . labelProperty ) {
6565 this . labelProperty = this . config . labelProperty ;
6666 }
67- if ( this . config . valueType ) {
67+ if ( this . config . valueType ) {
6868 this . valueType = this . config . valueType ;
6969 }
70- if ( typeof this . config . allowClear !== 'undefined' ) {
70+ if ( typeof this . config . allowClear !== 'undefined' ) {
7171 options . allowClear = this . config . allowClear ;
7272 }
7373
74- if ( typeof this . config . noSearch !== 'undefined' ) {
74+ if ( typeof this . config . noSearch !== 'undefined' ) {
7575 options . minimumResultsForSearch = Infinity ;
7676 }
7777
78- if ( this . config . id ) {
78+ if ( this . config . id ) {
7979 this . id = this . config . id ;
8080 } else {
81- this . id = ' select-' + parseInt ( Math . random ( ) * 16384 , 10 ) ;
81+ this . id = ` select-${ parseInt ( Math . random ( ) * 16384 , 10 ) } ` ;
8282 }
8383
84- if ( this . config . multiple === true || this . multiple === 'true' ) {
84+ if ( _ . get ( this , ' config.multiple' ) === true || _ . get ( this , ' multiple' ) === 'true' ) {
8585 options . multiple = true ;
8686 this . multiple = true ;
8787 }
@@ -96,7 +96,7 @@ export class Select2Picker {
9696 this . select2 = $select . select2 ( options ) ;
9797
9898 let tabIndex = this . config . tabIndex ;
99- if ( typeof tabIndex === 'undefined' ) {
99+ if ( typeof tabIndex === 'undefined' ) {
100100 tabIndex = - 1 ;
101101 }
102102
@@ -109,9 +109,9 @@ export class Select2Picker {
109109
110110 unbind ( ) {
111111 let select = this . element . firstElementChild ;
112- if ( select ) {
112+ if ( select ) {
113113 const $select = $ ( select ) ;
114- if ( $select . data ( 'select2' ) ) { // depending on the destory state need to check whether it still thinks it is a select2
114+ if ( $select . data ( 'select2' ) ) { // depending on the destroyed state need to check whether it still thinks it is a select2
115115 $select . select2 ( 'destroy' ) ;
116116 }
117117 }
@@ -122,28 +122,29 @@ export class Select2Picker {
122122 * @param e selection event.
123123 */
124124 onSelect ( e ) {
125- if ( e . params && e . params . data ) {
126- let data = e . params . data . id ;
127- if ( this . multiple === true ) {
128- let val = data ;
129- data = ( this . value ) ? _ . clone ( this . value ) : this . _getClearedValue ( ) ;
130- data . push ( val ) ;
131- this . value = _ . uniq ( data ) ; // prevent duplicates
132- } else if ( this . valueType === 'Number' ) {
133- this . value = data ? Number . parseInt ( data ) : this . _getClearedValue ( ) ;
125+ if ( e . params && e . params . data ) {
126+ let data = _ . get ( e , 'params.data.id' ) ;
127+ let originValue = this . value ;
128+ if ( this . multiple === true ) {
129+ originValue . push ( data ) ;
130+ this . value = this . _getClearedValue ( ) ;
131+ } else if ( this . valueType === 'Number' ) {
132+ originValue = data ? Number . parseInt ( data ) : this . _getClearedValue ( ) ;
134133 } else {
135- this . value = data ;
134+ originValue = data ;
136135 }
137- let changeEvent = EventHelper . changeEvent ( data ) ;
138- this . element . dispatchEvent ( changeEvent ) ;
136+ _ . defer ( ( ) => { // need to tickle the array
137+ this . value = originValue ;
138+ this . element . dispatchEvent ( EventHelper . changeEvent ( this . value ) ) ;
139+ } ) ;
139140 }
140141 }
141142
142143 _getClearedValue ( ) {
143144 let value = '' ;
144- if ( this . multiple === true ) {
145+ if ( this . multiple === true ) {
145146 value = [ ] ;
146- } else if ( this . valueType === 'Number' ) {
147+ } else if ( this . valueType === 'Number' ) {
147148 value = - 1 ;
148149 }
149150 return value ;
@@ -157,15 +158,27 @@ export class Select2Picker {
157158 return item [ this . labelProperty ] ;
158159 }
159160
160- onUnselect ( e ) {
161+ onUnselect ( ) {
161162 _ . defer ( ( ) => {
162163 $ ( this . select2 ) . off ( 'select2:opening' ) ;
163164 } ) ;
164165 }
165166
166167 onUnselecting ( e ) {
167- this . value = this . _getClearedValue ( ) ;
168- this . element . dispatchEvent ( EventHelper . changeEvent ( this . value ) ) ;
168+ let removalValue = _ . get ( e , 'params.args.data' ) ;
169+ let origValue = this . value ;
170+ if ( removalValue ) {
171+ if ( this . multiple ) {
172+ this . value = this . _getClearedValue ( ) ;
173+ _ . pull ( origValue , _ . get ( removalValue , 'id' ) ) ;
174+ } else {
175+ origValue = this . _getClearedValue ( ) ;
176+ }
177+ }
178+ _ . defer ( ( ) => {
179+ this . value = origValue ;
180+ this . element . dispatchEvent ( EventHelper . changeEvent ( origValue ) ) ;
181+ } ) ;
169182
170183 $ ( this . select2 ) . on ( 'select2:opening' , e => {
171184 e . preventDefault ( ) ;
@@ -198,11 +211,11 @@ export class Select2Picker {
198211 * Handle the conversion of the type from the modeled value to the String equivalence of the value used by select2
199212 */
200213 _convertToInternal ( val ) {
201- if ( typeof val === 'undefined' || _ . isNil ( val ) ) {
214+ if ( typeof val === 'undefined' || _ . isNil ( val ) ) {
202215 return undefined ;
203216 }
204217 let retValue = val ;
205- switch ( this . valueType ) {
218+ switch ( this . valueType ) {
206219 case 'Number' :
207220 retValue = val . toString ( ) ;
208221 break ;
@@ -215,9 +228,9 @@ export class Select2Picker {
215228 */
216229 attached ( ) {
217230 _ . debounce ( ( ) => {
218- if ( this . items && this . items . length > 0 ) { // cached items are loaded
219- this . valueChanged ( this . value ) ;
220- }
231+ if ( this . items && this . items . length > 0 ) { // cached items are loaded
232+ this . valueChanged ( this . value ) ;
233+ }
221234 } , 125 ) ( ) ;
222235 }
223236
@@ -229,7 +242,7 @@ export class Select2Picker {
229242 * @param oldValue
230243 */
231244 itemsChanged ( newValue , oldValue ) { //eslint-disable-line no-unused-vars
232- if ( newValue && newValue . length > 0 ) {
245+ if ( newValue && newValue . length > 0 ) {
233246 _ . defer ( ( ) => {
234247 this . valueChanged ( this . value ) ;
235248 } ) ;
0 commit comments