mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-18 15:36:02 +01:00
refactor(web/menu): radial menu item removing and refreshing
This commit is contained in:
@@ -1,30 +1,63 @@
|
|||||||
local isOpen = false
|
local isOpen = false
|
||||||
local menuItems = {}
|
local menuItems = {}
|
||||||
|
local activeItems = {}
|
||||||
|
|
||||||
|
local function getActiveItems()
|
||||||
|
activeItems = {}
|
||||||
|
for i = 1, #menuItems do
|
||||||
|
local item = menuItems[i]
|
||||||
|
if item.canInteract == nil or item.canInteract() then
|
||||||
|
activeItems[#activeItems+1] = {
|
||||||
|
icon = item.icon,
|
||||||
|
label = item.label,
|
||||||
|
key = item.key
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return activeItems
|
||||||
|
end
|
||||||
|
|
||||||
function lib.addRadialItem(items)
|
function lib.addRadialItem(items)
|
||||||
if table.type(items) == 'array' then
|
if table.type(items) == 'array' then
|
||||||
for i = 1, #items do
|
for i = 1, #items do
|
||||||
local itemOption = items[i]
|
local item = items[i]
|
||||||
menuItems[#menuItems+1] = itemOption
|
menuItems[#menuItems+1] = item
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
menuItems[#menuItems+1] = items
|
menuItems[#menuItems+1] = items
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function lib.removeRadialItem(key)
|
||||||
|
for i = 1, #menuItems do
|
||||||
|
local item = menuItems[i]
|
||||||
|
if item.key == key then
|
||||||
|
table.remove(menuItems, i)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if isOpen then
|
||||||
|
local refresh = false
|
||||||
|
for i = 1, #activeItems do
|
||||||
|
local activeItem = activeItems[i]
|
||||||
|
if activeItem.key == key then
|
||||||
|
table.remove(activeItems, i)
|
||||||
|
refresh = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if refresh then
|
||||||
|
local items = getActiveItems()
|
||||||
|
SendNUIMessage({
|
||||||
|
action = 'refreshItems',
|
||||||
|
data = items
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- TODO: Interval canInteract checking and sending new items to NUI
|
-- TODO: Interval canInteract checking and sending new items to NUI
|
||||||
local function openRadial()
|
local function openRadial()
|
||||||
isOpen = true
|
isOpen = true
|
||||||
local items = {}
|
local items = getActiveItems()
|
||||||
for i = 1, #menuItems do
|
|
||||||
local menuItem = menuItems[i]
|
|
||||||
if menuItem.canInteract == nil or menuItem.canInteract() then
|
|
||||||
items[#items+1] = {
|
|
||||||
icon = menuItem.icon,
|
|
||||||
label = menuItem.label
|
|
||||||
}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
SendNUIMessage({
|
SendNUIMessage({
|
||||||
action = 'openRadialMenu',
|
action = 'openRadialMenu',
|
||||||
data = {
|
data = {
|
||||||
|
|||||||
@@ -68,6 +68,10 @@ const RadialMenu: React.FC = () => {
|
|||||||
setVisible(true);
|
setVisible(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
useNuiEvent('refreshItems', (data: MenuItem[]) => {
|
||||||
|
setMenu({ ...menu, items: data });
|
||||||
|
});
|
||||||
|
|
||||||
const handleClick = (index: number) => {
|
const handleClick = (index: number) => {
|
||||||
fetchNui('radialClick', index);
|
fetchNui('radialClick', index);
|
||||||
// TODO: shouldClose
|
// TODO: shouldClose
|
||||||
|
|||||||
Reference in New Issue
Block a user