34
34
name : "Dropdown" ,
35
35
36
36
props : {
37
- container_class : "tc-dropdown"
37
+ CONTAINER_CLASS : "tc-dropdown"
38
38
} ,
39
39
40
40
proto : {
59
59
60
60
on_open : function ( instance ) { } ,
61
61
on_close : function ( instance ) { } ,
62
- on_keydown : function ( e ) { }
62
+ on_keydown : function ( e ) { } ,
63
+
64
+ fx : {
65
+ open : function ( $menu ) {
66
+ $menu . slideDown ( 100 ) ;
67
+ } ,
68
+ close : function ( $menu ) {
69
+ $menu . slideUp ( 100 ) ;
70
+ }
71
+ }
63
72
} ,
64
73
65
74
_init : function ( ) {
144
153
generate_from_dom : function ( options ) {
145
154
var me , $items ;
146
155
me = this ;
147
- this . _base . generate_from_dom . apply ( this , arguments ) ;
148
- this . $e . addClass ( field [ this . _name ] . container_class ) ;
156
+ field . _Base . prototype . generate_from_dom . apply ( this , arguments ) ;
157
+ this . $e . addClass ( field [ this . _name ] . CONTAINER_CLASS ) ;
149
158
this . _init_elements ( ) ;
150
159
$items = this . get_items ( true ) ;
151
160
if ( typeof ( this . internal . o . item_handlers ) === "object" ) {
170
179
171
180
generate_from_scratch : function ( options ) {
172
181
this . $e = $ ( "<div></div>" )
173
- . addClass ( field . co . BASE_CLASS + " " + field [ this . _name ] . container_class )
182
+ . addClass ( field . co . BASE_CLASS + " " + field [ this . _name ] . CONTAINER_CLASS )
174
183
. html ( "<div class='" + this . internal . o . display_class + "'></div>" +
175
184
"<div class='" + this . internal . o . menu_class + "'></div>" ) ;
176
185
this . _init_elements ( ) ;
271
280
_set_val : function ( val ) {
272
281
var me , $items ;
273
282
me = this ;
283
+
284
+ if ( val === this . internal . o . empty_val ) {
285
+ this . clear ( ) ;
286
+ return ;
287
+ }
288
+
274
289
$items = this . get_items ( true ) ;
275
290
276
291
$items . each ( function ( i ) {
277
292
var $item = $ ( this ) ;
278
293
if ( $item . attr ( field . co . VALUE ) == val ) {
279
- $items . removeClass ( field . co . ACTIVE_CLASS ) ;
294
+ if ( ! me . internal . multi ) {
295
+ $items . removeClass ( field . co . ACTIVE_CLASS ) ;
296
+ } else {
297
+ $item . children ( "input" ) . attr ( "checked" , true ) ;
298
+ }
280
299
$item . addClass ( field . co . ACTIVE_CLASS ) ;
281
300
me . _update_display ( $item ) ;
282
301
return false ;
296
315
297
316
$active_item = this . get_active_item ( ) ;
298
317
299
- if ( ! $active_item ) {
318
+ if ( ! $active_item || $active_item === this . internal . o . empty_val ) {
300
319
if ( direction === NI . co . directions . UP ) {
301
320
$t = $items . last ( ) ;
302
321
} else {
303
322
$t = $items . first ( ) ;
304
323
}
305
324
} else {
306
- index = $items . index ( $active_item ) ;
325
+ index = $items . index ( $active_item . last ( ) ) ;
307
326
if ( direction === NI . co . directions . UP ) {
308
327
$t = $items . eq ( index - 1 ) ;
309
328
if ( ! $t . length ) {
342
361
}
343
362
344
363
if ( typeof arguments [ 0 ] === "string" ) {
345
- this . elements . display . html ( "<span>" + arguments [ 0 ] + "</span>" ) ;
364
+ this . elements . display . html ( "<span class='" + field . co . MSG_CLASS + "' >"+ arguments [ 0 ] + "</span>" ) ;
346
365
return ;
347
366
}
348
367
371
390
return this ;
372
391
}
373
392
if ( this . get_items ( ) ) {
374
- this . elements . menu . show ( ) ;
393
+ if ( this . internal . o . fx && $ . isFunction ( this . internal . o . fx . open ) ) {
394
+ this . internal . o . fx . open ( this . elements . menu ) ;
395
+ } else {
396
+ this . elements . menu . show ( ) ;
397
+ }
375
398
this . $e . addClass ( field . co . OPEN_CLASS ) ;
376
399
$ ( window . document ) . bind ( "keydown." + this . _name , { me : this } , this . events . keydown ) ;
377
400
this . internal . open = true ;
384
407
385
408
// (chainable)
386
409
close : function ( ) {
387
- this . elements . menu . hide ( ) ;
410
+ if ( this . internal . o . fx && $ . isFunction ( this . internal . o . fx . close ) ) {
411
+ this . internal . o . fx . close ( this . elements . menu ) ;
412
+ } else {
413
+ this . elements . menu . hide ( ) ;
414
+ }
388
415
this . $e . removeClass ( field . co . OPEN_CLASS ) ;
389
416
$ ( window . document ) . unbind ( "keydown." + this . _name , this . events . keydown ) ;
390
417
this . internal . open = false ;
438
465
}
439
466
} ) ;
440
467
468
+ field . synthesize ( {
469
+ name : "MultiDropdown" ,
470
+
471
+ base : field . Dropdown ,
472
+ proto : {
473
+
474
+ options : {
475
+ item_handlers : {
476
+ click : function ( e ) {
477
+ e . data . me . toggle_item ( e . data . $item ) ;
478
+ }
479
+ } ,
480
+
481
+ empty_val : [ ] ,
482
+
483
+ circular : false ,
484
+ filtering : false
485
+ } ,
486
+
487
+ _init : function ( ) {
488
+ this . internal . multi = true ;
489
+ this . _base . _init . apply ( this , arguments ) ;
490
+ this . get_items ( true ) . each ( function ( ) {
491
+ var $item = $ ( this ) ;
492
+ if ( $item . hasClass ( field . co . ACTIVE_CLASS ) ) {
493
+ $item . children ( "input" ) . attr ( "checked" , true ) ;
494
+ } else {
495
+ $item . children ( "input" ) . removeAttr ( "checked" ) ;
496
+ }
497
+ } ) ;
498
+ } ,
499
+
500
+ get_val : function ( ) {
501
+ var $active , value_list ;
502
+ $active = this . get_active_item ( ) ;
503
+ if ( $active ) {
504
+ value_list = [ ] ;
505
+ $active . each ( function ( ) {
506
+ value_list . push ( $ ( this ) . attr ( field . co . VALUE ) ) ;
507
+ } ) ;
508
+ return value_list ;
509
+ }
510
+ return this . internal . o . empty_val ;
511
+ } ,
512
+
513
+ // (chainable)
514
+ toggle_item : function ( $item , prevent_change ) {
515
+ var value_list , val , index ;
516
+
517
+ value_list = this . get_val ( ) ;
518
+ val = $item . attr ( field . co . VALUE ) ;
519
+ if ( value_list === this . internal . o . empty_val ) {
520
+ return this . set_val ( val ) ;
521
+ }
522
+ index = $ . inArray ( val , value_list ) ;
523
+ if ( index > - 1 ) {
524
+ value_list . splice ( index , 1 ) ;
525
+ } else {
526
+ value_list . push ( val ) ;
527
+ }
528
+ return this . set_val ( value_list ) ;
529
+ } ,
530
+
531
+ _set_val : function ( val ) {
532
+ var me = this , $items ;
533
+
534
+ if ( ! $ . isArray ( val ) ) {
535
+ this . _base . _set_val . apply ( this , arguments ) ;
536
+ return ;
537
+ }
538
+
539
+ $items = this . get_items ( true ) ;
540
+
541
+ $items . each ( function ( ) {
542
+ var $item = $ ( this ) ;
543
+ $item . removeClass ( field . co . ACTIVE_CLASS ) ;
544
+ $item . children ( "input" ) . removeAttr ( "checked" ) ;
545
+ } ) ;
546
+
547
+ $ . each ( val , function ( i , v ) {
548
+ $items . each ( function ( i ) {
549
+ var $item = $ ( this ) ;
550
+ if ( $item . attr ( field . co . VALUE ) == v ) {
551
+ $item . addClass ( field . co . ACTIVE_CLASS ) ;
552
+ $item . children ( "input" ) . attr ( "checked" , true ) ;
553
+ return false ;
554
+ }
555
+ } ) ;
556
+ } ) ;
557
+
558
+ this . _update_display ( ) ;
559
+ } ,
560
+
561
+ clear : function ( ) {
562
+ this . get_items ( true ) . removeClass ( field . co . ACTIVE_CLASS ) . children ( "input" ) . removeAttr ( "checked" ) ;
563
+ this . _update_display ( false ) ;
564
+ return this ;
565
+ } ,
566
+
567
+ _update_display : function ( ) {
568
+ var display_list , $active , buf , i ;
569
+
570
+ if ( arguments [ 0 ] instanceof $ && arguments [ 0 ] . length > 1 ) {
571
+ display_list = [ ] ;
572
+ $ . each ( arguments [ 0 ] , function ( ) {
573
+ display_list . push ( field . get_display_text ( $ ( this ) ) ) ;
574
+ } ) ;
575
+
576
+ } else if ( arguments [ 0 ] !== false && ! arguments [ 0 ] ) {
577
+ $active = this . get_active_item ( ) ;
578
+ if ( $active ) {
579
+ display_list = [ ] ;
580
+ $active . each ( function ( ) {
581
+ display_list . push ( field . get_display_text ( $ ( this ) ) ) ;
582
+ } ) ;
583
+
584
+ } else {
585
+ this . _base . _update_display . apply ( this , [ false ] ) ;
586
+ return ;
587
+ }
588
+
589
+ } else {
590
+ this . _base . _update_display . apply ( this , arguments ) ;
591
+ return ;
592
+ }
593
+
594
+ buf = "" ;
595
+ for ( i = 0 ; i < display_list . length ; i += 1 ) {
596
+ buf += "<span>" + display_list [ i ] + "</span>" ;
597
+ if ( i < display_list . length - 1 ) {
598
+ buf += ", " ;
599
+ }
600
+ }
601
+ this . elements . display . html ( buf ) ;
602
+ }
603
+
604
+ }
605
+ } ) ;
606
+
441
607
} ( this , this . jQuery ) ) ;
0 commit comments