Skip to content

Commit

Permalink
GridContextMenu.setOpened(): add column parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
mvysny committed Aug 7, 2024
1 parent 38ffa68 commit 00f0d3c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -347,18 +347,22 @@ internal fun DynaNodeGroup.contextMenuTestbatch() {
test("setOpened(true) fires GridContextMenuOpenedEvent") {
var called = false
lateinit var cm: GridContextMenu<String>
UI.getCurrent().grid<String> {
val grid = UI.getCurrent().grid<String> {
cm = gridContextMenu {
item("click me")
}
addColumn({ it }).apply {
id_ = "foo!"
}
}
cm.addGridContextMenuOpenedListener() { e ->
cm.addGridContextMenuOpenedListener { e ->
called = true
expect(true) { e.isOpened }
expect(false) { e.isFromClient }
expect("foo") { e.item.orElse(null) }
expect("foo!") { e.columnId.get() }
}
cm.setOpened(true, "foo")
cm.setOpened(true, "foo", grid.columns[0])
expect(true) { called }
}

Expand All @@ -376,6 +380,7 @@ internal fun DynaNodeGroup.contextMenuTestbatch() {
expect(false) { e.isOpened }
expect(false) { e.isFromClient }
expect("foo") { e.item.orElse(null) }
expect(null) { e.columnId.orElse(null) }
}
cm.setOpened(false, "foo")
expect(true) { called }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ public fun <T> GridContextMenu<T>._clickItemWithCaption(caption: String, gridIte
*/
public fun <T> GridContextMenu<T>._clickItemMatching(searchSpec: SearchSpec<MenuItemBase<*, *, *>>, gridItem: T?) {
// fires ContextMenuOpenedListener to simulate menu opening
_setContextMenuTargetItemKey(gridItem)
element.setProperty("opened", true)
setOpened(true, gridItem)

// notify the context menu dynamic item generator
dynamicContentHandler?.also {
Expand All @@ -124,7 +123,7 @@ public fun <T> GridContextMenu<T>._clickItemMatching(searchSpec: SearchSpec<Menu
(item as GridMenuItem<T>)._click(gridItem)

// fires ContextMenuOpenedListener to simulate menu closing
element.setProperty("opened", false)
setOpened(false, gridItem)
}

private fun Component.getParentMap(): Map<MenuItemBase<*, *, *>, Component> {
Expand Down Expand Up @@ -206,7 +205,7 @@ private fun MenuItemBase<*, *, *>.checkMenuItemVisible(originalItem: MenuItemBas
}
}
val parent: Component = parentMap[this]
?: fail("${originalItem.toPrettyString()} is not part of\n${getContextMenu().toPrettyTree()}?!?")
?: fail("${originalItem.toPrettyString()} is not part of\n${contextMenu.toPrettyTree()}?!?")
when (parent) {
is MenuItem -> parent.checkMenuItemVisible(originalItem, parentMap)
is GridMenuItem<*> -> parent.checkMenuItemVisible(originalItem, parentMap)
Expand Down Expand Up @@ -269,7 +268,13 @@ public fun ContextMenu.setOpened(opened: Boolean) {
/**
* Opens or closes the menu. Fires the [ContextMenuBase.OpenedChangeEvent].
*/
public fun <T> GridContextMenu<T>.setOpened(opened: Boolean, gridItem: T?) {
@JvmOverloads
public fun <T> GridContextMenu<T>.setOpened(opened: Boolean, gridItem: T?, column: Grid.Column<T>? = null) {
_setContextMenuTargetItemKey(gridItem)
if (column != null) {
val id = requireNotNull(column.id_) { "Column $column must have an ID assigned in order to be identifiable in the event object" }
require(id.isNotBlank()) { "Column $column must have an ID assigned in order to be identifiable in the event object" }
target.element.setProperty("_contextMenuTargetColumnId", id)
}
element.setProperty("opened", opened)
}

0 comments on commit 00f0d3c

Please sign in to comment.