22import Input from "./input.js"
33import EventHandler from "./event-handler.js"
44
5- export function registerSearch ( el , options ) {
6- Input . composition ( search , onSearch ) ;
7- EventHandler . on ( search , 'keydown' , keydown ) ;
8-
9- if ( popover . isPopover ) {
10- EventHandler . on ( el , 'shown.bs.popover' , shown ) ;
11- }
12- else {
13- EventHandler . on ( el , 'shown.bs.dropdown' , shown ) ;
14- }
5+ export function registerSelect ( select ) {
6+ initSearchHandler ( select ) ;
7+ initKeydownHandler ( select ) ;
8+ initShownHandler ( select ) ;
159}
1610
17- export function unregisterSearch ( el ) {
11+ export function unregisterSelect ( select ) {
12+ const { el, search, keydownEl, popover } = select ;
1813 Input . dispose ( search ) ;
19- EventHandler . off ( search , 'keydown' ) ;
2014
15+ keydownEl . forEach ( ele => {
16+ EventHandler . off ( ele , 'keydown' ) ;
17+ } )
2118 if ( popover . isPopover ) {
2219 EventHandler . off ( el , 'shown.bs.popover' ) ;
2320 }
@@ -26,62 +23,86 @@ export function unregisterSearch(el) {
2623 }
2724}
2825
29- const onSearch = debounce ( async v => {
30- search . parentElement . classList . add ( 'l' ) ;
31- await invoke . invokeMethodAsync ( 'TriggerOnSearch' , v ) ;
32- search . parentElement . classList . remove ( 'l' ) ;
33- } ) ;
26+ const initKeydownHandler = select => {
27+ const { el, invoke, options, keydownEl, popover } = select ;
28+ const { confirmMethodCallback } = options ;
29+ const keydown = e => {
30+ const menu = popover . toggleMenu ;
31+ const key = e . key ;
32+ if ( key === "Enter" || key === 'NumpadEnter' ) {
33+ if ( popover . isPopover ) {
34+ popover . hide ( ) ;
35+ }
36+ else {
37+ menu . classList . remove ( 'show' ) ;
38+ }
39+ const activeItem = menu . querySelector ( '.active' ) ;
40+ let index = indexOf ( menu , activeItem )
41+ invoke . invokeMethodAsync ( confirmMethodCallback , index )
42+ }
43+ else if ( key === 'ArrowUp' || key === 'ArrowDown' ) {
44+ e . preventDefault ( ) ;
3445
35- const shown = ( ) => {
36- if ( search ) {
37- search . focus ( ) ;
38- }
39- const active = popover . toggleMenu . querySelector ( '.dropdown-item.active' ) ;
40- if ( active ) {
41- scrollIntoView ( el , active ) ;
42- }
43- }
46+ if ( menu . querySelector ( '.dropdown-virtual' ) ) {
47+ return ;
48+ }
4449
45- const keydown = e => {
46- const menu = popover . toggleMenu ;
47- const key = e . key ;
48- if ( key === "Enter" || key === 'NumpadEnter' ) {
49- if ( popover . isPopover ) {
50- popover . hide ( ) ;
51- }
52- else {
53- menu . classList . remove ( 'show' ) ;
50+ const items = [ ...menu . querySelectorAll ( '.dropdown-item:not(.disabled)' ) ] ;
51+ if ( items . length > 0 ) {
52+ let current = menu . querySelector ( '.active' ) ;
53+ if ( current !== null ) {
54+ current . classList . remove ( 'active' ) ;
55+ }
56+
57+ let index = current === null ? - 1 : items . indexOf ( current ) ;
58+ index = key === 'ArrowUp' ? index - 1 : index + 1 ;
59+ if ( index < 0 ) {
60+ index = items . length - 1 ;
61+ }
62+ else if ( index > items . length - 1 ) {
63+ index = 0 ;
64+ }
65+ current = items [ index ] ;
66+ current . classList . add ( 'active' ) ;
67+ scrollIntoView ( el , current ) ;
68+ }
5469 }
55- const activeItem = menu . querySelector ( '.active' ) ;
56- let index = indexOf ( menu , activeItem )
57- invoke . invokeMethodAsync ( confirmMethodCallback , index )
5870 }
59- else if ( key === 'ArrowUp' || key === 'ArrowDown' ) {
60- e . preventDefault ( ) ;
6171
62- if ( menu . querySelector ( '.dropdown-virtual' ) ) {
63- return ;
64- }
72+ keydownEl . forEach ( ele => {
73+ EventHandler . on ( ele , 'keydown' , keydown ) ;
74+ } ) ;
75+ }
6576
66- const items = [ ...menu . querySelectorAll ( '.dropdown-item:not(.disabled)' ) ] ;
67- if ( items . length > 0 ) {
68- let current = menu . querySelector ( '.active' ) ;
69- if ( current !== null ) {
70- current . classList . remove ( 'active' ) ;
71- }
77+ const initSearchHandler = select => {
78+ const { search, invoke, options } = select ;
79+ const { searchMethodCallback } = options ;
80+ const onSearch = debounce ( async v => {
81+ search . parentElement . classList . add ( 'l' ) ;
82+ await invoke . invokeMethodAsync ( searchMethodCallback , v ) ;
83+ search . parentElement . classList . remove ( 'l' ) ;
84+ } ) ;
7285
73- let index = current === null ? - 1 : items . indexOf ( current ) ;
74- index = key === 'ArrowUp' ? index - 1 : index + 1 ;
75- if ( index < 0 ) {
76- index = items . length - 1 ;
77- }
78- else if ( index > items . length - 1 ) {
79- index = 0 ;
80- }
81- current = items [ index ] ;
82- current . classList . add ( 'active' ) ;
83- scrollIntoView ( el , current ) ;
86+ Input . composition ( search , onSearch ) ;
87+ }
88+
89+ const initShownHandler = select => {
90+ const { el, search, popover } = select ;
91+ const shown = ( ) => {
92+ if ( search ) {
93+ search . focus ( ) ;
8494 }
95+ const active = popover . toggleMenu . querySelector ( '.dropdown-item.active' ) ;
96+ if ( active ) {
97+ scrollIntoView ( el , active ) ;
98+ }
99+ }
100+
101+ if ( popover . isPopover ) {
102+ EventHandler . on ( el , 'shown.bs.popover' , shown ) ;
103+ }
104+ else {
105+ EventHandler . on ( el , 'shown.bs.dropdown' , shown ) ;
85106 }
86107}
87108
0 commit comments