@@ -169,7 +169,7 @@ window.ComponentDot = class {
169
169
delete : 'delete'
170
170
} ;
171
171
172
- constructor ( name , selected , select , limit = 0 , menu_mode = false ) {
172
+ constructor ( name , selected , select , limit = 0 , menu_mode = false , menu_placeholder = '' ) {
173
173
if ( ! Array . isArray ( selected ) ) {
174
174
console . error ( 'Dot param selected must be array!' ) ;
175
175
return ;
@@ -178,6 +178,7 @@ window.ComponentDot = class {
178
178
console . error ( 'Dot param select must be object such as {key:val,key2:val2,...} !' ) ;
179
179
return ;
180
180
}
181
+ this . select = select ;
181
182
this . name = name ;
182
183
this . limit = limit ;
183
184
this . DOM = document . getElementById ( name ) ;
@@ -190,19 +191,26 @@ window.ComponentDot = class {
190
191
this . insert_data = [ ] ;
191
192
this . delete_data = [ ] ;
192
193
194
+ setTimeout ( ( ) => {
195
+ let queue = [ ] ;
196
+ this . CONTENT_DOM . childNodes . forEach ( ( D ) => {
197
+ let id = parseInt ( D . getAttribute ( 'data-id' ) ) ;
198
+ if ( selected . indexOf ( id ) !== - 1 ) {
199
+ queue . push ( D ) ;
200
+ }
201
+ } ) ;
202
+ queue . forEach ( ( D ) => D . click ( ) ) ;
203
+ } ) ;
193
204
if ( menu_mode === false ) {
194
205
this . make ( selected , select ) ;
195
- for ( let element of this . CONTENT_DOM . getElementsByClassName ( "dlp-label" ) ) {
196
- element . addEventListener ( 'click' , this . tagSelect . bind ( this , element ) , false ) ;
197
- }
198
206
} else {
207
+ this . menu_placeholder = menu_placeholder ;
199
208
this . menuMake ( selected , select ) ;
200
209
}
201
-
202
210
let search = this . DOM . querySelector ( `.dot-search` ) ;
203
211
search . addEventListener ( 'input' , ( ) => {
204
212
setTimeout ( ( ) => {
205
- this . search ( search ) ;
213
+ this . search ( search , menu_mode ) ;
206
214
} , 500 ) ;
207
215
} ) ;
208
216
}
@@ -221,16 +229,9 @@ window.ComponentDot = class {
221
229
this . selectInputDOM = document . querySelector ( `input[name='${ this . name } [select]']` ) ;
222
230
this . insertInputDOM = document . querySelector ( `input[name='${ this . name } [insert]']` ) ;
223
231
this . deleteInputDOM = document . querySelector ( `input[name='${ this . name } [delete]']` ) ;
224
- setTimeout ( ( ) => {
225
- let queue = [ ] ;
226
- this . CONTENT_DOM . childNodes . forEach ( ( D ) => {
227
- let id = parseInt ( D . getAttribute ( 'data-id' ) ) ;
228
- if ( selected . indexOf ( id ) !== - 1 ) {
229
- queue . push ( D ) ;
230
- }
231
- } ) ;
232
- queue . forEach ( ( D ) => D . click ( ) ) ;
233
- } ) ;
232
+ for ( let element of this . CONTENT_DOM . getElementsByClassName ( "dlp-label" ) ) {
233
+ element . addEventListener ( 'click' , this . tagSelect . bind ( this , element ) , false ) ;
234
+ }
234
235
}
235
236
236
237
menuMake ( selected , select ) {
@@ -239,7 +240,7 @@ window.ComponentDot = class {
239
240
240
241
let menu_select = document . createElement ( 'div' ) ;
241
242
menu_select . className = 'dlp-input dlp-dot-menu-select' ;
242
- menu_select . insertAdjacentHTML ( 'afterbegin' , `<div class="dlp dlp-text">测试 </div><div>▼</div>` ) ;
243
+ menu_select . insertAdjacentHTML ( 'afterbegin' , `<div class="dlp dlp-text">${ this . menu_placeholder } </div><div>▼</div>` ) ;
243
244
244
245
let menu_list = document . createElement ( 'div' ) ;
245
246
menu_list . className = 'dlp-input menu-list' ;
@@ -251,11 +252,38 @@ window.ComponentDot = class {
251
252
252
253
let list = document . createElement ( 'div' ) ;
253
254
list . className = 'list dlp-scroll' ;
255
+
256
+ let check = _component . check ;
257
+ check = check . replace ( `width="16" height="16"` , `width="12" height="12"` ) ;
258
+ this . id_line_hash = [ ] ;
259
+ let line = 0 ;
254
260
for ( let id in select ) {
255
261
if ( ! select . hasOwnProperty ( id ) ) continue ;
262
+ this . id_line_hash [ id ] = line ;
263
+ line ++ ;
256
264
let option = document . createElement ( 'div' ) ;
257
265
option . className = 'option' ;
266
+ option . setAttribute ( 'data-id' , id ) ;
258
267
option . insertAdjacentHTML ( 'afterbegin' , `<div class="dlp dlp-text" data-v="${ id } ">${ select [ id ] } </div><div></div>` ) ;
268
+ option . addEventListener ( 'click' , ( ) => {
269
+ id = parseInt ( id ) ;
270
+ if ( this . select_data . indexOf ( id ) !== - 1 ) {
271
+ /*cancel*/
272
+ this . tagCal ( id , this . MODE . delete ) ;
273
+ option . classList . remove ( 'option-active' ) ;
274
+ if ( option . lastChild instanceof HTMLElement ) option . lastChild . innerHTML = '' ;
275
+ this . menuSelect ( select ) ;
276
+ if ( this . select_data . length == 0 ) this . SELECTED_DOM . textContent = this . menu_placeholder ;
277
+ return ;
278
+ }
279
+ if ( this . limit > 0 && this . select_data . length >= this . limit ) {
280
+ list . childNodes [ this . id_line_hash [ this . select_data [ 0 ] . toString ( ) ] ] . click ( ) ;
281
+ }
282
+ option . classList . add ( 'option-active' ) ;
283
+ this . tagCal ( id , this . MODE . insert ) ;
284
+ ( option . lastChild instanceof HTMLElement ) && option . lastChild . insertAdjacentHTML ( 'afterbegin' , check ) ;
285
+ this . menuSelect ( select ) ;
286
+ } , false ) ;
259
287
list . append ( option ) ;
260
288
}
261
289
@@ -269,17 +297,34 @@ window.ComponentDot = class {
269
297
} ) ;
270
298
menu . addEventListener ( 'mouseleave' , ( ) => {
271
299
menu_list . style . display = 'none' ;
300
+ let search = this . DOM . querySelector ( `.dot-search` ) ;
301
+ search . value = '' ;
302
+ for ( let node of this . CONTENT_DOM . childNodes ) {
303
+ node . style . display = 'flex' ;
304
+ }
272
305
} ) ;
273
306
274
307
this . DOM . append ( menu ) ;
275
308
this . DOM . insertAdjacentHTML ( 'beforeend' , `<input name="${ this . name } [select]" value='${ JSON . stringify ( selected ) } ' type="hidden"><input name="${ this . name } [insert]" value="[]" type="hidden"><input name="${ this . name } [delete]" value="[]" type="hidden">` ) ;
276
- this . SELECTED_DOM = document . querySelector ( `#${ this . name } .dlp-dot-menu-select` ) ;
277
- this . CONTENT_DOM = document . querySelector ( `#${ this . name } .option ` ) ;
309
+ this . SELECTED_DOM = document . querySelector ( `#${ this . name } .dlp-dot-menu-select` ) . firstElementChild ;
310
+ this . CONTENT_DOM = document . querySelector ( `#${ this . name } .list ` ) ;
278
311
this . selectInputDOM = document . querySelector ( `input[name='${ this . name } [select]']` ) ;
279
312
this . insertInputDOM = document . querySelector ( `input[name='${ this . name } [insert]']` ) ;
280
313
this . deleteInputDOM = document . querySelector ( `input[name='${ this . name } [delete]']` ) ;
281
314
}
282
315
316
+ menuSelect ( select ) {
317
+ if ( this . limit == 1 ) {
318
+ this . SELECTED_DOM . innerHTML = `<p class="dlp-text">${ select [ this . select_data [ 0 ] ] } </p>` ;
319
+ return ;
320
+ }
321
+ let html = '' ;
322
+ for ( let id of this . select_data ) {
323
+ html += `<span class="dlp-text" title="${ select [ id ] } ">${ select [ id ] } </span>` ;
324
+ }
325
+ this . SELECTED_DOM . innerHTML = html ;
326
+ }
327
+
283
328
tagSelect ( element ) {
284
329
if ( this . limit > 0 && this . select_data . length >= this . limit && this . SELECTED_DOM . firstChild instanceof HTMLElement ) {
285
330
this . SELECTED_DOM . firstChild . click ( ) ;
@@ -288,7 +333,7 @@ window.ComponentDot = class {
288
333
clone . addEventListener ( 'click' , this . tagCancel . bind ( this , clone ) , false ) ;
289
334
this . SELECTED_DOM . appendChild ( clone ) ;
290
335
element . remove ( ) ;
291
- this . tagCal ( element , this . MODE . insert ) ;
336
+ this . tagCal ( parseInt ( element . getAttribute ( 'data-id' ) ) , this . MODE . insert ) ;
292
337
this . SELECTED_DOM . scrollTop = this . SELECTED_DOM . scrollHeight ;
293
338
}
294
339
@@ -297,11 +342,10 @@ window.ComponentDot = class {
297
342
clone . addEventListener ( 'click' , this . tagSelect . bind ( this , clone ) , false ) ;
298
343
this . CONTENT_DOM . appendChild ( clone ) ;
299
344
element . remove ( ) ;
300
- this . tagCal ( element , this . MODE . delete ) ;
345
+ this . tagCal ( parseInt ( element . getAttribute ( 'data-id' ) ) , this . MODE . delete ) ;
301
346
}
302
347
303
- tagCal ( element , operate ) {
304
- let id = parseInt ( element . getAttribute ( 'data-id' ) ) ;
348
+ tagCal ( id , operate ) {
305
349
let index = this . select_data . indexOf ( id ) ;
306
350
if ( operate === this . MODE . insert ) {
307
351
if ( index === - 1 ) {
@@ -334,7 +378,26 @@ window.ComponentDot = class {
334
378
}
335
379
}
336
380
337
- search ( search ) {
381
+ search ( search , menu_mode ) {
382
+ if ( menu_mode ) {
383
+ if ( search . value === '' ) {
384
+ for ( let node of this . CONTENT_DOM . childNodes ) {
385
+ node . style . display = 'flex' ;
386
+ }
387
+ return ;
388
+ }
389
+ for ( let id in this . select ) {
390
+ if ( ! this . select . hasOwnProperty ( id ) ) continue ;
391
+ let text = this . select [ id ] ;
392
+ let line = this . id_line_hash [ id ] ;
393
+ if ( text . indexOf ( search . value ) !== - 1 || search . value . indexOf ( text ) !== - 1 ) {
394
+ this . CONTENT_DOM . childNodes [ line ] . style . display = 'flex' ;
395
+ } else {
396
+ this . CONTENT_DOM . childNodes [ line ] . style . display = 'none' ;
397
+ }
398
+ }
399
+ return ;
400
+ }
338
401
if ( search . value === '' ) {
339
402
if ( this . SELECT_COVER_DOM instanceof HTMLElement ) {
340
403
let elements = [ ] ;
@@ -363,7 +426,7 @@ window.ComponentDot = class {
363
426
if ( element . className . indexOf ( 'dlp-label' ) === - 1 ) {
364
427
continue ;
365
428
}
366
- if ( element . innerText . indexOf ( search . value ) !== - 1 ) {
429
+ if ( element . innerText . indexOf ( search . value ) !== - 1 || search . value . indexOf ( element . innerText ) !== - 1 ) {
367
430
elements . push ( element ) ;
368
431
}
369
432
}
0 commit comments