@@ -13,8 +13,6 @@ define([
13
13
] , function ( _ , $ , searchTemplate , alert ) {
14
14
'use strict' ;
15
15
16
- var nativeConstructor = $ . fn . multiselect ;
17
-
18
16
$ . widget ( 'mage.multiselect2' , {
19
17
options : {
20
18
mselectContainer : 'section.mselect-list' ,
@@ -25,13 +23,13 @@ define([
25
23
selectedItemsCountClass : 'admin__action-multiselect-items-selected' ,
26
24
currentPage : 1 ,
27
25
lastAppendValue : 0 ,
28
- updateDelay : 500 ,
26
+ updateDelay : 1000 ,
29
27
optionsLoaded : false
30
28
} ,
31
29
32
30
/** @inheritdoc */
33
31
_create : function ( ) {
34
- nativeConstructor . call ( this . element , this . options ) ;
32
+ $ . fn . multiselect . call ( this . element , this . options ) ;
35
33
} ,
36
34
37
35
/** @inheritdoc */
@@ -42,6 +40,7 @@ define([
42
40
this . $wrapper = this . $container . find ( '.' + this . options . mselectItemsWrapperClass ) ;
43
41
this . $item = this . $wrapper . find ( 'div' ) . first ( ) ;
44
42
this . selectedValues = [ ] ;
43
+ this . values = { } ;
45
44
46
45
this . $container . addClass ( this . options . containerClass ) . prepend ( searchTemplate ) ;
47
46
this . $input = this . $container . find ( '.' + this . options . searchInputClass ) ;
@@ -109,15 +108,18 @@ define([
109
108
}
110
109
111
110
this . setFilter ( ) ;
112
- this . emptyMultiselectOptions ( ) ;
113
- this . loadOptions ( 1 ) ;
111
+ this . clearMultiselectOptions ( ) ;
112
+ this . setCurrentPage ( 0 ) ;
113
+ this . loadOptions ( ) ;
114
114
} ,
115
115
116
116
/**
117
117
* Callback for select change event
118
118
*/
119
119
onOptionsChange : function ( ) {
120
120
this . selectedValues = _ . map ( this . domElement . options , function ( option ) {
121
+ this . values [ option . value ] = true ;
122
+
121
123
return option . value ;
122
124
} , this ) ;
123
125
@@ -170,30 +172,32 @@ define([
170
172
/**
171
173
* Load options data.
172
174
*/
173
- loadOptions : function ( page ) {
174
- var self = this ,
175
- requestPage = page || ++ this . options . currentPage ;
175
+ loadOptions : function ( ) {
176
+ var nextPage = this . getCurrentPage ( ) + 1 ;
176
177
177
178
this . $wrapper . trigger ( 'processStart' ) ;
178
179
this . $input . prop ( 'disabled' , true ) ;
179
180
180
181
$ . get ( this . options . nextPageUrl , {
181
- p : requestPage ,
182
+ p : nextPage ,
182
183
s : this . filter
183
- } ) . done ( function ( response ) {
184
+ } )
185
+ . done ( function ( response ) {
184
186
if ( response . success ) {
185
- self . appendOptions ( response . result ) ;
187
+ this . appendOptions ( response . result ) ;
188
+ this . setCurrentPage ( nextPage ) ;
186
189
} else {
187
- self . onError ( response . errorMessage ) ;
190
+ this . onError ( response . errorMessage ) ;
188
191
}
189
- } ) . always ( function ( ) {
190
- self . $wrapper . trigger ( 'processStop' ) ;
191
- self . $input . prop ( 'disabled' , false ) ;
192
+ } . bind ( this ) )
193
+ . always ( function ( ) {
194
+ this . $wrapper . trigger ( 'processStop' ) ;
195
+ this . $input . prop ( 'disabled' , false ) ;
192
196
193
- if ( self . filter ) {
194
- self . $input . focus ( ) ;
197
+ if ( this . filter ) {
198
+ this . $input . focus ( ) ;
195
199
}
196
- } ) ;
200
+ } . bind ( this ) ) ;
197
201
} ,
198
202
199
203
/**
@@ -213,9 +217,12 @@ define([
213
217
}
214
218
215
219
options . forEach ( function ( option ) {
216
- option . selected = this . _isOptionSelected ( option ) ;
217
- divOptions . push ( this . _createMultiSelectOption ( option ) ) ;
218
- this . _setLastAppendOption ( option . value ) ;
220
+ if ( ! this . values [ option . value ] ) {
221
+ this . values [ option . value ] = true ;
222
+ option . selected = this . _isOptionSelected ( option ) ;
223
+ divOptions . push ( this . _createMultiSelectOption ( option ) ) ;
224
+ this . _setLastAppendOption ( option . value ) ;
225
+ }
219
226
} , this ) ;
220
227
221
228
this . $wrapper . append ( divOptions ) ;
@@ -224,8 +231,9 @@ define([
224
231
/**
225
232
* Clear multiselect options
226
233
*/
227
- emptyMultiselectOptions : function ( ) {
234
+ clearMultiselectOptions : function ( ) {
228
235
this . _setLastAppendOption ( 0 ) ;
236
+ this . values = { } ;
229
237
this . $wrapper . empty ( ) ;
230
238
} ,
231
239
@@ -240,6 +248,24 @@ define([
240
248
return this . options . optionsLoaded ;
241
249
} ,
242
250
251
+ /**
252
+ * Setter for current page.
253
+ *
254
+ * @param {Number } page
255
+ */
256
+ setCurrentPage : function ( page ) {
257
+ this . options . currentPage = page ;
258
+ } ,
259
+
260
+ /**
261
+ * Getter for current page.
262
+ *
263
+ * @return {Number }
264
+ */
265
+ getCurrentPage : function ( ) {
266
+ return this . options . currentPage ;
267
+ } ,
268
+
243
269
/**
244
270
* Creates new selected option for select element
245
271
*
0 commit comments