Skip to content

Commit

Permalink
Made buttons trigger action on Space
Browse files Browse the repository at this point in the history
  • Loading branch information
alansemenov committed Sep 9, 2024
1 parent b2456ef commit c8af314
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class BrowseFilterPanel<T>
});
this.hideFilterPanelButton.setTitle(i18n('tooltip.filterPanel.hide'));
this.hideFilterPanelButton.onClicked(() => this.notifyHidePanelButtonPressed());
this.hideFilterPanelButton.onEnterPressed(() => this.notifyHidePanelButtonPressed());
this.hideFilterPanelButton.onApplyKeyPressed(() => this.notifyHidePanelButtonPressed());

let showResultsButtonWrapper = new DivEl('show-filter-results');
this.showResultsButton = new SpanEl('show-filter-results-button');
Expand Down
5 changes: 3 additions & 2 deletions src/main/resources/assets/admin/common/js/dom/ButtonEl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ export class ButtonEl
constructor(className?: string, stylePrefix: string = StyleHelper.COMMON_PREFIX) {
super('button', className, stylePrefix);

this.onEnterPressed(() => {
const triggerAction = () => {
Body.get().setFocusedElement(this);
$(this.getHTMLElement()).simulate('click');
});
};
this.onApplyKeyPressed(triggerAction);
}

setEnabled(value: boolean) {
Expand Down
17 changes: 13 additions & 4 deletions src/main/resources/assets/admin/common/js/dom/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1121,12 +1121,21 @@ export class Element {
this.getEl().removeEventListener('DOMMouseScroll', listener);
}

onEnterPressed(callback: () => void) {
onApplyKeyPressed(callback: () => void) {
const callbackWrapper = (event: KeyboardEvent) => {
callback();
event.stopPropagation();
event.preventDefault();
};
this.onKeyDown((event: KeyboardEvent) => {
if (KeyHelper.isEnterKey(event)) {
callback();
event.stopPropagation();
event.preventDefault();
callbackWrapper(event);
}
});

this.onKeyUp((event: KeyboardEvent) => {
if (KeyHelper.isSpace(event)) {
callbackWrapper(event);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ export class ActionButton
this.getAction().execute();
};

this.onClicked(() => executeAction());
this.onEnterPressed(() => executeAction());
this.onClicked(executeAction);

this.onHelpKeyPressed = this.onHelpKeyPressed.bind(this);
this.syncButtonWithAction = this.syncButtonWithAction.bind(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ export class MenuButton
return this.dropdownHandle;
}

getChildControls(): Element[] {
return [this.actionButton, this.dropdownHandle];
}

addMenuActions(actions: Action[]) {
this.menu.addActions(actions);
this.initActions(actions);
Expand Down Expand Up @@ -204,7 +208,6 @@ export class MenuButton

private initDropdownHandle(): void {
this.dropdownHandle = new DropdownHandle();
this.dropdownHandle.onEnterPressed(() => this.toggleMenu());
}

private initActionButton(): void {
Expand Down Expand Up @@ -251,7 +254,6 @@ export class MenuButton
}
};
this.dropdownHandle.onClicked(onDropdownHandleClicked);
this.dropdownHandle.onEnterPressed(onDropdownHandleClicked);
this.dropdownHandle.onEscPressed(() => this.collapseMenu());

this.menu.onItemClicked((item: MenuItem) => {
Expand All @@ -262,7 +264,6 @@ export class MenuButton

const onActionButtonClicked = () => this.toggleMenuOnAction ? this.toggleMenu() : this.collapseMenu();
this.actionButton.onClicked(onActionButtonClicked);
this.actionButton.onEnterPressed(onActionButtonClicked);

this.menu.onClicked(() => this.dropdownHandle.giveFocus());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class FoldButton
private initListeners() {
(this.hostElement || this).onClicked(this.toggleMenu.bind(this));
this.dropdown.onClicked(this.onMenuClicked.bind(this));
this.onEnterPressed(this.toggleMenu.bind(this));
this.onApplyKeyPressed(this.toggleMenu.bind(this));
this.onEscPressed(this.collapse.bind(this));
}

Expand Down

0 comments on commit c8af314

Please sign in to comment.