1
1
import { POST } from '../modules/fetch.ts' ;
2
- import { addDelegatedEventListener , hideElem , queryElems , showElem , toggleElem } from '../utils/dom.ts' ;
2
+ import { addDelegatedEventListener , hideElem , showElem , toggleElem } from '../utils/dom.ts' ;
3
3
import { fomanticQuery } from '../modules/fomantic/base.ts' ;
4
4
import { camelize } from 'vue' ;
5
5
@@ -74,10 +74,9 @@ export function initGlobalDeleteButton(): void {
74
74
}
75
75
}
76
76
77
- function onShowPanelClick ( e : MouseEvent ) {
77
+ function onShowPanelClick ( el : HTMLElement , e : MouseEvent ) {
78
78
// a '.show-panel' element can show a panel, by `data-panel="selector"`
79
79
// if it has "toggle" class, it toggles the panel
80
- const el = e . currentTarget as HTMLElement ;
81
80
e . preventDefault ( ) ;
82
81
const sel = el . getAttribute ( 'data-panel' ) ;
83
82
if ( el . classList . contains ( 'toggle' ) ) {
@@ -87,9 +86,8 @@ function onShowPanelClick(e: MouseEvent) {
87
86
}
88
87
}
89
88
90
- function onHidePanelClick ( e : MouseEvent ) {
89
+ function onHidePanelClick ( el : HTMLElement , e : MouseEvent ) {
91
90
// a `.hide-panel` element can hide a panel, by `data-panel="selector"` or `data-panel-closest="selector"`
92
- const el = e . currentTarget as HTMLElement ;
93
91
e . preventDefault ( ) ;
94
92
let sel = el . getAttribute ( 'data-panel' ) ;
95
93
if ( sel ) {
@@ -104,15 +102,14 @@ function onHidePanelClick(e: MouseEvent) {
104
102
throw new Error ( 'no panel to hide' ) ; // should never happen, otherwise there is a bug in code
105
103
}
106
104
107
- function onShowModalClick ( e : MouseEvent ) {
105
+ function onShowModalClick ( el : HTMLElement , e : MouseEvent ) {
108
106
// A ".show-modal" button will show a modal dialog defined by its "data-modal" attribute.
109
107
// Each "data-modal-{target}" attribute will be filled to target element's value or text-content.
110
108
// * First, try to query '#target'
111
109
// * Then, try to query '[name=target]'
112
110
// * Then, try to query '.target'
113
111
// * Then, try to query 'target' as HTML tag
114
112
// If there is a ".{attr}" part like "data-modal-form.action", then the form's "action" attribute will be set.
115
- const el = e . currentTarget as HTMLElement ;
116
113
e . preventDefault ( ) ;
117
114
const modalSelector = el . getAttribute ( 'data-modal' ) ;
118
115
const elModal = document . querySelector ( modalSelector ) ;
@@ -160,7 +157,15 @@ export function initGlobalButtons(): void {
160
157
// There are a few cancel buttons in non-modal forms, and there are some dynamically created forms (eg: the "Edit Issue Content")
161
158
addDelegatedEventListener ( document , 'click' , 'form button.ui.cancel.button' , ( _ /* el */ , e ) => e . preventDefault ( ) ) ;
162
159
163
- queryElems ( document , '.show-panel' , ( el ) => el . addEventListener ( 'click' , onShowPanelClick ) ) ;
164
- queryElems ( document , '.hide-panel' , ( el ) => el . addEventListener ( 'click' , onHidePanelClick ) ) ;
165
- queryElems ( document , '.show-modal' , ( el ) => el . addEventListener ( 'click' , onShowModalClick ) ) ;
160
+ // Ideally these "button" events should be handled by registerGlobalEventFunc
161
+ // Refactoring would involve too many changes, so at the moment, just use the global event listener.
162
+ addDelegatedEventListener ( document , 'click' , '.show-panel, .hide-panel, .show-modal' , ( el , e : MouseEvent ) => {
163
+ if ( el . classList . contains ( 'show-panel' ) ) {
164
+ onShowPanelClick ( el , e ) ;
165
+ } else if ( el . classList . contains ( 'hide-panel' ) ) {
166
+ onHidePanelClick ( el , e ) ;
167
+ } else if ( el . classList . contains ( 'show-modal' ) ) {
168
+ onShowModalClick ( el , e ) ;
169
+ }
170
+ } ) ;
166
171
}
0 commit comments